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

[CLOUD-156] Add / remove instance server group #6

Merged
merged 1 commit into from
Feb 28, 2023
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
26 changes: 26 additions & 0 deletions edgecenter/resource_edgecenter_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,32 @@ func resourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m inter
}
}

if d.HasChange("server_group") {
oldSGRaw, newSGRaw := d.GetChange("server_group")
oldSGID, newSGID := oldSGRaw.(string), newSGRaw.(string)

clientSG, err := CreateClient(provider, d, ServerGroupsPoint, VersionPointV1)
if err != nil {
return diag.FromErr(err)
}

// delete old server group
if oldSGID != "" {
err := deleteServerGroup(clientSG, client, instanceID, oldSGID)
if err != nil {
return diag.FromErr(err)
}
}

// add new server group if needed
if newSGID != "" {
err := addServerGroup(clientSG, client, instanceID, newSGID)
if err != nil {
return diag.FromErr(err)
}
}
}

if d.HasChange("volume") {
vClient, err := CreateClient(provider, d, VolumesPoint, VersionPointV1)
if err != nil {
Expand Down
55 changes: 55 additions & 0 deletions edgecenter/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/Edge-Center/edgecentercloud-go/edgecenter/router/v1/routers"
"github.com/Edge-Center/edgecentercloud-go/edgecenter/securitygroup/v1/securitygroups"
typesSG "github.com/Edge-Center/edgecentercloud-go/edgecenter/securitygroup/v1/types"
"github.com/Edge-Center/edgecentercloud-go/edgecenter/servergroup/v1/servergroups"
"github.com/Edge-Center/edgecentercloud-go/edgecenter/subnet/v1/subnets"
"github.com/Edge-Center/edgecentercloud-go/edgecenter/task/v1/tasks"
)
Expand Down Expand Up @@ -949,3 +950,57 @@ func attachSecurityGroupToInstance(sgClient, instanceClient *edgecloud.ServiceCl

return nil
}

func deleteServerGroup(sgClient, instanceClient *edgecloud.ServiceClient, instanceID, sgID string) error {
log.Printf("[DEBUG] remove server group from instance: %s", instanceID)
results, err := instances.RemoveServerGroup(instanceClient, instanceID).Extract()
if err != nil {
return fmt.Errorf("failed to remove server group %s from instance %s: %w", sgID, instanceID, err)
}

err = tasks.WaitTaskAndProcessResult(sgClient, results.Tasks[0], true, InstanceCreatingTimeout, func(task tasks.TaskID) error {
sgInfo, err := servergroups.Get(sgClient, sgID).Extract()
if err != nil {
return fmt.Errorf("failed to get server group %s: %w", sgID, err)
}
for _, instanceInfo := range sgInfo.Instances {
if instanceInfo.InstanceID == instanceID {
return fmt.Errorf("server group %s was not removed from instance %s", sgID, instanceID)
}
}
return nil
})

if err != nil {
return err
}

return nil
}

func addServerGroup(sgClient, instanceClient *edgecloud.ServiceClient, instanceID, sgID string) error {
log.Printf("[DEBUG] add server group to instance: %s", instanceID)
results, err := instances.AddServerGroup(instanceClient, instanceID, instances.ServerGroupOpts{ServerGroupID: sgID}).Extract()
if err != nil {
return fmt.Errorf("failed to add server group %s to instance %s: %w", sgID, instanceID, err)
}

err = tasks.WaitTaskAndProcessResult(sgClient, results.Tasks[0], true, InstanceCreatingTimeout, func(task tasks.TaskID) error {
sgInfo, err := servergroups.Get(sgClient, sgID).Extract()
if err != nil {
return fmt.Errorf("cannot get server group with ID: %s. Error: %w", sgID, err)
}
for _, instanceInfo := range sgInfo.Instances {
if instanceInfo.InstanceID == instanceID {
return nil
}
}
return fmt.Errorf("the server group: %s was not added to the instance: %s. Error: %w", sgID, instanceID, err)
})

if err != nil {
return err
}

return nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/Edge-Center/edgecenter-dns-sdk-go v0.1.0
github.com/Edge-Center/edgecenter-storage-sdk-go v0.1.0
github.com/Edge-Center/edgecentercdn-go v0.1.0
github.com/Edge-Center/edgecentercloud-go v0.1.0
github.com/Edge-Center/edgecentercloud-go v0.1.1
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform v1.1.9
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ github.com/Edge-Center/edgecenter-storage-sdk-go v0.1.0 h1:5iKvDZsdkrvcQDIsh+tBS
github.com/Edge-Center/edgecenter-storage-sdk-go v0.1.0/go.mod h1:3Y8B9zGwtXhjIWMyjWHcr7F9aXTzfOvq+VfcHZkYGfg=
github.com/Edge-Center/edgecentercdn-go v0.1.0 h1:vPd7UMDhlNTRLD0xdWxvsUKRpYnnJ7vmnBmnq70r2s0=
github.com/Edge-Center/edgecentercdn-go v0.1.0/go.mod h1:RwEyxwPAmxor1mZKUTa2bIU2p5qM6kcAofUkaE4O1V4=
github.com/Edge-Center/edgecentercloud-go v0.1.0 h1:NwTCcTW2jL5tKFScb35BJi5XQQR84XLp3QEbJdNUpPQ=
github.com/Edge-Center/edgecentercloud-go v0.1.0/go.mod h1:Z1c8cEGb/MrtBmP0fFIPEpOQEAL04f1GVaEibSlqx/E=
github.com/Edge-Center/edgecentercloud-go v0.1.1 h1:B08+JMovdgBxoBiVEml0E2+8ivGxujkOPCEsAbcG+GU=
github.com/Edge-Center/edgecentercloud-go v0.1.1/go.mod h1:Z1c8cEGb/MrtBmP0fFIPEpOQEAL04f1GVaEibSlqx/E=
github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=
github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0=
github.com/Microsoft/go-winio v0.5.0 h1:Elr9Wn+sGKPlkaBvwu4mTrxtmOp3F3yV9qhaHbXGjwU=
Expand Down