From 2d889c77d7b6d71e50de76bfd08149811dd86a7d Mon Sep 17 00:00:00 2001 From: Manuel Vogel Date: Wed, 26 May 2021 08:35:46 +0200 Subject: [PATCH] fix: add service host flattener with space split (#205) * fix: add service host flattener with space split * fix(service): switch host and ip in flatten extra hosts --- .../resource_docker_container_funcs.go | 2 +- .../resource_docker_container_structures.go | 2 +- .../resource_docker_service_structures.go | 24 +++++++++++++++---- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/internal/provider/resource_docker_container_funcs.go b/internal/provider/resource_docker_container_funcs.go index 532f3195..8976d5d2 100644 --- a/internal/provider/resource_docker_container_funcs.go +++ b/internal/provider/resource_docker_container_funcs.go @@ -97,7 +97,7 @@ func resourceDockerContainerCreate(ctx context.Context, d *schema.ResourceData, } extraHosts := []string{} if v, ok := d.GetOk("host"); ok { - extraHosts = extraHostsSetToDockerExtraHosts(v.(*schema.Set)) + extraHosts = extraHostsSetToContainerExtraHosts(v.(*schema.Set)) } extraUlimits := []*units.Ulimit{} diff --git a/internal/provider/resource_docker_container_structures.go b/internal/provider/resource_docker_container_structures.go index 32529bd0..916af191 100644 --- a/internal/provider/resource_docker_container_structures.go +++ b/internal/provider/resource_docker_container_structures.go @@ -169,7 +169,7 @@ func ulimitsToDockerUlimits(extraUlimits *schema.Set) []*units.Ulimit { return retExtraUlimits } -func extraHostsSetToDockerExtraHosts(extraHosts *schema.Set) []string { +func extraHostsSetToContainerExtraHosts(extraHosts *schema.Set) []string { retExtraHosts := []string{} for _, hostInt := range extraHosts.List() { diff --git a/internal/provider/resource_docker_service_structures.go b/internal/provider/resource_docker_service_structures.go index b6d511b8..dc0fe8e3 100644 --- a/internal/provider/resource_docker_service_structures.go +++ b/internal/provider/resource_docker_service_structures.go @@ -288,9 +288,10 @@ func flattenServiceHosts(in []string) *schema.Set { out := make([]interface{}, len(in)) for i, v := range in { m := make(map[string]interface{}) - split := strings.Split(v, ":") - m["host"] = split[0] - m["ip"] = split[1] + split := strings.Split(v, " ") + log.Println("[DEBUG] got service hostnames to split:", split) + m["ip"] = split[0] + m["host"] = split[1] out[i] = m } taskSpecResource := resourceDockerService().Schema["task_spec"].Elem.(*schema.Resource) @@ -856,7 +857,7 @@ func createContainerSpec(v interface{}) (*swarm.ContainerSpec, error) { } } if value, ok := rawContainerSpec["hosts"]; ok { - containerSpec.Hosts = extraHostsSetToDockerExtraHosts(value.(*schema.Set)) + containerSpec.Hosts = extraHostsSetToServiceExtraHosts(value.(*schema.Set)) } if value, ok := rawContainerSpec["dns_config"]; ok { containerSpec.DNSConfig = &swarm.DNSConfig{} @@ -1335,3 +1336,18 @@ func mapSetToPlacementPlatforms(stringSet *schema.Set) []swarm.Platform { return ret } + +func extraHostsSetToServiceExtraHosts(extraHosts *schema.Set) []string { + retExtraHosts := []string{} + + for _, hostInt := range extraHosts.List() { + host := hostInt.(map[string]interface{}) + ip := host["ip"].(string) + hostname := host["host"].(string) + // the delimiter is a 'space' + hostname and ip are switched + // see https://github.com/kreuzwerker/terraform-provider-docker/issues/202#issuecomment-847715879 + retExtraHosts = append(retExtraHosts, ip+" "+hostname) + } + + return retExtraHosts +}