From c57cc32ef074aec72fda377ea678b95ccf42d875 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Fri, 4 Dec 2020 14:07:45 +0100 Subject: [PATCH] r/bot_channel_email: refactoring to use a Resource ID Parser --- .../bot/bot_channel_email_resource.go | 77 +++++++------------ 1 file changed, 27 insertions(+), 50 deletions(-) diff --git a/azurerm/internal/services/bot/bot_channel_email_resource.go b/azurerm/internal/services/bot/bot_channel_email_resource.go index 9e383d29bd6ae..9ff6de8adc451 100644 --- a/azurerm/internal/services/bot/bot_channel_email_resource.go +++ b/azurerm/internal/services/bot/bot_channel_email_resource.go @@ -12,6 +12,9 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/location" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/bot/parse" + azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) @@ -23,9 +26,10 @@ func resourceArmBotChannelEmail() *schema.Resource { Delete: resourceArmBotChannelEmailDelete, Update: resourceArmBotChannelEmailUpdate, - Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, - }, + Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { + _, err := parse.BotChannelID(id) + return err + }), Timeouts: &schema.ResourceTimeout{ Create: schema.DefaultTimeout(30 * time.Minute), @@ -64,21 +68,20 @@ func resourceArmBotChannelEmail() *schema.Resource { func resourceArmBotChannelEmailCreate(d *schema.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Bot.ChannelClient + subscriptionId := meta.(*clients.Client).Account.SubscriptionId ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) defer cancel() - resourceGroup := d.Get("resource_group_name").(string) - botName := d.Get("bot_name").(string) - + resourceId := parse.NewBotChannelID(subscriptionId, d.Get("resource_group_name").(string), d.Get("bot_name").(string), string(botservice.ChannelNameEmailChannel)) if d.IsNewResource() { - existing, err := client.Get(ctx, resourceGroup, string(botservice.ChannelNameEmailChannel), botName) + existing, err := client.Get(ctx, resourceId.ResourceGroup, resourceId.BotServiceName, resourceId.ChannelName) if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("Error checking for presence of creating Channel Email for Bot %q (Resource Group %q): %+v", resourceGroup, botName, err) + return fmt.Errorf("checking for presence of existing Email Channel for Bot %q (Resource Group %q): %+v", resourceId.BotServiceName, resourceId.ResourceGroup, err) } } if existing.ID != nil && *existing.ID != "" { - return tf.ImportAsExistsError("azurerm_bot_channel_email", *existing.ID) + return tf.ImportAsExistsError("azurerm_bot_channel_email", resourceId.ID("")) } } @@ -95,21 +98,11 @@ func resourceArmBotChannelEmailCreate(d *schema.ResourceData, meta interface{}) Kind: botservice.KindBot, } - if _, err := client.Create(ctx, resourceGroup, botName, botservice.ChannelNameEmailChannel, channel); err != nil { - return fmt.Errorf("Error issuing create request for Channel Email for Bot %q (Resource Group %q): %+v", resourceGroup, botName, err) - } - - resp, err := client.Get(ctx, resourceGroup, botName, string(botservice.ChannelNameEmailChannel)) - if err != nil { - return fmt.Errorf("Error making get request for Channel Email for Bot %q (Resource Group %q): %+v", resourceGroup, botName, err) - } - - if resp.ID == nil { - return fmt.Errorf("Cannot read Channel Email for Bot %q (Resource Group %q): %+v", resourceGroup, botName, err) + if _, err := client.Create(ctx, resourceId.ResourceGroup, resourceId.BotServiceName, botservice.ChannelNameEmailChannel, channel); err != nil { + return fmt.Errorf("creating Email Channel for Bot %q (Resource Group %q): %+v", resourceId.BotServiceName, resourceId.ResourceGroup, err) } - d.SetId(*resp.ID) - + d.SetId(resourceId.ID("")) return resourceArmBotChannelEmailRead(d, meta) } @@ -118,26 +111,25 @@ func resourceArmBotChannelEmailRead(d *schema.ResourceData, meta interface{}) er ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.BotChannelID(d.Id()) if err != nil { return err } - botName := id.Path["botServices"] - resp, err := client.Get(ctx, id.ResourceGroup, botName, string(botservice.ChannelNameEmailChannel)) + resp, err := client.Get(ctx, id.ResourceGroup, id.BotServiceName, string(botservice.ChannelNameEmailChannel)) if err != nil { if utils.ResponseWasNotFound(resp.Response) { - log.Printf("[INFO] Channel Email for Bot %q (Resource Group %q) was not found - removing from state!", id.ResourceGroup, botName) + log.Printf("[INFO] Email Channel for Bot %q (Resource Group %q) was not found - removing from state!", id.BotServiceName, id.ResourceGroup) d.SetId("") return nil } - return fmt.Errorf("Error reading Channel Email for Bot %q (Resource Group %q): %+v", id.ResourceGroup, botName, err) + return fmt.Errorf("retrieving Email Channel for Bot %q (Resource Group %q): %+v", id.BotServiceName, id.ResourceGroup, err) } + d.Set("bot_name", id.BotServiceName) d.Set("resource_group_name", id.ResourceGroup) - d.Set("location", resp.Location) - d.Set("bot_name", botName) + d.Set("location", location.NormalizeNilable(resp.Location)) if props := resp.Properties; props != nil { if channel, ok := props.AsEmailChannel(); ok { @@ -155,13 +147,11 @@ func resourceArmBotChannelEmailUpdate(d *schema.ResourceData, meta interface{}) ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.BotChannelID(d.Id()) if err != nil { return err } - botName := id.Path["botServices"] - channel := botservice.BotChannel{ Properties: botservice.EmailChannel{ Properties: &botservice.EmailChannelProperties{ @@ -175,21 +165,10 @@ func resourceArmBotChannelEmailUpdate(d *schema.ResourceData, meta interface{}) Kind: botservice.KindBot, } - if _, err := client.Update(ctx, id.ResourceGroup, botName, botservice.ChannelNameEmailChannel, channel); err != nil { - return fmt.Errorf("Error issuing create request for Channel Email for Bot %q (Resource Group %q): %+v", id.ResourceGroup, botName, err) - } - - resp, err := client.Get(ctx, id.ResourceGroup, botName, string(botservice.ChannelNameEmailChannel)) - if err != nil { - return fmt.Errorf("Error making get request for Channel Email for Bot %q (Resource Group %q): %+v", id.ResourceGroup, botName, err) - } - - if resp.ID == nil { - return fmt.Errorf("Cannot read Channel Email for Bot %q (Resource Group %q): %+v", id.ResourceGroup, botName, err) + if _, err := client.Update(ctx, id.ResourceGroup, id.BotServiceName, botservice.ChannelNameEmailChannel, channel); err != nil { + return fmt.Errorf("updating Email Channel for Bot %q (Resource Group %q): %+v", id.ResourceGroup, id.BotServiceName, err) } - d.SetId(*resp.ID) - return resourceArmBotChannelEmailRead(d, meta) } @@ -198,17 +177,15 @@ func resourceArmBotChannelEmailDelete(d *schema.ResourceData, meta interface{}) ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.BotChannelID(d.Id()) if err != nil { return err } - botName := id.Path["botServices"] - - resp, err := client.Delete(ctx, id.ResourceGroup, botName, string(botservice.ChannelNameEmailChannel)) + resp, err := client.Delete(ctx, id.ResourceGroup, id.BotServiceName, string(botservice.ChannelNameEmailChannel)) if err != nil { if !response.WasNotFound(resp.Response) { - return fmt.Errorf("Error deleting Channel Email for Bot %q (Resource Group %q): %+v", id.ResourceGroup, botName, err) + return fmt.Errorf("deleting Email Channel for Bot %q (Resource Group %q): %+v", id.ResourceGroup, id.BotServiceName, err) } }