Skip to content

Commit

Permalink
Merge pull request #20580 from hashicorp/refactor/computereservations…
Browse files Browse the repository at this point in the history
…-to-go-azure-sdk

refactor: `compute` / `capacityreservations` and `capacityreservationgroups` to `hashicorp/go-azure-sdk`
  • Loading branch information
tombuildsstuff authored Feb 21, 2023
2 parents 6be7c2a + f02fdd3 commit 8676436
Show file tree
Hide file tree
Showing 59 changed files with 2,148 additions and 729 deletions.
84 changes: 42 additions & 42 deletions internal/services/compute/capacity_reservation_group_resource.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package compute

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

"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-helpers/resourcemanager/zones"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-01/capacityreservationgroups"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/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/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
"github.com/tombuildsstuff/kermit/sdk/compute/2022-08-01/compute"
)

func resourceCapacityReservationGroup() *pluginsdk.Resource {
Expand All @@ -36,7 +35,7 @@ func resourceCapacityReservationGroup() *pluginsdk.Resource {
},

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.CapacityReservationGroupID(id)
_, err := capacityreservationgroups.ParseCapacityReservationGroupID(id)
return err
}),

Expand All @@ -54,7 +53,7 @@ func resourceCapacityReservationGroup() *pluginsdk.Resource {

"zones": commonschema.ZonesMultipleOptionalForceNew(),

"tags": tags.Schema(),
"tags": commonschema.Tags(),
},
}
}
Expand All @@ -65,22 +64,19 @@ func resourceCapacityReservationGroupCreate(d *pluginsdk.ResourceData, meta inte
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.NewCapacityReservationGroupID(subscriptionId, resourceGroup, name)
existing, err := client.Get(ctx, id.ResourceGroup, id.Name, "")
id := capacityreservationgroups.NewCapacityReservationGroupID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
existing, err := client.Get(ctx, id, capacityreservationgroups.DefaultGetOperationOptions())
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_capacity_reservation_group", id.ID())
}

parameters := compute.CapacityReservationGroup{
Name: utils.String(name),
Location: utils.String(location.Normalize(d.Get("location").(string))),
parameters := capacityreservationgroups.CapacityReservationGroup{
Location: location.Normalize(d.Get("location").(string)),
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

Expand All @@ -89,7 +85,7 @@ func resourceCapacityReservationGroupCreate(d *pluginsdk.ResourceData, meta inte
parameters.Zones = &zones
}

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

Expand All @@ -102,47 +98,55 @@ func resourceCapacityReservationGroupRead(d *pluginsdk.ResourceData, meta interf
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

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

resp, err := client.Get(ctx, id.ResourceGroup, id.Name, "")
resp, err := client.Get(ctx, *id, capacityreservationgroups.DefaultGetOperationOptions())
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
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", resp.Name)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("location", location.NormalizeNilable(resp.Location))
d.Set("zones", utils.FlattenStringSlice(resp.Zones))
return tags.FlattenAndSet(d, resp.Tags)
d.Set("name", id.CapacityReservationGroupName)
d.Set("resource_group_name", id.ResourceGroupName)

if model := resp.Model; model != nil {
d.Set("location", location.Normalize(model.Location))
d.Set("zones", utils.FlattenStringSlice(model.Zones))
if err := tags.FlattenAndSet(d, model.Tags); err != nil {
return fmt.Errorf("setting `tags`: %+v", err)
}
}

return nil
}

func resourceCapacityReservationGroupUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Compute.CapacityReservationGroupsClient
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

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

parameters := compute.CapacityReservationGroupUpdate{}
parameters := capacityreservationgroups.CapacityReservationGroupUpdate{}

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

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

return resourceCapacityReservationGroupRead(d, meta)
}

Expand All @@ -151,18 +155,24 @@ func resourceCapacityReservationGroupDelete(d *pluginsdk.ResourceData, meta inte
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

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

// It takes several seconds to sync the cache of reservations list in Capacity Reservation Group. Delete operation requires the list to be empty, and fails before the cache sync is completed.
// Retry the delete operation after a minute as a workaround. Issue is tracked by: https://github.com/Azure/azure-rest-api-specs/issues/18767
if _, err := client.Delete(ctx, id.ResourceGroup, id.Name); err != nil {
if _, err := client.Delete(ctx, *id); err != nil {
stateConf := &pluginsdk.StateChangeConf{
Pending: []string{"Deleting"},
Target: []string{"Deleted"},
Refresh: capacityReservationGroupDeleteRefreshFunc(ctx, client, id.ResourceGroup, id.Name),
Pending: []string{"Deleting"},
Target: []string{"Deleted"},
Refresh: func() (interface{}, string, error) {
res, err := client.Delete(ctx, *id)
if err != nil {
return res, "Deleting", nil
}
return res, "Deleted", nil
},
MinTimeout: 15 * time.Second,
Timeout: 1 * time.Minute,
}
Expand All @@ -174,13 +184,3 @@ func resourceCapacityReservationGroupDelete(d *pluginsdk.ResourceData, meta inte

return nil
}

func capacityReservationGroupDeleteRefreshFunc(ctx context.Context, client *compute.CapacityReservationGroupsClient, resourceGroup string, name string) pluginsdk.StateRefreshFunc {
return func() (interface{}, string, error) {
res, err := client.Delete(ctx, resourceGroup, name)
if err != nil {
return res, "Deleting", nil
}
return res, "Deleted", nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"fmt"
"testing"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-01/capacityreservationgroups"
"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/compute/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
Expand Down Expand Up @@ -79,20 +80,20 @@ func TestAccCapacityReservationGroup_tags(t *testing.T) {
}

func (r CapacityReservationGroupResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.CapacityReservationGroupID(state.ID)
id, err := capacityreservationgroups.ParseCapacityReservationGroupID(state.ID)
if err != nil {
return nil, err
}

resp, err := client.Compute.CapacityReservationGroupsClient.Get(ctx, id.ResourceGroup, id.Name, "")
resp, err := client.Compute.CapacityReservationGroupsClient.Get(ctx, *id, capacityreservationgroups.DefaultGetOperationOptions())
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
return utils.Bool(false), nil
}
return nil, fmt.Errorf("retrieving %s: %+v", id, err)
}

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

func (r CapacityReservationGroupResource) template(data acceptance.TestData) string {
Expand Down
Loading

0 comments on commit 8676436

Please sign in to comment.