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

azurerm_bot_channel_email - support for magic_code #23129

Merged
merged 7 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
35 changes: 32 additions & 3 deletions internal/services/bot/bot_channel_email_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,18 @@ func resourceBotChannelEmail() *pluginsdk.Resource {

"email_password": {
Type: pluginsdk.TypeString,
Required: true,
Optional: true,
Sensitive: true,
ValidateFunc: validation.StringIsNotEmpty,
ExactlyOneOf: []string{"email_password", "magic_code"},
},

"magic_code": {
Type: pluginsdk.TypeString,
Optional: true,
Sensitive: true,
ValidateFunc: validation.StringIsNotEmpty,
ExactlyOneOf: []string{"email_password", "magic_code"},
},
},
}
Expand Down Expand Up @@ -92,7 +101,6 @@ func resourceBotChannelEmailCreate(d *pluginsdk.ResourceData, meta interface{})
Properties: botservice.EmailChannel{
Properties: &botservice.EmailChannelProperties{
EmailAddress: utils.String(d.Get("email_address").(string)),
Password: utils.String(d.Get("email_password").(string)),
IsEnabled: utils.Bool(true),
},
ChannelName: botservice.ChannelNameBasicChannelChannelNameEmailChannel,
Expand All @@ -101,6 +109,17 @@ func resourceBotChannelEmailCreate(d *pluginsdk.ResourceData, meta interface{})
Kind: botservice.KindBot,
}

if v, ok := d.GetOk("email_password"); ok {
channel, _ := channel.Properties.AsEmailChannel()
channel.Properties.Password = utils.String(v.(string))
}

if v, ok := d.GetOk("magic_code"); ok {
channel, _ := channel.Properties.AsEmailChannel()
channel.Properties.AuthMethod = utils.Float(1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this indicates a Swagger/API issue since these should be exposed as Constant values and not an integer - can we raise a Swagger issue for that

Copy link
Contributor Author

@neil-yechenwei neil-yechenwei Sep 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tombuildsstuff , I assume it's by design since they added the meaning/description of the value. "0" means "Password" by default. "1" means "OAuth". See more details from rest api spec. Does it make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Service team confirmed that it's by design to them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated PR for this concern.

channel.Properties.MagicCode = utils.String(v.(string))
}

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)
}
Expand Down Expand Up @@ -159,7 +178,6 @@ func resourceBotChannelEmailUpdate(d *pluginsdk.ResourceData, meta interface{})
Properties: botservice.EmailChannel{
Properties: &botservice.EmailChannelProperties{
EmailAddress: utils.String(d.Get("email_address").(string)),
Password: utils.String(d.Get("email_password").(string)),
IsEnabled: utils.Bool(true),
},
ChannelName: botservice.ChannelNameBasicChannelChannelNameEmailChannel,
Expand All @@ -168,6 +186,17 @@ func resourceBotChannelEmailUpdate(d *pluginsdk.ResourceData, meta interface{})
Kind: botservice.KindBot,
}

if v, ok := d.GetOk("email_password"); ok {
channel, _ := channel.Properties.AsEmailChannel()
channel.Properties.Password = utils.String(v.(string))
}

if v, ok := d.GetOk("magic_code"); ok {
channel, _ := channel.Properties.AsEmailChannel()
channel.Properties.AuthMethod = utils.Float(1)
channel.Properties.MagicCode = utils.String(v.(string))
}

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)
}
Expand Down
35 changes: 35 additions & 0 deletions internal/services/bot/bot_channel_email_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ func testAccBotChannelEmail_update(t *testing.T) {
})
}

func testAccBotChannelEmail_magicCode(t *testing.T) {
if os.Getenv("ARM_TEST_BOT_RESOURCE_GROUP_NAME") == "" || os.Getenv("ARM_TEST_BOT_LOCATION") == "" || os.Getenv("ARM_TEST_BOT_NAME") == "" || os.Getenv("ARM_TEST_EMAIL") == "" || os.Getenv("ARM_TEST_MAGIC_CODE") == "" {
t.Skip("Skipping as one of `ARM_TEST_BOT_RESOURCE_GROUP_NAME`, `ARM_TEST_BOT_LOCATION`, `ARM_TEST_BOT_NAME`, `ARM_TEST_EMAIL`, AND `ARM_TEST_MAGIC_CODE` was not specified")
}

data := acceptance.BuildTestData(t, "azurerm_bot_channel_email", "test")
r := BotChannelEmailResource{}

data.ResourceSequentialTest(t, r, []acceptance.TestStep{
{
Config: r.magicCode(),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("email_password", "magic_code"),
})
}

func (t BotChannelEmailResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.BotChannelID(state.ID)
if err != nil {
Expand Down Expand Up @@ -108,6 +127,22 @@ resource "azurerm_bot_channel_email" "test" {
`, BotChannelsRegistrationResource{}.basicConfig(data), os.Getenv("ARM_TEST_EMAIL"), os.Getenv("ARM_TEST_EMAIL_PASSWORD"))
}

func (BotChannelEmailResource) magicCode() string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_bot_channel_email" "test" {
bot_name = "%s"
location = "%s"
resource_group_name = "%s"
email_address = "%s"
magic_code = "%s"
}
`, os.Getenv("ARM_TEST_BOT_NAME"), os.Getenv("ARM_TEST_BOT_LOCATION"), os.Getenv("ARM_TEST_BOT_RESOURCE_GROUP_NAME"), os.Getenv("ARM_TEST_EMAIL"), os.Getenv("ARM_TEST_MAGIC_CODE"))
}

func skipEmailChannel() bool {
if os.Getenv("ARM_TEST_EMAIL") == "" || os.Getenv("ARM_TEST_EMAIL_PASSWORD") == "" {
return true
Expand Down
1 change: 1 addition & 0 deletions internal/services/bot/bot_channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestAccBotChannelsRegistration(t *testing.T) {
"alexaRequiresImport": testAccBotChannelAlexa_requiresImport,
"emailBasic": testAccBotChannelEmail_basic,
"emailUpdate": testAccBotChannelEmail_update,
"emailMagicCode": testAccBotChannelEmail_magicCode,
"slackBasic": testAccBotChannelSlack_basic,
"slackComplete": testAccBotChannelSlack_complete,
"slackUpdate": testAccBotChannelSlack_update,
Expand Down
4 changes: 3 additions & 1 deletion website/docs/r/bot_channel_email.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ The following arguments are supported:

* `email_address` - (Required) The email address that the Bot will authenticate with.

* `email_password` - (Required) The email password that the Bot will authenticate with.
* `email_password` - (Optional) The email password that the Bot will authenticate with.

* `magic_code` - (Optional) The magic code for setting up the modern authentication(OAUTH).
katbyte marked this conversation as resolved.
Show resolved Hide resolved

## Attributes Reference

Expand Down