From 01a8f1a24c6e2a1bf7c98ff8b88f840e7e8feca5 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Fri, 14 Sep 2018 08:04:05 +0200 Subject: [PATCH] App Service Plan: refactoring / handling errors in flatten functions --- azurerm/data_source_app_service_plan.go | 8 ++- azurerm/resource_arm_app_service_plan.go | 79 +++++++++++++----------- 2 files changed, 49 insertions(+), 38 deletions(-) diff --git a/azurerm/data_source_app_service_plan.go b/azurerm/data_source_app_service_plan.go index 6dc82904949e..765e5d748139 100644 --- a/azurerm/data_source_app_service_plan.go +++ b/azurerm/data_source_app_service_plan.go @@ -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) diff --git a/azurerm/resource_arm_app_service_plan.go b/azurerm/resource_arm_app_service_plan.go index d10e9300bb4d..9a7e32fbaeb7 100644 --- a/azurerm/resource_arm_app_service_plan.go +++ b/azurerm/resource_arm_app_service_plan.go @@ -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, @@ -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) @@ -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) @@ -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 @@ -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 { @@ -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 { @@ -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 }