Skip to content

Commit

Permalink
New Resource: azurerm_bot_channels_registration (#4245)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbfrahry authored Sep 5, 2019
1 parent 3db6f6f commit e76b8c1
Show file tree
Hide file tree
Showing 20 changed files with 6,238 additions and 83 deletions.
3 changes: 3 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/authorization"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/automation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/batch"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/bot"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cdn"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cognitive"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/compute"
Expand Down Expand Up @@ -86,6 +87,7 @@ type ArmClient struct {
automation *automation.Client
authorization *authorization.Client
batch *batch.Client
bot *bot.Client
cdn *cdn.Client
cognitive *cognitive.Client
compute *compute.Client
Expand Down Expand Up @@ -210,6 +212,7 @@ func getArmClient(c *authentication.Config, skipProviderRegistration bool, partn
client.automation = automation.BuildClient(o)
client.authorization = authorization.BuildClient(o)
client.batch = batch.BuildClient(o)
client.bot = bot.BuildClient(o)
client.cdn = cdn.BuildClient(o)
client.cognitive = cognitive.BuildClient(o)
client.compute = compute.BuildClient(o)
Expand Down
20 changes: 20 additions & 0 deletions azurerm/internal/services/bot/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package bot

import (
"github.com/Azure/azure-sdk-for-go/services/preview/botservice/mgmt/2018-07-12/botservice"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common"
)

type Client struct {
BotClient *botservice.BotsClient
}

func BuildClient(o *common.ClientOptions) *Client {

BotClient := botservice.NewBotsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&BotClient.Client, o.ResourceManagerAuthorizer)

return &Client{
BotClient: &BotClient,
}
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_batch_account": resourceArmBatchAccount(),
"azurerm_batch_application": resourceArmBatchApplication(),
"azurerm_batch_certificate": resourceArmBatchCertificate(),
"azurerm_bot_channels_registration": resourceArmBotChannelsRegistration(),
"azurerm_batch_pool": resourceArmBatchPool(),
"azurerm_cdn_endpoint": resourceArmCdnEndpoint(),
"azurerm_cdn_profile": resourceArmCdnProfile(),
Expand Down
1 change: 1 addition & 0 deletions azurerm/required_resource_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func requiredResourceProviders() map[string]struct{} {
"Microsoft.ApiManagement": {},
"Microsoft.Authorization": {},
"Microsoft.Automation": {},
"Microsoft.BotService": {},
"Microsoft.Cache": {},
"Microsoft.Cdn": {},
"Microsoft.CognitiveServices": {},
Expand Down
268 changes: 268 additions & 0 deletions azurerm/resource_arm_bot_channels_registration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
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 resourceArmBotChannelsRegistration() *schema.Resource {
return &schema.Resource{
Create: resourceArmBotChannelsRegistrationCreate,
Read: resourceArmBotChannelsRegistrationRead,
Delete: resourceArmBotChannelsRegistrationDelete,
Update: resourceArmBotChannelsRegistrationUpdate,

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

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

"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: azure.ValidateResourceID,
},

"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,
},

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

func resourceArmBotChannelsRegistrationCreate(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 Bot Channels Registration %q (Resource Group %q): %+v", name, resourceGroup, err)
}
} else {
id, err := azure.CosmosGetIDFromResponse(existing.Response)
if err != nil {
return fmt.Errorf("Error generating import ID for Bot Channels Registration %q (Resource Group %q): %+v", name, resourceGroup, err)
}

return tf.ImportAsExistsError("azurerm_bot", id)
}
}

t := d.Get("tags").(map[string]interface{})
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)),
},
Location: utils.String(d.Get("location").(string)),
Sku: &botservice.Sku{
Name: botservice.SkuName(d.Get("sku").(string)),
},
Kind: botservice.KindBot,
Tags: tags.Expand(t),
}

if _, err := client.Create(ctx, resourceGroup, name, bot); err != nil {
return fmt.Errorf("Error issuing create request for Bot Channels Registration %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 Bot Channels Registration %q (Resource Group %q): %+v", name, resourceGroup, err)
}

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

d.SetId(*resp.ID)

return resourceArmBotChannelsRegistrationRead(d, meta)
}

func resourceArmBotChannelsRegistrationRead(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 Bot Channels Registration %q (Resource Group %q) - removing from state", name, id.ResourceGroup)
d.SetId("")
return nil
}

return fmt.Errorf("Error reading Bot Channels Registration %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)
}

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

func resourceArmBotChannelsRegistrationUpdate(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)
t := d.Get("tags").(map[string]interface{})
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)),
},
Location: utils.String(d.Get("location").(string)),
Sku: &botservice.Sku{
Name: botservice.SkuName(d.Get("sku").(string)),
},
Kind: botservice.KindBot,
Tags: tags.Expand(t),
}

if _, err := client.Update(ctx, resourceGroup, name, bot); err != nil {
return fmt.Errorf("Error issuing update request for Bot Channels Registration %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 Bot Channels Registration %q (Resource Group %q): %+v", name, resourceGroup, err)
}

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

d.SetId(*resp.ID)

return resourceArmBotChannelsRegistrationRead(d, meta)
}

func resourceArmBotChannelsRegistrationDelete(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 Bot Channels Registration %q (Resource Group %q): %+v", name, id.ResourceGroup, err)
}
}

return nil
}
Loading

0 comments on commit e76b8c1

Please sign in to comment.