Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/api_management_api_version_set: handling changes to the Azure Resource ID #7071

Merged
merged 6 commits into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,15 +177,12 @@ 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)
Expand All @@ -186,8 +194,8 @@ func resourceArmApiManagementApiVersionSetRead(d *schema.ResourceData, meta inte
}

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