Skip to content

Commit

Permalink
change resource_id to parent_id and name (#30)
Browse files Browse the repository at this point in the history
change resource_id to parent_id and name
  • Loading branch information
ms-henglu authored Jan 10, 2022
1 parent 6532493 commit 402f0b2
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 48 deletions.
19 changes: 17 additions & 2 deletions internal/services/azurerm_generic_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -55,6 +62,11 @@ func ResourceAzureGenericDataSource() *schema.Resource {
Computed: true,
},

"resource_id": {
Type: schema.TypeString,
Computed: true,
},

"tags": tags.SchemaTagsDataSource(),
},
}
Expand All @@ -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 {
Expand All @@ -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"])
Expand Down
5 changes: 3 additions & 2 deletions internal/services/azurerm_generic_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
22 changes: 18 additions & 4 deletions internal/services/azurerm_generic_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -86,6 +93,11 @@ func ResourceAzureGenericResource() *schema.Resource {
Computed: true,
},

"resource_id": {
Type: schema.TypeString,
Computed: true,
},

"tags": tags.SchemaTagsOC(),
},

Expand Down Expand Up @@ -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{})
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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))

Expand Down
76 changes: 49 additions & 27 deletions internal/services/azurerm_generic_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()...),
Expand All @@ -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),
Expand All @@ -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()...),
Expand All @@ -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()...),
Expand All @@ -90,27 +94,31 @@ 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()...),
{
Config: r.identityUserAssigned(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("resource_id").Exists(),
),
},
data.ImportStep(ignoredProperties()...),
{
Config: r.identitySystemAssigned(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("resource_id").Exists(),
),
},
data.ImportStep(ignoredProperties()...),
{
Config: r.complete(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("resource_id").Exists(),
),
},
data.ImportStep(ignoredProperties()...),
Expand All @@ -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"),
),
},
Expand All @@ -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"),
),
},
Expand All @@ -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"),
),
},
Expand All @@ -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"),
),
},
Expand Down Expand Up @@ -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 = <<BODY
name = "acctest%[2]s"
parent_id = azurerm_container_registry.test.id
type = "Microsoft.ContainerRegistry/registries/scopeMaps@2020-11-01-preview"
body = <<BODY
{
"properties": {
"description": "Developer Scopes",
Expand All @@ -209,9 +222,10 @@ func (r GenericResource) requiresImport(data acceptance.TestData) string {
%s
resource "azurerm-restapi_resource" "import" {
resource_id = azurerm-restapi_resource.test.resource_id
type = azurerm-restapi_resource.test.type
body = <<BODY
name = azurerm-restapi_resource.test.name
parent_id = azurerm-restapi_resource.test.parent_id
type = azurerm-restapi_resource.test.type
body = <<BODY
{
"properties": {
"description": "Developer Scopes",
Expand All @@ -236,9 +250,10 @@ resource "azurerm_user_assigned_identity" "test" {
}
resource "azurerm-restapi_resource" "test" {
resource_id = "${azurerm_resource_group.test.id}/providers/Microsoft.ContainerRegistry/registries/acctest%[2]s"
type = "Microsoft.ContainerRegistry/registries@2020-11-01-preview"
location = "%[3]s"
name = "acctest%[2]s"
parent_id = azurerm_resource_group.test.id
type = "Microsoft.ContainerRegistry/registries@2020-11-01-preview"
location = "%[3]s"
identity {
type = "SystemAssigned, UserAssigned"
identity_ids = [azurerm_user_assigned_identity.test.id]
Expand Down Expand Up @@ -276,8 +291,9 @@ resource "azurerm_user_assigned_identity" "test" {
}
resource "azurerm-restapi_resource" "test" {
resource_id = "${azurerm_resource_group.test.id}/providers/Microsoft.ContainerRegistry/registries/acctest%[2]s"
type = "Microsoft.ContainerRegistry/registries@2020-11-01-preview"
name = "acctest%[2]s"
parent_id = azurerm_resource_group.test.id
type = "Microsoft.ContainerRegistry/registries@2020-11-01-preview"
body = <<BODY
{
Expand Down Expand Up @@ -305,10 +321,11 @@ func (r GenericResource) identityNone(data acceptance.TestData) string {
%s
resource "azurerm-restapi_resource" "test" {
resource_id = "${azurerm_resource_group.test.id}/providers/Microsoft.ContainerRegistry/registries/acctest%[2]s"
type = "Microsoft.ContainerRegistry/registries@2020-11-01-preview"
location = "%[3]s"
body = <<BODY
name = "acctest%[2]s"
parent_id = azurerm_resource_group.test.id
type = "Microsoft.ContainerRegistry/registries@2020-11-01-preview"
location = "%[3]s"
body = <<BODY
{
"sku": {
"name": "Standard"
Expand All @@ -327,9 +344,10 @@ func (r GenericResource) identitySystemAssigned(data acceptance.TestData) string
%s
resource "azurerm-restapi_resource" "test" {
resource_id = "${azurerm_resource_group.test.id}/providers/Microsoft.ContainerRegistry/registries/acctest%[2]s"
type = "Microsoft.ContainerRegistry/registries@2020-11-01-preview"
location = "%[3]s"
name = "acctest%[2]s"
parent_id = azurerm_resource_group.test.id
type = "Microsoft.ContainerRegistry/registries@2020-11-01-preview"
location = "%[3]s"
identity {
type = "SystemAssigned"
}
Expand Down Expand Up @@ -358,9 +376,10 @@ resource "azurerm_user_assigned_identity" "test" {
}
resource "azurerm-restapi_resource" "test" {
resource_id = "${azurerm_resource_group.test.id}/providers/Microsoft.ContainerRegistry/registries/acctest%[2]s"
type = "Microsoft.ContainerRegistry/registries@2020-11-01-preview"
location = "%[3]s"
name = "acctest%[2]s"
parent_id = azurerm_resource_group.test.id
type = "Microsoft.ContainerRegistry/registries@2020-11-01-preview"
location = "%[3]s"
identity {
type = "UserAssigned"
identity_ids = [azurerm_user_assigned_identity.test.id]
Expand Down Expand Up @@ -393,8 +412,9 @@ provider "azurerm-restapi" {
}
resource "azurerm-restapi_resource" "test" {
resource_id = "${azurerm_resource_group.test.id}/providers/Microsoft.Automation/automationAccounts/acctest%[2]s"
type = "Microsoft.Automation/automationAccounts@2020-01-13-preview"
name = "acctest%[2]s"
parent_id = azurerm_resource_group.test.id
type = "Microsoft.Automation/automationAccounts@2020-01-13-preview"
location = azurerm_resource_group.test.location
identity {
Expand Down Expand Up @@ -422,8 +442,9 @@ provider "azurerm-restapi" {
}
resource "azurerm-restapi_resource" "test" {
resource_id = "${azurerm_resource_group.test.id}/providers/Microsoft.Automation/automationAccounts/acctest%[2]s"
type = "Microsoft.Automation/automationAccounts@2020-01-13-preview"
name = "acctest%[2]s"
parent_id = azurerm_resource_group.test.id
type = "Microsoft.Automation/automationAccounts@2020-01-13-preview"
location = azurerm_resource_group.test.location
identity {
Expand Down Expand Up @@ -455,8 +476,9 @@ provider "azurerm-restapi" {
}
resource "azurerm-restapi_resource" "test" {
resource_id = "${azurerm_resource_group.test.id}/providers/Microsoft.Automation/automationAccounts/acctest%[2]s"
type = "Microsoft.Automation/automationAccounts@2020-01-13-preview"
name = "acctest%[2]s"
parent_id = azurerm_resource_group.test.id
type = "Microsoft.Automation/automationAccounts@2020-01-13-preview"
location = azurerm_resource_group.test.location
identity {
Expand Down
Loading

0 comments on commit 402f0b2

Please sign in to comment.