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

healthbot: refactoring to use github.com/hashicorp/go-azure-sdk #19433

Merged
merged 5 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
106 changes: 47 additions & 59 deletions internal/services/bot/bot_healthbot_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ import (
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/healthbot/mgmt/2020-12-08/healthbot"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/healthbot/2020-12-08/healthbots"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"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/services/bot/validate"
"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 resourceHealthbotService() *pluginsdk.Resource {
Expand All @@ -34,7 +33,7 @@ func resourceHealthbotService() *pluginsdk.Resource {
},

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.BotHealthbotID(id)
_, err := healthbots.ParseHealthBotID(id)
return err
}),

Expand All @@ -51,147 +50,136 @@ func resourceHealthbotService() *pluginsdk.Resource {
"location": commonschema.Location(),

"sku_name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
string(healthbot.F0),
string(healthbot.S1),
}, false),
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(healthbots.PossibleValuesForSkuName(), false),
},

"bot_management_portal_url": {
Type: pluginsdk.TypeString,
Computed: true,
},

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

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

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

id := parse.NewBotHealthbotID(subscriptionId, resourceGroup, name)
id := healthbots.NewHealthBotID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

if d.IsNewResource() {
existing, err := client.Get(ctx, id.ResourceGroup, id.HealthBotName)
existing, err := client.BotsGet(ctx, id)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for existing %s: %+v", id, err)
}
}
if !utils.ResponseWasNotFound(existing.Response) {
if !response.WasNotFound(existing.HttpResponse) {
return tf.ImportAsExistsError("azurerm_healthbot", id.ID())
}
}

parameters := healthbot.HealthBot{
Location: utils.String(location.Normalize(d.Get("location").(string))),
Sku: &healthbot.Sku{
Name: healthbot.SkuName(d.Get("sku_name").(string)),
payload := healthbots.HealthBot{
Location: location.Normalize(d.Get("location").(string)),
Sku: healthbots.Sku{
Name: healthbots.SkuName(d.Get("sku_name").(string)),
},
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

future, err := client.Create(ctx, resourceGroup, name, parameters)
if err != nil {
if err := client.BotsCreateThenPoll(ctx, id, payload); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for creation of %s: %+v", id, err)
}

d.SetId(id.ID())

return resourceHealthbotServiceRead(d, meta)
}

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

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

resp, err := client.Get(ctx, id.ResourceGroup, id.HealthBotName)
resp, err := client.BotsGet(ctx, *id)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] healthbot %q does not exist - removing from state", d.Id())
if response.WasNotFound(resp.HttpResponse) {
log.Printf("[INFO] %s does not exist - removing from state", *id)
d.SetId("")
return nil
}
return fmt.Errorf("retrieving %s: %+v", id, err)
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

d.Set("name", id.HealthBotName)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("location", location.NormalizeNilable(resp.Location))
d.Set("name", id.BotName)
d.Set("resource_group_name", id.ResourceGroupName)

if sku := resp.Sku; sku != nil {
d.Set("sku_name", sku.Name)
}
if model := resp.Model; model != nil {
d.Set("location", location.Normalize(model.Location))
d.Set("sku_name", string(model.Sku.Name))
if props := model.Properties; props != nil {
d.Set("bot_management_portal_url", props.BotManagementPortalLink)
}

if props := resp.Properties; props != nil {
d.Set("bot_management_portal_url", props.BotManagementPortalLink)
if err := tags.FlattenAndSet(d, model.Tags); err != nil {
return fmt.Errorf("setting `tags`: %+v", err)
}
}
return tags.FlattenAndSet(d, resp.Tags)

return nil
}

func resourceHealthbotServiceUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Bot.HealthbotClient
client := meta.(*clients.Client).Bot.HealthBotClient.Healthbots
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

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

parameters := healthbot.UpdateParameters{}
payload := healthbots.HealthBotUpdateParameters{}
if d.HasChange("sku_name") {
parameters.Sku = &healthbot.Sku{
Name: healthbot.SkuName(d.Get("sku_name").(string)),
payload.Sku = &healthbots.Sku{
Name: healthbots.SkuName(d.Get("sku_name").(string)),
}
}

if d.HasChange("tags") {
parameters.Tags = tags.Expand(d.Get("tags").(map[string]interface{}))
payload.Tags = tags.Expand(d.Get("tags").(map[string]interface{}))
}

if _, err := client.Update(ctx, id.ResourceGroup, id.HealthBotName, parameters); err != nil {
if _, err := client.BotsUpdate(ctx, *id, payload); err != nil {
return fmt.Errorf("updating %s: %+v", id, err)
}
return resourceHealthbotServiceRead(d, meta)
}

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

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

future, err := client.Delete(ctx, id.ResourceGroup, id.HealthBotName)
if err != nil {
if err := client.BotsDeleteThenPoll(ctx, *id); err != nil {
return fmt.Errorf("deleting %s: %+v", id, err)
}

if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for deletion of %s: %+v", id, err)
}
return nil
}
10 changes: 5 additions & 5 deletions internal/services/bot/bot_healthbot_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"fmt"
"testing"

"github.com/hashicorp/go-azure-sdk/resource-manager/healthbot/2020-12-08/healthbots"
"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"
)
Expand Down Expand Up @@ -86,17 +86,17 @@ func TestAccBotHealthbot_update(t *testing.T) {
}

func (r HealthbotResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.BotHealthbotID(state.ID)
id, err := healthbots.ParseHealthBotID(state.ID)
if err != nil {
return nil, err
}

resp, err := client.Bot.HealthbotClient.Get(ctx, id.ResourceGroup, id.HealthBotName)
resp, err := client.Bot.HealthBotClient.Healthbots.BotsGet(ctx, *id)
if err != nil {
return nil, fmt.Errorf("retrieving Healthbot service %q (resource group: %q): %+v", id.HealthBotName, id.ResourceGroup, err)
return nil, fmt.Errorf("retrieving %s: %+v", *id, err)
}

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

func (r HealthbotResource) template(data acceptance.TestData) string {
Expand Down
10 changes: 8 additions & 2 deletions internal/services/bot/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package client
import (
"github.com/Azure/azure-sdk-for-go/services/healthbot/mgmt/2020-12-08/healthbot"
"github.com/Azure/azure-sdk-for-go/services/preview/botservice/mgmt/2021-05-01-preview/botservice"
"github.com/Azure/go-autorest/autorest"
healthbot_2020_12_08 "github.com/hashicorp/go-azure-sdk/resource-manager/healthbot/2020-12-08"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
BotClient *botservice.BotsClient
ConnectionClient *botservice.BotConnectionClient
ChannelClient *botservice.ChannelsClient
HealthbotClient *healthbot.BotsClient
HealthBotClient *healthbot_2020_12_08.Client
}

func NewClient(o *common.ClientOptions) *Client {
Expand All @@ -26,10 +28,14 @@ func NewClient(o *common.ClientOptions) *Client {
healthBotClient := healthbot.NewBotsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&healthBotClient.Client, o.ResourceManagerAuthorizer)

healthBotsClient := healthbot_2020_12_08.NewClientWithBaseURI(o.ResourceManagerEndpoint, func(c *autorest.Client) {
c.Authorizer = o.ResourceManagerAuthorizer
})

return &Client{
BotClient: &botClient,
ChannelClient: &channelClient,
ConnectionClient: &connectionClient,
HealthbotClient: &healthBotClient,
HealthBotClient: &healthBotsClient,
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading