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

Bot Service base resource and scoped resource azurerm_bot_service_azure_bot #14462

Merged
merged 7 commits into from
Dec 6, 2021
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 internal/provider/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func SupportedTypedServices() []sdk.TypedServiceRegistration {
appconfiguration.Registration{},
appservice.Registration{},
batch.Registration{},
bot.Registration{},
costmanagement.Registration{},
eventhub.Registration{},
loadbalancer.Registration{},
Expand Down
65 changes: 65 additions & 0 deletions internal/services/bot/bot_service_azure_bot_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package bot

import (
"github.com/Azure/azure-sdk-for-go/services/botservice/mgmt/2021-03-01/botservice"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/bot/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
)

type AzureBotServiceResource struct {
base botBaseResource
}

var _ sdk.ResourceWithUpdate = AzureBotServiceResource{}

var _ sdk.ResourceWithCustomImporter = AzureBotServiceResource{}

func (r AzureBotServiceResource) Arguments() map[string]*pluginsdk.Schema {
schema := map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
}
return r.base.arguments(schema)
}

func (r AzureBotServiceResource) Attributes() map[string]*pluginsdk.Schema {
return r.base.attributes()
}

func (r AzureBotServiceResource) ModelObject() interface{} {
return nil
}

func (r AzureBotServiceResource) ResourceType() string {
return "azurerm_bot_service_azure_bot"
}

func (r AzureBotServiceResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return validate.BotServiceID
}

func (r AzureBotServiceResource) Create() sdk.ResourceFunc {
return r.base.createFunc(r.ResourceType(), string(botservice.KindAzurebot))
}

func (r AzureBotServiceResource) Read() sdk.ResourceFunc {
return r.base.readFunc()
}

func (r AzureBotServiceResource) Delete() sdk.ResourceFunc {
return r.base.deleteFunc()
}

func (r AzureBotServiceResource) Update() sdk.ResourceFunc {
return r.base.updateFunc()
}

func (r AzureBotServiceResource) CustomImporter() sdk.ResourceRunFunc {
return r.base.importerFunc(string(botservice.KindAzurebot))
}
177 changes: 177 additions & 0 deletions internal/services/bot/bot_service_azure_bot_resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package bot_test

import (
"context"
"fmt"
"testing"

"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/services/bot/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type BotServiceAzureBotResource struct {
}

func TestAccBotServiceAzureBot_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_bot_service_azure_bot", "test")
r := BotServiceAzureBotResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("sku").HasValue("F0"),
check.That(data.ResourceName).Key("tags.%").HasValue("1"),
check.That(data.ResourceName).Key("tags.environment").HasValue("test"),
),
},
data.ImportStep(),
})
}

func TestAccBotServiceAzureBot_completeUpdate(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_bot_service_azure_bot", "test")
r := BotServiceAzureBotResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.update(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccBotServiceAzureBot_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_bot_service_azure_bot", "test")
r := BotServiceAzureBotResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
{
Config: r.requiresImport(data),
ExpectError: acceptance.RequiresImportError("azurerm_bot_service_azure_bot"),
},
})
}

func (t BotServiceAzureBotResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.BotServiceID(state.ID)
if err != nil {
return nil, err
}

resp, err := clients.Bot.BotClient.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
return nil, fmt.Errorf("retrieving %s: %v", *id, err)
}

return utils.Bool(resp.Properties != nil), nil
}

func (BotServiceAzureBotResource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

data "azurerm_client_config" "current" {
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%[1]d"
location = "%[2]s"
}

resource "azurerm_bot_service_azure_bot" "test" {
name = "acctestdf%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = "global"
sku = "F0"
microsoft_app_id = data.azurerm_client_config.current.client_id

tags = {
environment = "test"
}
}
`, data.RandomInteger, data.Locations.Primary)
}

func (BotServiceAzureBotResource) update(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

data "azurerm_client_config" "current" {
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%[1]d"
location = "%[2]s"
}

resource "azurerm_application_insights" "test" {
name = "acctestappinsights-%[1]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
application_type = "web"
}

resource "azurerm_application_insights_api_key" "test" {
name = "acctestappinsightsapikey-%[1]d"
application_insights_id = azurerm_application_insights.test.id
read_permissions = ["aggregate", "api", "draft", "extendqueries", "search"]
}

resource "azurerm_bot_service_azure_bot" "test" {
name = "acctestdf%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = "global"
microsoft_app_id = data.azurerm_client_config.current.client_id
sku = "F0"

endpoint = "https://example.com"
developer_app_insights_api_key = azurerm_application_insights_api_key.test.api_key
developer_app_insights_application_id = azurerm_application_insights.test.app_id

tags = {
environment = "test"
}
}
`, data.RandomInteger, data.Locations.Primary)
}

func (BotServiceAzureBotResource) requiresImport(data acceptance.TestData) string {
template := BotServiceAzureBotResource{}.basic(data)
return fmt.Sprintf(`
%s

resource "azurerm_bot_service_azure_bot" "import" {
name = azurerm_bot_service_azure_bot.test.name
resource_group_name = azurerm_bot_service_azure_bot.test.resource_group_name
location = azurerm_bot_service_azure_bot.test.location
sku = azurerm_bot_service_azure_bot.test.sku
microsoft_app_id = azurerm_bot_service_azure_bot.test.microsoft_app_id
}
`, template)
}
Loading