From 402f0b265b29c2c1c47ed9a8fa427530af8444a2 Mon Sep 17 00:00:00 2001 From: Heng Lu <79895375+ms-henglu@users.noreply.github.com> Date: Mon, 10 Jan 2022 13:43:20 +0800 Subject: [PATCH] change resource_id to parent_id and name (#30) change resource_id to parent_id and name --- .../services/azurerm_generic_data_source.go | 19 ++++- .../azurerm_generic_data_source_test.go | 5 +- internal/services/azurerm_generic_resource.go | 22 +++++- .../services/azurerm_generic_resource_test.go | 76 ++++++++++++------- internal/services/parse/resource.go | 36 +++++++++ utils/helper.go | 38 ++++++++++ .../d/azurerm-restapi_resource.html.markdown | 15 ++-- .../r/azurerm-restapi_resource.html.markdown | 17 +++-- 8 files changed, 180 insertions(+), 48 deletions(-) diff --git a/internal/services/azurerm_generic_data_source.go b/internal/services/azurerm_generic_data_source.go index 464213ca1..20f6d6a2a 100644 --- a/internal/services/azurerm_generic_data_source.go +++ b/internal/services/azurerm_generic_data_source.go @@ -15,6 +15,7 @@ import ( "github.com/Azure/terraform-provider-azurerm-restapi/internal/tf" "github.com/Azure/terraform-provider-azurerm-restapi/utils" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" ) func ResourceAzureGenericDataSource() *schema.Resource { @@ -26,7 +27,13 @@ func ResourceAzureGenericDataSource() *schema.Resource { }, Schema: map[string]*schema.Schema{ - "resource_id": { + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + + "parent_id": { Type: schema.TypeString, Required: true, ValidateFunc: validate.AzureResourceID, @@ -55,6 +62,11 @@ func ResourceAzureGenericDataSource() *schema.Resource { Computed: true, }, + "resource_id": { + Type: schema.TypeString, + Computed: true, + }, + "tags": tags.SchemaTagsDataSource(), }, } @@ -65,7 +77,7 @@ func resourceAzureGenericDataSourceRead(d *schema.ResourceData, meta interface{} ctx, cancel := tf.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() - id := parse.NewResourceID(d.Get("resource_id").(string), d.Get("type").(string)) + id := parse.BuildResourceID(d.Get("name").(string), d.Get("parent_id").(string), d.Get("type").(string)) responseBody, response, err := client.Get(ctx, id.AzureResourceId, id.ApiVersion) if err != nil { @@ -76,6 +88,9 @@ func resourceAzureGenericDataSourceRead(d *schema.ResourceData, meta interface{} return fmt.Errorf("reading %q: %+v", id, err) } d.SetId(id.ID()) + d.Set("name", id.Name) + d.Set("parent_id", id.ParentId) + d.Set("resource_id", id.AzureResourceId) if bodyMap, ok := responseBody.(map[string]interface{}); ok { d.Set("tags", tags.FlattenTags(bodyMap["tags"])) d.Set("location", bodyMap["location"]) diff --git a/internal/services/azurerm_generic_data_source_test.go b/internal/services/azurerm_generic_data_source_test.go index 4f06f03fc..e02920780 100644 --- a/internal/services/azurerm_generic_data_source_test.go +++ b/internal/services/azurerm_generic_data_source_test.go @@ -37,8 +37,9 @@ func (r GenericDataSource) basic(data acceptance.TestData) string { %s data "azurerm-restapi_resource" "test" { - resource_id = azurerm-restapi_resource.test.resource_id - type = azurerm-restapi_resource.test.type + name = azurerm-restapi_resource.test.name + parent_id = azurerm-restapi_resource.test.parent_id + type = azurerm-restapi_resource.test.type } `, GenericResource{}.complete(data)) } diff --git a/internal/services/azurerm_generic_resource.go b/internal/services/azurerm_generic_resource.go index a1729b85b..6f73c4d92 100644 --- a/internal/services/azurerm_generic_resource.go +++ b/internal/services/azurerm_generic_resource.go @@ -44,7 +44,14 @@ func ResourceAzureGenericResource() *schema.Resource { }, Schema: map[string]*schema.Schema{ - "resource_id": { + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + + "parent_id": { Type: schema.TypeString, Required: true, ForceNew: true, @@ -86,6 +93,11 @@ func ResourceAzureGenericResource() *schema.Resource { Computed: true, }, + "resource_id": { + Type: schema.TypeString, + Computed: true, + }, + "tags": tags.SchemaTagsOC(), }, @@ -118,7 +130,7 @@ func ResourceAzureGenericResource() *schema.Resource { } if !isConfigExist(config, "tags") && body["tags"] == nil { - id := parse.NewResourceID(d.Get("resource_id").(string), d.Get("type").(string)) + id := parse.BuildResourceID(d.Get("name").(string), d.Get("parent_id").(string), d.Get("type").(string)) resourceDef, err := azure.GetResourceDefinition(id.AzureResourceType, id.ApiVersion) if err == nil && resourceDef != nil { tempBody := make(map[string]interface{}) @@ -160,7 +172,7 @@ func ResourceAzureGenericResource() *schema.Resource { body["identity"] = identityModel } } - if err := schemaValidation(parse.NewResourceID(d.Get("resource_id").(string), d.Get("type").(string)), body); err != nil { + if err := schemaValidation(parse.BuildResourceID(d.Get("name").(string), d.Get("parent_id").(string), d.Get("type").(string)), body); err != nil { return err } } @@ -174,7 +186,7 @@ func resourceAzureGenericResourceCreateUpdate(d *schema.ResourceData, meta inter ctx, cancel := tf.ForCreateUpdate(meta.(*clients.Client).StopContext, d) defer cancel() - id := parse.NewResourceID(d.Get("resource_id").(string), d.Get("type").(string)) + id := parse.BuildResourceID(d.Get("name").(string), d.Get("parent_id").(string), d.Get("type").(string)) if d.IsNewResource() { existing, response, err := client.Get(ctx, id.AzureResourceId, id.ApiVersion) @@ -302,6 +314,8 @@ func resourceAzureGenericResourceRead(d *schema.ResourceData, meta interface{}) d.Set("body", string(data)) } + d.Set("name", id.Name) + d.Set("parent_id", id.ParentId) d.Set("resource_id", id.AzureResourceId) d.Set("type", fmt.Sprintf("%s@%s", id.AzureResourceType, id.ApiVersion)) diff --git a/internal/services/azurerm_generic_resource_test.go b/internal/services/azurerm_generic_resource_test.go index f813f880b..efc6acbab 100644 --- a/internal/services/azurerm_generic_resource_test.go +++ b/internal/services/azurerm_generic_resource_test.go @@ -30,6 +30,7 @@ func TestAccGenericResource_basic(t *testing.T) { Config: r.basic(data), Check: resource.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("resource_id").Exists(), ), }, data.ImportStep(ignoredProperties()...), @@ -45,6 +46,7 @@ func TestAccGenericResource_requiresImport(t *testing.T) { Config: r.basic(data), Check: resource.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("resource_id").Exists(), ), }, data.RequiresImportErrorStep(r.requiresImport), @@ -60,6 +62,7 @@ func TestAccGenericResource_complete(t *testing.T) { Config: r.complete(data), Check: resource.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("resource_id").Exists(), ), }, data.ImportStep(ignoredProperties()...), @@ -75,6 +78,7 @@ func TestAccGenericResource_completeBody(t *testing.T) { Config: r.completeBody(data), Check: resource.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("resource_id").Exists(), ), }, data.ImportStep(ignoredProperties()...), @@ -90,6 +94,7 @@ func TestAccGenericResource_identity(t *testing.T) { Config: r.identityNone(data), Check: resource.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("resource_id").Exists(), ), }, data.ImportStep(ignoredProperties()...), @@ -97,6 +102,7 @@ func TestAccGenericResource_identity(t *testing.T) { Config: r.identityUserAssigned(data), Check: resource.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("resource_id").Exists(), ), }, data.ImportStep(ignoredProperties()...), @@ -104,6 +110,7 @@ func TestAccGenericResource_identity(t *testing.T) { Config: r.identitySystemAssigned(data), Check: resource.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("resource_id").Exists(), ), }, data.ImportStep(ignoredProperties()...), @@ -111,6 +118,7 @@ func TestAccGenericResource_identity(t *testing.T) { Config: r.complete(data), Check: resource.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("resource_id").Exists(), ), }, data.ImportStep(ignoredProperties()...), @@ -126,6 +134,7 @@ func TestAccGenericResource_defaultTags(t *testing.T) { Config: r.defaultTag(data), Check: resource.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("resource_id").Exists(), check.That(data.ResourceName).Key("tags.key").HasValue("default"), ), }, @@ -134,6 +143,7 @@ func TestAccGenericResource_defaultTags(t *testing.T) { Config: r.defaultTagOverrideInBody(data), Check: resource.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("resource_id").Exists(), check.That(data.ResourceName).Key("tags.key").HasValue("override"), ), }, @@ -142,6 +152,7 @@ func TestAccGenericResource_defaultTags(t *testing.T) { Config: r.defaultTag(data), Check: resource.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("resource_id").Exists(), check.That(data.ResourceName).Key("tags.key").HasValue("default"), ), }, @@ -150,6 +161,7 @@ func TestAccGenericResource_defaultTags(t *testing.T) { Config: r.defaultTagOverrideInHcl(data), Check: resource.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("resource_id").Exists(), check.That(data.ResourceName).Key("tags.key").HasValue("override"), ), }, @@ -188,9 +200,10 @@ resource "azurerm_container_registry" "test" { } resource "azurerm-restapi_resource" "test" { - resource_id = "${azurerm_container_registry.test.id}/scopeMaps/acctest%[2]s" - type = "Microsoft.ContainerRegistry/registries/scopeMaps@2020-11-01-preview" - body = <@`. `` is the Azure resource type, for example, `Microsoft.Storage/storageAccounts`. `` is version of the API used to manage this azure resource. @@ -92,8 +93,10 @@ The following arguments are supported: In addition to the Arguments listed above - the following Attributes are exported: -* `id` - The ID of the azure resource. +* `id` - The ID of the azure resource which contains `api-version`. +* `resource_id` - The ID of the azure resource which can be used as a reference in other azurerm resources. + * `identity` - An `identity` block as defined below, which contains the Managed Service Identity information for this azure resource. * `location` - The Azure Region where the azure resource should exist. diff --git a/website/docs/r/azurerm-restapi_resource.html.markdown b/website/docs/r/azurerm-restapi_resource.html.markdown index ae242d1b0..eaad8f3e3 100644 --- a/website/docs/r/azurerm-restapi_resource.html.markdown +++ b/website/docs/r/azurerm-restapi_resource.html.markdown @@ -41,9 +41,10 @@ resource "azurerm_user_assigned_identity" "example" { // manage a container registry resource resource "azurerm-restapi_resource" "example" { - resource_id = "${azurerm_resource_group.example.id}/providers/Microsoft.ContainerRegistry/registries/registry1" - type = "Microsoft.ContainerRegistry/registries@2020-11-01-preview" - location = azurerm_resource_group.example.location + name = "registry1" + parent_id = azurerm_resource_group.example.id + type = "Microsoft.ContainerRegistry/registries@2020-11-01-preview" + location = azurerm_resource_group.example.location identity { type = "SystemAssigned, UserAssigned" identity_ids = [azurerm_user_assigned_identity.example.id] @@ -81,11 +82,11 @@ output "quarantine_policy" { ## Arguments Reference The following arguments are supported: -* `resource_id` - (Required) The ID of an azure source. +* `name` - (Required) Specifies the name of the azure resource. Changing this forces a new resource to be created. +* `parent_id` - (Required) The ID of the azure resource in which this resource is created. Changing this forces a new resource to be created. Here're some examples `Container Registry: /subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/mygroup1/providers/Microsoft.ContainerRegistry/registries/myregistry1` and - `Virtual Machine: /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/Microsoft.Compute/virtualMachines/machine1`. - Changing this forces a new azure resource to be created. + `Resource Group: /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1`. * `type` - (Required) It is in a format like `@`. `` is the Azure resource type, for example, `Microsoft.Storage/storageAccounts`. `` is version of the API used to manage this azure resource. @@ -129,7 +130,9 @@ A `identity` block supports the following: In addition to the Arguments listed above - the following Attributes are exported: -* `id` - The ID of the azure resource. +* `id` - The ID of the azure resource which contains `api-version`. + +* `resource_id` - The ID of the azure resource which can be used as a reference in other azurerm resources. * `identity` - An `identity` block as defined below, which contains the Managed Service Identity information for this azure resource.