From ee42aca8f973776608c394bf2ff83dc88f68df72 Mon Sep 17 00:00:00 2001 From: Grzegorz Bernady Date: Fri, 4 Oct 2024 15:36:22 +0200 Subject: [PATCH] Revert "Gcloud2 16070 gcore instance read fix (#145)" This reverts commit 1588d47cac36bf4e932c088f76acbe55bc09ab72. --- gcore/resource_gcore_instance.go | 5 ++++ gcore/resource_gcore_instancev2.go | 44 +++++++++++++++++++++--------- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/gcore/resource_gcore_instance.go b/gcore/resource_gcore_instance.go index 635dae0..1e2e4ed 100644 --- a/gcore/resource_gcore_instance.go +++ b/gcore/resource_gcore_instance.go @@ -604,6 +604,11 @@ func resourceInstanceRead(ctx context.Context, d *schema.ResourceData, m interfa i["type"] = iOpts.Type.String() } + var name string + if iface.Name != nil { + name = *iface.Name + } + i["name"] = name i["network_id"] = iface.NetworkID i["subnet_id"] = subnetID i["port_id"] = iface.PortID diff --git a/gcore/resource_gcore_instancev2.go b/gcore/resource_gcore_instancev2.go index f278b11..2979765 100644 --- a/gcore/resource_gcore_instancev2.go +++ b/gcore/resource_gcore_instancev2.go @@ -13,6 +13,7 @@ import ( "time" gcorecloud "github.com/G-Core/gcorelabscloud-go" + "github.com/G-Core/gcorelabscloud-go/gcore/floatingip/v1/floatingips" "github.com/G-Core/gcorelabscloud-go/gcore/instance/v1/instances" "github.com/G-Core/gcorelabscloud-go/gcore/instance/v1/types" instancesV2 "github.com/G-Core/gcorelabscloud-go/gcore/instance/v2/instances" @@ -221,7 +222,7 @@ inside an instance resource.`, Optional: true, }, "security_groups": { - Type: schema.TypeSet, + Type: schema.TypeList, Required: true, Description: "list of security group IDs, they will be attached to exact interface", Elem: &schema.Schema{Type: schema.TypeString}, @@ -589,7 +590,7 @@ func resourceInstanceV2Read(ctx context.Context, d *schema.ResourceData, m inter for i, sg := range port.SecurityGroups { sgs[i] = sg.ID } - i["security_groups"] = schema.NewSet(sgUniqueIDs, sgs) + i["security_groups"] = sgs } cleanInterfaces = append(cleanInterfaces, i) @@ -649,6 +650,10 @@ func resourceInstanceV2Update(ctx context.Context, d *schema.ResourceData, m int return diag.FromErr(err) } + clientFip, err := CreateClient(provider, d, floatingIPsPoint, versionPointV1) + if err != nil { + return diag.FromErr(err) + } clientSg, err := CreateClient(provider, d, securityGroupPoint, versionPointV1) if err != nil { return diag.FromErr(err) @@ -816,7 +821,9 @@ func resourceInstanceV2Update(ctx context.Context, d *schema.ResourceData, m int // detach what should be detached sgToDetach := make([]string, 0) for _, sg := range port.SecurityGroups { - if !iface["security_groups"].(*schema.Set).Contains(sg.ID) { + if !slices.ContainsFunc(iface["security_groups"].([]interface{}), func(s interface{}) bool { + return s.(string) == sg.ID + }) { sgToDetach = append(sgToDetach, sg.Name) } } @@ -832,7 +839,7 @@ func resourceInstanceV2Update(ctx context.Context, d *schema.ResourceData, m int // attach what should be attached sgToAttach := make([]string, 0) - for _, sg := range iface["security_groups"].(*schema.Set).List() { + for _, sg := range iface["security_groups"].([]interface{}) { if !slices.ContainsFunc(port.SecurityGroups, func(s gcorecloud.ItemIDName) bool { return s.ID == sg.(string) }) { @@ -855,6 +862,24 @@ func resourceInstanceV2Update(ctx context.Context, d *schema.ResourceData, m int log.Printf("[WARNING] Cannot attach security groups: %v", err) } } + + for _, fip := range fips { + log.Printf("[DEBUG] Reassign floatin IP %s to fixed IP %s port id %s", fip.FloatingIPAddress, fip.FixedIPAddress, fip.PortID) + mm := make(map[string]string) + for _, i := range fip.Metadata { + mm[i.Key] = i.Value + } + + _, err := floatingips.Assign(clientFip, fip.ID, floatingips.CreateOpts{ + PortID: fip.PortID, + FixedIPAddress: fip.FixedIPAddress, + Metadata: mm, + }).Extract() + + if err != nil { + return diag.Errorf("cannot reassign floating IP %s to fixed IP %s port id %s. Error: %v", fip.FloatingIPAddress, fip.FixedIPAddress, fip.PortID, err) + } + } } if d.HasChange("volume") { @@ -930,7 +955,7 @@ func resourceInstanceV2Update(ctx context.Context, d *schema.ResourceData, m int func instanceInterfaceUniqueID(i interface{}) int { e := i.(map[string]interface{}) h := md5.New() - securitygroupsRaw := e["security_groups"].(*schema.Set).List() + securitygroupsRaw := e["security_groups"].([]interface{}) var securitygroups string for _, sg := range securitygroupsRaw { securitygroups += sg.(string) @@ -947,13 +972,6 @@ func instanceInterfaceUniqueIDByName(i interface{}) int { return int(binary.BigEndian.Uint64(h.Sum(nil))) } -func sgUniqueIDs(i interface{}) int { - e := i.(string) - h := md5.New() - io.WriteString(h, e) - return int(binary.BigEndian.Uint64(h.Sum(nil))) -} - func attachNewInterface(i interface{}, client *gcorecloud.ServiceClient, instanceID string) error { iface := i.(map[string]interface{}) iType := types.InterfaceType(iface["type"].(string)) @@ -975,7 +993,7 @@ func attachNewInterface(i interface{}, client *gcorecloud.ServiceClient, instanc opts.PortID = iface["port_id"].(string) } - rawSgsID := iface["security_groups"].(*schema.Set).List() + rawSgsID := iface["security_groups"].([]interface{}) sgs := make([]gcorecloud.ItemID, len(rawSgsID)) for i, sgID := range rawSgsID { sgs[i] = gcorecloud.ItemID{ID: sgID.(string)}