diff --git a/outscale/data_source_outscale_nic.go b/outscale/data_source_outscale_nic.go index edfaa55a5..f49a10025 100644 --- a/outscale/data_source_outscale_nic.go +++ b/outscale/data_source_outscale_nic.go @@ -54,6 +54,10 @@ func DataSourceOutscaleNic() *schema.Resource { Computed: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ + "public_ip": { + Type: schema.TypeString, + Computed: true, + }, "public_ip_id": { Type: schema.TypeString, Computed: true, @@ -309,31 +313,11 @@ func DataSourceOutscaleNicRead(d *schema.ResourceData, meta interface{}) error { if err := d.Set("private_dns_name", eni.GetPrivateDnsName()); err != nil { return err } - - y := make([]map[string]interface{}, len(eni.GetPrivateIps())) - if eni.PrivateIps != nil { - for k, v := range eni.GetPrivateIps() { - b := make(map[string]interface{}) - if assoc, ok := v.GetLinkPublicIpOk(); ok { - d := make(map[string]interface{}) - d["public_ip_id"] = assoc.GetPublicIpId() - d["link_public_ip_id"] = assoc.GetLinkPublicIpId() - d["public_ip_account_id"] = assoc.GetPublicIpAccountId() - d["public_dns_name"] = assoc.GetPublicDnsName() - d["public_ip"] = assoc.GetPublicIp() - b["link_public_ip"] = d - } - b["private_dns_name"] = v.GetPrivateDnsName() - b["private_ip"] = v.GetPrivateIp() - b["is_primary"] = v.GetIsPrimary() - y[k] = b + if privIps, ok := eni.GetPrivateIpsOk(); ok { + if err := d.Set("private_ips", getOAPIPrivateIPsForNic(*privIps)); err != nil { + return err } } - - if err := d.Set("private_ips", y); err != nil { - return err - } - if err := d.Set("is_source_dest_checked", eni.GetIsSourceDestChecked()); err != nil { return err } diff --git a/outscale/instance_set_set.go b/outscale/instance_set_set.go index f1af1440a..67d748249 100644 --- a/outscale/instance_set_set.go +++ b/outscale/instance_set_set.go @@ -40,6 +40,16 @@ func getOAPILinkNic(l oscgo.LinkNic) []map[string]interface{} { }} } +func getOAPILinkPublicIPsForNic(l oscgo.LinkPublicIp) []map[string]interface{} { + return []map[string]interface{}{{ + "link_public_ip_id": l.GetLinkPublicIpId(), + "public_dns_name": l.GetPublicDnsName(), + "public_ip": l.GetPublicIp(), + "public_ip_account_id": l.GetPublicIpAccountId(), + "public_ip_id": l.GetPublicIpId(), + }} +} + func getOAPIBsuSet(bsu oscgo.BsuCreated) []map[string]interface{} { return []map[string]interface{}{{ "delete_on_vm_deletion": bsu.GetDeleteOnVmDeletion(), @@ -49,6 +59,14 @@ func getOAPIBsuSet(bsu oscgo.BsuCreated) []map[string]interface{} { }} } +func getOAPILinkPublicIpsForVm(l oscgo.LinkPublicIpLightForVm) []map[string]interface{} { + return []map[string]interface{}{{ + "public_dns_name": l.GetPublicDnsName(), + "public_ip": l.GetPublicIp(), + "public_ip_account_id": l.GetPublicIpAccountId(), + }} +} + func getOAPILinkPublicIPLight(l oscgo.LinkPublicIpLightForVm) *schema.Set { res := &schema.Set{ F: func(v interface{}) int { @@ -105,7 +123,7 @@ func getOAPIPrivateIPsLight(privateIPs []oscgo.PrivateIpLightForVm) *schema.Set return res } -func getOAPIPrivateIPs(privateIPs []oscgo.PrivateIp) (res []map[string]interface{}) { +func getOAPIPrivateIPsForNic(privateIPs []oscgo.PrivateIp) (res []map[string]interface{}) { for _, p := range privateIPs { r := map[string]interface{}{ "is_primary": p.GetIsPrimary(), @@ -113,7 +131,7 @@ func getOAPIPrivateIPs(privateIPs []oscgo.PrivateIp) (res []map[string]interface "private_ip": p.GetPrivateIp(), } if _, ok := p.GetLinkPublicIpOk(); ok { - r["link_public_ip"] = getOAPILinkPublicIP(p.GetLinkPublicIp()) + r["link_public_ip"] = getOAPILinkPublicIPsForNic(p.GetLinkPublicIp()) } res = append(res, r) } @@ -144,7 +162,7 @@ func getOAPIVMNetworkInterfaceLightSet(nics []oscgo.NicLight) (primaryNic []map[ } if nic.HasLinkPublicIp() { - nicMap["link_public_ip"] = getOAPILinkPublicIPLight(nic.GetLinkPublicIp()) + nicMap["link_public_ip"] = getOAPILinkPublicIpsForVm(nic.GetLinkPublicIp()) } if nic.HasPrivateIps() { @@ -174,7 +192,7 @@ func getOAPIVMNetworkInterfaceSet(nics []oscgo.Nic) (res []map[string]interface{ "net_id": nic.GetNetId(), "nic_id": nic.GetNicId(), "private_dns_name": nic.GetPrivateDnsName(), - "private_ips": getOAPIPrivateIPs(nic.GetPrivateIps()), + "private_ips": getOAPIPrivateIPsForNic(nic.GetPrivateIps()), "security_groups": securityGroups, "state": nic.GetState(), "subnet_id": nic.GetSubnetId(), @@ -185,7 +203,7 @@ func getOAPIVMNetworkInterfaceSet(nics []oscgo.Nic) (res []map[string]interface{ r["link_nic"] = getOAPILinkNic(nic.GetLinkNic()) } if _, ok := nic.GetLinkPublicIpOk(); ok { - r["link_public_ip"] = getOAPILinkPublicIP(nic.GetLinkPublicIp()) + r["link_public_ip"] = getOAPILinkPublicIPsForNic(nic.GetLinkPublicIp()) } res = append(res, r) } diff --git a/outscale/resource_outscale_nic.go b/outscale/resource_outscale_nic.go index 24c85bd8a..8ab1fb02f 100644 --- a/outscale/resource_outscale_nic.go +++ b/outscale/resource_outscale_nic.go @@ -362,33 +362,11 @@ func ResourceOutscaleNicRead(d *schema.ResourceData, meta interface{}) error { if err := d.Set("private_dns_name", eni.GetPrivateDnsName()); err != nil { return err } - - privateIps := make([]map[string]interface{}, len(eni.GetPrivateIps())) - if eni.PrivateIps != nil { - for k, v := range eni.GetPrivateIps() { - privIp := make(map[string]interface{}) - - if assoc, ok := v.GetLinkPublicIpOk(); ok { - linkPubIp := []map[string]interface{}{{ - "public_ip_id": assoc.GetPublicIpId(), - "link_public_ip_id": assoc.GetLinkPublicIpId(), - "public_ip_account_id": assoc.GetPublicIpAccountId(), - "public_dns_name": assoc.GetPublicDnsName(), - "public_ip": assoc.GetPublicIp(), - }} - privIp["link_public_ip"] = linkPubIp - } - privIp["private_dns_name"] = v.GetPrivateDnsName() - privIp["private_ip"] = v.GetPrivateIp() - privIp["is_primary"] = v.GetIsPrimary() - - privateIps[k] = privIp + if privIps, ok := eni.GetPrivateIpsOk(); ok { + if err := d.Set("private_ips", getOAPIPrivateIPsForNic(*privIps)); err != nil { + return err } } - if err := d.Set("private_ips", privateIps); err != nil { - return err - } - if err := d.Set("is_source_dest_checked", eni.GetIsSourceDestChecked()); err != nil { return err }