Skip to content

Commit

Permalink
azurerm_bot_channel_web_chat - site_names is removed in favour of…
Browse files Browse the repository at this point in the history
… `site` (#23161)

* azurerm_bot_channel_web_chat - support for site

* update code

* update code

* update code

* update code

* update code

* update code

* update code

* update code

* update code

* update code

* update code

* update code

* update code

* update code

* update code

* update code

* update code
  • Loading branch information
neil-yechenwei committed Sep 19, 2023
1 parent fc0aad7 commit 6afdc84
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 23 deletions.
176 changes: 161 additions & 15 deletions internal/services/bot/bot_channel_web_chat_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"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/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/bot/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/bot/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand All @@ -23,7 +24,7 @@ import (
)

func resourceBotChannelWebChat() *pluginsdk.Resource {
return &pluginsdk.Resource{
resource := &pluginsdk.Resource{
Create: resourceBotChannelWebChatCreate,
Read: resourceBotChannelWebChatRead,
Delete: resourceBotChannelWebChatDelete,
Expand Down Expand Up @@ -53,16 +54,61 @@ func resourceBotChannelWebChat() *pluginsdk.Resource {
ValidateFunc: validate.BotName,
},

"site_names": {
"site": {
Type: pluginsdk.TypeSet,
Required: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
Optional: true,
Computed: !features.FourPointOhBeta(),
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"user_upload_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"endpoint_parameters_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
},

"storage_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},
},
},
ExactlyOneOf: func() []string {
if !features.FourPointOhBeta() {
return []string{"site_names", "site"}
}
return []string{}
}(),
},
},
}

if !features.FourPointOhBeta() {
resource.Schema["site_names"] = &pluginsdk.Schema{
Type: pluginsdk.TypeSet,
Optional: true,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
},
Deprecated: "`site_names` will be removed in favour of the property `site` in version 4.0 of the AzureRM Provider.",
ExactlyOneOf: []string{"site_names", "site"},
}
}

return resource
}

func resourceBotChannelWebChatCreate(d *pluginsdk.ResourceData, meta interface{}) error {
Expand Down Expand Up @@ -99,20 +145,36 @@ func resourceBotChannelWebChatCreate(d *pluginsdk.ResourceData, meta interface{}

channel := botservice.BotChannel{
Properties: botservice.WebChatChannel{
Properties: &botservice.WebChatChannelProperties{
Sites: expandSiteNames(d.Get("site_names").(*pluginsdk.Set).List()),
},
Properties: &botservice.WebChatChannelProperties{},
ChannelName: botservice.ChannelNameBasicChannelChannelNameWebChatChannel,
},
Location: utils.String(azure.NormalizeLocation(d.Get("location").(string))),
Kind: botservice.KindBot,
}

if !features.FourPointOhBeta() {
if v, ok := d.GetOk("site_names"); ok {
channel, _ := channel.Properties.AsWebChatChannel()
channel.Properties.Sites = expandSiteNames(v.(*pluginsdk.Set).List())
}
}

if v, ok := d.GetOk("site"); ok {
channel, _ := channel.Properties.AsWebChatChannel()
channel.Properties.Sites = expandSites(v.(*pluginsdk.Set).List())
}

if _, err := client.Create(ctx, id.ResourceGroup, id.BotServiceName, botservice.ChannelNameWebChatChannel, channel); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

// Unable to add a new site with user_upload_enabled, endpoint_parameters_enabled, storage_enabled in the same operation, so we need to make two calls
if _, err := client.Update(ctx, id.ResourceGroup, id.BotServiceName, botservice.ChannelNameWebChatChannel, channel); err != nil {
return fmt.Errorf("updating %s: %+v", id, err)
}

d.SetId(id.ID())

return resourceBotChannelWebChatRead(d, meta)
}

Expand Down Expand Up @@ -144,8 +206,14 @@ func resourceBotChannelWebChatRead(d *pluginsdk.ResourceData, meta interface{})
if props := resp.Properties; props != nil {
if channel, ok := props.AsWebChatChannel(); ok {
if channelProps := channel.Properties; channelProps != nil {
if err := d.Set("site_names", flattenSiteNames(channelProps.Sites)); err != nil {
return fmt.Errorf("setting `site_names`: %+v", err)
if !features.FourPointOhBeta() {
if err := d.Set("site_names", flattenSiteNames(channelProps.Sites)); err != nil {
return fmt.Errorf("setting `site_names`: %+v", err)
}
}

if err := d.Set("site", flattenSites(channelProps.Sites)); err != nil {
return fmt.Errorf("setting `site`: %+v", err)
}
}
}
Expand All @@ -166,15 +234,30 @@ func resourceBotChannelWebChatUpdate(d *pluginsdk.ResourceData, meta interface{}

channel := botservice.BotChannel{
Properties: botservice.WebChatChannel{
Properties: &botservice.WebChatChannelProperties{
Sites: expandSiteNames(d.Get("site_names").(*pluginsdk.Set).List()),
},
Properties: &botservice.WebChatChannelProperties{},
ChannelName: botservice.ChannelNameBasicChannelChannelNameWebChatChannel,
},
Location: utils.String(azure.NormalizeLocation(d.Get("location").(string))),
Kind: botservice.KindBot,
}

if !features.FourPointOhBeta() {
if d.HasChange("site_names") {
channel, _ := channel.Properties.AsWebChatChannel()
channel.Properties.Sites = expandSiteNames(d.Get("site_names").(*pluginsdk.Set).List())
}
}

if d.HasChange("site") {
channel, _ := channel.Properties.AsWebChatChannel()
channel.Properties.Sites = expandSites(d.Get("site").(*pluginsdk.Set).List())
}

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

// Unable to add a new site with user_upload_enabled, endpoint_parameters_enabled, storage_enabled in the same operation, so we need to make two calls
if _, err := client.Update(ctx, id.ResourceGroup, id.BotServiceName, botservice.ChannelNameWebChatChannel, channel); err != nil {
return fmt.Errorf("updating %s: %+v", id, err)
}
Expand All @@ -192,6 +275,11 @@ func resourceBotChannelWebChatDelete(d *pluginsdk.ResourceData, meta interface{}
return err
}

existing, err := client.Get(ctx, id.ResourceGroup, id.BotServiceName, string(botservice.ChannelNameWebChatChannel))
if err != nil {
return err
}

channel := botservice.BotChannel{
Properties: botservice.WebChatChannel{
Properties: &botservice.WebChatChannelProperties{
Expand All @@ -204,7 +292,7 @@ func resourceBotChannelWebChatDelete(d *pluginsdk.ResourceData, meta interface{}
},
ChannelName: botservice.ChannelNameBasicChannelChannelNameWebChatChannel,
},
Location: utils.String(azure.NormalizeLocation(d.Get("location").(string))),
Location: utils.String(azure.NormalizeLocation(*existing.Location)),
Kind: botservice.KindBot,
}

Expand All @@ -230,6 +318,28 @@ func expandSiteNames(input []interface{}) *[]botservice.WebChatSite {
return &results
}

func expandSites(input []interface{}) *[]botservice.WebChatSite {
results := make([]botservice.WebChatSite, 0)

for _, item := range input {
site := item.(map[string]interface{})
result := botservice.WebChatSite{
IsEnabled: utils.Bool(true),
IsBlockUserUploadEnabled: utils.Bool(!site["user_upload_enabled"].(bool)),
IsEndpointParametersEnabled: utils.Bool(site["endpoint_parameters_enabled"].(bool)),
IsNoStorageEnabled: utils.Bool(!site["storage_enabled"].(bool)),
}

if siteName := site["name"].(string); siteName != "" {
result.SiteName = utils.String(siteName)
}

results = append(results, result)
}

return &results
}

func flattenSiteNames(input *[]botservice.WebChatSite) []interface{} {
results := make([]interface{}, 0)
if input == nil {
Expand All @@ -248,6 +358,42 @@ func flattenSiteNames(input *[]botservice.WebChatSite) []interface{} {
return results
}

func flattenSites(input *[]botservice.WebChatSite) []interface{} {
results := make([]interface{}, 0)

for _, item := range *input {
result := make(map[string]interface{})

var name string
if v := item.SiteName; v != nil {
name = *v
}
result["name"] = name

userUploadEnabled := true
if v := item.IsBlockUserUploadEnabled; v != nil {
userUploadEnabled = !*v
}
result["user_upload_enabled"] = userUploadEnabled

var endpointParametersEnabled bool
if v := item.IsEndpointParametersEnabled; v != nil {
endpointParametersEnabled = *v
}
result["endpoint_parameters_enabled"] = endpointParametersEnabled

storageEnabled := true
if v := item.IsNoStorageEnabled; v != nil {
storageEnabled = !*v
}
result["storage_enabled"] = storageEnabled

results = append(results, result)
}

return results
}

func includeDefaultWebChatSite(sites *[]botservice.WebChatSite) bool {
includeDefaultSite := false
for _, site := range *sites {
Expand Down
48 changes: 44 additions & 4 deletions internal/services/bot/bot_channel_web_chat_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/bot/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
Expand Down Expand Up @@ -68,9 +69,16 @@ func testAccBotChannelWebChat_update(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_bot_channel_web_chat", "test")
r := BotChannelWebChatResource{}

var basicConfig string
if features.FourPointOhBeta() {
basicConfig = r.basic(data)
} else {
basicConfig = r.siteNames(data)
}

data.ResourceSequentialTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Config: basicConfig,
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
Expand Down Expand Up @@ -115,7 +123,10 @@ resource "azurerm_bot_channel_web_chat" "test" {
bot_name = azurerm_bot_channels_registration.test.name
location = azurerm_bot_channels_registration.test.location
resource_group_name = azurerm_resource_group.test.name
site_names = ["TestSite"]
site {
name = "TestSite"
}
}
`, BotChannelsRegistrationResource{}.basicConfig(data))
}
Expand All @@ -128,7 +139,10 @@ resource "azurerm_bot_channel_web_chat" "import" {
bot_name = azurerm_bot_channel_web_chat.test.bot_name
location = azurerm_bot_channel_web_chat.test.location
resource_group_name = azurerm_bot_channel_web_chat.test.resource_group_name
site_names = ["TestSite"]
site {
name = "TestSite"
}
}
`, r.basic(data))
}
Expand All @@ -141,7 +155,33 @@ resource "azurerm_bot_channel_web_chat" "test" {
bot_name = azurerm_bot_channels_registration.test.name
location = azurerm_bot_channels_registration.test.location
resource_group_name = azurerm_resource_group.test.name
site_names = ["TestSite2", "TestSite3"]
site {
name = "TestSite1"
user_upload_enabled = false
endpoint_parameters_enabled = true
storage_enabled = false
}
site {
name = "TestSite2"
user_upload_enabled = true
endpoint_parameters_enabled = false
storage_enabled = true
}
}
`, BotChannelsRegistrationResource{}.basicConfig(data))
}

func (BotChannelWebChatResource) siteNames(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_bot_channel_web_chat" "test" {
bot_name = azurerm_bot_channels_registration.test.name
location = azurerm_bot_channels_registration.test.location
resource_group_name = azurerm_resource_group.test.name
site_names = ["TestSite"]
}
`, BotChannelsRegistrationResource{}.basicConfig(data))
}
24 changes: 20 additions & 4 deletions website/docs/r/bot_channel_web_chat.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ resource "azurerm_bot_channel_web_chat" "example" {
location = azurerm_bot_channels_registration.example.location
resource_group_name = azurerm_resource_group.example.name
site_names = [
"TestSite",
]
site {
name = "TestSite"
}
}
```

Expand All @@ -49,7 +49,23 @@ The following arguments are supported:

* `bot_name` - (Required) The name of the Bot Resource this channel will be associated with. Changing this forces a new resource to be created.

* `site_names` - (Required) A list of Web Chat Site names.
* `site_names` - (Optional) A list of Web Chat Site names.

~> **NOTE:** `site_names` is deprecated and will be removed in favour of the property `site` in version 4.0 of the AzureRM Provider.

* `site` - (Optional) A site represents a client application that you want to connect to your bot. Multiple `site` blocks may be defined as below

---

A `site` block has the following properties:

* `name` - (Required) The name of the site.

* `user_upload_enabled` - (Optional) Is the user upload enabled for this site? Defaults to `true`.

* `endpoint_parameters_enabled` - (Optional) Is the endpoint parameters enabled for this site?

* `storage_enabled` - (Optional) Is the storage site enabled for detailed logging? Defaults to `true`.

## Attributes Reference

Expand Down

0 comments on commit 6afdc84

Please sign in to comment.