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
This commit is contained in:
Manuel Vogel 2021-05-26 08:35:46 +02:00 committed by GitHub
parent ebe61896e9
commit 2d889c77d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 6 deletions

View file

@ -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{}

View file

@ -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() {

View file

@ -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
}