Skip to content

Commit

Permalink
r/bot_channel_ms_teams: switching to use a Resource ID Parser
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff committed Dec 4, 2020
1 parent 9f51d32 commit 67dfb04
Showing 1 changed file with 31 additions and 38 deletions.
69 changes: 31 additions & 38 deletions azurerm/internal/services/bot/bot_channel_ms_teams_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"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"
)
Expand All @@ -24,9 +27,10 @@ func resourceArmBotChannelMsTeams() *schema.Resource {
Delete: resourceArmBotChannelMsTeamsDelete,
Update: resourceArmBotChannelMsTeamsUpdate,

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),
Expand All @@ -43,6 +47,7 @@ func resourceArmBotChannelMsTeams() *schema.Resource {
"bot_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},

Expand All @@ -66,21 +71,20 @@ func resourceArmBotChannelMsTeams() *schema.Resource {

func resourceArmBotChannelMsTeamsCreate(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.ChannelNameMsTeamsChannel))
if d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, string(botservice.ChannelNameMsTeamsChannel), 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 MsTeams for Bot %q (Resource Group %q): %+v", resourceGroup, botName, err)
return fmt.Errorf("checking for the presence of existing MS Teams 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_ms_teams", *existing.ID)
return tf.ImportAsExistsError("azurerm_bot_channel_ms_teams", resourceId.ID(""))
}
}

Expand All @@ -101,21 +105,11 @@ func resourceArmBotChannelMsTeamsCreate(d *schema.ResourceData, meta interface{}
channel.Properties.CallingWebHook = utils.String(v.(string))
}

if _, err := client.Create(ctx, resourceGroup, botName, botservice.ChannelNameMsTeamsChannel, channel); err != nil {
return fmt.Errorf("creating Channel MsTeams for Bot %q (Resource Group %q): %+v", botName, resourceGroup, err)
}

resp, err := client.Get(ctx, resourceGroup, botName, string(botservice.ChannelNameMsTeamsChannel))
if err != nil {
return fmt.Errorf("retrieving Channel MsTeams for Bot %q (Resource Group %q): %+v", botName, resourceGroup, err)
}

if resp.ID == nil || *resp.ID == "" {
return fmt.Errorf("empty or nil ID returned for Channel MsTeams for Bot %q (Resource Group %q): %+v", botName, resourceGroup, err)
if _, err := client.Create(ctx, resourceId.ResourceGroup, resourceId.BotServiceName, botservice.ChannelNameMsTeamsChannel, channel); err != nil {
return fmt.Errorf("creating MS Teams Channel for Bot %q (Resource Group %q): %+v", resourceId.BotServiceName, resourceId.ResourceGroup, err)
}

d.SetId(*resp.ID)

d.SetId(resourceId.ID(""))
return resourceArmBotChannelMsTeamsRead(d, meta)
}

Expand All @@ -124,26 +118,25 @@ func resourceArmBotChannelMsTeamsRead(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.ChannelNameMsTeamsChannel))
resp, err := client.Get(ctx, id.ResourceGroup, id.BotServiceName, string(botservice.ChannelNameMsTeamsChannel))
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] Channel MsTeams for Bot %q (Resource Group %q) was not found - removing from state!", id.ResourceGroup, botName)
log.Printf("[INFO] MS Teams 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 MsTeams for Bot %q (Resource Group %q): %+v", id.ResourceGroup, botName, err)
return fmt.Errorf("retrieving MS Teams 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.AsMsTeamsChannel(); ok {
Expand All @@ -162,8 +155,10 @@ func resourceArmBotChannelMsTeamsUpdate(d *schema.ResourceData, meta interface{}
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.MsTeamsChannel{
Expand All @@ -178,8 +173,8 @@ func resourceArmBotChannelMsTeamsUpdate(d *schema.ResourceData, meta interface{}
Kind: botservice.KindBot,
}

if _, err := client.Update(ctx, resourceGroup, botName, botservice.ChannelNameMsTeamsChannel, channel); err != nil {
return fmt.Errorf("updating Channel MsTeams for Bot %q (Resource Group %q): %+v", botName, resourceGroup, err)
if _, err := client.Update(ctx, id.ResourceGroup, id.BotServiceName, botservice.ChannelNameMsTeamsChannel, channel); err != nil {
return fmt.Errorf("updating MS Teams Channel for Bot %q (Resource Group %q): %+v", id.BotServiceName, id.ResourceGroup, err)
}

return resourceArmBotChannelMsTeamsRead(d, meta)
Expand All @@ -190,17 +185,15 @@ func resourceArmBotChannelMsTeamsDelete(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.ChannelNameMsTeamsChannel))
resp, err := client.Delete(ctx, id.ResourceGroup, id.BotServiceName, string(botservice.ChannelNameMsTeamsChannel))
if err != nil {
if !response.WasNotFound(resp.Response) {
return fmt.Errorf("deleting Channel MsTeams for Bot %q (Resource Group %q): %+v", botName, id.ResourceGroup, err)
return fmt.Errorf("deleting MS Teams Channel for Bot %q (Resource Group %q): %+v", id.BotServiceName, id.ResourceGroup, err)
}
}

Expand Down

0 comments on commit 67dfb04

Please sign in to comment.