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 #13832

Closed
wants to merge 10 commits into from
295 changes: 295 additions & 0 deletions internal/services/bot/bot_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,295 @@
package bot

import (
"context"
"fmt"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/botservice/mgmt/2021-03-01/botservice"
"github.com/hashicorp/go-azure-helpers/response"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/location"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/bot/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func resourceBot() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceBotCreate,
Read: resourceBotRead,
Update: resourceBotUpdate,
Delete: resourceBotDelete,
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.BotServiceID(id)
return err
}),

Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Update: pluginsdk.DefaultTimeout(30 * time.Minute),
Delete: pluginsdk.DefaultTimeout(30 * time.Minute),
},

Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"resource_group_name": azure.SchemaResourceGroupName(),

"location": azure.SchemaLocation(),

"kind": {
Type: pluginsdk.TypeString,
Optional: true,
Default: string(botservice.KindAzurebot), // Azure Bot
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(botservice.KindAzurebot),
string(botservice.KindBot),
string(botservice.KindDesigner),
string(botservice.KindFunction),
string(botservice.KindSdk),
}, false),
},

"sku": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(botservice.SkuNameF0),
string(botservice.SkuNameS1),
}, false),
},

"microsoft_app_id": {
Type: pluginsdk.TypeString,
ForceNew: true,
Required: true,
ValidateFunc: validation.IsUUID,
},

"display_name": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"endpoint": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"developer_app_insights_key": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.IsUUID,
},

"developer_app_insights_api_key": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
Sensitive: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"developer_app_insights_application_id": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.IsUUID,
},

"luis_app_ids": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.IsUUID,
},
},

"luis_key": {
Type: pluginsdk.TypeString,
Optional: true,
Sensitive: true,
ValidateFunc: validation.StringIsNotEmpty,
},

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

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

resourceId := parse.NewBotServiceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
if d.IsNewResource() {
existing, err := client.Get(ctx, resourceId.ResourceGroup, resourceId.Name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of %s: %+v", resourceId, err)
}
}
if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_bot", resourceId.ID())
}
}

displayName := d.Get("display_name").(string)
if displayName == "" {
displayName = resourceId.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.Kind(d.Get("kind").(string)),
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

if _, err := client.Create(ctx, resourceId.ResourceGroup, resourceId.Name, bot); err != nil {
return fmt.Errorf("creating %s: %+v", resourceId, err)
}

// TODO: in 3.0 we should remove the "Default Site" on the Directline resource at this point if we can

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

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

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

resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] %s was not found - removing from state", *id)
d.SetId("")
return nil
}

return fmt.Errorf("retrieving %s: %+v", *id, err)
}

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

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

d.Set("kind", resp.Kind)

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 resourceBotUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Bot.BotClient
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

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

displayName := d.Get("display_name").(string)
if displayName == "" {
displayName = id.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.Kind(d.Get("kind").(string)),
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

if _, err := client.Update(ctx, id.ResourceGroup, id.Name, bot); err != nil {
return fmt.Errorf("updating %s: %+v", *id, err)
}

return resourceBotRead(d, meta)
}

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

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

resp, err := client.Delete(ctx, id.ResourceGroup, id.Name)
if err != nil {
if !response.WasNotFound(resp.Response) {
return fmt.Errorf("deleting %s: %+v", id, err)
}
}

return nil
}
Loading