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

Upgrade Bot Email Channel API version from 2021-05-01-preview to 2022-09-15 #24555

Merged
merged 3 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
91 changes: 47 additions & 44 deletions internal/services/bot/bot_channel_email_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import (
"log"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-sdk/resource-manager/botservice/2022-09-15/channel"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand All @@ -19,7 +22,6 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
"github.com/tombuildsstuff/kermit/sdk/botservice/2021-05-01-preview/botservice"
)

func resourceBotChannelEmail() *pluginsdk.Resource {
Expand Down Expand Up @@ -70,74 +72,76 @@ func resourceBotChannelEmail() *pluginsdk.Resource {
}

func resourceBotChannelEmailCreate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Bot.ChannelClient
client := meta.(*clients.Client).Bot.EmailChannelClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

resourceId := parse.NewBotChannelID(subscriptionId, d.Get("resource_group_name").(string), d.Get("bot_name").(string), string(botservice.ChannelNameEmailChannel))
resourceId := commonids.NewBotServiceChannelID(subscriptionId, d.Get("resource_group_name").(string), d.Get("bot_name").(string), string(channel.BotServiceChannelTypeEmailChannel))
if d.IsNewResource() {
existing, err := client.Get(ctx, resourceId.ResourceGroup, resourceId.BotServiceName, resourceId.ChannelName)
existing, err := client.Get(ctx, resourceId)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing Email Channel for Bot %q (Resource Group %q): %+v", resourceId.BotServiceName, resourceId.ResourceGroup, err)
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for presence of existing Email Channel for Bot %q (Resource Group %q): %+v", resourceId.BotServiceName, resourceId.ResourceGroupName, err)
}
}
if !utils.ResponseWasNotFound(existing.Response) {
if !response.WasNotFound(existing.HttpResponse) {
return tf.ImportAsExistsError("azurerm_bot_channel_email", resourceId.ID())
}
}

channel := botservice.BotChannel{
Properties: botservice.EmailChannel{
Properties: &botservice.EmailChannelProperties{
EmailAddress: utils.String(d.Get("email_address").(string)),
channel := channel.BotChannel{
Properties: channel.EmailChannel{
Properties: &channel.EmailChannelProperties{
EmailAddress: d.Get("email_address").(string),
Password: utils.String(d.Get("email_password").(string)),
IsEnabled: utils.Bool(true),
IsEnabled: true,
},
ChannelName: botservice.ChannelNameBasicChannelChannelNameEmailChannel,
},
Location: utils.String(azure.NormalizeLocation(d.Get("location").(string))),
Kind: botservice.KindBot,
Kind: pointer.To(channel.KindBot),
}

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)
if _, err := client.Create(ctx, resourceId, channel); err != nil {
return fmt.Errorf("creating Email Channel for Bot %q (Resource Group %q): %+v", resourceId.BotServiceName, resourceId.ResourceGroupName, err)
}

d.SetId(resourceId.ID())
return resourceBotChannelEmailRead(d, meta)
}

func resourceBotChannelEmailRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Bot.ChannelClient
client := meta.(*clients.Client).Bot.EmailChannelClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.BotChannelID(d.Id())
id, err := commonids.ParseBotServiceChannelID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.ResourceGroup, id.BotServiceName, string(botservice.ChannelNameEmailChannel))
resp, err := client.Get(ctx, *id)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] Email Channel for Bot %q (Resource Group %q) was not found - removing from state!", id.BotServiceName, id.ResourceGroup)
if response.WasNotFound(resp.HttpResponse) {
log.Printf("[INFO] Email Channel for Bot %q (Resource Group %q) was not found - removing from state!", id.BotServiceName, id.ResourceGroupName)
d.SetId("")
return nil
}

return fmt.Errorf("retrieving Email Channel for Bot %q (Resource Group %q): %+v", id.BotServiceName, id.ResourceGroup, err)
return fmt.Errorf("retrieving Email Channel for Bot %q (Resource Group %q): %+v", id.BotServiceName, id.ResourceGroupName, err)
}

d.Set("bot_name", id.BotServiceName)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("location", location.NormalizeNilable(resp.Location))
d.Set("resource_group_name", id.ResourceGroupName)

if props := resp.Properties; props != nil {
if channel, ok := props.AsEmailChannel(); ok {
if channelProps := channel.Properties; channelProps != nil {
d.Set("email_address", channelProps.EmailAddress)
if model := resp.Model; model != nil {
d.Set("location", location.NormalizeNilable(model.Location))

if props := model.Properties; props != nil {
if channel, ok := props.(channel.EmailChannel); ok {
if channelProps := channel.Properties; channelProps != nil {
d.Set("email_address", channelProps.EmailAddress)
}
}
}
}
Expand All @@ -146,49 +150,48 @@ func resourceBotChannelEmailRead(d *pluginsdk.ResourceData, meta interface{}) er
}

func resourceBotChannelEmailUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Bot.ChannelClient
client := meta.(*clients.Client).Bot.EmailChannelClient
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.BotChannelID(d.Id())
id, err := commonids.ParseBotServiceChannelID(d.Id())
if err != nil {
return err
}

channel := botservice.BotChannel{
Properties: botservice.EmailChannel{
Properties: &botservice.EmailChannelProperties{
EmailAddress: utils.String(d.Get("email_address").(string)),
channel := channel.BotChannel{
Properties: channel.EmailChannel{
Properties: &channel.EmailChannelProperties{
EmailAddress: d.Get("email_address").(string),
Password: utils.String(d.Get("email_password").(string)),
IsEnabled: utils.Bool(true),
IsEnabled: true,
},
ChannelName: botservice.ChannelNameBasicChannelChannelNameEmailChannel,
},
Location: utils.String(azure.NormalizeLocation(d.Get("location").(string))),
Kind: botservice.KindBot,
Kind: pointer.To(channel.KindBot),
}

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)
if _, err := client.Update(ctx, *id, channel); err != nil {
return fmt.Errorf("updating Email Channel for Bot %q (Resource Group %q): %+v", id.ResourceGroupName, id.BotServiceName, err)
}

return resourceBotChannelEmailRead(d, meta)
}

func resourceBotChannelEmailDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Bot.ChannelClient
client := meta.(*clients.Client).Bot.EmailChannelClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.BotChannelID(d.Id())
id, err := commonids.ParseBotServiceChannelID(d.Id())
if err != nil {
return err
}

resp, err := client.Delete(ctx, id.ResourceGroup, id.BotServiceName, string(botservice.ChannelNameEmailChannel))
resp, err := client.Delete(ctx, *id)
if err != nil {
if !response.WasNotFound(resp.Response) {
return fmt.Errorf("deleting Email Channel for Bot %q (Resource Group %q): %+v", id.ResourceGroup, id.BotServiceName, err)
if !response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("deleting Email Channel for Bot %q (Resource Group %q): %+v", id.ResourceGroupName, id.BotServiceName, err)
}
}

Expand Down
3 changes: 0 additions & 3 deletions internal/services/bot/bot_channel_email_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ func testAccBotChannelEmail_update(t *testing.T) {
if ok := skipEmailChannel(); ok {
t.Skip("Skipping as one of `ARM_TEST_EMAIL`, AND `ARM_TEST_EMAIL_PASSWORD` was not specified")
}
if ok := skipSlackChannel(); ok {
t.Skip("Skipping as one of `ARM_TEST_SLACK_CLIENT_ID`, `ARM_TEST_SLACK_CLIENT_SECRET`, or `ARM_TEST_SLACK_VERIFICATION_TOKEN` was not specified")
}
data := acceptance.BuildTestData(t, "azurerm_bot_channel_email", "test")
r := BotChannelEmailResource{}

Expand Down
25 changes: 17 additions & 8 deletions internal/services/bot/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ package client
import (
"fmt"

emailchannel_2022_09_15 "github.com/hashicorp/go-azure-sdk/resource-manager/botservice/2022-09-15/channel"
healthbot_2022_08_08 "github.com/hashicorp/go-azure-sdk/resource-manager/healthbot/2022-08-08"
"github.com/hashicorp/go-azure-sdk/sdk/client/resourcemanager"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
"github.com/tombuildsstuff/kermit/sdk/botservice/2021-05-01-preview/botservice"
)

type Client struct {
BotClient *botservice.BotsClient
ConnectionClient *botservice.BotConnectionClient
ChannelClient *botservice.ChannelsClient
HealthBotClient *healthbot_2022_08_08.Client
BotClient *botservice.BotsClient
ConnectionClient *botservice.BotConnectionClient
ChannelClient *botservice.ChannelsClient
EmailChannelClient *emailchannel_2022_09_15.ChannelClient
HealthBotClient *healthbot_2022_08_08.Client
}

func NewClient(o *common.ClientOptions) (*Client, error) {
Expand All @@ -29,6 +31,12 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
channelClient := botservice.NewChannelsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&channelClient.Client, o.ResourceManagerAuthorizer)

emailChannelClient, err := emailchannel_2022_09_15.NewChannelClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building EmailChannels client: %+v", err)
}
o.Configure(emailChannelClient.Client, o.Authorizers.ResourceManager)

healthBotsClient, err := healthbot_2022_08_08.NewClientWithBaseURI(o.Environment.ResourceManager, func(c *resourcemanager.Client) {
o.Configure(c, o.Authorizers.ResourceManager)
})
Expand All @@ -37,9 +45,10 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
}

return &Client{
BotClient: &botClient,
ChannelClient: &channelClient,
ConnectionClient: &connectionClient,
HealthBotClient: healthBotsClient,
BotClient: &botClient,
ChannelClient: &channelClient,
ConnectionClient: &connectionClient,
EmailChannelClient: emailChannelClient,
HealthBotClient: healthBotsClient,
}, nil
}
Loading
Loading