From f782e7aed34c94e091100022b63a30715f51dcb7 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Fri, 4 Dec 2020 14:15:10 +0100 Subject: [PATCH] r/bot_channel_directline: switching to use a Resource ID Parser --- .../bot/bot_channel_directline_resource.go | 96 ++++++++----------- 1 file changed, 38 insertions(+), 58 deletions(-) diff --git a/azurerm/internal/services/bot/bot_channel_directline_resource.go b/azurerm/internal/services/bot/bot_channel_directline_resource.go index c6582fb025957..401cfdf49c3d1 100644 --- a/azurerm/internal/services/bot/bot_channel_directline_resource.go +++ b/azurerm/internal/services/bot/bot_channel_directline_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 resourceArmBotChannelDirectline() *schema.Resource { Delete: resourceArmBotChannelDirectlineDelete, Update: resourceArmBotChannelDirectlineUpdate, - 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), @@ -42,6 +46,7 @@ func resourceArmBotChannelDirectline() *schema.Resource { "bot_name": { Type: schema.TypeString, Required: true, + ForceNew: true, ValidateFunc: validation.StringIsNotEmpty, }, @@ -50,7 +55,6 @@ func resourceArmBotChannelDirectline() *schema.Resource { Required: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ - "name": { Type: schema.TypeString, Required: true, @@ -115,21 +119,20 @@ func resourceArmBotChannelDirectline() *schema.Resource { func resourceArmBotChannelDirectlineCreate(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.ChannelNameDirectLineChannel)) if d.IsNewResource() { - existing, err := client.Get(ctx, resourceGroup, string(botservice.ChannelNameDirectLineChannel1), 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 Directline for Bot %q (Resource Group %q): %+v", resourceGroup, botName, err) + return fmt.Errorf("checking for presence of existing Directline 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_directline", *existing.ID) + return tf.ImportAsExistsError("azurerm_bot_channel_directline", resourceId.ID("")) } } @@ -144,26 +147,16 @@ func resourceArmBotChannelDirectlineCreate(d *schema.ResourceData, meta interfac Kind: botservice.KindBot, } - if _, err := client.Create(ctx, resourceGroup, botName, botservice.ChannelNameDirectLineChannel, channel); err != nil { - return fmt.Errorf("Error issuing create request for Channel Directline for Bot %q (Resource Group %q): %+v", resourceGroup, botName, err) + if _, err := client.Create(ctx, resourceId.ResourceGroup, resourceId.BotServiceName, botservice.ChannelNameDirectLineChannel, channel); err != nil { + return fmt.Errorf("creating Directline Channel for Bot %q (Resource Group %q): %+v", resourceId.BotServiceName, resourceId.ResourceGroup, err) } + d.SetId(resourceId.ID("")) // Unable to create a new site with enhanced_authentication_enabled in the same operation, so we need to make two calls - if _, err := client.Update(ctx, resourceGroup, botName, botservice.ChannelNameDirectLineChannel, channel); err != nil { - return fmt.Errorf("Error issuing create request for Channel Directline for Bot %q (Resource Group %q): %+v", resourceGroup, botName, err) + if _, err := client.Update(ctx, resourceId.ResourceGroup, resourceId.BotServiceName, botservice.ChannelNameDirectLineChannel, channel); err != nil { + return fmt.Errorf("updating Directline Channel for Bot %q (Resource Group %q): %+v", resourceId.BotServiceName, resourceId.ResourceGroup, err) } - resp, err := client.Get(ctx, resourceGroup, botName, string(botservice.ChannelNameDirectLineChannel1)) - if err != nil { - return fmt.Errorf("Error making get request for Channel Directline for Bot %q (Resource Group %q): %+v", resourceGroup, botName, err) - } - - if resp.ID == nil { - return fmt.Errorf("Cannot read Channel Directline for Bot %q (Resource Group %q): %+v", resourceGroup, botName, err) - } - - d.SetId(*resp.ID) - return resourceArmBotChannelDirectlineRead(d, meta) } @@ -172,28 +165,31 @@ func resourceArmBotChannelDirectlineRead(d *schema.ResourceData, meta interface{ 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.ChannelNameDirectLineChannel1)) + resp, err := client.Get(ctx, id.ResourceGroup, id.BotServiceName, string(botservice.ChannelNameDirectLineChannel1)) if err != nil { if utils.ResponseWasNotFound(resp.Response) { - log.Printf("[INFO] Channel Directline for Bot %q (Resource Group %q) was not found - removing from state!", id.ResourceGroup, botName) + log.Printf("[INFO] Directline Channel for Bot %q (Resource Group %q) was not found - removing from state!", id.ResourceGroup, id.BotServiceName) d.SetId("") return nil } - return fmt.Errorf("Error reading Channel Directline for Bot %q (Resource Group %q): %+v", id.ResourceGroup, botName, err) + return fmt.Errorf("retrieving Channel Directline for Bot %q (Resource Group %q): %+v", id.ResourceGroup, id.BotServiceName, err) + } + + channelsResp, err := client.ListWithKeys(ctx, id.ResourceGroup, id.BotServiceName, botservice.ChannelNameDirectLineChannel) + if err != nil { + return fmt.Errorf("listing Keys for Directline Channel for Bot %q (Resource Group %q): %+v", id.ResourceGroup, id.BotServiceName, 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)) - channelsResp, _ := client.ListWithKeys(ctx, id.ResourceGroup, botName, botservice.ChannelNameDirectLineChannel) if props := channelsResp.Properties; props != nil { if channel, ok := props.AsDirectLineChannel(); ok { if channelProps := channel.Properties; channelProps != nil { @@ -210,8 +206,10 @@ func resourceArmBotChannelDirectlineUpdate(d *schema.ResourceData, meta interfac ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d) defer cancel() - botName := d.Get("bot_name").(string) - resourceGroup := d.Get("resource_group_name").(string) + id, err := parse.BotChannelID(d.Id()) + if err != nil { + return err + } channel := botservice.BotChannel{ Properties: botservice.DirectLineChannel{ @@ -224,26 +222,10 @@ func resourceArmBotChannelDirectlineUpdate(d *schema.ResourceData, meta interfac Kind: botservice.KindBot, } - if _, err := client.Update(ctx, resourceGroup, botName, botservice.ChannelNameDirectLineChannel, channel); err != nil { - return fmt.Errorf("Error issuing create request for Channel Directline for Bot %q (Resource Group %q): %+v", resourceGroup, botName, err) - } - - // Unable to create a new site with enhanced_authentication_enabled in the same operation, so we need to make two calls - if _, err := client.Update(ctx, resourceGroup, botName, botservice.ChannelNameDirectLineChannel, channel); err != nil { - return fmt.Errorf("Error issuing create request for Channel Directline for Bot %q (Resource Group %q): %+v", resourceGroup, botName, err) - } - - resp, err := client.Get(ctx, resourceGroup, botName, string(botservice.ChannelNameDirectLineChannel1)) - if err != nil { - return fmt.Errorf("Error making get request for Channel Directline for Bot %q (Resource Group %q): %+v", resourceGroup, botName, err) - } - - if resp.ID == nil { - return fmt.Errorf("Cannot read Channel Directline for Bot %q (Resource Group %q): %+v", resourceGroup, botName, err) + if _, err := client.Update(ctx, id.ResourceGroup, id.BotServiceName, botservice.ChannelNameDirectLineChannel, channel); err != nil { + return fmt.Errorf("updating Directline Channel for Bot %q (Resource Group %q): %+v", id.BotServiceName, id.ResourceGroup, err) } - d.SetId(*resp.ID) - return resourceArmBotChannelDirectlineRead(d, meta) } @@ -252,17 +234,15 @@ func resourceArmBotChannelDirectlineDelete(d *schema.ResourceData, meta interfac 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.ChannelNameDirectLineChannel1)) + resp, err := client.Delete(ctx, id.ResourceGroup, id.BotServiceName, string(botservice.ChannelNameDirectLineChannel1)) if err != nil { if !response.WasNotFound(resp.Response) { - return fmt.Errorf("Error deleting Channel Directline for Bot %q (Resource Group %q): %+v", id.ResourceGroup, botName, err) + return fmt.Errorf("deleting Directline Channel for Bot %q (Resource Group %q): %+v", id.BotServiceName, id.ResourceGroup, err) } }