docker_network data source (#87). Closes #84

* Adds docker_network data source
* Adds documentation for the docker_network data source
* Adding basic network data source test
This commit is contained in:
Nemo 2019-05-27 00:50:01 +05:30 committed by Manuel Vogel
parent 8ad1a13c08
commit abcdd9ac29
4 changed files with 182 additions and 0 deletions

View file

@ -0,0 +1,108 @@
package docker
import (
"context"
"fmt"
"github.com/docker/docker/api/types"
"github.com/hashicorp/terraform/helper/schema"
)
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,
Optional: true,
ForceNew: true,
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,
},
},
},
Set: resourceDockerIpamConfigHash,
},
"scope": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
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)
d.Set("imap_config", network.IPAM)
return nil
}

View file

@ -0,0 +1,31 @@
package docker
import (
"testing"
"github.com/hashicorp/terraform/helper/resource"
)
func TestAccDockerNetworkDataSource_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDockerNetworkDataSourceConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.docker_network.bridge", "name", "bridge"),
resource.TestCheckResourceAttr("data.docker_network.bridge", "driver", "bridge"),
resource.TestCheckResourceAttr("data.docker_network.bridge", "internal", "false"),
resource.TestCheckResourceAttr("data.docker_network.bridge", "scope", "local"),
),
},
},
})
}
const testAccDockerNetworkDataSourceConfig = `
data "docker_network" "bridge" {
name = "bridge"
}
`

View file

@ -109,6 +109,7 @@ func Provider() terraform.ResourceProvider {
DataSourcesMap: map[string]*schema.Resource{
"docker_registry_image": dataSourceDockerRegistryImage(),
"docker_network": dataSourceDockerNetwork(),
},
ConfigureFunc: providerConfigure,

View file

@ -0,0 +1,42 @@
---
layout: "docker"
page_title: "Docker: docker_network"
sidebar_current: "docs-docker-datasource-docker-network"
description: |-
`docker_network` provides details about a specific Docker Network.
---
# docker\_network
Finds a specific docker network and returns information about it.
## Example Usage
```hcl
data "docker_network" "main" {
name = "main"
}
```
## Argument Reference
The following arguments are supported:
* `name` - (Optional, string) The name of the Docker network.
* `id` - (Optional, string) The id of the Docker network.
## Attributes Reference
The following attributes are exported in addition to the above configuration:
* `driver` - (Optional, string) The driver of the Docker network.
Possible values are `bridge`, `host`, `overlay`, `macvlan`.
See [docker docs][networkdocs] for more details.
* `options` - (Optional, map) Only available with bridge networks. See
[docker docs][bridgeoptionsdocs] for more details.
* `internal` (Optional, bool) Boolean flag for whether the network is internal.
* `ipam_config` (Optional, map) See [IPAM](#ipam) below for details.
* `scope` (Optional, string) Scope of the network. One of `swarm`, `global`, or `local`.
[networkdocs] https://docs.docker.com/network/#network-drivers
[bridgeoptionsdocs] https://docs.docker.com/engine/reference/commandline/network_create/#bridge-driver-options