From 3e6002959f46e69191e370df3f3da6156a2e6dd1 Mon Sep 17 00:00:00 2001 From: Yussuf Shaikh Date: Tue, 14 Mar 2023 12:02:54 +0530 Subject: [PATCH] No need to take instance action if already in state Signed-off-by: Yussuf Shaikh --- .../power/resource_ibm_pi_instance_action.go | 44 +++++++++++++------ .../resource_ibm_pi_instance_action_test.go | 16 +++++++ 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/ibm/service/power/resource_ibm_pi_instance_action.go b/ibm/service/power/resource_ibm_pi_instance_action.go index 071b3d2069..b7c78539a4 100644 --- a/ibm/service/power/resource_ibm_pi_instance_action.go +++ b/ibm/service/power/resource_ibm_pi_instance_action.go @@ -123,7 +123,10 @@ func resourceIBMPIInstanceActionRead(ctx context.Context, d *schema.ResourceData func resourceIBMPIInstanceActionUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { if d.HasChange(Arg_PVMInstanceActionType) { - takeInstanceAction(ctx, d, meta, d.Timeout(schema.TimeoutUpdate)) + adiag := takeInstanceAction(ctx, d, meta, d.Timeout(schema.TimeoutUpdate)) + if adiag != nil { + return adiag + } } return resourceIBMPIInstanceActionRead(ctx, d, meta) @@ -146,18 +149,6 @@ func takeInstanceAction(ctx context.Context, d *schema.ResourceData, meta interf action := d.Get(Arg_PVMInstanceActionType).(string) targetHealthStatus := d.Get(Arg_PVMInstanceHealthStatus).(string) - body := &models.PVMInstanceAction{Action: &action} - log.Printf("Calling the IBM PI Action %s on the instance %s", action, id) - client := st.NewIBMPIInstanceClient(ctx, sess, cloudInstanceID) - - err = client.Action(id, body) - if err != nil { - log.Printf("[ERROR] failed to perform the action on the instance %v", err) - return diag.FromErr(err) - } - - log.Printf("Executed the action on the instance") - var targetStatus string if action == "stop" || action == "immediate-shutdown" { targetStatus = "SHUTOFF" @@ -169,6 +160,33 @@ func takeInstanceAction(ctx context.Context, d *schema.ResourceData, meta interf targetStatus = "ACTIVE" } + client := st.NewIBMPIInstanceClient(ctx, sess, cloudInstanceID) + + // special case for action "start", "stop", "immediate-shutdown" + // skip calling action if instance is already in desired state + if action == "start" || action == "stop" || action == "immediate-shutdown" { + pvm, err := client.Get(id) + if err != nil { + return diag.FromErr(err) + } + + if *pvm.Status == targetStatus && pvm.Health != nil && (pvm.Health.Status == targetHealthStatus || pvm.Health.Status == PVMInstanceHealthOk) { + log.Printf("[DEBUG] skipping as action %s not needed on the instance %s", action, id) + return nil + } + } + + body := &models.PVMInstanceAction{Action: &action} + log.Printf("Calling the IBM PI Action %s on the instance %s", action, id) + + err = client.Action(id, body) + if err != nil { + log.Printf("[ERROR] failed to perform the action on the instance %v", err) + return diag.FromErr(err) + } + + log.Printf("Executed the action on the instance") + log.Printf("Calling the check for %s opertion to check for status %s", action, targetStatus) _, err = isWaitForPIInstanceActionStatus(ctx, client, id, timeout, targetStatus, targetHealthStatus) if err != nil { diff --git a/ibm/service/power/resource_ibm_pi_instance_action_test.go b/ibm/service/power/resource_ibm_pi_instance_action_test.go index 507ef26248..901af8cf54 100644 --- a/ibm/service/power/resource_ibm_pi_instance_action_test.go +++ b/ibm/service/power/resource_ibm_pi_instance_action_test.go @@ -25,6 +25,22 @@ func TestAccIBMPIInstanceAction(t *testing.T) { ), }, { + // Try to stop already stopped instance + Config: testAccCheckIBMPIInstanceActionConfig("stop"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "ibm_pi_instance_action.example", "status", "SHUTOFF"), + ), + }, + { + Config: testAccCheckIBMPIInstanceActionConfig("start"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "ibm_pi_instance_action.example", "status", "ACTIVE"), + ), + }, + { + // Try to start already started instance Config: testAccCheckIBMPIInstanceActionConfig("start"), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(