Skip to content

Commit

Permalink
azurerm_synapse_integration_runtime_* - a state migration to work a…
Browse files Browse the repository at this point in the history
…round the previously incorrect id casing (#19485)

* azurerm_synapse_integration_runtime_* - a state migration to work around the previously incorrect id casing

* goimports
  • Loading branch information
mbfrahry authored Nov 30, 2022
1 parent 3f4f0e5 commit 6d718c2
Show file tree
Hide file tree
Showing 10 changed files with 339 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package migration

import (
"context"
"log"

"github.com/hashicorp/terraform-provider-azurerm/internal/services/synapse/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

type SynapseIntegrationRuntimeAzureV0ToV1 struct{}

func (s SynapseIntegrationRuntimeAzureV0ToV1) Schema() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"synapse_workspace_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"location": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"compute_type": {
Type: pluginsdk.TypeString,
Optional: true,
},

"core_count": {
Type: pluginsdk.TypeInt,
Optional: true,
},

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

"time_to_live_min": {
Type: pluginsdk.TypeInt,
Optional: true,
},
}
}

func (s SynapseIntegrationRuntimeAzureV0ToV1) UpgradeFunc() pluginsdk.StateUpgraderFunc {
return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
oldId := rawState["id"].(string)
newId, err := parse.IntegrationRuntimeIDInsensitively(oldId)
if err != nil {
return nil, err
}

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

rawState["id"] = newId.ID()
return rawState, nil
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package migration

import (
"context"
"log"

"github.com/hashicorp/terraform-provider-azurerm/internal/services/synapse/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

type SynapseIntegrationRuntimeSelfHostedV0ToV1 struct{}

func (s SynapseIntegrationRuntimeSelfHostedV0ToV1) Schema() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"synapse_workspace_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

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

"authorization_key_primary": {
Type: pluginsdk.TypeString,
Computed: true,
},

"authorization_key_secondary": {
Type: pluginsdk.TypeString,
Computed: true,
},
}
}

func (s SynapseIntegrationRuntimeSelfHostedV0ToV1) UpgradeFunc() pluginsdk.StateUpgraderFunc {
return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
oldId := rawState["id"].(string)
newId, err := parse.IntegrationRuntimeIDInsensitively(oldId)
if err != nil {
return nil, err
}

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

rawState["id"] = newId.ID()
return rawState, nil
}
}
60 changes: 58 additions & 2 deletions internal/services/synapse/parse/integration_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (id IntegrationRuntimeId) String() string {
}

func (id IntegrationRuntimeId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Synapse/workspaces/%s/integrationruntimes/%s"
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Synapse/workspaces/%s/integrationRuntimes/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.WorkspaceName, id.Name)
}

Expand All @@ -63,7 +63,63 @@ func IntegrationRuntimeID(input string) (*IntegrationRuntimeId, error) {
if resourceId.WorkspaceName, err = id.PopSegment("workspaces"); err != nil {
return nil, err
}
if resourceId.Name, err = id.PopSegment("integrationruntimes"); err != nil {
if resourceId.Name, err = id.PopSegment("integrationRuntimes"); err != nil {
return nil, err
}

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

return &resourceId, nil
}

// IntegrationRuntimeIDInsensitively parses an IntegrationRuntime ID into an IntegrationRuntimeId struct, insensitively
// This should only be used to parse an ID for rewriting, the IntegrationRuntimeID
// method should be used instead for validation etc.
//
// Whilst this may seem strange, this enables Terraform have consistent casing
// which works around issues in Core, whilst handling broken API responses.
func IntegrationRuntimeIDInsensitively(input string) (*IntegrationRuntimeId, error) {
id, err := resourceids.ParseAzureResourceID(input)
if err != nil {
return nil, err
}

resourceId := IntegrationRuntimeId{
SubscriptionId: id.SubscriptionID,
ResourceGroup: id.ResourceGroup,
}

if resourceId.SubscriptionId == "" {
return nil, fmt.Errorf("ID was missing the 'subscriptions' element")
}

if resourceId.ResourceGroup == "" {
return nil, fmt.Errorf("ID was missing the 'resourceGroups' element")
}

// find the correct casing for the 'workspaces' segment
workspacesKey := "workspaces"
for key := range id.Path {
if strings.EqualFold(key, workspacesKey) {
workspacesKey = key
break
}
}
if resourceId.WorkspaceName, err = id.PopSegment(workspacesKey); err != nil {
return nil, err
}

// find the correct casing for the 'integrationRuntimes' segment
integrationRuntimesKey := "integrationRuntimes"
for key := range id.Path {
if strings.EqualFold(key, integrationRuntimesKey) {
integrationRuntimesKey = key
break
}
}
if resourceId.Name, err = id.PopSegment(integrationRuntimesKey); err != nil {
return nil, err
}

Expand Down
142 changes: 139 additions & 3 deletions internal/services/synapse/parse/integration_runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var _ resourceids.Id = IntegrationRuntimeId{}

func TestIntegrationRuntimeIDFormatter(t *testing.T) {
actual := NewIntegrationRuntimeID("12345678-1234-9876-4563-123456789012", "resGroup1", "workspace1", "IntegrationRuntime1").ID()
expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/workspaces/workspace1/integrationruntimes/IntegrationRuntime1"
expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/workspaces/workspace1/integrationRuntimes/IntegrationRuntime1"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
Expand Down Expand Up @@ -75,13 +75,13 @@ func TestIntegrationRuntimeID(t *testing.T) {

{
// missing value for Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/workspaces/workspace1/integrationruntimes/",
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/workspaces/workspace1/integrationRuntimes/",
Error: true,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/workspaces/workspace1/integrationruntimes/IntegrationRuntime1",
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/workspaces/workspace1/integrationRuntimes/IntegrationRuntime1",
Expected: &IntegrationRuntimeId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "resGroup1",
Expand Down Expand Up @@ -126,3 +126,139 @@ func TestIntegrationRuntimeID(t *testing.T) {
}
}
}

func TestIntegrationRuntimeIDInsensitively(t *testing.T) {
testData := []struct {
Input string
Error bool
Expected *IntegrationRuntimeId
}{

{
// empty
Input: "",
Error: true,
},

{
// missing SubscriptionId
Input: "/",
Error: true,
},

{
// missing value for SubscriptionId
Input: "/subscriptions/",
Error: true,
},

{
// missing ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/",
Error: true,
},

{
// missing value for ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/",
Error: true,
},

{
// missing WorkspaceName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/",
Error: true,
},

{
// missing value for WorkspaceName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/workspaces/",
Error: true,
},

{
// missing Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/workspaces/workspace1/",
Error: true,
},

{
// missing value for Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/workspaces/workspace1/integrationRuntimes/",
Error: true,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/workspaces/workspace1/integrationRuntimes/IntegrationRuntime1",
Expected: &IntegrationRuntimeId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "resGroup1",
WorkspaceName: "workspace1",
Name: "IntegrationRuntime1",
},
},

{
// lower-cased segment names
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/workspaces/workspace1/integrationruntimes/IntegrationRuntime1",
Expected: &IntegrationRuntimeId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "resGroup1",
WorkspaceName: "workspace1",
Name: "IntegrationRuntime1",
},
},

{
// upper-cased segment names
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/WORKSPACES/workspace1/INTEGRATIONRUNTIMES/IntegrationRuntime1",
Expected: &IntegrationRuntimeId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "resGroup1",
WorkspaceName: "workspace1",
Name: "IntegrationRuntime1",
},
},

{
// mixed-cased segment names
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/WoRkSpAcEs/workspace1/InTeGrAtIoNrUnTiMeS/IntegrationRuntime1",
Expected: &IntegrationRuntimeId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "resGroup1",
WorkspaceName: "workspace1",
Name: "IntegrationRuntime1",
},
},
}

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

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

t.Fatalf("Expect a value but got an error: %s", err)
}
if v.Error {
t.Fatal("Expect an error but didn't get one")
}

if actual.SubscriptionId != v.Expected.SubscriptionId {
t.Fatalf("Expected %q but got %q for SubscriptionId", v.Expected.SubscriptionId, actual.SubscriptionId)
}
if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup)
}
if actual.WorkspaceName != v.Expected.WorkspaceName {
t.Fatalf("Expected %q but got %q for WorkspaceName", v.Expected.WorkspaceName, actual.WorkspaceName)
}
if actual.Name != v.Expected.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name)
}
}
}
2 changes: 1 addition & 1 deletion internal/services/synapse/resourceids.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package synapse

//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=FirewallRule -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/workspaces/workspace1/firewallRules/firewallRule1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=IntegrationRuntime -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/workspaces/workspace1/integrationruntimes/IntegrationRuntime1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=IntegrationRuntime -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/workspaces/workspace1/integrationRuntimes/IntegrationRuntime1 -rewrite=true
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=LinkedService -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/workspaces/workspace1/linkedServices/linkedservice1 -rewrite=true
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=ManagedPrivateEndpoint -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/workspaces/workspace1/managedVirtualNetworks/default/managedPrivateEndpoints/endpoint1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=PrivateLinkHub -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/privateLinkHubs/privateLinkHub1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/synapse/migration"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/synapse/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/synapse/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand All @@ -30,6 +31,11 @@ func resourceSynapseIntegrationRuntimeAzure() *pluginsdk.Resource {
return err
}),

SchemaVersion: 1,
StateUpgraders: pluginsdk.StateUpgrades(map[int]pluginsdk.StateUpgrade{
0: migration.SynapseIntegrationRuntimeAzureV0ToV1{},
}),

Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Expand Down
Loading

0 comments on commit 6d718c2

Please sign in to comment.