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

App Service Plan: refactoring / handling errors in flatten functions #1926

Merged
merged 1 commit into from
Sep 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 5 additions & 3 deletions azurerm/data_source_app_service_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,17 @@ func dataSourceAppServicePlanRead(d *schema.ResourceData, meta interface{}) erro
}

if props := resp.AppServicePlanProperties; props != nil {
d.Set("properties", flattenAppServiceProperties(props))
if err := d.Set("properties", flattenAppServiceProperties(props)); err != nil {
return fmt.Errorf("Error flattening `properties`: %+v", err)
}

if props.MaximumNumberOfWorkers != nil {
d.Set("maximum_number_of_workers", int(*props.MaximumNumberOfWorkers))
}
}

if sku := resp.Sku; sku != nil {
d.Set("sku", flattenAppServicePlanSku(sku))
if err := d.Set("sku", flattenAppServicePlanSku(resp.Sku)); err != nil {
return fmt.Errorf("Error flattening `sku`: %+v", err)
}

flattenAndSetTags(d, resp.Tags)
Expand Down
79 changes: 44 additions & 35 deletions azurerm/resource_arm_app_service_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func resourceArmAppServicePlanCreateUpdate(d *schema.ResourceData, meta interfac
tags := d.Get("tags").(map[string]interface{})

sku := expandAzureRmAppServicePlanSku(d)
properties := expandAppServicePlanProperties(d, name)
properties := expandAppServicePlanProperties(d)

appServicePlan := web.AppServicePlan{
Location: &location,
Expand All @@ -131,19 +131,19 @@ func resourceArmAppServicePlanCreateUpdate(d *schema.ResourceData, meta interfac
Sku: &sku,
}

createFuture, err := client.CreateOrUpdate(ctx, resGroup, name, appServicePlan)
future, err := client.CreateOrUpdate(ctx, resGroup, name, appServicePlan)
if err != nil {
return err
return fmt.Errorf("Error creating/updating App Service Plan %q (Resource Group %q): %+v", name, resGroup, err)
}

err = createFuture.WaitForCompletionRef(ctx, client.Client)
err = future.WaitForCompletionRef(ctx, client.Client)
if err != nil {
return err
return fmt.Errorf("Error waiting for the create/update of App Service Plan %q (Resource Group %q): %+v", name, resGroup, err)
}

read, err := client.Get(ctx, resGroup, name)
if err != nil {
return err
return fmt.Errorf("Error retrieving App Service Plan %q (Resource Group %q): %+v", name, resGroup, err)
}
if read.ID == nil {
return fmt.Errorf("Cannot read AzureRM App Service Plan %q (resource group %q) ID", name, resGroup)
Expand All @@ -164,37 +164,40 @@ func resourceArmAppServicePlanRead(d *schema.ResourceData, meta interface{}) err

log.Printf("[DEBUG] Reading Azure App Service Plan %s", id)

resGroup := id.ResourceGroup
resourceGroup := id.ResourceGroup
name := id.Path["serverfarms"]

ctx := meta.(*ArmClient).StopContext
resp, err := client.Get(ctx, resGroup, name)
resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] App Service Plan %q was not found in Resource Group %q - removnig from state!", name, resourceGroup)
d.SetId("")
return nil
}

return fmt.Errorf("Error making Read request on Azure App Service Plan %s: %+v", name, err)
return fmt.Errorf("Error making Read request on App Service Plan %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.Set("name", name)
d.Set("resource_group_name", resGroup)
d.Set("resource_group_name", resourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azureRMNormalizeLocation(*location))
}
d.Set("kind", resp.Kind)

if props := resp.AppServicePlanProperties; props != nil {
d.Set("properties", flattenAppServiceProperties(props))
if err := d.Set("properties", flattenAppServiceProperties(props)); err != nil {
return fmt.Errorf("Error flattening `properties`: %+v", err)
}

if props.MaximumNumberOfWorkers != nil {
d.Set("maximum_number_of_workers", int(*props.MaximumNumberOfWorkers))
}
}

if sku := resp.Sku; sku != nil {
d.Set("sku", flattenAppServicePlanSku(sku))
if err := d.Set("sku", flattenAppServicePlanSku(resp.Sku)); err != nil {
return fmt.Errorf("Error flattening `sku`: %+v", err)
}

flattenAndSetTags(d, resp.Tags)
Expand All @@ -210,19 +213,16 @@ func resourceArmAppServicePlanDelete(d *schema.ResourceData, meta interface{}) e
if err != nil {
return err
}
resGroup := id.ResourceGroup
resourceGroup := id.ResourceGroup
name := id.Path["serverfarms"]

log.Printf("[DEBUG] Deleting app service plan %s: %s", resGroup, name)

resp, err := client.Delete(ctx, resGroup, name)
log.Printf("[DEBUG] Deleting App Service Plan %q (Resource Group %q)", name, resourceGroup)

resp, err := client.Delete(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp) {
return nil
if !utils.ResponseWasNotFound(resp) {
return fmt.Errorf("Error deleting App Service Plan %q (Resource Group %q): %+v", name, resourceGroup, err)
}

return err
}

return nil
Expand All @@ -236,9 +236,9 @@ func expandAzureRmAppServicePlanSku(d *schema.ResourceData) web.SkuDescription {
size := config["size"].(string)

sku := web.SkuDescription{
Name: &size,
Tier: &tier,
Size: &size,
Name: utils.String(size),
Tier: utils.String(tier),
Size: utils.String(size),
}

if v, ok := config["capacity"]; ok {
Expand All @@ -249,23 +249,32 @@ func expandAzureRmAppServicePlanSku(d *schema.ResourceData) web.SkuDescription {
return sku
}

func flattenAppServicePlanSku(profile *web.SkuDescription) []interface{} {
skus := make([]interface{}, 0, 1)
sku := make(map[string]interface{}, 2)
func flattenAppServicePlanSku(input *web.SkuDescription) []interface{} {
outputs := make([]interface{}, 0)
if input == nil {
return outputs
}

output := make(map[string]interface{}, 2)

sku["tier"] = *profile.Tier
sku["size"] = *profile.Size
if input.Tier != nil {
output["tier"] = *input.Tier
}

if input.Size != nil {
output["size"] = *input.Size
}

if profile.Capacity != nil {
sku["capacity"] = *profile.Capacity
if input.Capacity != nil {
output["capacity"] = *input.Capacity
}

skus = append(skus, sku)
outputs = append(outputs, output)

return skus
return outputs
}

func expandAppServicePlanProperties(d *schema.ResourceData, name string) *web.AppServicePlanProperties {
func expandAppServicePlanProperties(d *schema.ResourceData) *web.AppServicePlanProperties {
configs := d.Get("properties").([]interface{})
properties := web.AppServicePlanProperties{}
if len(configs) == 0 {
Expand Down Expand Up @@ -293,7 +302,7 @@ func flattenAppServiceProperties(props *web.AppServicePlanProperties) []interfac
result := make([]interface{}, 0, 1)
properties := make(map[string]interface{}, 0)

if props.HostingEnvironmentProfile != nil {
if props.HostingEnvironmentProfile != nil && props.HostingEnvironmentProfile.ID != nil {
properties["app_service_environment_id"] = *props.HostingEnvironmentProfile.ID
}

Expand Down