2021-03-18 03:30:54 -04:00
|
|
|
package provider
|
2019-05-26 15:20:01 -04:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2021-04-19 09:33:13 -04:00
|
|
|
"log"
|
2019-11-23 08:42:05 -05:00
|
|
|
|
2019-05-26 15:20:01 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2021-04-19 09:33:13 -04:00
|
|
|
"github.com/docker/docker/api/types/network"
|
2021-03-18 03:30:54 -04:00
|
|
|
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
|
|
|
|
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
2019-05-26 15:20:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func dataSourceDockerNetwork() *schema.Resource {
|
|
|
|
|
return &schema.Resource{
|
2021-03-18 03:30:54 -04:00
|
|
|
ReadContext: dataSourceDockerNetworkRead,
|
2019-05-26 15:20:01 -04:00
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2020-12-02 06:06:39 -05:00
|
|
|
"name": {
|
2019-05-26 15:20:01 -04:00
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Optional: true,
|
|
|
|
|
},
|
|
|
|
|
|
2020-12-02 06:06:39 -05:00
|
|
|
"id": {
|
2019-05-26 15:20:01 -04:00
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Optional: true,
|
|
|
|
|
},
|
|
|
|
|
|
2020-12-02 06:06:39 -05:00
|
|
|
"driver": {
|
2019-05-26 15:20:01 -04:00
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Computed: true,
|
|
|
|
|
},
|
|
|
|
|
|
2020-12-02 06:06:39 -05:00
|
|
|
"options": {
|
2019-05-26 15:20:01 -04:00
|
|
|
Type: schema.TypeMap,
|
|
|
|
|
Computed: true,
|
|
|
|
|
},
|
|
|
|
|
|
2020-12-02 06:06:39 -05:00
|
|
|
"internal": {
|
2019-05-26 15:20:01 -04:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
|
Computed: true,
|
|
|
|
|
},
|
|
|
|
|
|
2020-12-02 06:06:39 -05:00
|
|
|
"ipam_config": {
|
2019-05-26 15:20:01 -04:00
|
|
|
Type: schema.TypeSet,
|
2020-02-01 07:09:36 -05:00
|
|
|
Computed: true,
|
2019-05-26 15:20:01 -04:00
|
|
|
Elem: &schema.Resource{
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2020-12-02 06:06:39 -05:00
|
|
|
"subnet": {
|
2019-05-26 15:20:01 -04:00
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
|
2020-12-02 06:06:39 -05:00
|
|
|
"ip_range": {
|
2019-05-26 15:20:01 -04:00
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
|
2020-12-02 06:06:39 -05:00
|
|
|
"gateway": {
|
2019-05-26 15:20:01 -04:00
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
|
2020-12-02 06:06:39 -05:00
|
|
|
"aux_address": {
|
2019-05-26 15:20:01 -04:00
|
|
|
Type: schema.TypeMap,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
2020-12-02 06:06:39 -05:00
|
|
|
"scope": {
|
2019-05-26 15:20:01 -04:00
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Computed: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-01 07:09:36 -05:00
|
|
|
type ipamMap map[string]interface{}
|
|
|
|
|
|
2021-03-18 03:30:54 -04:00
|
|
|
func dataSourceDockerNetworkRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
2019-05-26 15:20:01 -04:00
|
|
|
name, nameOk := d.GetOk("name")
|
|
|
|
|
_, idOk := d.GetOk("id")
|
|
|
|
|
|
|
|
|
|
if !nameOk && !idOk {
|
2021-03-18 03:30:54 -04:00
|
|
|
return diag.Errorf("One of id or name must be assigned")
|
2019-05-26 15:20:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client := meta.(*ProviderConfig).DockerClient
|
|
|
|
|
|
2021-03-18 03:30:54 -04:00
|
|
|
network, err := client.NetworkInspect(ctx, name.(string), types.NetworkInspectOptions{})
|
2019-05-26 15:20:01 -04:00
|
|
|
if err != nil {
|
2021-03-18 03:30:54 -04:00
|
|
|
return diag.Errorf("Could not find docker network: %s", err)
|
2019-05-26 15:20:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
2021-04-19 09:33:13 -04:00
|
|
|
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 {
|
2020-02-01 07:09:36 -05:00
|
|
|
ipam[i] = ipamMap{
|
|
|
|
|
"subnet": config.Subnet,
|
|
|
|
|
"gateway": config.Gateway,
|
|
|
|
|
"aux_address": config.AuxAddress,
|
|
|
|
|
"ip_range": config.IPRange,
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-26 15:20:01 -04:00
|
|
|
|
2021-04-19 09:33:13 -04:00
|
|
|
return ipam
|
2019-05-26 15:20:01 -04:00
|
|
|
}
|