Skip to content

Commit

Permalink
ID Parsers: compute resources (#14934)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbfrahry authored Jan 14, 2022
1 parent dbd7fbd commit ed2a623
Show file tree
Hide file tree
Showing 33 changed files with 299 additions and 431 deletions.
13 changes: 7 additions & 6 deletions internal/services/compute/availability_set_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"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"
Expand Down Expand Up @@ -59,22 +60,22 @@ func dataSourceAvailabilitySet() *pluginsdk.Resource {

func dataSourceAvailabilitySetRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Compute.AvailabilitySetsClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

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

resp, err := client.Get(ctx, resGroup, name)
resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Availability Set %q (Resource Group %q) was not found", name, resGroup)
return fmt.Errorf("%s was not found", id)
}

return fmt.Errorf("making Read request on Availability Set %q (Resource Group %q): %+v", name, resGroup, err)
return fmt.Errorf("making Read request on %s: %+v", id, err)
}

d.SetId(*resp.ID)
d.SetId(id.ID())
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
Expand Down
15 changes: 7 additions & 8 deletions internal/services/compute/availability_set_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,18 @@ func resourceAvailabilitySet() *pluginsdk.Resource {

func resourceAvailabilitySetCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Compute.AvailabilitySetsClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

log.Printf("[INFO] preparing arguments for AzureRM Availability Set creation.")

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

if d.IsNewResource() {
existing, err := client.Get(ctx, resGroup, name)
existing, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing Availability Set %q (Resource Group %q): %s", name, resGroup, err)
return fmt.Errorf("checking for presence of existing %s: %s", id, err)
}
}

Expand All @@ -123,7 +122,7 @@ func resourceAvailabilitySetCreateUpdate(d *pluginsdk.ResourceData, meta interfa
t := d.Get("tags").(map[string]interface{})

availSet := compute.AvailabilitySet{
Name: &name,
Name: &id.Name,
Location: &location,
AvailabilitySetProperties: &compute.AvailabilitySetProperties{
PlatformFaultDomainCount: utils.Int32(int32(faultDomainCount)),
Expand All @@ -145,12 +144,12 @@ func resourceAvailabilitySetCreateUpdate(d *pluginsdk.ResourceData, meta interfa
}
}

resp, err := client.CreateOrUpdate(ctx, resGroup, name, availSet)
_, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, availSet)
if err != nil {
return err
}

d.SetId(*resp.ID)
d.SetId(id.ID())

return resourceAvailabilitySetRead(d, meta)
}
Expand Down
17 changes: 9 additions & 8 deletions internal/services/compute/dedicated_host_group_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"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"
Expand Down Expand Up @@ -52,24 +53,24 @@ func dataSourceDedicatedHostGroup() *pluginsdk.Resource {

func dataSourceDedicatedHostGroupRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Compute.DedicatedHostGroupsClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

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

resp, err := client.Get(ctx, resourceGroupName, name, "")
resp, err := client.Get(ctx, id.ResourceGroup, id.HostGroupName, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Dedicated Host Group %q (Resource Group %q) was not found", name, resourceGroupName)
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("reading Dedicated Host Group %q (Resource Group %q): %+v", name, resourceGroupName, err)
return fmt.Errorf("reading %s: %+v", id, err)
}

d.SetId(*resp.ID)
d.SetId(id.ID())

d.Set("name", name)
d.Set("resource_group_name", resourceGroupName)
d.Set("name", id.HostGroupName)
d.Set("resource_group_name", id.ResourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
Expand Down
17 changes: 9 additions & 8 deletions internal/services/compute/disk_access_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"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/timeouts"
Expand Down Expand Up @@ -35,24 +36,24 @@ func dataSourceDiskAccess() *pluginsdk.Resource {

func dataSourceDiskAccessRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Compute.DiskAccessClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

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

resp, err := client.Get(ctx, resourceGroup, name)
resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Disk Access %q (Resource Group %q) was not found", name, resourceGroup)
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("Error: Error making Read request on Azure Disk Access %q (Resource Group %q): %s", name, resourceGroup, err)
return fmt.Errorf("making Read request on %s: %s", id, err)
}

d.SetId(*resp.ID)
d.SetId(id.ID())

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

return tags.FlattenAndSet(d, resp.Tags)
}
26 changes: 9 additions & 17 deletions internal/services/compute/disk_access_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,20 @@ func resourceDiskAccess() *pluginsdk.Resource {

func resourceDiskAccessCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Compute.DiskAccessClient
subscriptionid := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

log.Printf("[INFO] preparing arguments for Azure ARM Disk Access creation.")

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
id := parse.NewDiskAccessID(subscriptionid, d.Get("resource_group_name").(string), d.Get("name").(string))
t := d.Get("tags").(map[string]interface{})

if d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, name)
existing, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing Disk Access %q (Resource Group %q): %s", name, resourceGroup, err)
return fmt.Errorf("checking for presence of existing %s: %s", id, err)
}
}
if existing.ID != nil && *existing.ID != "" {
Expand All @@ -77,29 +77,21 @@ func resourceDiskAccessCreateUpdate(d *pluginsdk.ResourceData, meta interface{})
location := azure.NormalizeLocation(d.Get("location").(string))

createDiskAccess := compute.DiskAccess{
Name: &name,
Name: &id.Name,
Location: &location,
Tags: tags.Expand(t),
}

future, err := client.CreateOrUpdate(ctx, resourceGroup, name, createDiskAccess)
future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, createDiskAccess)
if err != nil {
return fmt.Errorf("creating/updating Disk Access %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("creating/updating %s: %+v", id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for create/update of Disk Access %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("waiting for create/update of %s: %+v", id, err)
}

read, err := client.Get(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("retrieving Disk Access %q (Resource Group %q): %+v", name, resourceGroup, err)
}
if read.ID == nil {
return fmt.Errorf("reading Disk Access %s (Resource Group %q): ID was nil", name, resourceGroup)
}

d.SetId(*read.ID)
d.SetId(id.ID())

return resourceDiskAccessRead(d, meta)
}
Expand Down
17 changes: 9 additions & 8 deletions internal/services/compute/disk_encryption_set_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"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"
Expand Down Expand Up @@ -44,24 +45,24 @@ func dataSourceDiskEncryptionSet() *pluginsdk.Resource {

func dataSourceDiskEncryptionSetRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Compute.DiskEncryptionSetsClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

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

resp, err := client.Get(ctx, resourceGroup, name)
resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Disk Encryption Set %q (Resource Group %q) was not found", name, resourceGroup)
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("reading Disk Encryption Set %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("reading %s: %+v", id, err)
}

d.SetId(*resp.ID)
d.SetId(id.ID())

d.Set("name", name)
d.Set("resource_group_name", resourceGroup)
d.Set("name", id.Name)
d.Set("resource_group_name", id.ResourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
Expand Down
23 changes: 8 additions & 15 deletions internal/services/compute/disk_encryption_set_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,16 @@ func resourceDiskEncryptionSetCreate(d *pluginsdk.ResourceData, meta interface{}
client := meta.(*clients.Client).Compute.DiskEncryptionSetsClient
keyVaultsClient := meta.(*clients.Client).KeyVault
resourcesClient := meta.(*clients.Client).Resource
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
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.NewDiskEncryptionSetID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

existing, err := client.Get(ctx, resourceGroup, name)
existing, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for present of existing Disk Encryption Set %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("checking for present of existing %s: %+v", id, err)
}
}
if existing.ID != nil && *existing.ID != "" {
Expand Down Expand Up @@ -163,22 +163,15 @@ func resourceDiskEncryptionSetCreate(d *pluginsdk.ResourceData, meta interface{}
Tags: tags.Expand(t),
}

future, err := client.CreateOrUpdate(ctx, resourceGroup, name, params)
future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, params)
if err != nil {
return fmt.Errorf("creating Disk Encryption Set %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("creating %s: %+v", id, err)
}
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for creation of Disk Encryption Set %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("waiting for creation of %s: %+v", id, err)
}

resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("retrieving Disk Encryption Set %q (Resource Group %q): %+v", name, resourceGroup, err)
}
if resp.ID == nil {
return fmt.Errorf("Cannot read Disk Encryption Set %q (Resource Group %q) ID", name, resourceGroup)
}
d.SetId(*resp.ID)
d.SetId(id.ID())

return resourceDiskEncryptionSetRead(d, meta)
}
Expand Down
26 changes: 13 additions & 13 deletions internal/services/compute/image_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-07-01/compute"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"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"
Expand Down Expand Up @@ -123,38 +124,37 @@ func dataSourceImage() *pluginsdk.Resource {

func dataSourceImageRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Compute.ImagesClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

resGroup := d.Get("resource_group_name").(string)

name := d.Get("name").(string)
id := parse.NewImageID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
nameRegex, nameRegexOk := d.GetOk("name_regex")

if name == "" && !nameRegexOk {
if id.Name == "" && !nameRegexOk {
return fmt.Errorf("[ERROR] either name or name_regex is required")
}

var img compute.Image

if !nameRegexOk {
var err error
if img, err = client.Get(ctx, resGroup, name, ""); err != nil {
if img, err = client.Get(ctx, id.ResourceGroup, id.Name, ""); err != nil {
if utils.ResponseWasNotFound(img.Response) {
return fmt.Errorf("image %q was not found in resource group %q", name, resGroup)
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("[ERROR] Error making Read request on Azure Image %q (resource group %q): %+v", name, resGroup, err)
return fmt.Errorf("making Read request on %s: %+v", id, err)
}
} else {
r := regexp.MustCompile(nameRegex.(string))

list := make([]compute.Image, 0)
resp, err := client.ListByResourceGroupComplete(ctx, resGroup)
resp, err := client.ListByResourceGroupComplete(ctx, id.ResourceGroup)
if err != nil {
if utils.ResponseWasNotFound(resp.Response().Response) {
return fmt.Errorf("No Images were found for Resource Group %q", resGroup)
return fmt.Errorf("no Images were found for Resource Group %q", id.ResourceGroup)
}
return fmt.Errorf("[ERROR] Error getting list of images (resource group %q): %+v", resGroup, err)
return fmt.Errorf("getting list of images (resource group %q): %+v", id.ResourceGroup, err)
}

for resp.NotDone() {
Expand All @@ -170,7 +170,7 @@ func dataSourceImageRead(d *pluginsdk.ResourceData, meta interface{}) error {
}

if 1 > len(list) {
return fmt.Errorf("No Images were found for Resource Group %q", resGroup)
return fmt.Errorf("no Images were found for Resource Group %q", id.ResourceGroup)
}

if len(list) > 1 {
Expand All @@ -185,9 +185,9 @@ func dataSourceImageRead(d *pluginsdk.ResourceData, meta interface{}) error {
img = list[0]
}

d.SetId(*img.ID)
d.SetId(id.ID())
d.Set("name", img.Name)
d.Set("resource_group_name", resGroup)
d.Set("resource_group_name", id.ResourceGroup)
if location := img.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
Expand Down
Loading

0 comments on commit ed2a623

Please sign in to comment.