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

Revert "Gcloud2 16070 gcore instance read fix" #148

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: 5 additions & 0 deletions gcore/resource_gcore_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 31 additions & 13 deletions gcore/resource_gcore_instancev2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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)
}) {
Expand All @@ -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") {
Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand All @@ -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)}
Expand Down
Loading