Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update VM and GPU timeouts #394

Merged
merged 2 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions outscale/resource_outscale_flexible_gpu_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ func resourceOutscaleOAPIFlexibleGpuLink() *schema.Resource {
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(5 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(5 * time.Minute),
},

Schema: map[string]*schema.Schema{
"flexible_gpu_id": {
Expand Down Expand Up @@ -57,7 +63,7 @@ func resourceOutscaleOAPIFlexibleGpuLinkCreate(d *schema.ResourceData, meta inte
VmId: vmId,
}
var resp oscgo.LinkFlexibleGpuResponse
err := resource.Retry(60*time.Second, func() *resource.RetryError {
err := resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
var err error
rp, httpResp, err := conn.FlexibleGpuApi.LinkFlexibleGpu(
context.Background()).LinkFlexibleGpuRequest(reqLink).Execute()
Expand All @@ -77,7 +83,7 @@ func resourceOutscaleOAPIFlexibleGpuLinkCreate(d *schema.ResourceData, meta inte
}

var respV oscgo.ReadFlexibleGpusResponse
err = resource.Retry(60*time.Second, func() *resource.RetryError {
err = resource.Retry(d.Timeout(schema.TimeoutRead), func() *resource.RetryError {
rp, httpResp, err := conn.FlexibleGpuApi.ReadFlexibleGpus(context.Background()).
ReadFlexibleGpusRequest(*reqFlex).Execute()
if err != nil {
Expand All @@ -99,7 +105,7 @@ func resourceOutscaleOAPIFlexibleGpuLinkCreate(d *schema.ResourceData, meta inte
return fmt.Errorf("Unable to link Flexible GPU")
}

if err := changeShutdownBehavior(conn, vmId); err != nil {
if err := changeShutdownBehavior(conn, vmId, d.Timeout(schema.TimeoutDelete)); err != nil {
return fmt.Errorf("Unable to change ShutdownBehavior: %s\n", err)
}

Expand All @@ -121,7 +127,7 @@ func resourceOutscaleOAPIFlexibleGpuLinkRead(d *schema.ResourceData, meta interf

var resp oscgo.ReadFlexibleGpusResponse
var err error
err = resource.Retry(60*time.Second, func() *resource.RetryError {
err = resource.Retry(d.Timeout(schema.TimeoutRead), func() *resource.RetryError {
rp, httpResp, err := conn.FlexibleGpuApi.ReadFlexibleGpus(
context.Background()).
ReadFlexibleGpusRequest(*req).Execute()
Expand Down Expand Up @@ -170,7 +176,7 @@ func resourceOutscaleOAPIFlexibleGpuLinkDelete(d *schema.ResourceData, meta inte
}

var err error
err = resource.Retry(20*time.Second, func() *resource.RetryError {
err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
_, httpResp, err := conn.FlexibleGpuApi.UnlinkFlexibleGpu(
context.Background()).UnlinkFlexibleGpuRequest(*req).Execute()
if err != nil {
Expand All @@ -184,7 +190,7 @@ func resourceOutscaleOAPIFlexibleGpuLinkDelete(d *schema.ResourceData, meta inte
}

var resp oscgo.ReadFlexibleGpusResponse
err = resource.Retry(60*time.Second, func() *resource.RetryError {
err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
rp, httpResp, err := conn.FlexibleGpuApi.ReadFlexibleGpus(context.Background()).
ReadFlexibleGpusRequest(*reqFlex).Execute()
if err != nil {
Expand All @@ -204,7 +210,7 @@ func resourceOutscaleOAPIFlexibleGpuLinkDelete(d *schema.ResourceData, meta inte
return fmt.Errorf("Unable to unlink Flexible GPU")
}

if err := changeShutdownBehavior(conn, vmId); err != nil {
if err := changeShutdownBehavior(conn, vmId, d.Timeout(schema.TimeoutDelete)); err != nil {
return fmt.Errorf("Unable to change ShutdownBehavior: %s\n", err)
}

Expand All @@ -213,10 +219,10 @@ func resourceOutscaleOAPIFlexibleGpuLinkDelete(d *schema.ResourceData, meta inte

}

func changeShutdownBehavior(conn *oscgo.APIClient, vmId string) error {
func changeShutdownBehavior(conn *oscgo.APIClient, vmId string, timeOut time.Duration) error {

var resp oscgo.ReadVmsResponse
err := resource.Retry(20*time.Second, func() *resource.RetryError {
err := resource.Retry(timeOut, func() *resource.RetryError {
rp, httpResp, err := conn.VmApi.ReadVms(context.Background()).ReadVmsRequest(oscgo.ReadVmsRequest{
Filters: &oscgo.FiltersVm{
VmIds: &[]string{vmId},
Expand Down Expand Up @@ -244,7 +250,7 @@ func changeShutdownBehavior(conn *oscgo.APIClient, vmId string) error {
}
}

if err := stopVM(vmId, conn); err != nil {
if err := stopVM(vmId, conn, timeOut); err != nil {
return err
}

Expand All @@ -256,7 +262,7 @@ func changeShutdownBehavior(conn *oscgo.APIClient, vmId string) error {
}
}

if err := startVM(vmId, conn); err != nil {
if err := startVM(vmId, conn, timeOut); err != nil {
return err
}
return nil
Expand Down
35 changes: 18 additions & 17 deletions outscale/resource_outscale_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ func resourceOutscaleOApiVM() *schema.Resource {
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(5 * time.Minute),
Delete: schema.DefaultTimeout(5 * time.Minute),
},

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -735,7 +736,7 @@ func resourceOAPIVMCreate(d *schema.ResourceData, meta interface{}) error {

// Create the vm
var resp oscgo.CreateVmsResponse
err = resource.Retry(120*time.Second, func() *resource.RetryError {
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
rp, httpResp, err := conn.VmApi.CreateVms(context.Background()).CreateVmsRequest(vmOpts).Execute()
if err != nil {
return utils.CheckThrottling(httpResp, err)
Expand Down Expand Up @@ -824,7 +825,7 @@ func resourceOAPIVMRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*OutscaleClient).OSCAPI

var resp oscgo.ReadVmsResponse
err := resource.Retry(60*time.Second, func() *resource.RetryError {
err := resource.Retry(d.Timeout(schema.TimeoutRead), func() *resource.RetryError {
rp, httpResp, err := conn.VmApi.ReadVms(context.Background()).ReadVmsRequest(oscgo.ReadVmsRequest{
Filters: &oscgo.FiltersVm{
VmIds: &[]string{d.Id()},
Expand Down Expand Up @@ -927,7 +928,7 @@ func resourceOAPIVMUpdate(d *schema.ResourceData, meta interface{}) error {
if !d.IsNewResource() &&
(d.HasChange("vm_type") || d.HasChange("user_data") ||
d.HasChange("performance") || d.HasChange("nested_virtualization")) {
if err := stopVM(id, conn); err != nil {
if err := stopVM(id, conn, d.Timeout(schema.TimeoutUpdate)); err != nil {
return err
}
}
Expand Down Expand Up @@ -1079,11 +1080,11 @@ func resourceOAPIVMUpdate(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error: state should be `stopped or running`")
}
if upState == "stopped" {
if err := stopVM(id, conn); err != nil {
if err := stopVM(id, conn, d.Timeout(schema.TimeoutUpdate)); err != nil {
return err
}
} else {
if err := startVM(id, conn); err != nil {
if err := startVM(id, conn, d.Timeout(schema.TimeoutUpdate)); err != nil {
return err
}
}
Expand All @@ -1101,7 +1102,7 @@ func resourceOAPIVMDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] Terminating VM: %s", id)

var err error
err = resource.Retry(30*time.Second, func() *resource.RetryError {
err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
_, httpResp, err := conn.VmApi.DeleteVms(context.Background()).DeleteVmsRequest(oscgo.DeleteVmsRequest{
VmIds: []string{id},
}).Execute()
Expand Down Expand Up @@ -1388,8 +1389,8 @@ func vmStateRefreshFunc(conn *oscgo.APIClient, instanceID, failState string) res
}
}

func stopVM(vmID string, conn *oscgo.APIClient) error {
vmResp, _, err := readVM(vmID, conn)
func stopVM(vmID string, conn *oscgo.APIClient, timeOut time.Duration) error {
vmResp, _, err := readVM(vmID, conn, timeOut)
if err != nil {
return err
}
Expand All @@ -1405,7 +1406,7 @@ func stopVM(vmID string, conn *oscgo.APIClient) error {
}
}

err = resource.Retry(50*time.Second, func() *resource.RetryError {
err = resource.Retry(timeOut, func() *resource.RetryError {
_, httpResp, err := conn.VmApi.StopVms(context.Background()).StopVmsRequest(oscgo.StopVmsRequest{
VmIds: []string{vmID},
}).Execute()
Expand All @@ -1423,7 +1424,7 @@ func stopVM(vmID string, conn *oscgo.APIClient) error {
Pending: []string{"pending", "running", "shutting-down", "stopped", "stopping"},
Target: []string{"stopped"},
Refresh: vmStateRefreshFunc(conn, vmID, ""),
Timeout: 10 * time.Minute,
Timeout: timeOut,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
Expand All @@ -1444,8 +1445,8 @@ func stopVM(vmID string, conn *oscgo.APIClient) error {
return nil
}

func startVM(vmID string, conn *oscgo.APIClient) error {
err := resource.Retry(50*time.Second, func() *resource.RetryError {
func startVM(vmID string, conn *oscgo.APIClient, timeOut time.Duration) error {
err := resource.Retry(timeOut, func() *resource.RetryError {
_, httpResp, err := conn.VmApi.StartVms(context.Background()).StartVmsRequest(oscgo.StartVmsRequest{
VmIds: []string{vmID},
}).Execute()
Expand All @@ -1463,7 +1464,7 @@ func startVM(vmID string, conn *oscgo.APIClient) error {
Pending: []string{"pending", "stopped"},
Target: []string{"running"},
Refresh: vmStateRefreshFunc(conn, vmID, ""),
Timeout: 10 * time.Minute,
Timeout: timeOut,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
Expand Down Expand Up @@ -1494,10 +1495,10 @@ func updateVmAttr(conn *oscgo.APIClient, instanceAttrOpts oscgo.UpdateVmRequest)
return nil
}

func readVM(vmID string, conn *oscgo.APIClient) (oscgo.ReadVmsResponse, *http.Response, error) {
func readVM(vmID string, conn *oscgo.APIClient, timeOut time.Duration) (oscgo.ReadVmsResponse, *http.Response, error) {
var resp oscgo.ReadVmsResponse
var httpResult *http.Response
err := resource.Retry(50*time.Second, func() *resource.RetryError {
err := resource.Retry(timeOut, func() *resource.RetryError {
rp, httpResp, err := conn.VmApi.ReadVms(context.Background()).ReadVmsRequest(oscgo.ReadVmsRequest{
Filters: &oscgo.FiltersVm{
VmIds: &[]string{vmID},
Expand Down
Loading