terraform-provider-docker/internal/provider/resource_docker_network.go
Manuel Vogel 6c796e15a5
feat/doc generation (#193)
* chore: add tfplugindocs tool

* feat: add tfplugin doc dependency and make target

* chore: apply documentation generation

* docs(contributing): update for documentation generation

* fix: adapt website-lint target to new do folder

* docs(network): update ds descriptions

* docs: add template for index.md

* docs: add network resource generation

* chore(ci): updates paths for website checks

* docs: add plugin data source generation

* docs: add import cmd for network resource

* docs: add plugin resource generation

* feat: outlines remaining resources with example and import cmd

* feat: add descriptions to docs

* chore: add DevSkim ignores and fix capitalized errors

* docs: complete ds registry image

* docs: add container resource generation

* docs: add lables description to missing resources

* docs: remove computed:true from network data

so the list is rendered in the description

* Revert "docs: remove computed:true from network data"

This reverts commit dce9b7a5a2.

* docs: add docker image descriptions to generate the docs

* docs: add docker registry image descriptions to generate the docs

* docs: add docker service descriptions to generate the docs

* docs: add docker volume descriptions to generate the docs

* docs(index): clarifies description

so more docker resources are mentioned

* docs(network): fixes required and read-only attributes

so the ds can only be read by-name

* docs(plugin): clarifies the ds docs attributes

* docs: fix typo registry image ds

* docs(config): clarifies attributes and enhances examples

Provide a long example and import command

* fix(config): make data non-sensitive

Because only secrets data is

* docs(containter): clarifies attributes

and enhances examples with import

* docs(config): fix typo

* docs(image): clarifies attributes and remove import

* docs(network): clarifies attributes and adapts import

* docs(plugin): clarifies attributes and import

* docs(registry_image): clarifies attributes and removes import

* chore(secret): remove typo

* docs(service): clarifies attributes and import

* docs(volume): clarifies attributes and import

* fix: correct md linter rules after doc gen

* docs(volume): regenerated

* docs: add config custom template

* docs: add templates for all resources

* docs(config): templates all sections and examples

for better redability and structure

* docs(config): fix md linter

* docs(container): templates all sections and examples

* docs(image): templates all sections and examples

* docs(image): fix import resource by renaming

* docs(network): templates all sections and examples

* docs(service): templates all sections and examples

* docs(volume): templates all sections and examples

* fix(lint): replace website with doc directory

* fix(ci): link check file extension check

* fix: markdown links

* chore: remove old website folder

* chore: fix website-lint terrafmr dir and pattern

* fix: lint fix target website folder

* fix: website links

* docs(provider): update examples

with templates on auth and certs

* docs(provider): add tf-plugin-docs line

* docs(contributing): split doc generation section

* docs: final brush up for readability and structure

* chore(ci): add website-generation job

to see if files changed and it should run locally again

* chore(ci): remove explicit docker setup

from website lint because it's installed by default
2021-05-21 21:30:56 +09:00

205 lines
5.7 KiB
Go

package provider
import (
"context"
"log"
"net"
"regexp"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceDockerNetwork() *schema.Resource {
return &schema.Resource{
Description: "`docker_network` provides details about a specific Docker Network.",
CreateContext: resourceDockerNetworkCreate,
ReadContext: resourceDockerNetworkRead,
DeleteContext: resourceDockerNetworkDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Description: "The name of the Docker network.",
Required: true,
ForceNew: true,
},
"labels": {
Type: schema.TypeSet,
Description: "User-defined key/value metadata",
Optional: true,
ForceNew: true,
Elem: labelSchema,
},
"check_duplicate": {
Type: schema.TypeBool,
Description: "Requests daemon to check for networks with same name.",
Optional: true,
ForceNew: true,
},
"driver": {
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,
},
"options": {
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,
},
"internal": {
Type: schema.TypeBool,
Description: "Whether the network is internal.",
Optional: true,
Computed: true,
ForceNew: true,
},
"attachable": {
Type: schema.TypeBool,
Description: "Enable manual container attachment to the network.",
Optional: true,
ForceNew: true,
},
"ingress": {
Type: schema.TypeBool,
Description: "Create swarm routing-mesh network. Defaults to `false`.",
Optional: true,
ForceNew: true,
},
"ipv6": {
Type: schema.TypeBool,
Description: "Enable IPv6 networking. Defaults to `false`.",
Optional: true,
ForceNew: true,
},
"ipam_driver": {
Type: schema.TypeString,
Description: "Driver used by the custom IP scheme of the network. Defaults to `default`",
Default: "default",
Optional: true,
ForceNew: true,
},
"ipam_config": {
Type: schema.TypeSet,
Description: "The IPAM configuration options",
Optional: true,
Computed: true,
ForceNew: true,
// DiffSuppressFunc: suppressIfIPAMConfigWithIpv6Changes(),
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"subnet": {
Type: schema.TypeString,
Description: "The subnet in CIDR form",
Optional: true,
ForceNew: true,
},
"ip_range": {
Type: schema.TypeString,
Description: "The ip range in CIDR form",
Optional: true,
ForceNew: true,
},
"gateway": {
Type: schema.TypeString,
Description: "The IP address of the gateway",
Optional: true,
ForceNew: true,
},
"aux_address": {
Type: schema.TypeMap,
Description: "Auxiliary IPv4 or IPv6 addresses used by Network driver",
Optional: true,
ForceNew: true,
},
},
},
},
"scope": {
Type: schema.TypeString,
Description: "Scope of the network. One of `swarm`, `global`, or `local`.",
Computed: true,
},
},
SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Version: 0,
Type: resourceDockerNetworkV0().CoreConfigSchema().ImpliedType(),
Upgrade: func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
return replaceLabelsMapFieldWithSetField(rawState), nil
},
},
},
}
}
func suppressIfIPAMConfigWithIpv6Changes() schema.SchemaDiffSuppressFunc { //nolint:deadcode,unused
return func(k, old, new string, d *schema.ResourceData) bool {
// the initial case when the resource is created
if old == "" && new != "" {
return false
}
// if ipv6 is not given we do not consider
ipv6, ok := d.GetOk("ipv6")
if !ok {
return false
}
// 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
}
}
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
}
}