2019-05-26 15:20:01 -04:00
|
|
|
package docker
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
2019-11-23 08:42:05 -05:00
|
|
|
|
2019-05-26 15:20:01 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2019-10-09 14:25:38 -04:00
|
|
|
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
|
2019-05-26 15:20:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func dataSourceDockerNetwork() *schema.Resource {
|
|
|
|
|
return &schema.Resource{
|
|
|
|
|
Read: dataSourceDockerNetworkRead,
|
|
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
|
"name": &schema.Schema{
|
|
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Optional: true,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"id": &schema.Schema{
|
|
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Optional: true,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"driver": &schema.Schema{
|
|
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Computed: true,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"options": &schema.Schema{
|
|
|
|
|
Type: schema.TypeMap,
|
|
|
|
|
Computed: true,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"internal": &schema.Schema{
|
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
|
Computed: true,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"ipam_config": &schema.Schema{
|
|
|
|
|
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{
|
|
|
|
|
"subnet": &schema.Schema{
|
|
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"ip_range": &schema.Schema{
|
|
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"gateway": &schema.Schema{
|
|
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"aux_address": &schema.Schema{
|
|
|
|
|
Type: schema.TypeMap,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"scope": &schema.Schema{
|
|
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Computed: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-01 07:09:36 -05:00
|
|
|
type ipamMap map[string]interface{}
|
|
|
|
|
|
2019-05-26 15:20:01 -04:00
|
|
|
func dataSourceDockerNetworkRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
|
|
|
|
|
|
name, nameOk := d.GetOk("name")
|
|
|
|
|
_, idOk := d.GetOk("id")
|
|
|
|
|
|
|
|
|
|
if !nameOk && !idOk {
|
|
|
|
|
return fmt.Errorf("One of id or name must be assigned")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client := meta.(*ProviderConfig).DockerClient
|
|
|
|
|
|
|
|
|
|
network, err := client.NetworkInspect(context.Background(), name.(string), types.NetworkInspectOptions{})
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.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)
|
2020-02-01 07:09:36 -05:00
|
|
|
ipam := make([]ipamMap, len(network.IPAM.Config))
|
|
|
|
|
for i, config := range network.IPAM.Config {
|
|
|
|
|
ipam[i] = ipamMap{
|
|
|
|
|
"subnet": config.Subnet,
|
|
|
|
|
"gateway": config.Gateway,
|
|
|
|
|
"aux_address": config.AuxAddress,
|
|
|
|
|
"ip_range": config.IPRange,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
err = d.Set("ipam_config", ipam)
|
2019-05-26 15:20:01 -04:00
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|