-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Resource:
azurerm_bot_channel_email
(#4389)
- Loading branch information
Showing
5 changed files
with
466 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
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 | ||
} |
Oops, something went wrong.