Skip to content

Commit

Permalink
Remove hardcoding of VM power state strings and remove bogus values
Browse files Browse the repository at this point in the history
  • Loading branch information
ddelnano committed Oct 31, 2023
1 parent d5dd4e5 commit df6c222
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 25 deletions.
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func GetConfigFromEnv() Config {
if err == nil {
retryMaxTime = duration
} else {
fmt.Println("[ERROR] failed to set retry mode, disabling retries\n")
fmt.Println("[ERROR] failed to set retry mode, disabling retries")
}
}
return Config{
Expand Down
31 changes: 18 additions & 13 deletions client/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ type allObjectResponse struct {
Objects map[string]Vm `json:"-"`
}

const (
HaltedPowerState string = "Halted"
PausedPowerState string = "Paused"
RunningPowerState string = "Running"
SuspendedPowerState string = "Suspended"
)

type CPUs struct {
Number int `json:"number"`
Max int `json:"max"`
Expand Down Expand Up @@ -213,7 +220,7 @@ func (c *Client) CreateVm(vmReq Vm, createTime time.Duration) (*Vm, error) {
}

bootAfterCreate := true
if vmReq.PowerState != "Running" {
if vmReq.PowerState != RunningPowerState {
bootAfterCreate = false
}

Expand Down Expand Up @@ -430,8 +437,8 @@ func (c *Client) StartVm(id string) error {
return c.waitForVmState(
id,
StateChangeConf{
Pending: []string{"Halted", "Stopped"},
Target: []string{"Running"},
Pending: []string{HaltedPowerState},
Target: []string{RunningPowerState},
Timeout: 2 * time.Minute,
},
)
Expand Down Expand Up @@ -459,8 +466,8 @@ func (c *Client) HaltVm(id string) error {
return c.waitForVmState(
id,
StateChangeConf{
Pending: []string{"Running", "Stopped"},
Target: []string{"Halted"},
Pending: []string{RunningPowerState},
Target: []string{HaltedPowerState},
Timeout: 2 * time.Minute,
},
)
Expand Down Expand Up @@ -554,17 +561,15 @@ func (c *Client) waitForVmState(id string, stateConf StateChangeConf) error {
return err
}

func (c *Client) waitForModifyVm(id, desiredPowerState string, waitForIp bool, timeout time.Duration) error {
func (c *Client) waitForModifyVm(id string, desiredPowerState string, waitForIp bool, timeout time.Duration) error {
if !waitForIp {
var pending []string
target := desiredPowerState
switch desiredPowerState {
case "Running":
pending = []string{"Halted", "Stopped"}
case "Stopped":
pending = []string{"Running", "Halted"}
case "Halted":
pending = []string{"Stopped", "Running"}
case RunningPowerState:
pending = []string{HaltedPowerState}
case HaltedPowerState:
pending = []string{RunningPowerState}
default:
return errors.New(fmt.Sprintf("Invalid VM power state requested: %s\n", desiredPowerState))
}
Expand Down Expand Up @@ -594,7 +599,7 @@ func (c *Client) waitForModifyVm(id, desiredPowerState string, waitForIp bool, t
}

l := len(vm.Addresses)
if l == 0 || vm.PowerState != "Running" {
if l == 0 || vm.PowerState != RunningPowerState {
return vm, "Waiting", nil
}

Expand Down
3 changes: 2 additions & 1 deletion xoa/internal/state/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"net"

"github.com/ddelnano/terraform-provider-xenorchestra/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
Expand Down Expand Up @@ -355,7 +356,7 @@ func suppressAttachedDiffWhenHalted(k, old, new string, d *schema.ResourceData)
powerState := d.Get("power_state").(string)
suppress = true

if powerState == "Running" {
if powerState == client.RunningPowerState {
suppress = false
}
log.Printf("[DEBUG] VM '%s' attribute has transitioned from '%s' to '%s' when PowerState '%s'. Suppress diff: %t", k, old, new, powerState, suppress)
Expand Down
16 changes: 8 additions & 8 deletions xoa/resource_xenorchestra_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ var validFirmware = []string{
}

var validPowerState = []string{
"Running",
"Halted",
"Paused",
"Suspended",
client.HaltedPowerState,
client.PausedPowerState,
client.RunningPowerState,
client.SuspendedPowerState,
}

var validInstallationMethods = []string{
Expand Down Expand Up @@ -118,7 +118,7 @@ func resourceVmSchema() map[string]*schema.Schema {
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice(validPowerState, false),
Optional: true,
Default: "Running",
Default: client.RunningPowerState,
},
"installation_method": &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -909,13 +909,13 @@ func resourceVmUpdate(d *schema.ResourceData, m interface{}) error {
log.Printf("[DEBUG] powerStateChanged=%t newPowerState=%s\n", powerStateChanged, newPowerState)
if haltForUpdates || powerStateChanged {
switch newPowerState {
case "Running":
case client.RunningPowerState:
err := c.StartVm(vmReq.Id)

if err != nil {
return err
}
case "Halted":
case client.HaltedPowerState:
if haltPerformed {
// Nothing here since we are already halted
} else {
Expand Down Expand Up @@ -1375,7 +1375,7 @@ func suppressAttachedDiffWhenHalted(k, old, new string, d *schema.ResourceData)
log.Printf("[DEBUG] Power state has been changed\n")
}

if !ok && powerState == "Running" {
if !ok && powerState == client.RunningPowerState {
suppress = false
}
log.Printf("[DEBUG] VM '%s' attribute has transitioned from '%s' to '%s' when PowerState '%s'. Suppress diff: %t", k, old, new, powerState, suppress)
Expand Down
4 changes: 2 additions & 2 deletions xoa/resource_xenorchestra_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ func TestAccXenorchestraVm_createWithShorterResourceTimeout(t *testing.T) {
func TestAccXenorchestraVm_createWithPowerStateChanges(t *testing.T) {
resourceName := "xenorchestra_vm.bar"
vmName := fmt.Sprintf("%s - %s", accTestPrefix, t.Name())
runningPowerState := "Running"
stoppedPowerState := "Halted"
runningPowerState := client.RunningPowerState
stoppedPowerState := client.HaltedPowerState
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand Down

0 comments on commit df6c222

Please sign in to comment.