Skip to content

Commit 62a4eda

Browse files
authored
Merge pull request #24 from smartxworks/vmtools-on-boots
optimize: optimize vm resource updation
2 parents 8f2b637 + 69f5a00 commit 62a4eda

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

internal/helper/vmHelper.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"time"
77

8+
"github.com/hashicorp/terraform-plugin-log/tflog"
89
"github.com/hashicorp/terraform-provider-cloudtower/internal/cloudtower"
910
"github.com/hashicorp/terraform-provider-cloudtower/internal/utils"
1011
"github.com/smartxworks/cloudtower-go-sdk/v2/client/vm"
@@ -110,6 +111,24 @@ func StartVmTemporary(ctx context.Context, ct *cloudtower.Client, vmId string) (
110111
}
111112
// vm has been started temporary, need to power off when done
112113
return func() error {
114+
// shutdown vm first
115+
shutdownParams := vm.NewShutDownVMParams()
116+
shutdownParams.RequestBody = &models.VMOperateParams{
117+
Where: &models.VMWhereInput{
118+
ID: &vmId,
119+
},
120+
}
121+
shutDownResp, err := utils.RetryWithExponentialBackoff(ctx, func() (*vm.ShutDownVMOK, error) {
122+
return ct.Api.VM.ShutDownVM(shutdownParams)
123+
}, utils.RetryWithExponentialBackoffOptions{})
124+
if err == nil {
125+
_, err = ct.WaitTasksFinish(ctx, []string{*shutDownResp.Payload[0].TaskID})
126+
if err == nil {
127+
return nil
128+
}
129+
}
130+
tflog.Warn(ctx, "failed to shutdown VM, power off it directly")
131+
// if shutdown failed, power off vm
113132
powerOffParams := vm.NewPoweroffVMParams()
114133
powerOffParams.RequestBody = &models.VMOperateParams{
115134
Where: &models.VMWhereInput{

internal/provider/resource_vm.go

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"reflect"
78
"sort"
89
"strings"
910
"time"
@@ -778,6 +779,9 @@ func resourceVmRead(ctx context.Context, d *schema.ResourceData, meta interface{
778779
return diags
779780
}
780781

782+
if err := d.Set("name", v.Name); err != nil {
783+
return diag.FromErr(err)
784+
}
781785
// set computed variables
782786
if err := d.Set("cluster_id", v.Cluster.ID); err != nil {
783787
return diag.FromErr(err)
@@ -1410,7 +1414,7 @@ func resourceVmUpdate(ctx context.Context, d *schema.ResourceData, meta interfac
14101414
ID: &id,
14111415
},
14121416
Data: &models.VMStartParamsData{
1413-
HostID: basic.HostId,
1417+
HostID: hostId,
14141418
},
14151419
}
14161420
uvp.Context = ctx
@@ -1565,6 +1569,10 @@ func resourceVmUpdate(ctx context.Context, d *schema.ResourceData, meta interfac
15651569
}
15661570
}
15671571

1572+
if d.HasChange("cluster_id") {
1573+
return diag.Errorf("cross cluster migration is not supported yet")
1574+
}
1575+
15681576
// then migrate the vm if needed
15691577
if d.HasChange("host_id") {
15701578
hostId := d.Get("host_id").(string)
@@ -1594,27 +1602,36 @@ func resourceVmUpdate(ctx context.Context, d *schema.ResourceData, meta interfac
15941602
}
15951603

15961604
// execute vm basic updation in the last
1597-
_, err := utils.RetryWithExponentialBackoff(ctx, func() (interface{}, error) {
1598-
return nil, ct.GraphqlApi.Mutate(ctx, &updateVm, map[string]interface{}{
1599-
"data": updateParams,
1600-
"effect": updateEffect,
1601-
"where": VmWhereUniqueInput{
1602-
"id": d.Id(),
1603-
},
1604-
}, graphql.OperationName("updateVm"))
1605-
}, utils.RetryWithExponentialBackoffOptions{})
1606-
1607-
if err != nil {
1608-
return diag.FromErr(err)
1605+
if !reflect.ValueOf(updateParams).IsZero() {
1606+
// skip basic params change if struct is zero
1607+
_, err := utils.RetryWithExponentialBackoff(ctx, func() (interface{}, error) {
1608+
return nil, ct.GraphqlApi.Mutate(ctx, &updateVm, map[string]interface{}{
1609+
"data": updateParams,
1610+
"effect": UpdateVmEffect{},
1611+
"where": VmWhereUniqueInput{
1612+
"id": d.Id(),
1613+
},
1614+
}, graphql.OperationName("updateVm"))
1615+
}, utils.RetryWithExponentialBackoffOptions{})
1616+
if err != nil {
1617+
return diag.FromErr(err)
1618+
}
1619+
_, err = ct.WaitTaskForResource(ctx, d.Id(), "updateVm")
1620+
if err != nil {
1621+
return diag.FromErr(err)
1622+
}
16091623
}
1610-
ct.WaitTaskForResource(ctx, d.Id(), "updateVm")
16111624
if runStatusChangeFirst && statusChangeFunc != nil {
16121625
err := statusChangeFunc()
16131626
if err != nil {
16141627
return diag.FromErr(err)
16151628
}
16161629
}
16171630
if needUpdateVmToolsAttribute {
1631+
_, err := helper.WaitVmToolsRunning(ctx, ct, d.Id())
1632+
if err != nil {
1633+
return diag.FromErr(err)
1634+
}
16181635
_, err = utils.RetryWithExponentialBackoff(ctx, func() (interface{}, error) {
16191636
return nil, ct.GraphqlApi.Mutate(ctx, &updateVm, map[string]interface{}{
16201637
"data": updateVmToolsAttributeParams,

0 commit comments

Comments
 (0)