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_web_app #4411

Merged
merged 1 commit into from
Sep 24, 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 @@ -198,6 +198,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_bot_channel_slack": resourceArmBotChannelSlack(),
"azurerm_bot_channels_registration": resourceArmBotChannelsRegistration(),
"azurerm_bot_connection": resourceArmBotConnection(),
"azurerm_bot_web_app": resourceArmBotWebApp(),
"azurerm_batch_pool": resourceArmBatchPool(),
"azurerm_cdn_endpoint": resourceArmCdnEndpoint(),
"azurerm_cdn_profile": resourceArmCdnProfile(),
Expand Down
5 changes: 5 additions & 0 deletions azurerm/resource_arm_bot_channels_registration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func TestAccAzureRMBotChannelsRegistration(t *testing.T) {
"slackBasic": testAccAzureRMBotChannelSlack_basic,
"slackUpdate": testAccAzureRMBotChannelSlack_update,
},
"web_app": {
"basic": testAccAzureRMBotWebApp_basic,
"update": testAccAzureRMBotWebApp_update,
"complete": testAccAzureRMBotWebApp_complete,
},
}

for group, m := range testCases {
Expand Down
283 changes: 283 additions & 0 deletions azurerm/resource_arm_bot_web_app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
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/hashicorp/terraform/helper/validation"
"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/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmBotWebApp() *schema.Resource {
return &schema.Resource{
Create: resourceArmBotWebAppCreate,
Read: resourceArmBotWebAppRead,
Delete: resourceArmBotWebAppDelete,
Update: resourceArmBotWebAppUpdate,

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

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.NoEmptyStrings,
},

"resource_group_name": azure.SchemaResourceGroupName(),

"location": azure.SchemaLocation(),

"sku": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(botservice.F0),
string(botservice.S1),
}, false),
},

"microsoft_app_id": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
ValidateFunc: validate.UUID,
},

"display_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validate.NoEmptyStrings,
},

"endpoint": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validate.NoEmptyStrings,
},

"developer_app_insights_key": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validate.UUID,
},

"developer_app_insights_api_key": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Sensitive: true,
ValidateFunc: validate.NoEmptyStrings,
},

"developer_app_insights_application_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validate.UUID,
},

"luis_app_ids": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validate.UUID,
},
},

"luis_key": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
ValidateFunc: validate.NoEmptyStrings,
},

"tags": tags.Schema(),
},
}
}

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

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)

if features.ShouldResourcesBeImported() && d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of creating Web App Bot %q (Resource Group %q): %+v", name, resourceGroup, err)
}
}
if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_bot_web_app", *existing.ID)
}
}

displayName := d.Get("display_name").(string)
if displayName == "" {
displayName = name
}

bot := botservice.Bot{
Properties: &botservice.BotProperties{
DisplayName: utils.String(displayName),
Endpoint: utils.String(d.Get("endpoint").(string)),
MsaAppID: utils.String(d.Get("microsoft_app_id").(string)),
DeveloperAppInsightKey: utils.String(d.Get("developer_app_insights_key").(string)),
DeveloperAppInsightsAPIKey: utils.String(d.Get("developer_app_insights_api_key").(string)),
DeveloperAppInsightsApplicationID: utils.String(d.Get("developer_app_insights_application_id").(string)),
LuisAppIds: utils.ExpandStringSlice(d.Get("luis_app_ids").([]interface{})),
LuisKey: utils.String(d.Get("luis_key").(string)),
},
Location: utils.String(d.Get("location").(string)),
Sku: &botservice.Sku{
Name: botservice.SkuName(d.Get("sku").(string)),
},
Kind: botservice.KindSdk,
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

if _, err := client.Create(ctx, resourceGroup, name, bot); err != nil {
return fmt.Errorf("Error issuing create request for Web App Bot %q (Resource Group %q): %+v", name, resourceGroup, err)
}

resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("Error making get request for Web App Bot %q (Resource Group %q): %+v", name, resourceGroup, err)
}

if resp.ID == nil {
return fmt.Errorf("Cannot read Web App Bot %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.SetId(*resp.ID)

return resourceArmBotWebAppRead(d, meta)
}

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

id, err := azure.ParseAzureResourceID(d.Id())
if err != nil {
return err
}
name := id.Path["botServices"]

resp, err := client.Get(ctx, id.ResourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] Error reading Web App Bot %q (Resource Group %q) - removing from state", name, id.ResourceGroup)
d.SetId("")
return nil
}

return fmt.Errorf("Error reading Web App Bot %q (Resource Group %q): %+v", name, id.ResourceGroup, err)
}

d.Set("resource_group_name", id.ResourceGroup)
d.Set("name", resp.Name)
d.Set("location", resp.Location)

if sku := resp.Sku; sku != nil {
d.Set("sku", string(sku.Name))
}

if props := resp.Properties; props != nil {
d.Set("microsoft_app_id", props.MsaAppID)
d.Set("endpoint", props.Endpoint)
d.Set("display_name", props.DisplayName)
d.Set("developer_app_insights_key", props.DeveloperAppInsightKey)
d.Set("developer_app_insights_application_id", props.DeveloperAppInsightsApplicationID)
d.Set("luis_app_ids", props.LuisAppIds)
}

return tags.FlattenAndSet(d, resp.Tags)
}

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

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
displayName := d.Get("display_name").(string)
if displayName == "" {
displayName = name
}

bot := botservice.Bot{
Properties: &botservice.BotProperties{
DisplayName: utils.String(displayName),
Endpoint: utils.String(d.Get("endpoint").(string)),
MsaAppID: utils.String(d.Get("microsoft_app_id").(string)),
DeveloperAppInsightKey: utils.String(d.Get("developer_app_insights_key").(string)),
DeveloperAppInsightsAPIKey: utils.String(d.Get("developer_app_insights_api_key").(string)),
DeveloperAppInsightsApplicationID: utils.String(d.Get("developer_app_insights_application_id").(string)),
LuisAppIds: utils.ExpandStringSlice(d.Get("luis_app_ids").([]interface{})),
LuisKey: utils.String(d.Get("luis_key").(string)),
},
Location: utils.String(d.Get("location").(string)),
Sku: &botservice.Sku{
Name: botservice.SkuName(d.Get("sku").(string)),
},
Kind: botservice.KindSdk,
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

if _, err := client.Update(ctx, resourceGroup, name, bot); err != nil {
return fmt.Errorf("Error issuing update request for Web App Bot %q (Resource Group %q): %+v", name, resourceGroup, err)
}

resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("Error making get request for Web App Bot %q (Resource Group %q): %+v", name, resourceGroup, err)
}

if resp.ID == nil {
return fmt.Errorf("Cannot read Web App Bot %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.SetId(*resp.ID)

return resourceArmBotWebAppRead(d, meta)
}

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

id, err := azure.ParseAzureResourceID(d.Id())
if err != nil {
return err
}
name := id.Path["botServices"]

resp, err := client.Delete(ctx, id.ResourceGroup, name)
if err != nil {
if !response.WasNotFound(resp.Response) {
return fmt.Errorf("Error deleting Web App Bot %q (Resource Group %q): %+v", name, id.ResourceGroup, err)
}
}

return nil
}
Loading