2016-01-02 06:20:55 -05:00
|
|
|
package docker
|
|
|
|
|
|
|
|
|
|
import (
|
2019-11-23 08:42:05 -05:00
|
|
|
"log"
|
|
|
|
|
"net"
|
|
|
|
|
"regexp"
|
|
|
|
|
"strconv"
|
2016-01-02 06:20:55 -05:00
|
|
|
|
2019-10-09 14:25:38 -04:00
|
|
|
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
|
2016-01-02 06:20:55 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func resourceDockerNetwork() *schema.Resource {
|
|
|
|
|
return &schema.Resource{
|
|
|
|
|
Create: resourceDockerNetworkCreate,
|
|
|
|
|
Read: resourceDockerNetworkRead,
|
|
|
|
|
Delete: resourceDockerNetworkDelete,
|
2019-11-23 08:42:05 -05:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
|
},
|
2016-01-02 06:20:55 -05:00
|
|
|
|
2019-11-12 17:41:59 -05:00
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
|
"name": {
|
|
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Required: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"labels": {
|
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
Elem: labelSchema,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"check_duplicate": {
|
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"driver": {
|
|
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
Computed: true,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"options": {
|
|
|
|
|
Type: schema.TypeMap,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
Computed: true,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"internal": {
|
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
|
Optional: true,
|
|
|
|
|
Computed: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"attachable": {
|
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"ingress": {
|
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"ipv6": {
|
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"ipam_driver": {
|
|
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
2019-12-17 11:28:25 -05:00
|
|
|
Default: "default",
|
2019-11-12 17:41:59 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"ipam_config": {
|
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
|
Optional: true,
|
2019-12-17 11:28:25 -05:00
|
|
|
Computed: true,
|
2019-11-12 17:41:59 -05:00
|
|
|
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": {
|
|
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"ip_range": {
|
|
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"gateway": {
|
|
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"aux_address": {
|
|
|
|
|
Type: schema.TypeMap,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"scope": {
|
|
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Computed: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
SchemaVersion: 1,
|
|
|
|
|
StateUpgraders: []schema.StateUpgrader{
|
|
|
|
|
{
|
|
|
|
|
Version: 0,
|
|
|
|
|
Type: resourceDockerNetworkV0().CoreConfigSchema().ImpliedType(),
|
|
|
|
|
Upgrade: func(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
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func resourceDockerNetworkV0() *schema.Resource {
|
|
|
|
|
return &schema.Resource{
|
2020-12-02 06:06:39 -05:00
|
|
|
// This is only used for state migration, so the CRUD
|
|
|
|
|
// callbacks are no longer relevant
|
2016-01-02 06:20:55 -05:00
|
|
|
Schema: map[string]*schema.Schema{
|
2019-03-01 16:02:17 -05:00
|
|
|
"name": {
|
2016-01-02 06:20:55 -05:00
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Required: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-01 16:02:17 -05:00
|
|
|
"labels": {
|
2018-10-18 06:39:58 -04:00
|
|
|
Type: schema.TypeMap,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-01 16:02:17 -05:00
|
|
|
"check_duplicate": {
|
2016-01-02 06:20:55 -05:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-01 16:02:17 -05:00
|
|
|
"driver": {
|
2016-01-02 06:20:55 -05:00
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
2019-11-23 08:42:05 -05:00
|
|
|
Default: "bridge",
|
2016-01-02 06:20:55 -05:00
|
|
|
},
|
|
|
|
|
|
2019-03-01 16:02:17 -05:00
|
|
|
"options": {
|
2016-01-02 06:20:55 -05:00
|
|
|
Type: schema.TypeMap,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
Computed: true,
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-01 16:02:17 -05:00
|
|
|
"internal": {
|
2017-01-03 06:46:16 -05:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
|
Optional: true,
|
|
|
|
|
Computed: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-01 16:02:17 -05:00
|
|
|
"attachable": {
|
2018-10-18 06:39:58 -04:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-01 16:02:17 -05:00
|
|
|
"ingress": {
|
2018-10-18 06:39:58 -04:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-01 16:02:17 -05:00
|
|
|
"ipv6": {
|
2018-10-18 06:39:58 -04:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-01 16:02:17 -05:00
|
|
|
"ipam_driver": {
|
2016-01-02 06:20:55 -05:00
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
2019-11-23 08:42:05 -05:00
|
|
|
Default: "default",
|
2016-01-02 06:20:55 -05:00
|
|
|
},
|
|
|
|
|
|
2019-03-01 16:02:17 -05:00
|
|
|
"ipam_config": {
|
2016-01-02 06:20:55 -05:00
|
|
|
Type: schema.TypeSet,
|
|
|
|
|
Optional: true,
|
2019-11-23 08:42:05 -05:00
|
|
|
Computed: true,
|
2016-01-02 06:20:55 -05:00
|
|
|
ForceNew: true,
|
2019-11-23 08:42:05 -05:00
|
|
|
// DiffSuppressFunc: suppressIfIPAMConfigWithIpv6Changes(),
|
2018-01-29 06:01:46 -05:00
|
|
|
Elem: &schema.Resource{
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2019-03-01 16:02:17 -05:00
|
|
|
"subnet": {
|
2018-01-29 06:01:46 -05:00
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-01 16:02:17 -05:00
|
|
|
"ip_range": {
|
2018-01-29 06:01:46 -05:00
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-01 16:02:17 -05:00
|
|
|
"gateway": {
|
2018-01-29 06:01:46 -05:00
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-01 16:02:17 -05:00
|
|
|
"aux_address": {
|
2018-01-29 06:01:46 -05:00
|
|
|
Type: schema.TypeMap,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2016-01-02 06:20:55 -05:00
|
|
|
},
|
|
|
|
|
|
2019-03-01 16:02:17 -05:00
|
|
|
"scope": {
|
2016-01-02 06:20:55 -05:00
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Computed: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-23 08:42:05 -05:00
|
|
|
func suppressIfIPAMConfigWithIpv6Changes() schema.SchemaDiffSuppressFunc {
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|