Skip to content

Commit

Permalink
Fix Error when setting nic state
Browse files Browse the repository at this point in the history
  • Loading branch information
outscale-toa authored and outscale-rce committed Sep 11, 2024
1 parent b0b4ca4 commit 47497a6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 53 deletions.
30 changes: 7 additions & 23 deletions outscale/data_source_outscale_nic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down
28 changes: 23 additions & 5 deletions outscale/instance_set_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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 {
Expand Down Expand Up @@ -105,15 +123,15 @@ 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(),
"private_dns_name": p.GetPrivateDnsName(),
"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)
}
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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(),
Expand All @@ -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)
}
Expand Down
28 changes: 3 additions & 25 deletions outscale/resource_outscale_nic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 47497a6

Please sign in to comment.