Skip to content

Commit

Permalink
Merge pull request #17608 from hashicorp/refactor/availability-sets-t…
Browse files Browse the repository at this point in the history
…o-go-azure-sdk

refactor: updating `compute/availability-sets` to `hashicorp/go-azure-sdk`
  • Loading branch information
tombuildsstuff authored Jul 12, 2022
2 parents bb97837 + 67a0bc1 commit b9fed9a
Show file tree
Hide file tree
Showing 33 changed files with 1,312 additions and 373 deletions.
62 changes: 31 additions & 31 deletions internal/services/compute/availability_set_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ package compute

import (
"fmt"
"strconv"
"strings"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2021-11-01/availabilitysets"
"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/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 dataSourceAvailabilitySet() *pluginsdk.Resource {
Expand All @@ -26,26 +25,23 @@ func dataSourceAvailabilitySet() *pluginsdk.Resource {
},

Schema: map[string]*pluginsdk.Schema{
"resource_group_name": commonschema.ResourceGroupNameForDataSource(),

"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"location": {
Type: pluginsdk.TypeString,
Computed: true,
},
"resource_group_name": commonschema.ResourceGroupNameForDataSource(),

"location": commonschema.LocationComputed(),

"platform_update_domain_count": {
Type: pluginsdk.TypeString,
Type: pluginsdk.TypeInt,
Computed: true,
},

"platform_fault_domain_count": {
Type: pluginsdk.TypeString,
Type: pluginsdk.TypeInt,
Computed: true,
},

Expand All @@ -54,7 +50,7 @@ func dataSourceAvailabilitySet() *pluginsdk.Resource {
Computed: true,
},

"tags": tags.SchemaDataSource(),
"tags": commonschema.TagsDataSource(),
},
}
}
Expand All @@ -65,31 +61,35 @@ func dataSourceAvailabilitySetRead(d *pluginsdk.ResourceData, meta interface{})
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id := parse.NewAvailabilitySetID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
id := availabilitysets.NewAvailabilitySetID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
resp, err := client.Get(ctx, id)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("%s was not found", id)
}

return fmt.Errorf("making Read request on %s: %+v", id, err)
return fmt.Errorf("retrieving %s: %+v", id, err)
}

d.SetId(id.ID())
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
if resp.Sku != nil && resp.Sku.Name != nil {
d.Set("managed", strings.EqualFold(*resp.Sku.Name, "Aligned"))
}
if props := resp.AvailabilitySetProperties; props != nil {
if v := props.PlatformUpdateDomainCount; v != nil {
d.Set("platform_update_domain_count", strconv.Itoa(int(*v)))

if model := resp.Model; model != nil {
d.Set("location", location.Normalize(model.Location))
managed := false
if model.Sku != nil && model.Sku.Name != nil {
managed = strings.EqualFold(*model.Sku.Name, "Aligned")
}
if v := props.PlatformFaultDomainCount; v != nil {
d.Set("platform_fault_domain_count", strconv.Itoa(int(*v)))
d.Set("managed", managed)

if props := model.Properties; props != nil {
d.Set("platform_fault_domain_count", props.PlatformFaultDomainCount)
d.Set("platform_update_domain_count", props.PlatformUpdateDomainCount)
}

if err := tags.FlattenAndSet(d, model.Tags); err != nil {
return err
}
}
return tags.FlattenAndSet(d, resp.Tags)

return nil
}
103 changes: 55 additions & 48 deletions internal/services/compute/availability_set_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"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/compute/2021-11-01/availabilitysets"
"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/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/suppress"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
Expand All @@ -27,7 +28,7 @@ func resourceAvailabilitySet() *pluginsdk.Resource {
Update: resourceAvailabilitySetCreateUpdate,
Delete: resourceAvailabilitySetDelete,
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.AvailabilitySetID(id)
_, err := availabilitysets.ParseAvailabilitySetID(id)
return err
}),

Expand All @@ -49,9 +50,9 @@ func resourceAvailabilitySet() *pluginsdk.Resource {
),
},

"resource_group_name": azure.SchemaResourceGroupName(),
"resource_group_name": commonschema.ResourceGroupName(),

"location": azure.SchemaLocation(),
"location": commonschema.Location(),

"platform_update_domain_count": {
Type: pluginsdk.TypeInt,
Expand Down Expand Up @@ -88,7 +89,7 @@ func resourceAvailabilitySet() *pluginsdk.Resource {
DiffSuppressFunc: suppress.CaseDifference,
},

"tags": tags.Schema(),
"tags": commonschema.Tags(),
},
}
}
Expand All @@ -99,58 +100,53 @@ func resourceAvailabilitySetCreateUpdate(d *pluginsdk.ResourceData, meta interfa
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

log.Printf("[INFO] preparing arguments for AzureRM Availability Set creation.")
id := parse.NewAvailabilitySetID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

id := availabilitysets.NewAvailabilitySetID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
if d.IsNewResource() {
existing, err := client.Get(ctx, id.ResourceGroup, id.Name)
existing, err := client.Get(ctx, id)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for presence of existing %s: %s", id, err)
}
}

if !utils.ResponseWasNotFound(existing.Response) {
if !response.WasNotFound(existing.HttpResponse) {
return tf.ImportAsExistsError("azurerm_availability_set", id.ID())
}
}

location := azure.NormalizeLocation(d.Get("location").(string))
updateDomainCount := d.Get("platform_update_domain_count").(int)
faultDomainCount := d.Get("platform_fault_domain_count").(int)
managed := d.Get("managed").(bool)
t := d.Get("tags").(map[string]interface{})

availSet := compute.AvailabilitySet{
Name: &id.Name,
Location: &location,
AvailabilitySetProperties: &compute.AvailabilitySetProperties{
PlatformFaultDomainCount: utils.Int32(int32(faultDomainCount)),
PlatformUpdateDomainCount: utils.Int32(int32(updateDomainCount)),
payload := availabilitysets.AvailabilitySet{
Location: location.Normalize(d.Get("location").(string)),
Properties: &availabilitysets.AvailabilitySetProperties{
PlatformFaultDomainCount: utils.Int64(int64(faultDomainCount)),
PlatformUpdateDomainCount: utils.Int64(int64(updateDomainCount)),
},
Tags: tags.Expand(t),
}

if v, ok := d.GetOk("proximity_placement_group_id"); ok {
availSet.AvailabilitySetProperties.ProximityPlacementGroup = &compute.SubResource{
ID: utils.String(v.(string)),
payload.Properties.ProximityPlacementGroup = &availabilitysets.SubResource{
Id: utils.String(v.(string)),
}
}

if managed {
n := "Aligned"
availSet.Sku = &compute.Sku{
payload.Sku = &availabilitysets.Sku{
Name: &n,
}
}

_, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, availSet)
_, err := client.CreateOrUpdate(ctx, id, payload)
if err != nil {
return err
return fmt.Errorf("creating/updating %s: %+v", id, err)
}

d.SetId(id.ID())

return resourceAvailabilitySetRead(d, meta)
}

Expand All @@ -159,51 +155,62 @@ func resourceAvailabilitySetRead(d *pluginsdk.ResourceData, meta interface{}) er
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.AvailabilitySetID(d.Id())
id, err := availabilitysets.ParseAvailabilitySetID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
resp, err := client.Get(ctx, *id)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
log.Printf("[DEBUG] %s was not found - removing from state!", *id)
d.SetId("")
return nil
}
return fmt.Errorf("making Read request on Azure Availability Set %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
return fmt.Errorf("retrieving %s: %+v", id, err)
}

d.Set("name", resp.Name)
d.Set("resource_group_name", id.ResourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
if resp.Sku != nil && resp.Sku.Name != nil {
d.Set("managed", strings.EqualFold(*resp.Sku.Name, "Aligned"))
}
d.Set("name", id.AvailabilitySetName)
d.Set("resource_group_name", id.ResourceGroupName)

if model := resp.Model; model != nil {
d.Set("location", location.Normalize(model.Location))
managed := false
if model.Sku != nil && model.Sku.Name != nil {
managed = strings.EqualFold(*model.Sku.Name, "Aligned")
}
d.Set("managed", managed)

if props := resp.AvailabilitySetProperties; props != nil {
d.Set("platform_update_domain_count", props.PlatformUpdateDomainCount)
d.Set("platform_fault_domain_count", props.PlatformFaultDomainCount)
if props := model.Properties; props != nil {
d.Set("platform_update_domain_count", props.PlatformUpdateDomainCount)
d.Set("platform_fault_domain_count", props.PlatformFaultDomainCount)

if proximityPlacementGroup := props.ProximityPlacementGroup; proximityPlacementGroup != nil {
d.Set("proximity_placement_group_id", proximityPlacementGroup.ID)
if proximityPlacementGroup := props.ProximityPlacementGroup; proximityPlacementGroup != nil {
d.Set("proximity_placement_group_id", proximityPlacementGroup.Id)
}
}

if err := tags.FlattenAndSet(d, model.Tags); err != nil {
return err
}
}

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

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

id, err := parse.AvailabilitySetID(d.Id())
id, err := availabilitysets.ParseAvailabilitySetID(d.Id())
if err != nil {
return err
}

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

return nil
}
18 changes: 9 additions & 9 deletions internal/services/compute/availability_set_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"testing"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2021-11-01/availabilitysets"
"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 @@ -141,29 +141,29 @@ func TestAccAvailabilitySet_unmanaged(t *testing.T) {
}

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

resp, err := clients.Compute.AvailabilitySetsClient.Get(ctx, id.ResourceGroup, id.Name)
resp, err := clients.Compute.AvailabilitySetsClient.Get(ctx, *id)
if err != nil {
return nil, fmt.Errorf("retrieving Compute Availability Set %q", id.String())
return nil, fmt.Errorf("retrieving %s: %+v", *id, err)
}

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

func (AvailabilitySetResource) Destroy(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.AvailabilitySetID(state.ID)
id, err := availabilitysets.ParseAvailabilitySetID(state.ID)
if err != nil {
return nil, err
}

resp, err := client.Compute.AvailabilitySetsClient.Delete(ctx, id.ResourceGroup, id.Name)
resp, err := client.Compute.AvailabilitySetsClient.Delete(ctx, *id)
if err != nil {
if !response.WasNotFound(resp.Response) {
return nil, fmt.Errorf("deleting on availSetClient: %+v", err)
if !response.WasNotFound(resp.HttpResponse) {
return nil, fmt.Errorf("deleting %s: %+v", *id, err)
}
}

Expand Down
5 changes: 3 additions & 2 deletions internal/services/compute/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package client
import (
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
"github.com/Azure/azure-sdk-for-go/services/marketplaceordering/mgmt/2015-06-01/marketplaceordering"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2021-11-01/availabilitysets"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
AvailabilitySetsClient *compute.AvailabilitySetsClient
AvailabilitySetsClient *availabilitysets.AvailabilitySetsClient
CapacityReservationsClient *compute.CapacityReservationsClient
CapacityReservationGroupsClient *compute.CapacityReservationGroupsClient
DedicatedHostsClient *compute.DedicatedHostsClient
Expand Down Expand Up @@ -37,7 +38,7 @@ type Client struct {
}

func NewClient(o *common.ClientOptions) *Client {
availabilitySetsClient := compute.NewAvailabilitySetsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
availabilitySetsClient := availabilitysets.NewAvailabilitySetsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&availabilitySetsClient.Client, o.ResourceManagerAuthorizer)

capacityReservationsClient := compute.NewCapacityReservationsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
Expand Down
Loading

0 comments on commit b9fed9a

Please sign in to comment.