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

New Resource: azurerm_bot_channel_email #4389

Merged
merged 3 commits into from
Sep 27, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_batch_account": resourceArmBatchAccount(),
"azurerm_batch_application": resourceArmBatchApplication(),
"azurerm_batch_certificate": resourceArmBatchCertificate(),
"azurerm_bot_channel_email": resourceArmBotChannelEmail(),
"azurerm_bot_channel_slack": resourceArmBotChannelSlack(),
"azurerm_bot_channels_registration": resourceArmBotChannelsRegistration(),
"azurerm_bot_connection": resourceArmBotConnection(),
Expand Down
203 changes: 203 additions & 0 deletions azurerm/resource_arm_bot_channel_email.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
package azurerm

import (
"fmt"
"log"

"github.com/Azure/azure-sdk-for-go/services/preview/botservice/mgmt/2018-07-12/botservice"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response"
"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/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmBotChannelEmail() *schema.Resource {
return &schema.Resource{
Create: resourceArmBotChannelEmailCreate,
Read: resourceArmBotChannelEmailRead,
Delete: resourceArmBotChannelEmailDelete,
Update: resourceArmBotChannelEmailUpdate,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"resource_group_name": azure.SchemaResourceGroupName(),

"location": azure.SchemaLocation(),

"bot_name": {
Type: schema.TypeString,
Required: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

looks like this needs to be ForceNew?

ForceNew: true,
ValidateFunc: validate.NoEmptyStrings,
},

"email_address": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
},

"email_password": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
ValidateFunc: validate.NoEmptyStrings,
},
},
}
}

func resourceArmBotChannelEmailCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).bot.ChannelClient
ctx := meta.(*ArmClient).StopContext

resourceGroup := d.Get("resource_group_name").(string)
botName := d.Get("bot_name").(string)

if features.ShouldResourcesBeImported() && d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, string(botservice.ChannelNameEmailChannel), botName)
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)
}
}
if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_bot_channel_email", *existing.ID)
}
}

channel := botservice.BotChannel{
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.ChannelNameEmailChannel1,
},
Location: utils.String(azure.NormalizeLocation(d.Get("location").(string))),
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)
}

d.SetId(*resp.ID)

return resourceArmBotChannelEmailRead(d, meta)
}

func resourceArmBotChannelEmailRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).bot.ChannelClient
ctx := meta.(*ArmClient).StopContext

id, err := azure.ParseAzureResourceID(d.Id())
if err != nil {
return err
}

botName := id.Path["botServices"]
resp, err := client.Get(ctx, id.ResourceGroup, botName, 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)
d.SetId("")
return nil
}

return fmt.Errorf("Error reading Channel Email for Bot %q (Resource Group %q): %+v", id.ResourceGroup, botName, err)
}

d.Set("resource_group_name", id.ResourceGroup)
d.Set("location", resp.Location)
d.Set("bot_name", botName)

if props := resp.Properties; props != nil {
if channel, ok := props.AsEmailChannel(); ok {
if channelProps := channel.Properties; channelProps != nil {
d.Set("email_address", channelProps.EmailAddress)
}
}
}

return nil
}

func resourceArmBotChannelEmailUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).bot.ChannelClient
ctx := meta.(*ArmClient).StopContext

id, err := azure.ParseAzureResourceID(d.Id())
if err != nil {
return err
}

botName := id.Path["botServices"]

channel := botservice.BotChannel{
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.ChannelNameEmailChannel1,
},
Location: utils.String(azure.NormalizeLocation(d.Get("location").(string))),
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)
}

d.SetId(*resp.ID)

return resourceArmBotChannelEmailRead(d, meta)
}

func resourceArmBotChannelEmailDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).bot.ChannelClient
ctx := meta.(*ArmClient).StopContext

id, err := azure.ParseAzureResourceID(d.Id())
if err != nil {
return err
}

botName := id.Path["botServices"]

resp, err := client.Delete(ctx, id.ResourceGroup, botName, 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 nil
}
Loading