Skip to content

Commit

Permalink
Id Parsers: Various Resources (#14974)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbfrahry authored Jan 15, 2022
1 parent 49bd9f9 commit 4bb2252
Show file tree
Hide file tree
Showing 43 changed files with 375 additions and 577 deletions.
21 changes: 7 additions & 14 deletions internal/services/compute/dedicated_host_group_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@ func resourceDedicatedHostGroup() *pluginsdk.Resource {

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

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

if d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroupName, 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 Dedicated Host Group %q (Resource Group %q): %+v", name, resourceGroupName, err)
return fmt.Errorf("checking for presence of %s: %+v", id, err)
}
}
if existing.ID != nil && *existing.ID != "" {
Expand All @@ -113,18 +113,11 @@ func resourceDedicatedHostGroupCreate(d *pluginsdk.ResourceData, meta interface{
parameters.DedicatedHostGroupProperties.SupportAutomaticPlacement = utils.Bool(v.(bool))
}

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

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

return resourceDedicatedHostGroupRead(d, meta)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,21 @@ func resourceDevTestGlobalVMShutdownScheduleCreateUpdate(d *pluginsdk.ResourceDa
defer cancel()

vmID := d.Get("virtual_machine_id").(string)
id, err := computeParse.VirtualMachineID(vmID)
vmId, err := computeParse.VirtualMachineID(vmID)
if err != nil {
return err
}

// Can't find any official documentation on this, but the API returns a 400 for any other name.
// The best example I could find is here: https://social.msdn.microsoft.com/Forums/en-US/25a02403-dba9-4bcb-bdcc-1f4afcba5b65/powershell-script-to-autoshutdown-azure-virtual-machine?forum=WAVirtualMachinesforWindows
name := "shutdown-computevm-" + id.Name
name := "shutdown-computevm-" + vmId.Name
id := parse.NewScheduleID(vmId.SubscriptionId, vmId.ResourceGroup, name)

if d.IsNewResource() {
existing, err := client.Get(ctx, id.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 Schedule %q (Resource Group %q): %s", name, id.ResourceGroup, err)
return fmt.Errorf("checking for presence of existing %s: %s", id, err)
}
}

Expand Down Expand Up @@ -165,16 +166,7 @@ func resourceDevTestGlobalVMShutdownScheduleCreateUpdate(d *pluginsdk.ResourceDa
return err
}

read, err := client.Get(ctx, id.ResourceGroup, name, "")
if err != nil {
return err
}

if read.ID == nil {
return fmt.Errorf("Cannot read Dev Test Global Schedule %s (resource group %s) ID", name, id.ResourceGroup)
}

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

return resourceDevTestGlobalVMShutdownScheduleRead(d, meta)
}
Expand Down
15 changes: 8 additions & 7 deletions internal/services/devtestlabs/dev_test_lab_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/devtestlabs/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/devtestlabs/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand Down Expand Up @@ -74,25 +75,25 @@ func dataSourceDevTestLab() *pluginsdk.Resource {

func dataSourceDevTestLabRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).DevTestLabs.LabsClient
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.NewDevTestLabID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

read, err := client.Get(ctx, resourceGroup, name, "")
read, err := client.Get(ctx, id.ResourceGroup, id.LabName, "")
if err != nil {
if utils.ResponseWasNotFound(read.Response) {
return fmt.Errorf("DevTest Lab %q was not found in Resource Group %q", name, resourceGroup)
return fmt.Errorf("%s was not found", id)
}

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

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

d.Set("name", read.Name)
d.Set("resource_group_name", resourceGroup)
d.Set("resource_group_name", id.ResourceGroup)
if location := read.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/devtestlabs/mgmt/2018-09-15/dtl"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/devtestlabs/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/devtestlabs/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
Expand Down Expand Up @@ -97,26 +98,25 @@ func dataSourceArmDevTestVirtualNetwork() *pluginsdk.Resource {

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

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

resp, err := client.Get(ctx, resGroup, labName, name, "")
resp, err := client.Get(ctx, id.ResourceGroup, id.LabName, id.VirtualNetworkName, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Virtual Network %q in Dev Test Lab %q (Resource Group %q) was not found", name, labName, resGroup)
return fmt.Errorf("%s was not found", id)
}

return fmt.Errorf("making Read request on Virtual Network %q in Dev Test Lab %q (Resource Group %q): %+v", name, labName, resGroup, err)
return fmt.Errorf("making Read request on %s: %+v", id, err)
}

if resp.ID == nil || *resp.ID == "" {
return fmt.Errorf("API returns a nil/empty id on Virtual Network %q in Dev Test Lab %q (Resource Group %q): %+v", name, labName, resGroup, err)
return fmt.Errorf("API returns a nil/empty id on %s: %+v", id, err)
}
d.SetId(*resp.ID)
d.SetId(id.ID())

if props := resp.VirtualNetworkProperties; props != nil {
if as := props.AllowedSubnets; as != nil {
Expand Down
26 changes: 8 additions & 18 deletions internal/services/hpccache/hpc_cache_blob_target_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,18 @@ func resourceHPCCacheBlobTarget() *pluginsdk.Resource {

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

log.Printf("[INFO] preparing arguments for Azure HPC Cache Blob Target creation.")
name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
cache := d.Get("cache_name").(string)
id := parse.NewStorageTargetID(subscriptionId, d.Get("resource_group_name").(string), d.Get("cache_name").(string), d.Get("name").(string))

if d.IsNewResource() {
resp, err := client.Get(ctx, resourceGroup, cache, name)
resp, err := client.Get(ctx, id.ResourceGroup, id.CacheName, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("checking for existing HPC Cache Blob Target %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("checking for existing %s: %+v", id, err)
}
}

Expand Down Expand Up @@ -121,25 +120,16 @@ func resourceHPCCacheBlobTargetCreateOrUpdate(d *pluginsdk.ResourceData, meta in
},
}

future, err := client.CreateOrUpdate(ctx, resourceGroup, cache, name, param)
future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.CacheName, id.Name, param)
if err != nil {
return fmt.Errorf("creating HPC Cache Blob Target %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 HPC Cache Blob Target %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("waiting for creation of %s: %+v", id, err)
}

read, err := client.Get(ctx, resourceGroup, cache, name)
if err != nil {
return fmt.Errorf("retrieving HPC Cache Blob Target %q (Resource Group %q): %+v", name, resourceGroup, err)
}

if read.ID == nil {
return fmt.Errorf("retrieving HPC Cache Blob Target %q (Resource Group %q): `id` was nil", name, resourceGroup)
}

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

return resourceHPCCacheBlobTargetRead(d, meta)
}
Expand Down
26 changes: 8 additions & 18 deletions internal/services/hpccache/hpc_cache_nfs_target_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,18 @@ func resourceHPCCacheNFSTarget() *pluginsdk.Resource {

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

log.Printf("[INFO] preparing arguments for Azure HPC Cache NFS Target creation.")
name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
cache := d.Get("cache_name").(string)
id := parse.NewStorageTargetID(subscriptionId, d.Get("resource_group_name").(string), d.Get("cache_name").(string), d.Get("name").(string))

if d.IsNewResource() {
resp, err := client.Get(ctx, resourceGroup, cache, name)
resp, err := client.Get(ctx, id.ResourceGroup, id.CacheName, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("checking for existing HPC Cache NFS Target %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("checking for existing %s: %+v", id, err)
}
}

Expand All @@ -149,25 +148,16 @@ func resourceHPCCacheNFSTargetCreateOrUpdate(d *pluginsdk.ResourceData, meta int
},
}

future, err := client.CreateOrUpdate(ctx, resourceGroup, cache, name, param)
future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.CacheName, id.Name, param)
if err != nil {
return fmt.Errorf("creating HPC Cache NFS Target %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 HPC Cache NFS Target %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("waiting for creation of %s: %+v", id, err)
}

read, err := client.Get(ctx, resourceGroup, cache, name)
if err != nil {
return fmt.Errorf("retrieving HPC Cache NFS Target %q (Resource Group %q): %+v", name, resourceGroup, err)
}

if read.ID == nil {
return fmt.Errorf("retrieving HPC Cache NFS Target %q (Resource Group %q): `id` was nil", name, resourceGroup)
}

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

return resourceHPCCacheNFSTargetRead(d, meta)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,30 +109,30 @@ func resourceIoTTimeSeriesInsightsGen2Environment() *pluginsdk.Resource {

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

name := d.Get("name").(string)
id := parse.NewEnvironmentID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
location := azure.NormalizeLocation(d.Get("location").(string))
resourceGroup := d.Get("resource_group_name").(string)
t := d.Get("tags").(map[string]interface{})
sku, err := convertEnvironmentSkuName(d.Get("sku_name").(string))
if err != nil {
return fmt.Errorf("expanding sku: %+v", err)
}

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 IoT Time Series Insights Gen2 Environment %q (Resource Group %q): %s", name, resourceGroup, err)
return fmt.Errorf("checking for presence of existing %s: %s", id, err)
}
}

if existing.Value != nil {
environment, ok := existing.Value.AsGen2EnvironmentResource()
if !ok {
return fmt.Errorf("exisiting resource was not IoT Time Series Insights Gen2 Environment %q (Resource Group %q)", name, resourceGroup)
return fmt.Errorf("exisiting resource was not %s", id)
}

if environment.ID != nil && *environment.ID != "" {
Expand All @@ -157,30 +157,16 @@ func resourceIoTTimeSeriesInsightsGen2EnvironmentCreateUpdate(d *pluginsdk.Resou
}
}

future, err := client.CreateOrUpdate(ctx, resourceGroup, name, environment)
future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, environment)
if err != nil {
return fmt.Errorf("creating/updating IoT Time Series Gen2 Standard Environment %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 completion of IoT Time Series Insights Gen2 Environment %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("waiting for completion of %s: %+v", id, err)
}

resp, err := client.Get(ctx, resourceGroup, name, "")
if err != nil {
return fmt.Errorf("retrieving IoT Time Series Insights Gen2 Environment %q (Resource Group %q): %+v", name, resourceGroup, err)
}

read, ok := resp.Value.AsGen2EnvironmentResource()
if !ok {
return fmt.Errorf("resource was not IoT Time Series Insights Gen2 Environment %q (Resource Group %q)", name, resourceGroup)
}

if read.ID == nil || *read.ID == "" {
return fmt.Errorf("cannot read IoT Time Series Insights Gen2 Environment %q (Resource Group %q) ID", name, resourceGroup)
}

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

return resourceIoTTimeSeriesInsightsGen2EnvironmentRead(d, meta)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,20 @@ func resourceIoTTimeSeriesInsightsReferenceDataSetCreateUpdate(d *pluginsdk.Reso
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

name := d.Get("name").(string)
environmentID := d.Get("time_series_insights_environment_id").(string)
id, err := parse.EnvironmentID(environmentID)
envId, err := parse.EnvironmentID(environmentID)
if err != nil {
return err
}
id := parse.NewReferenceDataSetID(envId.SubscriptionId, envId.ResourceGroup, envId.Name, d.Get("name").(string))
location := azure.NormalizeLocation(d.Get("location").(string))
t := d.Get("tags").(map[string]interface{})

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

Expand All @@ -136,20 +136,11 @@ func resourceIoTTimeSeriesInsightsReferenceDataSetCreateUpdate(d *pluginsdk.Reso
},
}

if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, name, dataset); err != nil {
return fmt.Errorf("creating/updating IoT Time Series Insights Reference Data Set %q (Resource Group %q): %+v", name, id.ResourceGroup, err)
if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.EnvironmentName, id.Name, dataset); err != nil {
return fmt.Errorf("creating/updating %s: %+v", id, err)
}

resp, err := client.Get(ctx, id.ResourceGroup, id.Name, name)
if err != nil {
return fmt.Errorf("retrieving IoT Time Series Insights Reference Data Set %q (Resource Group %q): %+v", name, id.ResourceGroup, err)
}

if resp.ID == nil || *resp.ID == "" {
return fmt.Errorf("cannot read IoT Time Series Insights Reference Data Set %q (Resource Group %q) ID", name, id.ResourceGroup)
}

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

return resourceIoTTimeSeriesInsightsReferenceDataSetRead(d, meta)
}
Expand Down
Loading

0 comments on commit 4bb2252

Please sign in to comment.