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

azurerm_container_registry_webhook - a state migration to work around the previously incorrect id casing #19507

Merged
merged 2 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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/containers/migration"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/containers/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/containers/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
Expand All @@ -31,6 +32,11 @@ func resourceContainerRegistryWebhook() *pluginsdk.Resource {
return err
}),

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

Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package migration

import (
"context"
"log"

"github.com/Azure/azure-sdk-for-go/services/preview/containerregistry/mgmt/2021-08-01-preview/containerregistry"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/containers/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

type RegistryWebhookV0ToV1 struct{}

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

"resource_group_name": commonschema.ResourceGroupName(),

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

"service_uri": {
Type: pluginsdk.TypeString,
Required: true,
},

"custom_headers": {
Type: pluginsdk.TypeMap,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

"status": {
Type: pluginsdk.TypeString,
Optional: true,
Default: containerregistry.WebhookStatusEnabled,
},

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

"actions": {
Type: pluginsdk.TypeSet,
Required: true,
MinItems: 1,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

"location": commonschema.Location(),

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

func (s RegistryWebhookV0ToV1) 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.WebhookIDInsensitively(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/containers/parse/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (id WebhookId) String() string {
}

func (id WebhookId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.ContainerRegistry/registries/%s/webhooks/%s"
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.ContainerRegistry/registries/%s/webHooks/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.RegistryName, id.Name)
}

Expand All @@ -63,7 +63,63 @@ func WebhookID(input string) (*WebhookId, error) {
if resourceId.RegistryName, err = id.PopSegment("registries"); err != nil {
return nil, err
}
if resourceId.Name, err = id.PopSegment("webhooks"); err != nil {
if resourceId.Name, err = id.PopSegment("webHooks"); err != nil {
return nil, err
}

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

return &resourceId, nil
}

// WebhookIDInsensitively parses an Webhook ID into an WebhookId struct, insensitively
// This should only be used to parse an ID for rewriting, the WebhookID
// 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 WebhookIDInsensitively(input string) (*WebhookId, error) {
id, err := resourceids.ParseAzureResourceID(input)
if err != nil {
return nil, err
}

resourceId := WebhookId{
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 'registries' segment
registriesKey := "registries"
for key := range id.Path {
if strings.EqualFold(key, registriesKey) {
registriesKey = key
break
}
}
if resourceId.RegistryName, err = id.PopSegment(registriesKey); err != nil {
return nil, err
}

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

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

func TestWebhookIDFormatter(t *testing.T) {
actual := NewWebhookID("12345678-1234-9876-4563-123456789012", "group1", "registry1", "webhook1").ID()
expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.ContainerRegistry/registries/registry1/webhooks/webhook1"
expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.ContainerRegistry/registries/registry1/webHooks/webhook1"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
Expand Down Expand Up @@ -75,13 +75,13 @@ func TestWebhookID(t *testing.T) {

{
// missing value for Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.ContainerRegistry/registries/registry1/webhooks/",
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.ContainerRegistry/registries/registry1/webHooks/",
Error: true,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.ContainerRegistry/registries/registry1/webhooks/webhook1",
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.ContainerRegistry/registries/registry1/webHooks/webhook1",
Expected: &WebhookId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "group1",
Expand Down Expand Up @@ -126,3 +126,139 @@ func TestWebhookID(t *testing.T) {
}
}
}

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

{
// 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 RegistryName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.ContainerRegistry/",
Error: true,
},

{
// missing value for RegistryName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.ContainerRegistry/registries/",
Error: true,
},

{
// missing Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.ContainerRegistry/registries/registry1/",
Error: true,
},

{
// missing value for Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.ContainerRegistry/registries/registry1/webHooks/",
Error: true,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.ContainerRegistry/registries/registry1/webHooks/webhook1",
Expected: &WebhookId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "group1",
RegistryName: "registry1",
Name: "webhook1",
},
},

{
// lower-cased segment names
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.ContainerRegistry/registries/registry1/webhooks/webhook1",
Expected: &WebhookId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "group1",
RegistryName: "registry1",
Name: "webhook1",
},
},

{
// upper-cased segment names
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.ContainerRegistry/REGISTRIES/registry1/WEBHOOKS/webhook1",
Expected: &WebhookId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "group1",
RegistryName: "registry1",
Name: "webhook1",
},
},

{
// mixed-cased segment names
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.ContainerRegistry/ReGiStRiEs/registry1/WeBhOoKs/webhook1",
Expected: &WebhookId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "group1",
RegistryName: "registry1",
Name: "webhook1",
},
},
}

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

actual, err := WebhookIDInsensitively(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.RegistryName != v.Expected.RegistryName {
t.Fatalf("Expected %q but got %q for RegistryName", v.Expected.RegistryName, actual.RegistryName)
}
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/containers/resourceids.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ package containers
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=ContainerRegistryToken -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ContainerRegistry/registries/registry1/tokens/token1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=ContainerRegistryTokenPassword -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ContainerRegistry/registries/registry1/tokens/token1/passwords/password
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=Registry -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.ContainerRegistry/registries/registry1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=Webhook -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.ContainerRegistry/registries/registry1/webhooks/webhook1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=Webhook -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.ContainerRegistry/registries/registry1/webHooks/webhook1 -rewrite=true
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=ContainerConnectedRegistry -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.ContainerRegistry/registries/registry1/connectedRegistries/registry1
4 changes: 2 additions & 2 deletions internal/services/containers/validate/webhook_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ func TestWebhookID(t *testing.T) {

{
// missing value for Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.ContainerRegistry/registries/registry1/webhooks/",
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.ContainerRegistry/registries/registry1/webHooks/",
Valid: false,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.ContainerRegistry/registries/registry1/webhooks/webhook1",
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.ContainerRegistry/registries/registry1/webHooks/webhook1",
Valid: true,
},

Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/container_registry_webhook.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,5 @@ The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/l
Container Registry Webhooks can be imported using the `resource id`, e.g.

```shell
terraform import azurerm_container_registry_webhook.example /subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/mygroup1/providers/Microsoft.ContainerRegistry/registries/myregistry1/webhooks/mywebhook1
terraform import azurerm_container_registry_webhook.example /subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/mygroup1/providers/Microsoft.ContainerRegistry/registries/myregistry1/webHooks/mywebhook1
```