Skip to content

Commit

Permalink
No need to take instance action if already in state
Browse files Browse the repository at this point in the history
Signed-off-by: Yussuf Shaikh <yussuf.shaikh1@ibm.com>
  • Loading branch information
yussufsh committed Mar 14, 2023
1 parent 5e3b90e commit 3e60029
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
44 changes: 31 additions & 13 deletions ibm/service/power/resource_ibm_pi_instance_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
Expand All @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions ibm/service/power/resource_ibm_pi_instance_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 3e60029

Please sign in to comment.