diff --git a/edgecenter/resource_edgecenter_instance.go b/edgecenter/resource_edgecenter_instance.go index cb0c3d68..4f6eaa8f 100644 --- a/edgecenter/resource_edgecenter_instance.go +++ b/edgecenter/resource_edgecenter_instance.go @@ -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 { diff --git a/edgecenter/utils.go b/edgecenter/utils.go index 97733306..0d7f112f 100644 --- a/edgecenter/utils.go +++ b/edgecenter/utils.go @@ -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" ) @@ -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 +} diff --git a/go.mod b/go.mod index 1563a41a..4a33d667 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index ab4c0b21..1fa72ee7 100644 --- a/go.sum +++ b/go.sum @@ -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=