Skip to content

Commit

Permalink
r/api_management_api_version_set: handling changes to the Azure Resou…
Browse files Browse the repository at this point in the history
…rce ID (#7071)

This PR is a continuation of the changes done by @jochenrichter in #7055 but adds a State Migration for migrating from the old -> new ID

Fixes #6609
  • Loading branch information
tombuildsstuff authored May 25, 2020
1 parent c2d9c96 commit 3c3ef81
Show file tree
Hide file tree
Showing 11 changed files with 337 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/apimanagement/migration"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/apimanagement/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand All @@ -32,6 +34,15 @@ func resourceArmApiManagementApiVersionSet() *schema.Resource {
Delete: schema.DefaultTimeout(30 * time.Minute),
},

SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Type: migration.ApiVersionSetUpgradeV0Schema().CoreConfigSchema().ImpliedType(),
Upgrade: migration.ApiVersionSetUpgradeV0ToV1,
Version: 0,
},
},

Schema: map[string]*schema.Schema{
"name": azure.SchemaApiManagementChildName(),

Expand Down Expand Up @@ -166,28 +177,25 @@ func resourceArmApiManagementApiVersionSetRead(d *schema.ResourceData, meta inte
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.APIVersionSetID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
serviceName := id.Path["service"]
name := id.Path["api-version-sets"]

resp, err := client.Get(ctx, resourceGroup, serviceName, name)
resp, err := client.Get(ctx, id.ResourceGroup, id.ServiceName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] Api Version Set %q (Resource Group %q / Api Management Service %q) was not found - removing from state!", name, resourceGroup, serviceName)
log.Printf("[DEBUG] Api Version Set %q (Resource Group %q / Api Management Service %q) was not found - removing from state!", id.Name, id.ResourceGroup, id.ServiceName)
d.SetId("")
return nil
}

return fmt.Errorf("making Read request for Api Version Set %q (Resource Group %q / Api Management Service %q): %+v", name, resourceGroup, serviceName, err)
return fmt.Errorf("making Read request for Api Version Set %q (Resource Group %q / Api Management Service %q): %+v", id.Name, id.ResourceGroup, id.ServiceName, err)
}

d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
d.Set("api_management_name", serviceName)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("api_management_name", id.ServiceName)

if props := resp.APIVersionSetContractProperties; props != nil {
d.Set("description", props.Description)
Expand All @@ -205,17 +213,14 @@ func resourceArmApiManagementApiVersionSetDelete(d *schema.ResourceData, meta in
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.APIVersionSetID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
serviceName := id.Path["service"]
name := id.Path["api-version-sets"]

if resp, err := client.Delete(ctx, resourceGroup, serviceName, name, ""); err != nil {
if resp, err := client.Delete(ctx, id.ResourceGroup, id.ServiceName, id.Name, ""); err != nil {
if !utils.ResponseWasNotFound(resp) {
return fmt.Errorf("deleting Api Version Set %q (Resource Group %q / Api Management Service %q): %+v", name, resourceGroup, serviceName, err)
return fmt.Errorf("deleting Api Version Set %q (Resource Group %q / Api Management Service %q): %+v", id.Name, id.ResourceGroup, id.ServiceName, err)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package migration

import (
"log"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

func ApiVersionSetUpgradeV0Schema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"name": azure.SchemaApiManagementChildName(),

"resource_group_name": azure.SchemaResourceGroupName(),

"api_management_name": azure.SchemaApiManagementName(),

"display_name": {
Type: schema.TypeString,
Required: true,
},

"versioning_scheme": {
Type: schema.TypeString,
Required: true,
},

"description": {
Type: schema.TypeString,
Optional: true,
},

"version_header_name": {
Type: schema.TypeString,
Optional: true,
},

"version_query_name": {
Type: schema.TypeString,
Optional: true,
},
},
}
}

func ApiVersionSetUpgradeV0ToV1(rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
oldId := rawState["id"].(string)
newId := strings.Replace(rawState["id"].(string), "/api-version-set/", "/apiVersionSets/", 1)

log.Printf("[DEBUG] Updating ID from %q to %q", oldId, newId)

rawState["id"] = newId

return rawState, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package parse

import "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"

type ApiVersionSetId struct {
ResourceGroup string
ServiceName string
Name string
}

func APIVersionSetID(input string) (*ApiVersionSetId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, err
}

logger := ApiVersionSetId{
ResourceGroup: id.ResourceGroup,
}

if logger.ServiceName, err = id.PopSegment("service"); err != nil {
return nil, err
}

if logger.Name, err = id.PopSegment("apiVersionSets"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &logger, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package parse

import "testing"

func TestApiVersionSetID(t *testing.T) {
testData := []struct {
Name string
Input string
Expected *ApiVersionSetId
}{
{
Name: "Empty",
Input: "",
Expected: nil,
},
{
Name: "No Resource Groups Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000",
Expected: nil,
},
{
Name: "No Resource Groups Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/",
Expected: nil,
},
{
Name: "Resource Group ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/",
Expected: nil,
},
{
Name: "Missing Service Name",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/",
Expected: nil,
},
{
Name: "Missing Diagnostic",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1",
Expected: nil,
},
{
Name: "Missing Diagnostic Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/apiVersionSets",
Expected: nil,
},
{
Name: "Diagnostic ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/apiVersionSets/set1",
Expected: &ApiVersionSetId{
Name: "set1",
ServiceName: "service1",
ResourceGroup: "resGroup1",
},
},
{
Name: "Wrong Casing",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/ApiVersionSets/set1",
Expected: nil,
},
{
Name: "Legacy ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/api-version-sets/set1",
Expected: nil,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Name)

actual, err := APIVersionSetID(v.Input)
if err != nil {
if v.Expected == nil {
continue
}

t.Fatalf("Expected a value but got an error: %s", err)
}

if actual.Name != v.Expected.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name)
}

if actual.ServiceName != v.Expected.ServiceName {
t.Fatalf("Expected %q but got %q for Service Name", v.Expected.Name, actual.Name)
}

if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,6 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

type ApiManagementLoggerId struct {
ResourceGroup string
ServiceName string
Name string
}

func ApiManagementLoggerID(input string) (*ApiManagementLoggerId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("parsing Api Management Logger ID %q: %+v", input, err)
}

logger := ApiManagementLoggerId{
ResourceGroup: id.ResourceGroup,
}

if logger.ServiceName, err = id.PopSegment("service"); err != nil {
return nil, err
}

if logger.Name, err = id.PopSegment("loggers"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &logger, nil
}

type ApiManagementDiagnosticId struct {
ResourceGroup string
ServiceName string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,89 +2,6 @@ package parse

import "testing"

func TestApiManagementLoggerID(t *testing.T) {
testData := []struct {
Name string
Input string
Expected *ApiManagementLoggerId
}{
{
Name: "Empty",
Input: "",
Expected: nil,
},
{
Name: "No Resource Groups Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000",
Expected: nil,
},
{
Name: "No Resource Groups Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/",
Expected: nil,
},
{
Name: "Resource Group ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/",
Expected: nil,
},
{
Name: "Missing Service Name",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/",
Expected: nil,
},
{
Name: "Missing Logger",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1",
Expected: nil,
},
{
Name: "Missing Logger Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/loggers",
Expected: nil,
},
{
Name: "Logger ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/loggers/logger1",
Expected: &ApiManagementLoggerId{
Name: "logger1",
ServiceName: "service1",
ResourceGroup: "resGroup1",
},
},
{
Name: "Wrong Casing",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/Loggers/logger1",
Expected: nil,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Name)

actual, err := ApiManagementLoggerID(v.Input)
if err != nil {
if v.Expected == nil {
continue
}

t.Fatalf("Expected a value but got an error: %s", err)
}

if actual.Name != v.Expected.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name)
}

if actual.ServiceName != v.Expected.ServiceName {
t.Fatalf("Expected %q but got %q for Service Name", v.Expected.Name, actual.Name)
}

if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup)
}
}
}

func TestApiManagementDiagnosticID(t *testing.T) {
testData := []struct {
Name string
Expand Down
Loading

0 comments on commit 3c3ef81

Please sign in to comment.