Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
njuCZ committed Jan 20, 2021
1 parent d0c8eb5 commit fdcfa62
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions azurerm/internal/services/iothub/iothub_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,12 +565,23 @@ func resourceIotHubCreateUpdate(d *schema.ResourceData, meta interface{}) error
props.Properties.MinTLSVersion = utils.String(v.(string))
}

future, err := client.CreateOrUpdate(ctx, resourceGroup, name, props, "")
_, err = client.CreateOrUpdate(ctx, resourceGroup, name, props, "")
if err != nil {
return fmt.Errorf("Error creating/updating IotHub %q (Resource Group %q): %+v", name, resourceGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
timeout := schema.TimeoutUpdate
if d.IsNewResource() {
timeout = schema.TimeoutCreate
}
stateConf := &resource.StateChangeConf{
Pending: []string{"Activating", "Transitioning"},
Target: []string{"Succeeded"},
Refresh: iothubStateStatusCodeCreateUpdateRefreshFunc(ctx, client, resourceGroup, name),
Timeout: d.Timeout(timeout),
}

if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("Error waiting for the completion of the creating/updating of IotHub %q (Resource Group %q): %+v", name, resourceGroup, err)
}

Expand Down Expand Up @@ -709,7 +720,7 @@ func waitForIotHubToBeDeleted(ctx context.Context, client *devices.IotHubResourc
// we can't use the Waiter here since the API returns a 404 once it's deleted which is considered a polling status code..
log.Printf("[DEBUG] Waiting for IotHub (%q in Resource Group %q) to be deleted", name, resourceGroup)
stateConf := &resource.StateChangeConf{
Pending: []string{"200"},
Pending: []string{"200", "409"},
Target: []string{"404"},
Refresh: iothubStateStatusCodeRefreshFunc(ctx, client, resourceGroup, name),
Timeout: d.Timeout(schema.TimeoutDelete),
Expand All @@ -722,6 +733,27 @@ func waitForIotHubToBeDeleted(ctx context.Context, client *devices.IotHubResourc
return nil
}

func iothubStateStatusCodeCreateUpdateRefreshFunc(ctx context.Context, client *devices.IotHubResourceClient, resourceGroup, name string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
res, err := client.Get(ctx, resourceGroup, name)

log.Printf("Retrieving IoTHub %q (Resource Group %q) returned Status %d", resourceGroup, name, res.StatusCode)

if err != nil {
if utils.ResponseWasNotFound(res.Response) {
return res, "NotFound", nil
}
return nil, "", fmt.Errorf("polling for the Provisioning State of the IotHub %q (RG: %q): %+v", name, resourceGroup, err)
}

if res.Properties == nil || res.Properties.ProvisioningState == nil {
return res, "", fmt.Errorf("polling for the Provisioning State of the IotHub %q (RG: %q): %+v", name, resourceGroup, err)
}

return res, *res.Properties.ProvisioningState, nil
}
}

func iothubStateStatusCodeRefreshFunc(ctx context.Context, client *devices.IotHubResourceClient, resourceGroup, name string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
res, err := client.Get(ctx, resourceGroup, name)
Expand All @@ -732,7 +764,7 @@ func iothubStateStatusCodeRefreshFunc(ctx context.Context, client *devices.IotHu
if utils.ResponseWasNotFound(res.Response) {
return res, strconv.Itoa(res.StatusCode), nil
}
return nil, "", fmt.Errorf("Error polling for the status of the IotHub %q (RG: %q): %+v", name, resourceGroup, err)
return nil, strconv.Itoa(res.StatusCode), fmt.Errorf("Error polling for the status of the IotHub %q (RG: %q): %+v", name, resourceGroup, err)
}

return res, strconv.Itoa(res.StatusCode), nil
Expand Down

0 comments on commit fdcfa62

Please sign in to comment.