Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gcloud2 8378 fix reading interface #149

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions gcore/resource_gcore_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,11 +604,6 @@ 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
Expand Down
44 changes: 13 additions & 31 deletions gcore/resource_gcore_instancev2.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ 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"
Expand Down Expand Up @@ -222,7 +221,7 @@ inside an instance resource.`,
Optional: true,
},
"security_groups": {
Type: schema.TypeList,
Type: schema.TypeSet,
Required: true,
Description: "list of security group IDs, they will be attached to exact interface",
Elem: &schema.Schema{Type: schema.TypeString},
Expand Down Expand Up @@ -590,7 +589,7 @@ func resourceInstanceV2Read(ctx context.Context, d *schema.ResourceData, m inter
for i, sg := range port.SecurityGroups {
sgs[i] = sg.ID
}
i["security_groups"] = sgs
i["security_groups"] = schema.NewSet(sgUniqueIDs, sgs)
}

cleanInterfaces = append(cleanInterfaces, i)
Expand Down Expand Up @@ -650,10 +649,6 @@ 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)
Expand Down Expand Up @@ -821,9 +816,7 @@ 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 !slices.ContainsFunc(iface["security_groups"].([]interface{}), func(s interface{}) bool {
return s.(string) == sg.ID
}) {
if !iface["security_groups"].(*schema.Set).Contains(sg.ID) {
sgToDetach = append(sgToDetach, sg.Name)
}
}
Expand All @@ -839,7 +832,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"].([]interface{}) {
for _, sg := range iface["security_groups"].(*schema.Set).List() {
if !slices.ContainsFunc(port.SecurityGroups, func(s gcorecloud.ItemIDName) bool {
return s.ID == sg.(string)
}) {
Expand All @@ -862,24 +855,6 @@ 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") {
Expand Down Expand Up @@ -955,7 +930,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"].([]interface{})
securitygroupsRaw := e["security_groups"].(*schema.Set).List()
var securitygroups string
for _, sg := range securitygroupsRaw {
securitygroups += sg.(string)
Expand All @@ -972,6 +947,13 @@ 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))
Expand All @@ -993,7 +975,7 @@ func attachNewInterface(i interface{}, client *gcorecloud.ServiceClient, instanc
opts.PortID = iface["port_id"].(string)
}

rawSgsID := iface["security_groups"].([]interface{})
rawSgsID := iface["security_groups"].(*schema.Set).List()
sgs := make([]gcorecloud.ItemID, len(rawSgsID))
for i, sgID := range rawSgsID {
sgs[i] = gcorecloud.ItemID{ID: sgID.(string)}
Expand Down
2 changes: 1 addition & 1 deletion gcore/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func extractInstanceInterfacesMapV2(interfaces []interface{}) ([]instances.Inter
I.FloatingIP = &fip
}

rawSgsID := inter["security_groups"].([]interface{})
rawSgsID := inter["security_groups"].(*schema.Set).List()
sgs := make([]gcorecloud.ItemID, len(rawSgsID))
for i, sgID := range rawSgsID {
sgs[i] = gcorecloud.ItemID{ID: sgID.(string)}
Expand Down
Loading