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-05-21 08:30:56 -04:00
Description : "`docker_network` provides details about a specific Docker Network." ,
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" : {
2021-05-21 08:30:56 -04:00
Type : schema . TypeString ,
Description : "The name of the Docker network." ,
Required : true ,
2019-05-26 15:20:01 -04:00
} ,
2020-12-02 06:06:39 -05:00
"id" : {
2019-05-26 15:20:01 -04:00
Type : schema . TypeString ,
2021-05-21 08:30:56 -04:00
Computed : true ,
2019-05-26 15:20:01 -04:00
} ,
2020-12-02 06:06:39 -05:00
"driver" : {
2021-05-21 08:30:56 -04:00
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 ,
2019-05-26 15:20:01 -04:00
} ,
2020-12-02 06:06:39 -05:00
"options" : {
2021-05-21 08:30:56 -04:00
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 ,
2019-05-26 15:20:01 -04:00
} ,
2020-12-02 06:06:39 -05:00
"internal" : {
2021-05-21 08:30:56 -04:00
Type : schema . TypeBool ,
Description : "If `true`, the network is internal." ,
Computed : true ,
2019-05-26 15:20:01 -04:00
} ,
2020-12-02 06:06:39 -05:00
"ipam_config" : {
2021-05-21 08:30:56 -04:00
Type : schema . TypeSet ,
Description : "The IPAM configuration options" ,
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" : {
2021-05-21 08:30:56 -04:00
Type : schema . TypeString ,
Description : "The subnet in CIDR form" ,
Optional : true ,
ForceNew : true ,
2019-05-26 15:20:01 -04:00
} ,
2020-12-02 06:06:39 -05:00
"ip_range" : {
2021-05-21 08:30:56 -04:00
Type : schema . TypeString ,
Description : "The ip range in CIDR form" ,
Optional : true ,
ForceNew : true ,
2019-05-26 15:20:01 -04:00
} ,
2020-12-02 06:06:39 -05:00
"gateway" : {
2021-05-21 08:30:56 -04:00
Type : schema . TypeString ,
Description : "The IP address of the gateway" ,
Optional : true ,
ForceNew : true ,
2019-05-26 15:20:01 -04:00
} ,
2020-12-02 06:06:39 -05:00
"aux_address" : {
2021-05-21 08:30:56 -04:00
Type : schema . TypeMap ,
Description : "Auxiliary IPv4 or IPv6 addresses used by Network driver" ,
Optional : true ,
ForceNew : true ,
2019-05-26 15:20:01 -04:00
} ,
} ,
} ,
} ,
2020-12-02 06:06:39 -05:00
"scope" : {
2021-05-21 08:30:56 -04:00
Type : schema . TypeString ,
Description : "Scope of the network. One of `swarm`, `global`, or `local`." ,
Computed : true ,
2019-05-26 15:20:01 -04:00
} ,
} ,
}
}
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
}