From 7a24764c150fad36c7b048dd77d94e6431e182f6 Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Fri, 9 Oct 2015 14:05:43 +0100 Subject: [PATCH 1/3] provider/docker: Add hosts parameter for containers --- .../docker/resource_docker_container.go | 37 +++++++++++++++++++ .../docker/resource_docker_container_funcs.go | 22 ++++++++++- .../docker/r/container.html.markdown | 11 ++++++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/builtin/providers/docker/resource_docker_container.go b/builtin/providers/docker/resource_docker_container.go index 242462e1a..6ad5176ac 100644 --- a/builtin/providers/docker/resource_docker_container.go +++ b/builtin/providers/docker/resource_docker_container.go @@ -130,6 +130,28 @@ func resourceDockerContainer() *schema.Resource { Set: resourceDockerPortsHash, }, + "hosts": &schema.Schema{ + Type: schema.TypeSet, + Optional: true, + ForceNew: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "ip": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + + "host": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + }, + }, + Set: resourceDockerHostsHash, + }, + "env": &schema.Schema{ Type: schema.TypeSet, Optional: true, @@ -323,6 +345,21 @@ func resourceDockerPortsHash(v interface{}) int { return hashcode.String(buf.String()) } +func resourceDockerHostsHash(v interface{}) int { + var buf bytes.Buffer + m := v.(map[string]interface{}) + + if v, ok := m["ip"]; ok { + buf.WriteString(fmt.Sprintf("%v-", v.(string))) + } + + if v, ok := m["host"]; ok { + buf.WriteString(fmt.Sprintf("%v-", v.(string))) + } + + return hashcode.String(buf.String()) +} + func resourceDockerVolumesHash(v interface{}) int { var buf bytes.Buffer m := v.(map[string]interface{}) diff --git a/builtin/providers/docker/resource_docker_container_funcs.go b/builtin/providers/docker/resource_docker_container_funcs.go index 814941bba..bf2bc9e2f 100644 --- a/builtin/providers/docker/resource_docker_container_funcs.go +++ b/builtin/providers/docker/resource_docker_container_funcs.go @@ -67,6 +67,11 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err createOpts.Config.ExposedPorts = exposedPorts } + extraHosts := []string{} + if v, ok := d.GetOk("extra_hosts"); ok { + extraHosts = extraHostsSetToDockerExtraHosts(v.(*schema.Set)) + } + volumes := map[string]struct{}{} binds := []string{} volumesFrom := []string{} @@ -100,7 +105,9 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err if len(portBindings) != 0 { hostConfig.PortBindings = portBindings } - + if len(extraHosts) != 0 { + hostConfig.ExtraHosts = extraHosts + } if len(binds) != 0 { hostConfig.Binds = binds } @@ -312,6 +319,19 @@ func portSetToDockerPorts(ports *schema.Set) (map[dc.Port]struct{}, map[dc.Port] return retExposedPorts, retPortBindings } +func extraHostsSetToDockerExtraHosts(extraHosts *schema.Set) []string { + retExtraHosts := []string{} + + for _, hostInt := range extraHosts.List() { + host := hostInt.(map[string]interface{}) + ip := host["ip"].(string) + hostname := host["host"].(string) + retExtraHosts = append(retExtraHosts, hostname+":"+ip) + } + + return retExtraHosts +} + func volumeSetToDockerVolumes(volumes *schema.Set) (map[string]struct{}, []string, []string, error) { retVolumeMap := map[string]struct{}{} retHostConfigBinds := []string{} diff --git a/website/source/docs/providers/docker/r/container.html.markdown b/website/source/docs/providers/docker/r/container.html.markdown index 920288eb2..2f23d3db3 100644 --- a/website/source/docs/providers/docker/r/container.html.markdown +++ b/website/source/docs/providers/docker/r/container.html.markdown @@ -57,6 +57,7 @@ The following arguments are supported: kept running. If false, then as long as the container exists, Terraform assumes it is successful. * `ports` - (Optional) See [Ports](#ports) below for details. +* `extra_hosts` - (Optional) See [Extra Hosts](#extra_hosts) below for details. * `privileged` - (Optional, bool) Run container in privileged mode. * `publish_all_ports` - (Optional, bool) Publish all ports of the container. * `volumes` - (Optional) See [Volumes](#volumes) below for details. @@ -82,6 +83,16 @@ the following: * `protocol` - (Optional, string) Protocol that can be used over this port, defaults to TCP. + +## Extra Hosts + +`extra_hosts` is a block within the configuration that can be repeated to specify +the extra host mappings for the container. Each `extra_hosts` block supports +the following: + +* `host` - (Required, int) Hostname to add. +* `ip` - (Required, int) IP address this hostname should resolve to.. + ## Volumes From 52d178b7ac7f378cddc64d1395f1c0c86cf2ebd0 Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Thu, 3 Dec 2015 10:51:59 +0000 Subject: [PATCH 2/3] provider/docker: Inline ports and volumes schemas for consistency --- .../docker/resource_docker_container.go | 122 ++++++++---------- 1 file changed, 57 insertions(+), 65 deletions(-) diff --git a/builtin/providers/docker/resource_docker_container.go b/builtin/providers/docker/resource_docker_container.go index 6ad5176ac..76b3e153d 100644 --- a/builtin/providers/docker/resource_docker_container.go +++ b/builtin/providers/docker/resource_docker_container.go @@ -118,16 +118,69 @@ func resourceDockerContainer() *schema.Resource { Type: schema.TypeSet, Optional: true, ForceNew: true, - Elem: getVolumesElem(), - Set: resourceDockerVolumesHash, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "from_container": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + + "container_path": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + + "host_path": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + + "read_only": &schema.Schema{ + Type: schema.TypeBool, + Optional: true, + ForceNew: true, + }, + }, + }, + Set: resourceDockerVolumesHash, }, "ports": &schema.Schema{ Type: schema.TypeSet, Optional: true, ForceNew: true, - Elem: getPortsElem(), - Set: resourceDockerPortsHash, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "internal": &schema.Schema{ + Type: schema.TypeInt, + Required: true, + ForceNew: true, + }, + + "external": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + }, + + "ip": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + + "protocol": &schema.Schema{ + Type: schema.TypeString, + Default: "tcp", + Optional: true, + ForceNew: true, + }, + }, + }, + Set: resourceDockerPortsHash, }, "hosts": &schema.Schema{ @@ -263,67 +316,6 @@ func resourceDockerContainer() *schema.Resource { } } -func getVolumesElem() *schema.Resource { - return &schema.Resource{ - Schema: map[string]*schema.Schema{ - "from_container": &schema.Schema{ - Type: schema.TypeString, - Optional: true, - ForceNew: true, - }, - - "container_path": &schema.Schema{ - Type: schema.TypeString, - Optional: true, - ForceNew: true, - }, - - "host_path": &schema.Schema{ - Type: schema.TypeString, - Optional: true, - ForceNew: true, - }, - - "read_only": &schema.Schema{ - Type: schema.TypeBool, - Optional: true, - ForceNew: true, - }, - }, - } -} - -func getPortsElem() *schema.Resource { - return &schema.Resource{ - Schema: map[string]*schema.Schema{ - "internal": &schema.Schema{ - Type: schema.TypeInt, - Required: true, - ForceNew: true, - }, - - "external": &schema.Schema{ - Type: schema.TypeInt, - Optional: true, - ForceNew: true, - }, - - "ip": &schema.Schema{ - Type: schema.TypeString, - Optional: true, - ForceNew: true, - }, - - "protocol": &schema.Schema{ - Type: schema.TypeString, - Default: "tcp", - Optional: true, - ForceNew: true, - }, - }, - } -} - func resourceDockerPortsHash(v interface{}) int { var buf bytes.Buffer m := v.(map[string]interface{}) From 3380f08e5a3a5fa10e71b07e19d12f7b7a0c596b Mon Sep 17 00:00:00 2001 From: James Nugent Date: Fri, 15 Jan 2016 02:59:07 +0000 Subject: [PATCH 3/3] provider/docker: Tweak and test `host_entry` This adds acceptance tests for specifying extra hosts on Docker containers. It also renames the repeating block from `hosts` to `host`, which reads more naturally in the schema when multiple instances of the block are declared. --- .../docker/resource_docker_container.go | 2 +- .../docker/resource_docker_container_funcs.go | 2 +- .../docker/resource_docker_container_test.go | 51 ++++++++++++++----- .../docker/r/container.html.markdown | 9 ++-- 4 files changed, 45 insertions(+), 19 deletions(-) diff --git a/builtin/providers/docker/resource_docker_container.go b/builtin/providers/docker/resource_docker_container.go index 3ee87cf76..c8f363e3f 100644 --- a/builtin/providers/docker/resource_docker_container.go +++ b/builtin/providers/docker/resource_docker_container.go @@ -184,7 +184,7 @@ func resourceDockerContainer() *schema.Resource { Set: resourceDockerPortsHash, }, - "hosts": &schema.Schema{ + "host": &schema.Schema{ Type: schema.TypeSet, Optional: true, ForceNew: true, diff --git a/builtin/providers/docker/resource_docker_container_funcs.go b/builtin/providers/docker/resource_docker_container_funcs.go index 1c3f3d1d3..47878f934 100644 --- a/builtin/providers/docker/resource_docker_container_funcs.go +++ b/builtin/providers/docker/resource_docker_container_funcs.go @@ -68,7 +68,7 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err } extraHosts := []string{} - if v, ok := d.GetOk("extra_hosts"); ok { + if v, ok := d.GetOk("host"); ok { extraHosts = extraHostsSetToDockerExtraHosts(v.(*schema.Set)) } diff --git a/builtin/providers/docker/resource_docker_container_test.go b/builtin/providers/docker/resource_docker_container_test.go index a5c36a5c2..aa1eee961 100644 --- a/builtin/providers/docker/resource_docker_container_test.go +++ b/builtin/providers/docker/resource_docker_container_test.go @@ -72,6 +72,18 @@ func TestAccDockerContainer_customized(t *testing.T) { return fmt.Errorf("Container does not have the correct max-file log option: %v", c.HostConfig.LogConfig.Config["max-file"]) } + if len(c.HostConfig.ExtraHosts) != 2 { + return fmt.Errorf("Container does not have correct number of extra host entries, got %d", len(c.HostConfig.ExtraHosts)) + } + + if c.HostConfig.ExtraHosts[0] != "testhost2:10.0.2.0" { + return fmt.Errorf("Container has incorrect extra host string: %q", c.HostConfig.ExtraHosts[0]) + } + + if c.HostConfig.ExtraHosts[1] != "testhost:10.0.1.0" { + return fmt.Errorf("Container has incorrect extra host string: %q", c.HostConfig.ExtraHosts[1]) + } + return nil } @@ -132,6 +144,7 @@ resource "docker_container" "foo" { image = "${docker_image.foo.latest}" } ` + const testAccDockerContainerCustomizedConfig = ` resource "docker_image" "foo" { name = "nginx:latest" @@ -140,21 +153,31 @@ resource "docker_image" "foo" { resource "docker_container" "foo" { name = "tf-test" image = "${docker_image.foo.latest}" - entrypoint = ["/bin/bash", "-c", "ping localhost"] - restart = "on-failure" - max_retry_count = 5 - memory = 512 - memory_swap = 2048 - cpu_shares = 32 - labels { - env = "prod" - role = "test" - } - log_driver = "json-file" - log_opts = { - max-size = "10m" - max-file = 20 + entrypoint = ["/bin/bash", "-c", "ping localhost"] + restart = "on-failure" + max_retry_count = 5 + memory = 512 + memory_swap = 2048 + cpu_shares = 32 + labels { + env = "prod" + role = "test" + } + log_driver = "json-file" + log_opts = { + max-size = "10m" + max-file = 20 } network_mode = "bridge" + + host { + host = "testhost" + ip = "10.0.1.0" + } + + host { + host = "testhost2" + ip = "10.0.2.0" + } } ` diff --git a/website/source/docs/providers/docker/r/container.html.markdown b/website/source/docs/providers/docker/r/container.html.markdown index 8065a998b..dd39a34ff 100644 --- a/website/source/docs/providers/docker/r/container.html.markdown +++ b/website/source/docs/providers/docker/r/container.html.markdown @@ -57,7 +57,7 @@ The following arguments are supported: kept running. If false, then as long as the container exists, Terraform assumes it is successful. * `ports` - (Optional) See [Ports](#ports) below for details. -* `extra_hosts` - (Optional) See [Extra Hosts](#extra_hosts) below for details. +* `host_entry` - (Optional) See [Extra Hosts](#extra_hosts) below for details. * `privileged` - (Optional, bool) Run container in privileged mode. * `publish_all_ports` - (Optional, bool) Publish all ports of the container. * `volumes` - (Optional) See [Volumes](#volumes) below for details. @@ -88,13 +88,16 @@ the following: ## Extra Hosts -`extra_hosts` is a block within the configuration that can be repeated to specify -the extra host mappings for the container. Each `extra_hosts` block supports +`host_entry` is a block within the configuration that can be repeated to specify +the extra host mappings for the container. Each `host_entry` block supports the following: * `host` - (Required, int) Hostname to add. * `ip` - (Required, int) IP address this hostname should resolve to.. +This is equivalent to using the `--add-host` option when using the `run` +command of the Docker CLI. + ## Volumes