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

dedicated hosts: refactoring to use hashicorp/go-azure-sdk #17616

Merged
merged 8 commits into from
Aug 13, 2022
Merged
10 changes: 6 additions & 4 deletions internal/services/compute/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ 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/go-azure-sdk/resource-manager/compute/2021-11-01/dedicatedhostgroups"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2021-11-01/dedicatedhosts"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2021-11-01/proximityplacementgroups"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2021-11-01/sshpublickeys"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
Expand All @@ -13,8 +15,8 @@ type Client struct {
AvailabilitySetsClient *availabilitysets.AvailabilitySetsClient
CapacityReservationsClient *compute.CapacityReservationsClient
CapacityReservationGroupsClient *compute.CapacityReservationGroupsClient
DedicatedHostsClient *compute.DedicatedHostsClient
DedicatedHostGroupsClient *compute.DedicatedHostGroupsClient
DedicatedHostsClient *dedicatedhosts.DedicatedHostsClient
DedicatedHostGroupsClient *dedicatedhostgroups.DedicatedHostGroupsClient
DisksClient *compute.DisksClient
DiskAccessClient *compute.DiskAccessesClient
DiskEncryptionSetsClient *compute.DiskEncryptionSetsClient
Expand Down Expand Up @@ -49,10 +51,10 @@ func NewClient(o *common.ClientOptions) *Client {
capacityReservationGroupsClient := compute.NewCapacityReservationGroupsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&capacityReservationGroupsClient.Client, o.ResourceManagerAuthorizer)

dedicatedHostsClient := compute.NewDedicatedHostsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
dedicatedHostsClient := dedicatedhosts.NewDedicatedHostsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&dedicatedHostsClient.Client, o.ResourceManagerAuthorizer)

dedicatedHostGroupsClient := compute.NewDedicatedHostGroupsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
dedicatedHostGroupsClient := dedicatedhostgroups.NewDedicatedHostGroupsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&dedicatedHostGroupsClient.Client, o.ResourceManagerAuthorizer)

disksClient := compute.NewDisksClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
Expand Down
27 changes: 17 additions & 10 deletions internal/services/compute/dedicated_host_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"fmt"
"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-sdk/resource-manager/compute/2021-11-01/dedicatedhosts"
"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"
)

func dataSourceDedicatedHost() *pluginsdk.Resource {
Expand Down Expand Up @@ -40,7 +40,7 @@ func dataSourceDedicatedHost() *pluginsdk.Resource {

"location": commonschema.LocationComputed(),

"tags": tags.SchemaDataSource(),
"tags": commonschema.TagsDataSource(),
},
}
}
Expand All @@ -51,22 +51,29 @@ func dataSourceDedicatedHostRead(d *pluginsdk.ResourceData, meta interface{}) er
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id := parse.NewDedicatedHostID(subscriptionId, d.Get("resource_group_name").(string), d.Get("dedicated_host_group_name").(string), d.Get("name").(string))
id := dedicatedhosts.NewHostID(subscriptionId, d.Get("resource_group_name").(string), d.Get("dedicated_host_group_name").(string), d.Get("name").(string))

resp, err := client.Get(ctx, id.ResourceGroup, id.HostGroupName, id.HostName, "")
resp, err := client.Get(ctx, id, dedicatedhosts.DefaultGetOperationOptions())
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("%s was not found", id)
}

return fmt.Errorf("reading %s: %+v", id, err)
}

d.SetId(id.ID())
d.Set("name", id.HostName)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("resource_group_name", id.ResourceGroupName)
d.Set("dedicated_host_group_name", id.HostGroupName)

d.Set("location", location.NormalizeNilable(resp.Location))
if model := resp.Model; model != nil {
d.Set("location", location.Normalize(model.Location))

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

return tags.FlattenAndSet(d, resp.Tags)
return nil
}
33 changes: 17 additions & 16 deletions internal/services/compute/dedicated_host_group_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import (
"regexp"
"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/2021-11-01/dedicatedhostgroups"
"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 dataSourceDedicatedHostGroup() *pluginsdk.Resource {
Expand Down Expand Up @@ -59,10 +59,10 @@ func dataSourceDedicatedHostGroupRead(d *pluginsdk.ResourceData, meta interface{
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id := parse.NewDedicatedHostGroupID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
resp, err := client.Get(ctx, id.ResourceGroup, id.HostGroupName, "")
id := dedicatedhostgroups.NewHostGroupID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
resp, err := client.Get(ctx, id, dedicatedhostgroups.DefaultGetOperationOptions())
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("reading %s: %+v", id, err)
Expand All @@ -71,20 +71,21 @@ func dataSourceDedicatedHostGroupRead(d *pluginsdk.ResourceData, meta interface{
d.SetId(id.ID())

d.Set("name", id.HostGroupName)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("resource_group_name", id.ResourceGroupName)

d.Set("location", location.NormalizeNilable(resp.Location))
d.Set("zones", zones.Flatten(resp.Zones))
if model := resp.Model; model != nil {
d.Set("location", location.Normalize(model.Location))
d.Set("zones", zones.Flatten(model.Zones))

if props := resp.DedicatedHostGroupProperties; props != nil {
d.Set("automatic_placement_enabled", props.SupportAutomaticPlacement)
if props := model.Properties; props != nil {
d.Set("automatic_placement_enabled", props.SupportAutomaticPlacement)
d.Set("platform_fault_domain_count", props.PlatformFaultDomainCount)
}

platformFaultDomainCount := 0
if props.PlatformFaultDomainCount != nil {
platformFaultDomainCount = int(*props.PlatformFaultDomainCount)
if err := tags.FlattenAndSet(d, model.Tags); err != nil {
return err
}
d.Set("platform_fault_domain_count", platformFaultDomainCount)
}

return tags.FlattenAndSet(d, resp.Tags)
return nil
}
106 changes: 51 additions & 55 deletions internal/services/compute/dedicated_host_group_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import (
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
"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/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2021-11-01/dedicatedhostgroups"
"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/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
Expand All @@ -28,7 +27,7 @@ func resourceDedicatedHostGroup() *pluginsdk.Resource {
Delete: resourceDedicatedHostGroupDelete,

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.HostGroupID(id)
_, err := dedicatedhostgroups.ParseHostGroupID(id)
return err
}),

Expand All @@ -47,11 +46,9 @@ func resourceDedicatedHostGroup() *pluginsdk.Resource {
ValidateFunc: validate.DedicatedHostGroupName(),
},

"location": azure.SchemaLocation(),
"resource_group_name": commonschema.ResourceGroupName(),

// There's a bug in the Azure API where this is returned in upper-case
// BUG: https://github.com/Azure/azure-rest-api-specs/issues/8068
"resource_group_name": azure.SchemaResourceGroupNameDiffSuppress(),
"location": commonschema.Location(),

"platform_fault_domain_count": {
Type: pluginsdk.TypeInt,
Expand All @@ -68,7 +65,7 @@ func resourceDedicatedHostGroup() *pluginsdk.Resource {
},
"zone": commonschema.ZoneSingleOptionalForceNew(),

"tags": tags.Schema(),
"tags": commonschema.Tags(),
},
}
}
Expand All @@ -79,48 +76,45 @@ func resourceDedicatedHostGroupCreate(d *pluginsdk.ResourceData, meta interface{
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

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

id := dedicatedhostgroups.NewHostGroupID(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, dedicatedhostgroups.DefaultGetOperationOptions())
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for presence of %s: %+v", id, err)
}
}
if !utils.ResponseWasNotFound(existing.Response) {
if !response.WasNotFound(existing.HttpResponse) {
return tf.ImportAsExistsError("azurerm_dedicated_host_group", id.ID())
}
}

location := azure.NormalizeLocation(d.Get("location").(string))
platformFaultDomainCount := d.Get("platform_fault_domain_count").(int)
t := d.Get("tags").(map[string]interface{})

parameters := compute.DedicatedHostGroup{
Location: utils.String(location),
DedicatedHostGroupProperties: &compute.DedicatedHostGroupProperties{
PlatformFaultDomainCount: utils.Int32(int32(platformFaultDomainCount)),
payload := dedicatedhostgroups.DedicatedHostGroup{
Location: location.Normalize(d.Get("location").(string)),
Properties: &dedicatedhostgroups.DedicatedHostGroupProperties{
PlatformFaultDomainCount: int64(platformFaultDomainCount),
},
Tags: tags.Expand(t),
}

if zone, ok := d.GetOk("zone"); ok {
parameters.Zones = &[]string{
payload.Zones = &[]string{
zone.(string),
}
}

if v, ok := d.GetOk("automatic_placement_enabled"); ok {
parameters.DedicatedHostGroupProperties.SupportAutomaticPlacement = utils.Bool(v.(bool))
payload.Properties.SupportAutomaticPlacement = utils.Bool(v.(bool))
}

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

d.SetId(id.ID())

return resourceDedicatedHostGroupRead(d, meta)
}

Expand All @@ -129,61 +123,63 @@ func resourceDedicatedHostGroupRead(d *pluginsdk.ResourceData, meta interface{})
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.HostGroupID(d.Id())
id, err := dedicatedhostgroups.ParseHostGroupID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.ResourceGroup, id.Name, "")
resp, err := client.Get(ctx, *id, dedicatedhostgroups.DefaultGetOperationOptions())
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] Dedicated Host Group %q does not exist - removing from state", d.Id())
if response.WasNotFound(resp.HttpResponse) {
log.Printf("[INFO] %q was not found - removing from state", *id)
d.SetId("")
return nil
}
return fmt.Errorf("reading Dedicated Host Group %q (: %+v", id.String(), err)
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

d.Set("name", id.Name)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("name", id.HostGroupName)
d.Set("resource_group_name", id.ResourceGroupName)

d.Set("location", location.NormalizeNilable(resp.Location))
if model := resp.Model; model != nil {
d.Set("location", location.Normalize(model.Location))

zone := ""
if resp.Zones != nil && len(*resp.Zones) > 0 {
z := *resp.Zones
zone = z[0]
}
d.Set("zone", zone)
zone := ""
if model.Zones != nil && len(*model.Zones) > 0 {
z := *model.Zones
zone = z[0]
}
d.Set("zone", zone)

if props := resp.DedicatedHostGroupProperties; props != nil {
platformFaultDomainCount := 0
if props.PlatformFaultDomainCount != nil {
platformFaultDomainCount = int(*props.PlatformFaultDomainCount)
if props := model.Properties; props != nil {
d.Set("platform_fault_domain_count", props.PlatformFaultDomainCount)
d.Set("automatic_placement_enabled", props.SupportAutomaticPlacement)
}
d.Set("platform_fault_domain_count", platformFaultDomainCount)

d.Set("automatic_placement_enabled", props.SupportAutomaticPlacement)
if err := tags.FlattenAndSet(d, model.Tags); err != nil {
return err
}
}

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

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

name := d.Get("name").(string)
resourceGroupName := d.Get("resource_group_name").(string)
t := d.Get("tags").(map[string]interface{})
id, err := dedicatedhostgroups.ParseHostGroupID(d.Id())
if err != nil {
return err
}

parameters := compute.DedicatedHostGroupUpdate{
Tags: tags.Expand(t),
payload := dedicatedhostgroups.DedicatedHostGroupUpdate{
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

if _, err := client.Update(ctx, resourceGroupName, name, parameters); err != nil {
return fmt.Errorf("updating Dedicated Host Group %q (Resource Group %q): %+v", name, resourceGroupName, err)
if _, err := client.Update(ctx, *id, payload); err != nil {
return fmt.Errorf("updating %s: %+v", *id, err)
}

return resourceDedicatedHostGroupRead(d, meta)
Expand All @@ -194,13 +190,13 @@ func resourceDedicatedHostGroupDelete(d *pluginsdk.ResourceData, meta interface{
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.HostGroupID(d.Id())
id, err := dedicatedhostgroups.ParseHostGroupID(d.Id())
if err != nil {
return err
}

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

return nil
Expand Down
Loading