mirror of
https://github.com/kreuzwerker/terraform-provider-docker.git
synced 2025-12-18 23:06:10 -05:00
Some checks failed
Acc Tests / acc-test (TestAccDockerConfig, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerConfig, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerNetwork, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerNetwork, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerPlugin, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerPlugin, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerSecret, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerSecret, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerTag, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerTag, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerVolume, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerVolume, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (true, TestAccDockerContainer, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (true, TestAccDockerContainer, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (true, TestAccDockerImage, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (true, TestAccDockerImage, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (true, TestAccDockerRegistryImage, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (true, TestAccDockerRegistryImage, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (true, TestAccDockerService, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (true, TestAccDockerService, 1.8.x) (push) Has been cancelled
Compile Binaries / compile-fast (push) Has been cancelled
Compile Binaries / compile (push) Has been cancelled
golangci-lint / lint (push) Has been cancelled
Unit Tests / unit-test (push) Has been cancelled
Website Checks / markdown-link-check (push) Has been cancelled
Docs and Website Lint / website-generation (push) Has been cancelled
Docs and Website Lint / website-lint-spellcheck-tffmt (push) Has been cancelled
Docs and Website Lint / markdown-lint (push) Has been cancelled
139 lines
3.6 KiB
Go
139 lines
3.6 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
|
|
"github.com/docker/docker/api/types/network"
|
|
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
|
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
|
)
|
|
|
|
func dataSourceDockerNetwork() *schema.Resource {
|
|
return &schema.Resource{
|
|
Description: "`docker_network` provides details about a specific Docker Network.",
|
|
|
|
ReadContext: dataSourceDockerNetworkRead,
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
"name": {
|
|
Type: schema.TypeString,
|
|
Description: "The name of the Docker network.",
|
|
Required: true,
|
|
},
|
|
|
|
"id": {
|
|
Type: schema.TypeString,
|
|
Computed: 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.",
|
|
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.",
|
|
Computed: true,
|
|
},
|
|
|
|
"internal": {
|
|
Type: schema.TypeBool,
|
|
Description: "If `true`, the network is internal.",
|
|
Computed: true,
|
|
},
|
|
|
|
"ipam_config": {
|
|
Type: schema.TypeSet,
|
|
Description: "The IPAM configuration options",
|
|
Computed: true,
|
|
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,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
type ipamMap map[string]interface{}
|
|
|
|
func dataSourceDockerNetworkRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
|
name, nameOk := d.GetOk("name")
|
|
_, idOk := d.GetOk("id")
|
|
|
|
if !nameOk && !idOk {
|
|
return diag.Errorf("One of id or name must be assigned")
|
|
}
|
|
|
|
client, err := meta.(*ProviderConfig).MakeClient(ctx, d)
|
|
if err != nil {
|
|
return diag.Errorf("failed to create Docker client: %v", err)
|
|
}
|
|
|
|
network, err := client.NetworkInspect(ctx, name.(string), network.InspectOptions{})
|
|
if err != nil {
|
|
return diag.Errorf("Could not find docker network: %s", err)
|
|
}
|
|
|
|
d.SetId(network.ID)
|
|
d.Set("name", network.Name)
|
|
d.Set("scope", network.Scope)
|
|
d.Set("driver", network.Driver)
|
|
d.Set("options", network.Options)
|
|
d.Set("internal", network.Internal)
|
|
if err = d.Set("ipam_config", flattenIpamConfig(network.IPAM.Config)); err != nil {
|
|
log.Printf("[WARN] failed to set ipam config from API: %s", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func flattenIpamConfig(in []network.IPAMConfig) []ipamMap {
|
|
ipam := make([]ipamMap, len(in))
|
|
for i, config := range in {
|
|
ipam[i] = ipamMap{
|
|
"subnet": config.Subnet,
|
|
"gateway": config.Gateway,
|
|
"aux_address": config.AuxAddress,
|
|
"ip_range": config.IPRange,
|
|
}
|
|
}
|
|
|
|
return ipam
|
|
}
|