2021-03-18 03:30:54 -04:00
package provider
2016-01-02 06:20:55 -05:00
import (
2021-03-18 03:30:54 -04:00
"context"
2019-11-23 08:42:05 -05:00
"log"
"net"
"regexp"
"strconv"
2016-01-02 06:20:55 -05:00
2021-03-18 03:30:54 -04:00
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2016-01-02 06:20:55 -05:00
)
func resourceDockerNetwork ( ) * schema . Resource {
return & schema . Resource {
2022-08-01 07:22:15 -04:00
Description : "`docker_network` provides a docker network resource." ,
2021-05-21 08:30:56 -04:00
2021-03-18 03:30:54 -04:00
CreateContext : resourceDockerNetworkCreate ,
ReadContext : resourceDockerNetworkRead ,
DeleteContext : resourceDockerNetworkDelete ,
2019-11-23 08:42:05 -05:00
Importer : & schema . ResourceImporter {
2021-03-18 03:30:54 -04:00
StateContext : schema . ImportStatePassthroughContext ,
2019-11-23 08:42:05 -05:00
} ,
2016-01-02 06:20:55 -05:00
2019-11-12 17:41:59 -05:00
Schema : map [ string ] * schema . Schema {
"name" : {
2021-05-21 08:30:56 -04:00
Type : schema . TypeString ,
Description : "The name of the Docker network." ,
Required : true ,
ForceNew : true ,
2019-11-12 17:41:59 -05:00
} ,
"labels" : {
2021-05-21 08:30:56 -04:00
Type : schema . TypeSet ,
Description : "User-defined key/value metadata" ,
Optional : true ,
ForceNew : true ,
Elem : labelSchema ,
2019-11-12 17:41:59 -05:00
} ,
"check_duplicate" : {
2021-05-21 08:30:56 -04:00
Type : schema . TypeBool ,
Description : "Requests daemon to check for networks with same name." ,
Optional : true ,
ForceNew : true ,
2019-11-12 17:41:59 -05:00
} ,
"driver" : {
2021-05-21 08:30:56 -04:00
Type : schema . TypeString ,
Description : "The driver of the Docker network. Possible values are `bridge`, `host`, `overlay`, `macvlan`. See [network docs](https://docs.docker.com/network/#network-drivers) for more details." ,
Optional : true ,
ForceNew : true ,
Computed : true ,
2019-11-12 17:41:59 -05:00
} ,
"options" : {
2021-05-21 08:30:56 -04:00
Type : schema . TypeMap ,
Description : "Only available with bridge networks. See [bridge options docs](https://docs.docker.com/engine/reference/commandline/network_create/#bridge-driver-options) for more details." ,
Optional : true ,
ForceNew : true ,
Computed : true ,
2019-11-12 17:41:59 -05:00
} ,
"internal" : {
2021-05-21 08:30:56 -04:00
Type : schema . TypeBool ,
Description : "Whether the network is internal." ,
Optional : true ,
Computed : true ,
ForceNew : true ,
2019-11-12 17:41:59 -05:00
} ,
"attachable" : {
2021-05-21 08:30:56 -04:00
Type : schema . TypeBool ,
Description : "Enable manual container attachment to the network." ,
Optional : true ,
ForceNew : true ,
2019-11-12 17:41:59 -05:00
} ,
"ingress" : {
2021-05-21 08:30:56 -04:00
Type : schema . TypeBool ,
Description : "Create swarm routing-mesh network. Defaults to `false`." ,
Optional : true ,
ForceNew : true ,
2019-11-12 17:41:59 -05:00
} ,
"ipv6" : {
2021-05-21 08:30:56 -04:00
Type : schema . TypeBool ,
Description : "Enable IPv6 networking. Defaults to `false`." ,
Optional : true ,
ForceNew : true ,
2019-11-12 17:41:59 -05:00
} ,
"ipam_driver" : {
2021-05-21 08:30:56 -04:00
Type : schema . TypeString ,
Description : "Driver used by the custom IP scheme of the network. Defaults to `default`" ,
Default : "default" ,
Optional : true ,
ForceNew : true ,
2019-11-12 17:41:59 -05:00
} ,
2022-12-21 11:24:51 -05:00
"ipam_options" : {
Type : schema . TypeMap ,
Description : "Provide explicit options to the IPAM driver. Valid options vary with `ipam_driver` and refer to that driver's documentation for more details." ,
Optional : true ,
ForceNew : true ,
} ,
2019-11-12 17:41:59 -05:00
"ipam_config" : {
2021-05-21 08:30:56 -04:00
Type : schema . TypeSet ,
Description : "The IPAM configuration options" ,
Optional : true ,
Computed : true ,
ForceNew : true ,
2019-12-17 11:28:25 -05:00
// DiffSuppressFunc: suppressIfIPAMConfigWithIpv6Changes(),
2019-11-12 17:41:59 -05:00
Elem : & schema . Resource {
Schema : map [ string ] * schema . Schema {
"subnet" : {
2021-05-21 08:30:56 -04:00
Type : schema . TypeString ,
Description : "The subnet in CIDR form" ,
Optional : true ,
ForceNew : true ,
2019-11-12 17:41:59 -05:00
} ,
"ip_range" : {
2021-05-21 08:30:56 -04:00
Type : schema . TypeString ,
Description : "The ip range in CIDR form" ,
Optional : true ,
ForceNew : true ,
2019-11-12 17:41:59 -05:00
} ,
"gateway" : {
2021-05-21 08:30:56 -04:00
Type : schema . TypeString ,
Description : "The IP address of the gateway" ,
Optional : true ,
ForceNew : true ,
2019-11-12 17:41:59 -05:00
} ,
"aux_address" : {
2021-05-21 08:30:56 -04:00
Type : schema . TypeMap ,
Description : "Auxiliary IPv4 or IPv6 addresses used by Network driver" ,
Optional : true ,
ForceNew : true ,
2019-11-12 17:41:59 -05:00
} ,
} ,
} ,
} ,
"scope" : {
2021-05-21 08:30:56 -04:00
Type : schema . TypeString ,
Description : "Scope of the network. One of `swarm`, `global`, or `local`." ,
Computed : true ,
2019-11-12 17:41:59 -05:00
} ,
} ,
SchemaVersion : 1 ,
StateUpgraders : [ ] schema . StateUpgrader {
{
Version : 0 ,
Type : resourceDockerNetworkV0 ( ) . CoreConfigSchema ( ) . ImpliedType ( ) ,
2021-03-18 03:30:54 -04:00
Upgrade : func ( ctx context . Context , rawState map [ string ] interface { } , meta interface { } ) ( map [ string ] interface { } , error ) {
2019-11-14 17:44:58 -05:00
return replaceLabelsMapFieldWithSetField ( rawState ) , nil
2019-11-12 17:41:59 -05:00
} ,
} ,
} ,
}
}
2020-12-20 05:04:51 -05:00
func suppressIfIPAMConfigWithIpv6Changes ( ) schema . SchemaDiffSuppressFunc { //nolint:deadcode,unused
2019-11-23 08:42:05 -05:00
return func ( k , old , new string , d * schema . ResourceData ) bool {
// the initial case when the resource is created
if old == "" && new != "" {
return false
}
2016-01-02 06:20:55 -05:00
2019-11-23 08:42:05 -05:00
// if ipv6 is not given we do not consider
ipv6 , ok := d . GetOk ( "ipv6" )
if ! ok {
return false
2016-01-02 06:20:55 -05:00
}
2019-11-23 08:42:05 -05:00
// if ipv6 is given but false we do not consider
isIPv6 := ipv6 . ( bool )
if ! isIPv6 {
return false
}
if k == "ipam_config.#" {
log . Printf ( "[INFO] ipam_config: k: %q, old: %s, new: %s\n" , k , old , new )
oldVal , _ := strconv . Atoi ( string ( old ) )
newVal , _ := strconv . Atoi ( string ( new ) )
log . Printf ( "[INFO] ipam_config: oldVal: %d, newVal: %d\n" , oldVal , newVal )
if newVal <= oldVal {
log . Printf ( "[INFO] suppressingDiff for ipam_config: oldVal: %d, newVal: %d\n" , oldVal , newVal )
return true
}
2016-01-02 06:20:55 -05:00
}
2019-11-23 08:42:05 -05:00
if regexp . MustCompile ( ` ipam_config\.\d+\.gateway ` ) . MatchString ( k ) {
ip := net . ParseIP ( old )
ipv4Address := ip . To4 ( )
log . Printf ( "[INFO] ipam_config.gateway: k: %q, old: %s, new: %s - %v\n" , k , old , new , ipv4Address != nil )
// is an ipv4Address and content changed from non-empty to empty
if ipv4Address != nil && old != "" && new == "" {
log . Printf ( "[INFO] suppressingDiff for ipam_config.gateway %q: oldVal: %s, newVal: %s\n" , ipv4Address . String ( ) , old , new )
return true
}
}
if regexp . MustCompile ( ` ipam_config\.\d+\.subnet ` ) . MatchString ( k ) {
if old != "" && new == "" {
log . Printf ( "[INFO] suppressingDiff for ipam_config.subnet: oldVal: %s, newVal: %s\n" , old , new )
return true
}
}
return false
2016-01-02 06:20:55 -05:00
}
}