From cf8568fe55f9356f5b089181e0731551d5730fad Mon Sep 17 00:00:00 2001 From: Januar Date: Mon, 22 May 2017 17:20:32 +0400 Subject: [PATCH] provider/docker network alias (#14710) * Add Network Alias configuration with network options * Handle case where there's no network option * Handle use case where network option is not available * Handle use case where network option is not available * Network alias only on user defined network * Update documentation for docker provider on network aliases * Remove unused variable * Update documentation * add unit test for docker container network * fix unit test for docker container network --- builtin/providers/docker/resource_docker_container.go | 8 ++++++++ .../docker/resource_docker_container_funcs.go | 9 ++++++++- .../docker/resource_docker_container_test.go | 11 +++++++++++ .../docs/providers/docker/r/container.html.markdown | 1 + 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/builtin/providers/docker/resource_docker_container.go b/builtin/providers/docker/resource_docker_container.go index a084ad434..543dc930c 100644 --- a/builtin/providers/docker/resource_docker_container.go +++ b/builtin/providers/docker/resource_docker_container.go @@ -376,6 +376,14 @@ func resourceDockerContainer() *schema.Resource { ForceNew: true, }, + "network_alias": &schema.Schema{ + Type: schema.TypeSet, + Optional: true, + ForceNew: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + }, + "network_mode": &schema.Schema{ Type: schema.TypeString, Optional: true, diff --git a/builtin/providers/docker/resource_docker_container_funcs.go b/builtin/providers/docker/resource_docker_container_funcs.go index ba7d54aa2..4a494c5bd 100644 --- a/builtin/providers/docker/resource_docker_container_funcs.go +++ b/builtin/providers/docker/resource_docker_container_funcs.go @@ -188,7 +188,14 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err d.SetId(retContainer.ID) if v, ok := d.GetOk("networks"); ok { - connectionOpts := dc.NetworkConnectionOptions{Container: retContainer.ID} + var connectionOpts dc.NetworkConnectionOptions + if v, ok := d.GetOk("network_alias"); ok { + endpointConfig := &dc.EndpointConfig{} + endpointConfig.Aliases = stringSetToStringSlice(v.(*schema.Set)) + connectionOpts = dc.NetworkConnectionOptions{Container: retContainer.ID, EndpointConfig: endpointConfig} + } else { + connectionOpts = dc.NetworkConnectionOptions{Container: retContainer.ID} + } for _, rawNetwork := range v.(*schema.Set).List() { network := rawNetwork.(string) diff --git a/builtin/providers/docker/resource_docker_container_test.go b/builtin/providers/docker/resource_docker_container_test.go index e9c3bb950..dcc6affa8 100644 --- a/builtin/providers/docker/resource_docker_container_test.go +++ b/builtin/providers/docker/resource_docker_container_test.go @@ -203,6 +203,10 @@ func TestAccDockerContainer_customized(t *testing.T) { return fmt.Errorf("Container has incorrect extra host string: %q", c.HostConfig.ExtraHosts[1]) } + if _, ok := c.NetworkSettings.Networks["test"]; !ok { + return fmt.Errorf("Container is not connected to the right user defined network: test") + } + return nil } @@ -370,6 +374,9 @@ resource "docker_container" "foo" { } network_mode = "bridge" + networks = ["${docker_network.test_network.name}"] + network_alias = ["tftest"] + host { host = "testhost" ip = "10.0.1.0" @@ -380,6 +387,10 @@ resource "docker_container" "foo" { ip = "10.0.2.0" } } + +resource "docker_network" "test_network" { + name = "test" +} ` const testAccDockerContainerUploadConfig = ` diff --git a/website/source/docs/providers/docker/r/container.html.markdown b/website/source/docs/providers/docker/r/container.html.markdown index d0996c992..a267cb244 100644 --- a/website/source/docs/providers/docker/r/container.html.markdown +++ b/website/source/docs/providers/docker/r/container.html.markdown @@ -77,6 +77,7 @@ The following arguments are supported: Defaults to "json-file". * `log_opts` - (Optional, map of strings) Key/value pairs to use as options for the logging driver. +* `network_alias` - (Optional, set of strings) Network aliases of the container for user-defined networks only. * `network_mode` - (Optional, string) Network mode of the container. * `networks` - (Optional, set of strings) Id of the networks in which the container is.