From e5701a2d02de29ba19f728b2760344f65dac8705 Mon Sep 17 00:00:00 2001 From: magodo Date: Tue, 25 Aug 2020 10:01:09 +0800 Subject: [PATCH 01/10] `azurerm_cnd_endpoint` - Standardize the resource ID to be case sensitive This is a workaround for #Azure/azure-rest-api-specs#10576. Fixes: #8191 --- .../services/cdn/cdn_endpoint_resource.go | 7 +- .../services/cdn/parse/cdn_endpoint.go | 10 +++ .../cdn/tests/cdn_endpoint_resource_test.go | 66 +++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) diff --git a/azurerm/internal/services/cdn/cdn_endpoint_resource.go b/azurerm/internal/services/cdn/cdn_endpoint_resource.go index 78025a2c02b9..b01c9c765f46 100644 --- a/azurerm/internal/services/cdn/cdn_endpoint_resource.go +++ b/azurerm/internal/services/cdn/cdn_endpoint_resource.go @@ -303,7 +303,12 @@ func resourceArmCdnEndpointCreate(d *schema.ResourceData, meta interface{}) erro return fmt.Errorf("Error retrieving CDN Endpoint %q (Profile %q / Resource Group %q): %+v", name, profileName, resourceGroup, err) } - d.SetId(*read.ID) + id, err := parse.CdnEndpointID(*read.ID) + if err != nil { + return err + } + + d.SetId(id.ID()) return resourceArmCdnEndpointRead(d, meta) } diff --git a/azurerm/internal/services/cdn/parse/cdn_endpoint.go b/azurerm/internal/services/cdn/parse/cdn_endpoint.go index e1ab676e7280..e77feec9e0eb 100644 --- a/azurerm/internal/services/cdn/parse/cdn_endpoint.go +++ b/azurerm/internal/services/cdn/parse/cdn_endpoint.go @@ -7,6 +7,8 @@ import ( ) type CdnEndpointId struct { + Subscription string + Provider string ResourceGroup string ProfileName string Name string @@ -19,6 +21,8 @@ func CdnEndpointID(input string) (*CdnEndpointId, error) { } endpoint := CdnEndpointId{ + Subscription: id.SubscriptionID, + Provider: id.Provider, ResourceGroup: id.ResourceGroup, } @@ -36,3 +40,9 @@ func CdnEndpointID(input string) (*CdnEndpointId, error) { return &endpoint, nil } + +// This ID is a workaround for issue: https://github.com/Azure/azure-rest-api-specs/issues/10576 +func (id CdnEndpointId) ID() string { + return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/%s/profiles/%s/endpoints/%s", + id.Subscription, id.ResourceGroup, id.Provider, id.ProfileName, id.Name) +} diff --git a/azurerm/internal/services/cdn/tests/cdn_endpoint_resource_test.go b/azurerm/internal/services/cdn/tests/cdn_endpoint_resource_test.go index c46c87ad5f96..cbc8dfe5e252 100644 --- a/azurerm/internal/services/cdn/tests/cdn_endpoint_resource_test.go +++ b/azurerm/internal/services/cdn/tests/cdn_endpoint_resource_test.go @@ -345,6 +345,25 @@ func TestAccAzureRMCdnEndpoint_deliveryRule(t *testing.T) { }) } +func TestAccAzureRMCdnEndpoint_dnsAlias(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_cdn_endpoint", "test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + CheckDestroy: testCheckAzureRMCdnEndpointDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMCdnEndpoint_dnsAlias(data), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMCdnEndpointExists(data.ResourceName), + ), + }, + data.ImportStep(), + }, + }) +} + func testCheckAzureRMCdnEndpointExists(resourceName string) resource.TestCheckFunc { return func(s *terraform.State) error { conn := acceptance.AzureProvider.Meta().(*clients.Client).Cdn.EndpointsClient @@ -1109,3 +1128,50 @@ resource "azurerm_cdn_endpoint" "test" { } `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger) } + +func testAccAzureRMCdnEndpoint_dnsAlias(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_dns_zone" "test" { + name = "acctestcdnep%d.com" + resource_group_name = azurerm_resource_group.test.name +} + +resource "azurerm_cdn_profile" "test" { + name = "acctestcdnep%d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + sku = "Standard_Verizon" +} + +resource "azurerm_cdn_endpoint" "test" { + name = "acctestcdnep%d" + profile_name = azurerm_cdn_profile.test.name + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + + origin { + name = "acceptanceTestCdnOrigin1" + host_name = "www.contoso.com" + https_port = 443 + http_port = 80 + } +} + +resource "azurerm_dns_a_record" "test" { + name = "myarecord%d" + resource_group_name = azurerm_resource_group.test.name + zone_name = azurerm_dns_zone.test.name + ttl = 300 + target_resource_id = azurerm_cdn_endpoint.test.id +} +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger) +} From 8aa62988730babcc462900f8d64afdf70f9ff3b0 Mon Sep 17 00:00:00 2001 From: magodo Date: Tue, 25 Aug 2020 12:11:08 +0800 Subject: [PATCH 02/10] conform to the Formatter style --- .../services/cdn/cdn_endpoint_resource.go | 3 ++- .../services/cdn/cdn_profile_data_source.go | 10 +++++++++- .../services/cdn/cdn_profile_resource.go | 8 +++++++- .../services/cdn/parse/cdn_endpoint.go | 19 +++++++++++-------- .../services/cdn/parse/cdn_endpoint_test.go | 13 +++++++++++++ .../services/cdn/parse/cdn_profile.go | 13 ++++++++++++- .../services/cdn/parse/cdn_profile_test.go | 13 +++++++++++++ 7 files changed, 67 insertions(+), 12 deletions(-) diff --git a/azurerm/internal/services/cdn/cdn_endpoint_resource.go b/azurerm/internal/services/cdn/cdn_endpoint_resource.go index b01c9c765f46..7180a3df6ecf 100644 --- a/azurerm/internal/services/cdn/cdn_endpoint_resource.go +++ b/azurerm/internal/services/cdn/cdn_endpoint_resource.go @@ -205,6 +205,7 @@ func resourceArmCdnEndpoint() *schema.Resource { } func resourceArmCdnEndpointCreate(d *schema.ResourceData, meta interface{}) error { + subscriptionId := meta.(*clients.Client).Account.SubscriptionId endpointsClient := meta.(*clients.Client).Cdn.EndpointsClient profilesClient := meta.(*clients.Client).Cdn.ProfilesClient ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) @@ -308,7 +309,7 @@ func resourceArmCdnEndpointCreate(d *schema.ResourceData, meta interface{}) erro return err } - d.SetId(id.ID()) + d.SetId(id.ID(subscriptionId)) return resourceArmCdnEndpointRead(d, meta) } diff --git a/azurerm/internal/services/cdn/cdn_profile_data_source.go b/azurerm/internal/services/cdn/cdn_profile_data_source.go index 1607cf9ac854..cf239b1a730a 100644 --- a/azurerm/internal/services/cdn/cdn_profile_data_source.go +++ b/azurerm/internal/services/cdn/cdn_profile_data_source.go @@ -4,6 +4,8 @@ import ( "fmt" "time" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cdn/parse" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" @@ -41,6 +43,7 @@ func dataSourceArmCdnProfile() *schema.Resource { } func dataSourceArmCdnProfileRead(d *schema.ResourceData, meta interface{}) error { + subscriptionId := meta.(*clients.Client).Account.SubscriptionId client := meta.(*clients.Client).Cdn.ProfilesClient ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() @@ -56,7 +59,12 @@ func dataSourceArmCdnProfileRead(d *schema.ResourceData, meta interface{}) error return fmt.Errorf("Error making Read request on Azure CDN Profile %q (Resource Group %q): %+v", name, resourceGroup, err) } - d.SetId(*resp.ID) + id, err := parse.CdnProfileID(*resp.ID) + if err != nil { + return err + } + + d.SetId(id.ID(subscriptionId)) d.Set("name", name) d.Set("resource_group_name", resourceGroup) diff --git a/azurerm/internal/services/cdn/cdn_profile_resource.go b/azurerm/internal/services/cdn/cdn_profile_resource.go index 279608c4481b..d4c98dd1f994 100644 --- a/azurerm/internal/services/cdn/cdn_profile_resource.go +++ b/azurerm/internal/services/cdn/cdn_profile_resource.go @@ -70,6 +70,7 @@ func resourceArmCdnProfile() *schema.Resource { } func resourceArmCdnProfileCreate(d *schema.ResourceData, meta interface{}) error { + subscriptionId := meta.(*clients.Client).Account.SubscriptionId client := meta.(*clients.Client).Cdn.ProfilesClient ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) defer cancel() @@ -121,7 +122,12 @@ func resourceArmCdnProfileCreate(d *schema.ResourceData, meta interface{}) error return fmt.Errorf("Cannot read CDN Profile %s (resource group %s) ID", name, resGroup) } - d.SetId(*read.ID) + id, err := parse.CdnProfileID(*read.ID) + if err != nil { + return err + } + + d.SetId(id.ID(subscriptionId)) return resourceArmCdnProfileRead(d, meta) } diff --git a/azurerm/internal/services/cdn/parse/cdn_endpoint.go b/azurerm/internal/services/cdn/parse/cdn_endpoint.go index e77feec9e0eb..012a40c35d1d 100644 --- a/azurerm/internal/services/cdn/parse/cdn_endpoint.go +++ b/azurerm/internal/services/cdn/parse/cdn_endpoint.go @@ -7,13 +7,19 @@ import ( ) type CdnEndpointId struct { - Subscription string - Provider string ResourceGroup string ProfileName string Name string } +func NewCdnEndpointID(id CdnProfileId, name string) CdnEndpointId { + return CdnEndpointId{ + ResourceGroup: id.ResourceGroup, + ProfileName: id.Name, + Name: name, + } +} + func CdnEndpointID(input string) (*CdnEndpointId, error) { id, err := azure.ParseAzureResourceID(input) if err != nil { @@ -21,8 +27,6 @@ func CdnEndpointID(input string) (*CdnEndpointId, error) { } endpoint := CdnEndpointId{ - Subscription: id.SubscriptionID, - Provider: id.Provider, ResourceGroup: id.ResourceGroup, } @@ -41,8 +45,7 @@ func CdnEndpointID(input string) (*CdnEndpointId, error) { return &endpoint, nil } -// This ID is a workaround for issue: https://github.com/Azure/azure-rest-api-specs/issues/10576 -func (id CdnEndpointId) ID() string { - return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/%s/profiles/%s/endpoints/%s", - id.Subscription, id.ResourceGroup, id.Provider, id.ProfileName, id.Name) +func (id CdnEndpointId) ID(subscription string) string { + base := NewCdnProfileID(id.ResourceGroup, id.ProfileName).ID(subscription) + return fmt.Sprintf("%s/endpoints/%s", base, id.Name) } diff --git a/azurerm/internal/services/cdn/parse/cdn_endpoint_test.go b/azurerm/internal/services/cdn/parse/cdn_endpoint_test.go index 914cbc5a59ba..8147bcfdb57b 100644 --- a/azurerm/internal/services/cdn/parse/cdn_endpoint_test.go +++ b/azurerm/internal/services/cdn/parse/cdn_endpoint_test.go @@ -2,8 +2,21 @@ package parse import ( "testing" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/resourceid" ) +var _ resourceid.Formatter = CdnEndpointId{} + +func TestCdnEndpointIDFormatter(t *testing.T) { + subscriptionId := "12345678-1234-5678-1234-123456789012" + actual := NewCdnEndpointID(NewCdnProfileID("group1", "profile1"), "endpoint1").ID(subscriptionId) + expected := "/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Cdn/profiles/profile1/endpoints/endpoint1" + if actual != expected { + t.Fatalf("Expected %q but got %q", expected, actual) + } +} + func TestCdnEndpointId(t *testing.T) { testData := []struct { Name string diff --git a/azurerm/internal/services/cdn/parse/cdn_profile.go b/azurerm/internal/services/cdn/parse/cdn_profile.go index ea43900f94fe..f3c097f0c59d 100644 --- a/azurerm/internal/services/cdn/parse/cdn_profile.go +++ b/azurerm/internal/services/cdn/parse/cdn_profile.go @@ -9,7 +9,13 @@ import ( type CdnProfileId struct { ResourceGroup string Name string - ProfileName string +} + +func NewCdnProfileID(resourceGroup, name string) CdnProfileId { + return CdnProfileId{ + ResourceGroup: resourceGroup, + Name: name, + } } func CdnProfileID(input string) (*CdnProfileId, error) { @@ -32,3 +38,8 @@ func CdnProfileID(input string) (*CdnProfileId, error) { return &profile, nil } + +func (id CdnProfileId) ID(subscription string) string { + return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Cdn/profiles/%s", + subscription, id.ResourceGroup, id.Name) +} diff --git a/azurerm/internal/services/cdn/parse/cdn_profile_test.go b/azurerm/internal/services/cdn/parse/cdn_profile_test.go index cde57127dc8d..2112828f9456 100644 --- a/azurerm/internal/services/cdn/parse/cdn_profile_test.go +++ b/azurerm/internal/services/cdn/parse/cdn_profile_test.go @@ -2,8 +2,21 @@ package parse import ( "testing" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/resourceid" ) +var _ resourceid.Formatter = CdnProfileId{} + +func TestCdnProfileIDFormatter(t *testing.T) { + subscriptionId := "12345678-1234-5678-1234-123456789012" + actual := NewCdnProfileID("group1", "profile1").ID(subscriptionId) + expected := "/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Cdn/profiles/profile1" + if actual != expected { + t.Fatalf("Expected %q but got %q", expected, actual) + } +} + func TestCdnProfileId(t *testing.T) { testData := []struct { Name string From ab8a15947cf8e30013375301186b677ca8975f0b Mon Sep 17 00:00:00 2001 From: magodo Date: Tue, 25 Aug 2020 14:17:44 +0800 Subject: [PATCH 03/10] add migration logic to cdn profile/endpoint, both managed and data resources --- .../services/cdn/cdn_endpoint_resource.go | 11 ++++ .../services/cdn/cdn_profile_data_source.go | 11 ++++ .../services/cdn/cdn_profile_resource.go | 11 ++++ .../services/cdn/migration/cdn_endpoint.go | 41 ++++++++++++++ .../cdn/migration/cdn_endpoint_test.go | 55 +++++++++++++++++++ .../services/cdn/migration/cdn_profile.go | 37 +++++++++++++ .../cdn/migration/cdn_profile_test.go | 55 +++++++++++++++++++ 7 files changed, 221 insertions(+) create mode 100644 azurerm/internal/services/cdn/migration/cdn_endpoint.go create mode 100644 azurerm/internal/services/cdn/migration/cdn_endpoint_test.go create mode 100644 azurerm/internal/services/cdn/migration/cdn_profile.go create mode 100644 azurerm/internal/services/cdn/migration/cdn_profile_test.go diff --git a/azurerm/internal/services/cdn/cdn_endpoint_resource.go b/azurerm/internal/services/cdn/cdn_endpoint_resource.go index 7180a3df6ecf..be08519af25d 100644 --- a/azurerm/internal/services/cdn/cdn_endpoint_resource.go +++ b/azurerm/internal/services/cdn/cdn_endpoint_resource.go @@ -5,6 +5,8 @@ import ( "log" "time" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cdn/migration" + "github.com/Azure/azure-sdk-for-go/services/cdn/mgmt/2019-04-15/cdn" "github.com/hashicorp/go-azure-helpers/response" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" @@ -201,6 +203,15 @@ func resourceArmCdnEndpoint() *schema.Resource { "tags": tags.Schema(), }, + + SchemaVersion: 1, + StateUpgraders: []schema.StateUpgrader{ + { + Type: resourceArmCdnEndpoint().CoreConfigSchema().ImpliedType(), + Upgrade: migration.CdnEndpointV0ToV1, + Version: 0, + }, + }, } } diff --git a/azurerm/internal/services/cdn/cdn_profile_data_source.go b/azurerm/internal/services/cdn/cdn_profile_data_source.go index cf239b1a730a..260dea178f1c 100644 --- a/azurerm/internal/services/cdn/cdn_profile_data_source.go +++ b/azurerm/internal/services/cdn/cdn_profile_data_source.go @@ -4,6 +4,8 @@ import ( "fmt" "time" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cdn/migration" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cdn/parse" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" @@ -39,6 +41,15 @@ func dataSourceArmCdnProfile() *schema.Resource { "tags": tags.SchemaDataSource(), }, + + SchemaVersion: 1, + StateUpgraders: []schema.StateUpgrader{ + { + Type: resourceArmCdnProfile().CoreConfigSchema().ImpliedType(), + Upgrade: migration.CdnProfileV0ToV1, + Version: 0, + }, + }, } } diff --git a/azurerm/internal/services/cdn/cdn_profile_resource.go b/azurerm/internal/services/cdn/cdn_profile_resource.go index d4c98dd1f994..e17a12dc11b1 100644 --- a/azurerm/internal/services/cdn/cdn_profile_resource.go +++ b/azurerm/internal/services/cdn/cdn_profile_resource.go @@ -5,6 +5,8 @@ import ( "log" "time" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cdn/migration" + "github.com/Azure/azure-sdk-for-go/services/cdn/mgmt/2019-04-15/cdn" "github.com/hashicorp/go-azure-helpers/response" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" @@ -66,6 +68,15 @@ func resourceArmCdnProfile() *schema.Resource { "tags": tags.Schema(), }, + + SchemaVersion: 1, + StateUpgraders: []schema.StateUpgrader{ + { + Type: resourceArmCdnProfile().CoreConfigSchema().ImpliedType(), + Upgrade: migration.CdnProfileV0ToV1, + Version: 0, + }, + }, } } diff --git a/azurerm/internal/services/cdn/migration/cdn_endpoint.go b/azurerm/internal/services/cdn/migration/cdn_endpoint.go new file mode 100644 index 000000000000..2d0db55550a5 --- /dev/null +++ b/azurerm/internal/services/cdn/migration/cdn_endpoint.go @@ -0,0 +1,41 @@ +package migration + +import ( + "log" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cdn/parse" +) + +func CdnEndpointV0ToV1(rawState map[string]interface{}, _ interface{}) (map[string]interface{}, error) { + // old + // /subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.Cdn/profiles/{profileName}/endpoints/{endpointName} + // new: + // /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Cdn/profiles/{profileName}/endpoints/{endpointName} + // summary: + // resourcegroups -> resourceGroups + oldId := rawState["id"].(string) + oldParsedId, err := azure.ParseAzureResourceID(oldId) + if err != nil { + return rawState, err + } + + resourceGroup := oldParsedId.ResourceGroup + profileName, err := oldParsedId.PopSegment("profiles") + if err != nil { + return rawState, err + } + name, err := oldParsedId.PopSegment("endpoints") + if err != nil { + return rawState, err + } + + newId := parse.NewCdnEndpointID(parse.NewCdnProfileID(resourceGroup, profileName), name) + newIdStr := newId.ID(oldParsedId.SubscriptionID) + + log.Printf("[DEBUG] Updating ID from %q to %q", oldId, newIdStr) + + rawState["id"] = newIdStr + + return rawState, nil +} diff --git a/azurerm/internal/services/cdn/migration/cdn_endpoint_test.go b/azurerm/internal/services/cdn/migration/cdn_endpoint_test.go new file mode 100644 index 000000000000..7cc4128deaa0 --- /dev/null +++ b/azurerm/internal/services/cdn/migration/cdn_endpoint_test.go @@ -0,0 +1,55 @@ +package migration + +import ( + "testing" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func TestCdnEndpointV1ToV2(t *testing.T) { + testData := []struct { + name string + input map[string]interface{} + expected *string + }{ + { + name: "missing id", + input: map[string]interface{}{ + "id": "", + }, + expected: nil, + }, + { + name: "old id", + input: map[string]interface{}{ + "id": "/subscriptions/12345678-1234-5678-1234-123456789012/resourcegroups/group1/providers/Microsoft.Cdn/profiles/profile1/endpoints/endpoint1", + }, + expected: utils.String("/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Cdn/profiles/profile1/endpoints/endpoint1"), + }, + { + name: "new id", + input: map[string]interface{}{ + "id": "/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Cdn/profiles/profile1/endpoints/endpoint1", + }, + expected: utils.String("/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Cdn/profiles/profile1/endpoints/endpoint1"), + }, + } + for _, test := range testData { + t.Logf("Testing %q..", test.name) + result, err := CdnEndpointV0ToV1(test.input, nil) + if err != nil && test.expected == nil { + continue + } else { + if err == nil && test.expected == nil { + t.Fatalf("Expected an error but didn't get one") + } else if err != nil && test.expected != nil { + t.Fatalf("Expected no error but got: %+v", err) + } + } + + actualId := result["id"].(string) + if *test.expected != actualId { + t.Fatalf("expected %q but got %q!", *test.expected, actualId) + } + } +} diff --git a/azurerm/internal/services/cdn/migration/cdn_profile.go b/azurerm/internal/services/cdn/migration/cdn_profile.go new file mode 100644 index 000000000000..4283d081bc30 --- /dev/null +++ b/azurerm/internal/services/cdn/migration/cdn_profile.go @@ -0,0 +1,37 @@ +package migration + +import ( + "log" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cdn/parse" +) + +func CdnProfileV0ToV1(rawState map[string]interface{}, _ interface{}) (map[string]interface{}, error) { + // old + // /subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.Cdn/profiles/{profileName} + // new: + // /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Cdn/profiles/{profileName} + // summary: + // resourcegroups -> resourceGroups + oldId := rawState["id"].(string) + oldParsedId, err := azure.ParseAzureResourceID(oldId) + if err != nil { + return rawState, err + } + + resourceGroup := oldParsedId.ResourceGroup + name, err := oldParsedId.PopSegment("profiles") + if err != nil { + return rawState, err + } + + newId := parse.NewCdnProfileID(resourceGroup, name) + newIdStr := newId.ID(oldParsedId.SubscriptionID) + + log.Printf("[DEBUG] Updating ID from %q to %q", oldId, newIdStr) + + rawState["id"] = newIdStr + + return rawState, nil +} diff --git a/azurerm/internal/services/cdn/migration/cdn_profile_test.go b/azurerm/internal/services/cdn/migration/cdn_profile_test.go new file mode 100644 index 000000000000..d58e25e9bac9 --- /dev/null +++ b/azurerm/internal/services/cdn/migration/cdn_profile_test.go @@ -0,0 +1,55 @@ +package migration + +import ( + "testing" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func TestCdnProfileV1ToV2(t *testing.T) { + testData := []struct { + name string + input map[string]interface{} + expected *string + }{ + { + name: "missing id", + input: map[string]interface{}{ + "id": "", + }, + expected: nil, + }, + { + name: "old id", + input: map[string]interface{}{ + "id": "/subscriptions/12345678-1234-5678-1234-123456789012/resourcegroups/group1/providers/Microsoft.Cdn/profiles/profile1", + }, + expected: utils.String("/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Cdn/profiles/profile1"), + }, + { + name: "new id", + input: map[string]interface{}{ + "id": "/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Cdn/profiles/profile1", + }, + expected: utils.String("/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Cdn/profiles/profile1"), + }, + } + for _, test := range testData { + t.Logf("Testing %q..", test.name) + result, err := CdnProfileV0ToV1(test.input, nil) + if err != nil && test.expected == nil { + continue + } else { + if err == nil && test.expected == nil { + t.Fatalf("Expected an error but didn't get one") + } else if err != nil && test.expected != nil { + t.Fatalf("Expected no error but got: %+v", err) + } + } + + actualId := result["id"].(string) + if *test.expected != actualId { + t.Fatalf("expected %q but got %q!", *test.expected, actualId) + } + } +} From 99fab3d1efc60ca53a2c13a91e558d9958fa4b80 Mon Sep 17 00:00:00 2001 From: magodo Date: Tue, 25 Aug 2020 15:24:16 +0800 Subject: [PATCH 04/10] remove migration from data source and resolve a self reference --- .../services/cdn/cdn_endpoint_resource.go | 302 +++++++++--------- .../services/cdn/cdn_profile_data_source.go | 11 - .../services/cdn/cdn_profile_resource.go | 58 ++-- 3 files changed, 182 insertions(+), 189 deletions(-) diff --git a/azurerm/internal/services/cdn/cdn_endpoint_resource.go b/azurerm/internal/services/cdn/cdn_endpoint_resource.go index be08519af25d..1d66623c3bf9 100644 --- a/azurerm/internal/services/cdn/cdn_endpoint_resource.go +++ b/azurerm/internal/services/cdn/cdn_endpoint_resource.go @@ -23,191 +23,193 @@ import ( ) func resourceArmCdnEndpoint() *schema.Resource { - return &schema.Resource{ - Create: resourceArmCdnEndpointCreate, - Read: resourceArmCdnEndpointRead, - Update: resourceArmCdnEndpointUpdate, - Delete: resourceArmCdnEndpointDelete, - - Timeouts: &schema.ResourceTimeout{ - Create: schema.DefaultTimeout(30 * time.Minute), - Read: schema.DefaultTimeout(5 * time.Minute), - Update: schema.DefaultTimeout(30 * time.Minute), - Delete: schema.DefaultTimeout(30 * time.Minute), + schemaDef := map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, }, - Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { - _, err := parse.CdnEndpointID(id) - return err - }), - - Schema: map[string]*schema.Schema{ - "name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, - - "location": azure.SchemaLocation(), + "location": azure.SchemaLocation(), - "resource_group_name": azure.SchemaResourceGroupName(), + "resource_group_name": azure.SchemaResourceGroupName(), - "profile_name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, + "profile_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, - "origin_host_header": { - Type: schema.TypeString, - Optional: true, - }, + "origin_host_header": { + Type: schema.TypeString, + Optional: true, + }, - "is_http_allowed": { - Type: schema.TypeBool, - Optional: true, - Default: true, - }, + "is_http_allowed": { + Type: schema.TypeBool, + Optional: true, + Default: true, + }, - "is_https_allowed": { - Type: schema.TypeBool, - Optional: true, - Default: true, - }, + "is_https_allowed": { + Type: schema.TypeBool, + Optional: true, + Default: true, + }, - "origin": { - Type: schema.TypeSet, - Required: true, - ForceNew: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, + "origin": { + Type: schema.TypeSet, + Required: true, + ForceNew: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, - "host_name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, + "host_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, - "http_port": { - Type: schema.TypeInt, - Optional: true, - ForceNew: true, - Default: 80, - }, + "http_port": { + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + Default: 80, + }, - "https_port": { - Type: schema.TypeInt, - Optional: true, - ForceNew: true, - Default: 443, - }, + "https_port": { + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + Default: 443, }, }, }, + }, - "origin_path": { - Type: schema.TypeString, - Optional: true, - Computed: true, - }, + "origin_path": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, - "querystring_caching_behaviour": { - Type: schema.TypeString, - Optional: true, - Default: string(cdn.IgnoreQueryString), - ValidateFunc: validation.StringInSlice([]string{ - string(cdn.BypassCaching), - string(cdn.IgnoreQueryString), - string(cdn.NotSet), - string(cdn.UseQueryString), - }, false), - }, + "querystring_caching_behaviour": { + Type: schema.TypeString, + Optional: true, + Default: string(cdn.IgnoreQueryString), + ValidateFunc: validation.StringInSlice([]string{ + string(cdn.BypassCaching), + string(cdn.IgnoreQueryString), + string(cdn.NotSet), + string(cdn.UseQueryString), + }, false), + }, - "content_types_to_compress": { - Type: schema.TypeSet, - Optional: true, - Computed: true, - Elem: &schema.Schema{ - Type: schema.TypeString, - }, - Set: schema.HashString, + "content_types_to_compress": { + Type: schema.TypeSet, + Optional: true, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, }, + Set: schema.HashString, + }, - "is_compression_enabled": { - Type: schema.TypeBool, - Optional: true, - Default: false, - }, + "is_compression_enabled": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, - "probe_path": { - Type: schema.TypeString, - Optional: true, - Computed: true, - }, + "probe_path": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, - "geo_filter": { - Type: schema.TypeList, - Optional: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "relative_path": { - Type: schema.TypeString, - Required: true, - }, - "action": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validation.StringInSlice([]string{ - string(cdn.Allow), - string(cdn.Block), - }, true), - DiffSuppressFunc: suppress.CaseDifference, - }, - "country_codes": { - Type: schema.TypeList, - Required: true, - Elem: &schema.Schema{ - Type: schema.TypeString, - }, + "geo_filter": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "relative_path": { + Type: schema.TypeString, + Required: true, + }, + "action": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice([]string{ + string(cdn.Allow), + string(cdn.Block), + }, true), + DiffSuppressFunc: suppress.CaseDifference, + }, + "country_codes": { + Type: schema.TypeList, + Required: true, + Elem: &schema.Schema{ + Type: schema.TypeString, }, }, }, }, + }, - "optimization_type": { - Type: schema.TypeString, - Optional: true, - ValidateFunc: validation.StringInSlice([]string{ - string(cdn.DynamicSiteAcceleration), - string(cdn.GeneralMediaStreaming), - string(cdn.GeneralWebDelivery), - string(cdn.LargeFileDownload), - string(cdn.VideoOnDemandMediaStreaming), - }, true), - DiffSuppressFunc: suppress.CaseDifference, - }, + "optimization_type": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{ + string(cdn.DynamicSiteAcceleration), + string(cdn.GeneralMediaStreaming), + string(cdn.GeneralWebDelivery), + string(cdn.LargeFileDownload), + string(cdn.VideoOnDemandMediaStreaming), + }, true), + DiffSuppressFunc: suppress.CaseDifference, + }, - "host_name": { - Type: schema.TypeString, - Computed: true, - }, + "host_name": { + Type: schema.TypeString, + Computed: true, + }, + + "global_delivery_rule": endpointGlobalDeliveryRule(), - "global_delivery_rule": endpointGlobalDeliveryRule(), + "delivery_rule": endpointDeliveryRule(), - "delivery_rule": endpointDeliveryRule(), + "tags": tags.Schema(), + } - "tags": tags.Schema(), + return &schema.Resource{ + Create: resourceArmCdnEndpointCreate, + Read: resourceArmCdnEndpointRead, + Update: resourceArmCdnEndpointUpdate, + Delete: resourceArmCdnEndpointDelete, + + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(30 * time.Minute), + Read: schema.DefaultTimeout(5 * time.Minute), + Update: schema.DefaultTimeout(30 * time.Minute), + Delete: schema.DefaultTimeout(30 * time.Minute), }, + Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { + _, err := parse.CdnEndpointID(id) + return err + }), + + Schema: schemaDef, + SchemaVersion: 1, StateUpgraders: []schema.StateUpgrader{ { - Type: resourceArmCdnEndpoint().CoreConfigSchema().ImpliedType(), + Type: schema.Resource{Schema: schemaDef}.CoreConfigSchema().ImpliedType(), Upgrade: migration.CdnEndpointV0ToV1, Version: 0, }, diff --git a/azurerm/internal/services/cdn/cdn_profile_data_source.go b/azurerm/internal/services/cdn/cdn_profile_data_source.go index 260dea178f1c..cf239b1a730a 100644 --- a/azurerm/internal/services/cdn/cdn_profile_data_source.go +++ b/azurerm/internal/services/cdn/cdn_profile_data_source.go @@ -4,8 +4,6 @@ import ( "fmt" "time" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cdn/migration" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cdn/parse" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" @@ -41,15 +39,6 @@ func dataSourceArmCdnProfile() *schema.Resource { "tags": tags.SchemaDataSource(), }, - - SchemaVersion: 1, - StateUpgraders: []schema.StateUpgrader{ - { - Type: resourceArmCdnProfile().CoreConfigSchema().ImpliedType(), - Upgrade: migration.CdnProfileV0ToV1, - Version: 0, - }, - }, } } diff --git a/azurerm/internal/services/cdn/cdn_profile_resource.go b/azurerm/internal/services/cdn/cdn_profile_resource.go index e17a12dc11b1..3f2226d17f3e 100644 --- a/azurerm/internal/services/cdn/cdn_profile_resource.go +++ b/azurerm/internal/services/cdn/cdn_profile_resource.go @@ -23,6 +23,34 @@ import ( ) func resourceArmCdnProfile() *schema.Resource { + schemaDef := map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "location": azure.SchemaLocation(), + + "resource_group_name": azure.SchemaResourceGroupName(), + + "sku": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{ + string(cdn.StandardAkamai), + string(cdn.StandardChinaCdn), + string(cdn.StandardVerizon), + string(cdn.StandardMicrosoft), + string(cdn.PremiumVerizon), + }, true), + DiffSuppressFunc: suppress.CaseDifference, + }, + + "tags": tags.Schema(), + } + return &schema.Resource{ Create: resourceArmCdnProfileCreate, Read: resourceArmCdnProfileRead, @@ -41,38 +69,12 @@ func resourceArmCdnProfile() *schema.Resource { return err }), - Schema: map[string]*schema.Schema{ - "name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, - - "location": azure.SchemaLocation(), - - "resource_group_name": azure.SchemaResourceGroupName(), - - "sku": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - ValidateFunc: validation.StringInSlice([]string{ - string(cdn.StandardAkamai), - string(cdn.StandardChinaCdn), - string(cdn.StandardVerizon), - string(cdn.StandardMicrosoft), - string(cdn.PremiumVerizon), - }, true), - DiffSuppressFunc: suppress.CaseDifference, - }, - - "tags": tags.Schema(), - }, + Schema: schemaDef, SchemaVersion: 1, StateUpgraders: []schema.StateUpgrader{ { - Type: resourceArmCdnProfile().CoreConfigSchema().ImpliedType(), + Type: schema.Resource{Schema: schemaDef}.CoreConfigSchema().ImpliedType(), Upgrade: migration.CdnProfileV0ToV1, Version: 0, }, From 0fd7b9dfcef6cd82e5007c4313a30d001f79671e Mon Sep 17 00:00:00 2001 From: magodo Date: Tue, 25 Aug 2020 15:56:27 +0800 Subject: [PATCH 05/10] fix build error... --- .../services/cdn/cdn_endpoint_resource.go | 270 +++++++++--------- .../services/cdn/cdn_profile_resource.go | 54 ++-- 2 files changed, 164 insertions(+), 160 deletions(-) diff --git a/azurerm/internal/services/cdn/cdn_endpoint_resource.go b/azurerm/internal/services/cdn/cdn_endpoint_resource.go index 1d66623c3bf9..40d4107c05e6 100644 --- a/azurerm/internal/services/cdn/cdn_endpoint_resource.go +++ b/azurerm/internal/services/cdn/cdn_endpoint_resource.go @@ -23,167 +23,169 @@ import ( ) func resourceArmCdnEndpoint() *schema.Resource { - schemaDef := map[string]*schema.Schema{ - "name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, + tmpResource := &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, - "location": azure.SchemaLocation(), + "location": azure.SchemaLocation(), - "resource_group_name": azure.SchemaResourceGroupName(), + "resource_group_name": azure.SchemaResourceGroupName(), - "profile_name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, + "profile_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, - "origin_host_header": { - Type: schema.TypeString, - Optional: true, - }, + "origin_host_header": { + Type: schema.TypeString, + Optional: true, + }, - "is_http_allowed": { - Type: schema.TypeBool, - Optional: true, - Default: true, - }, + "is_http_allowed": { + Type: schema.TypeBool, + Optional: true, + Default: true, + }, - "is_https_allowed": { - Type: schema.TypeBool, - Optional: true, - Default: true, - }, + "is_https_allowed": { + Type: schema.TypeBool, + Optional: true, + Default: true, + }, - "origin": { - Type: schema.TypeSet, - Required: true, - ForceNew: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, + "origin": { + Type: schema.TypeSet, + Required: true, + ForceNew: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, - "host_name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, + "host_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, - "http_port": { - Type: schema.TypeInt, - Optional: true, - ForceNew: true, - Default: 80, - }, + "http_port": { + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + Default: 80, + }, - "https_port": { - Type: schema.TypeInt, - Optional: true, - ForceNew: true, - Default: 443, + "https_port": { + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + Default: 443, + }, }, }, }, - }, - "origin_path": { - Type: schema.TypeString, - Optional: true, - Computed: true, - }, + "origin_path": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, - "querystring_caching_behaviour": { - Type: schema.TypeString, - Optional: true, - Default: string(cdn.IgnoreQueryString), - ValidateFunc: validation.StringInSlice([]string{ - string(cdn.BypassCaching), - string(cdn.IgnoreQueryString), - string(cdn.NotSet), - string(cdn.UseQueryString), - }, false), - }, + "querystring_caching_behaviour": { + Type: schema.TypeString, + Optional: true, + Default: string(cdn.IgnoreQueryString), + ValidateFunc: validation.StringInSlice([]string{ + string(cdn.BypassCaching), + string(cdn.IgnoreQueryString), + string(cdn.NotSet), + string(cdn.UseQueryString), + }, false), + }, - "content_types_to_compress": { - Type: schema.TypeSet, - Optional: true, - Computed: true, - Elem: &schema.Schema{ - Type: schema.TypeString, + "content_types_to_compress": { + Type: schema.TypeSet, + Optional: true, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + Set: schema.HashString, }, - Set: schema.HashString, - }, - "is_compression_enabled": { - Type: schema.TypeBool, - Optional: true, - Default: false, - }, + "is_compression_enabled": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, - "probe_path": { - Type: schema.TypeString, - Optional: true, - Computed: true, - }, + "probe_path": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, - "geo_filter": { - Type: schema.TypeList, - Optional: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "relative_path": { - Type: schema.TypeString, - Required: true, - }, - "action": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validation.StringInSlice([]string{ - string(cdn.Allow), - string(cdn.Block), - }, true), - DiffSuppressFunc: suppress.CaseDifference, - }, - "country_codes": { - Type: schema.TypeList, - Required: true, - Elem: &schema.Schema{ - Type: schema.TypeString, + "geo_filter": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "relative_path": { + Type: schema.TypeString, + Required: true, + }, + "action": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice([]string{ + string(cdn.Allow), + string(cdn.Block), + }, true), + DiffSuppressFunc: suppress.CaseDifference, + }, + "country_codes": { + Type: schema.TypeList, + Required: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, }, }, }, }, - }, - "optimization_type": { - Type: schema.TypeString, - Optional: true, - ValidateFunc: validation.StringInSlice([]string{ - string(cdn.DynamicSiteAcceleration), - string(cdn.GeneralMediaStreaming), - string(cdn.GeneralWebDelivery), - string(cdn.LargeFileDownload), - string(cdn.VideoOnDemandMediaStreaming), - }, true), - DiffSuppressFunc: suppress.CaseDifference, - }, + "optimization_type": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{ + string(cdn.DynamicSiteAcceleration), + string(cdn.GeneralMediaStreaming), + string(cdn.GeneralWebDelivery), + string(cdn.LargeFileDownload), + string(cdn.VideoOnDemandMediaStreaming), + }, true), + DiffSuppressFunc: suppress.CaseDifference, + }, - "host_name": { - Type: schema.TypeString, - Computed: true, - }, + "host_name": { + Type: schema.TypeString, + Computed: true, + }, - "global_delivery_rule": endpointGlobalDeliveryRule(), + "global_delivery_rule": endpointGlobalDeliveryRule(), - "delivery_rule": endpointDeliveryRule(), + "delivery_rule": endpointDeliveryRule(), - "tags": tags.Schema(), + "tags": tags.Schema(), + }, } return &schema.Resource{ @@ -204,12 +206,12 @@ func resourceArmCdnEndpoint() *schema.Resource { return err }), - Schema: schemaDef, + Schema: tmpResource.Schema, SchemaVersion: 1, StateUpgraders: []schema.StateUpgrader{ { - Type: schema.Resource{Schema: schemaDef}.CoreConfigSchema().ImpliedType(), + Type: tmpResource.CoreConfigSchema().ImpliedType(), Upgrade: migration.CdnEndpointV0ToV1, Version: 0, }, diff --git a/azurerm/internal/services/cdn/cdn_profile_resource.go b/azurerm/internal/services/cdn/cdn_profile_resource.go index 3f2226d17f3e..cdaf62700b70 100644 --- a/azurerm/internal/services/cdn/cdn_profile_resource.go +++ b/azurerm/internal/services/cdn/cdn_profile_resource.go @@ -23,32 +23,34 @@ import ( ) func resourceArmCdnProfile() *schema.Resource { - schemaDef := map[string]*schema.Schema{ - "name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, + tmpResource := &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, - "location": azure.SchemaLocation(), - - "resource_group_name": azure.SchemaResourceGroupName(), - - "sku": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - ValidateFunc: validation.StringInSlice([]string{ - string(cdn.StandardAkamai), - string(cdn.StandardChinaCdn), - string(cdn.StandardVerizon), - string(cdn.StandardMicrosoft), - string(cdn.PremiumVerizon), - }, true), - DiffSuppressFunc: suppress.CaseDifference, - }, + "location": azure.SchemaLocation(), + + "resource_group_name": azure.SchemaResourceGroupName(), + + "sku": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{ + string(cdn.StandardAkamai), + string(cdn.StandardChinaCdn), + string(cdn.StandardVerizon), + string(cdn.StandardMicrosoft), + string(cdn.PremiumVerizon), + }, true), + DiffSuppressFunc: suppress.CaseDifference, + }, - "tags": tags.Schema(), + "tags": tags.Schema(), + }, } return &schema.Resource{ @@ -69,12 +71,12 @@ func resourceArmCdnProfile() *schema.Resource { return err }), - Schema: schemaDef, + Schema: tmpResource.Schema, SchemaVersion: 1, StateUpgraders: []schema.StateUpgrader{ { - Type: schema.Resource{Schema: schemaDef}.CoreConfigSchema().ImpliedType(), + Type: tmpResource.CoreConfigSchema().ImpliedType(), Upgrade: migration.CdnProfileV0ToV1, Version: 0, }, From 94505f1abcfd60ca6a6b60e3cd0a5b1283ed3dea Mon Sep 17 00:00:00 2001 From: magodo Date: Wed, 26 Aug 2020 21:32:25 +0800 Subject: [PATCH 06/10] PIT copy paste the v0 schema --- .../services/cdn/cdn_endpoint_resource.go | 43 +- .../services/cdn/migration/cdn_endpoint.go | 370 ++++++++++++++++++ 2 files changed, 390 insertions(+), 23 deletions(-) diff --git a/azurerm/internal/services/cdn/cdn_endpoint_resource.go b/azurerm/internal/services/cdn/cdn_endpoint_resource.go index 40d4107c05e6..2f6c6001a115 100644 --- a/azurerm/internal/services/cdn/cdn_endpoint_resource.go +++ b/azurerm/internal/services/cdn/cdn_endpoint_resource.go @@ -23,7 +23,25 @@ import ( ) func resourceArmCdnEndpoint() *schema.Resource { - tmpResource := &schema.Resource{ + + return &schema.Resource{ + Create: resourceArmCdnEndpointCreate, + Read: resourceArmCdnEndpointRead, + Update: resourceArmCdnEndpointUpdate, + Delete: resourceArmCdnEndpointDelete, + + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(30 * time.Minute), + Read: schema.DefaultTimeout(5 * time.Minute), + Update: schema.DefaultTimeout(30 * time.Minute), + Delete: schema.DefaultTimeout(30 * time.Minute), + }, + + Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { + _, err := parse.CdnEndpointID(id) + return err + }), + Schema: map[string]*schema.Schema{ "name": { Type: schema.TypeString, @@ -186,32 +204,11 @@ func resourceArmCdnEndpoint() *schema.Resource { "tags": tags.Schema(), }, - } - - return &schema.Resource{ - Create: resourceArmCdnEndpointCreate, - Read: resourceArmCdnEndpointRead, - Update: resourceArmCdnEndpointUpdate, - Delete: resourceArmCdnEndpointDelete, - - Timeouts: &schema.ResourceTimeout{ - Create: schema.DefaultTimeout(30 * time.Minute), - Read: schema.DefaultTimeout(5 * time.Minute), - Update: schema.DefaultTimeout(30 * time.Minute), - Delete: schema.DefaultTimeout(30 * time.Minute), - }, - - Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { - _, err := parse.CdnEndpointID(id) - return err - }), - - Schema: tmpResource.Schema, SchemaVersion: 1, StateUpgraders: []schema.StateUpgrader{ { - Type: tmpResource.CoreConfigSchema().ImpliedType(), + Type: migration.CdnEndpointV0Schema().CoreConfigSchema().ImpliedType(), Upgrade: migration.CdnEndpointV0ToV1, Version: 0, }, diff --git a/azurerm/internal/services/cdn/migration/cdn_endpoint.go b/azurerm/internal/services/cdn/migration/cdn_endpoint.go index 2d0db55550a5..8e9f0c6b11e9 100644 --- a/azurerm/internal/services/cdn/migration/cdn_endpoint.go +++ b/azurerm/internal/services/cdn/migration/cdn_endpoint.go @@ -3,10 +3,380 @@ package migration import ( "log" + "github.com/Azure/azure-sdk-for-go/services/cdn/mgmt/2019-04-15/cdn" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cdn/deliveryruleactions" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cdn/deliveryruleconditions" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cdn/parse" ) +func CdnEndpointV0Schema() *schema.Resource { + return &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "location": azure.SchemaLocation(), + + "resource_group_name": azure.SchemaResourceGroupName(), + + "profile_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "origin_host_header": { + Type: schema.TypeString, + Optional: true, + }, + + "is_http_allowed": { + Type: schema.TypeBool, + Optional: true, + Default: true, + }, + + "is_https_allowed": { + Type: schema.TypeBool, + Optional: true, + Default: true, + }, + + "origin": { + Type: schema.TypeSet, + Required: true, + ForceNew: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "host_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "http_port": { + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + Default: 80, + }, + + "https_port": { + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + Default: 443, + }, + }, + }, + }, + + "origin_path": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + + "querystring_caching_behaviour": { + Type: schema.TypeString, + Optional: true, + Default: string(cdn.IgnoreQueryString), + ValidateFunc: validation.StringInSlice([]string{ + string(cdn.BypassCaching), + string(cdn.IgnoreQueryString), + string(cdn.NotSet), + string(cdn.UseQueryString), + }, false), + }, + + "content_types_to_compress": { + Type: schema.TypeSet, + Optional: true, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + Set: schema.HashString, + }, + + "is_compression_enabled": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + + "probe_path": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + + "geo_filter": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "relative_path": { + Type: schema.TypeString, + Required: true, + }, + "action": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice([]string{ + string(cdn.Allow), + string(cdn.Block), + }, true), + DiffSuppressFunc: suppress.CaseDifference, + }, + "country_codes": { + Type: schema.TypeList, + Required: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + + "optimization_type": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{ + string(cdn.DynamicSiteAcceleration), + string(cdn.GeneralMediaStreaming), + string(cdn.GeneralWebDelivery), + string(cdn.LargeFileDownload), + string(cdn.VideoOnDemandMediaStreaming), + }, true), + DiffSuppressFunc: suppress.CaseDifference, + }, + + "host_name": { + Type: schema.TypeString, + Computed: true, + }, + + // This is a point-in-time copy paste of the return value of `endpointGlobalDeliveryRule()` used in V1 schema + "global_delivery_rule": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "cache_expiration_action": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: deliveryruleactions.CacheExpiration(), + }, + + "cache_key_query_string_action": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: deliveryruleactions.CacheKeyQueryString(), + }, + + "modify_request_header_action": { + Type: schema.TypeList, + Optional: true, + Elem: deliveryruleactions.ModifyRequestHeader(), + }, + + "modify_response_header_action": { + Type: schema.TypeList, + Optional: true, + Elem: deliveryruleactions.ModifyResponseHeader(), + }, + + "url_redirect_action": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: deliveryruleactions.URLRedirect(), + }, + + "url_rewrite_action": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: deliveryruleactions.URLRewrite(), + }, + }, + }, + }, + + // This is a point-in-time copy paste of the return value of `endpointDeliveryRule()` used in V1 schema + "delivery_rule": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validate.EndpointDeliveryRuleName(), + }, + + "order": { + Type: schema.TypeInt, + Required: true, + ValidateFunc: validation.IntAtLeast(1), + }, + + "cookies_condition": { + Type: schema.TypeList, + Optional: true, + Elem: deliveryruleconditions.Cookies(), + }, + + "http_version_condition": { + Type: schema.TypeList, + Optional: true, + Elem: deliveryruleconditions.HTTPVersion(), + }, + + "device_condition": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: deliveryruleconditions.Device(), + }, + + "post_arg_condition": { + Type: schema.TypeList, + Optional: true, + Elem: deliveryruleconditions.PostArg(), + }, + + "query_string_condition": { + Type: schema.TypeList, + Optional: true, + Elem: deliveryruleconditions.QueryString(), + }, + + "remote_address_condition": { + Type: schema.TypeList, + Optional: true, + Elem: deliveryruleconditions.RemoteAddress(), + }, + + "request_body_condition": { + Type: schema.TypeList, + Optional: true, + Elem: deliveryruleconditions.RequestBody(), + }, + + "request_header_condition": { + Type: schema.TypeList, + Optional: true, + Elem: deliveryruleconditions.RequestHeader(), + }, + + "request_method_condition": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: deliveryruleconditions.RequestMethod(), + }, + + "request_scheme_condition": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: deliveryruleconditions.RequestScheme(), + }, + + "request_uri_condition": { + Type: schema.TypeList, + Optional: true, + Elem: deliveryruleconditions.RequestURI(), + }, + + "url_file_extension_condition": { + Type: schema.TypeList, + Optional: true, + Elem: deliveryruleconditions.URLFileExtension(), + }, + + "url_file_name_condition": { + Type: schema.TypeList, + Optional: true, + Elem: deliveryruleconditions.URLFileName(), + }, + + "url_path_condition": { + Type: schema.TypeList, + Optional: true, + Elem: deliveryruleconditions.URLPath(), + }, + + "cache_expiration_action": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: deliveryruleactions.CacheExpiration(), + }, + + "cache_key_query_string_action": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: deliveryruleactions.CacheKeyQueryString(), + }, + + "modify_request_header_action": { + Type: schema.TypeList, + Optional: true, + Elem: deliveryruleactions.ModifyRequestHeader(), + }, + + "modify_response_header_action": { + Type: schema.TypeList, + Optional: true, + Elem: deliveryruleactions.ModifyResponseHeader(), + }, + + "url_redirect_action": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: deliveryruleactions.URLRedirect(), + }, + + "url_rewrite_action": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: deliveryruleactions.URLRewrite(), + }, + }, + }, + }, + + "tags": tags.Schema(), + }, + } +} + func CdnEndpointV0ToV1(rawState map[string]interface{}, _ interface{}) (map[string]interface{}, error) { // old // /subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.Cdn/profiles/{profileName}/endpoints/{endpointName} From 212911118fd31d6fab6943507205e5d4ddcd433c Mon Sep 17 00:00:00 2001 From: magodo Date: Wed, 26 Aug 2020 22:04:09 +0800 Subject: [PATCH 07/10] lint error --- azurerm/internal/services/cdn/cdn_endpoint_resource.go | 1 - 1 file changed, 1 deletion(-) diff --git a/azurerm/internal/services/cdn/cdn_endpoint_resource.go b/azurerm/internal/services/cdn/cdn_endpoint_resource.go index 2f6c6001a115..7fd678299c55 100644 --- a/azurerm/internal/services/cdn/cdn_endpoint_resource.go +++ b/azurerm/internal/services/cdn/cdn_endpoint_resource.go @@ -23,7 +23,6 @@ import ( ) func resourceArmCdnEndpoint() *schema.Resource { - return &schema.Resource{ Create: resourceArmCdnEndpointCreate, Read: resourceArmCdnEndpointRead, From cc631d1219e955b12a4877fe1201aa210b7375ad Mon Sep 17 00:00:00 2001 From: magodo Date: Thu, 27 Aug 2020 17:12:26 +0800 Subject: [PATCH 08/10] PIT C/P cdn profile schema & expand all function and remove validation in V0 schema --- .../services/cdn/cdn_profile_resource.go | 42 +- .../services/cdn/migration/cdn_endpoint.go | 738 ++++++++++++++++-- .../services/cdn/migration/cdn_profile.go | 45 ++ 3 files changed, 737 insertions(+), 88 deletions(-) diff --git a/azurerm/internal/services/cdn/cdn_profile_resource.go b/azurerm/internal/services/cdn/cdn_profile_resource.go index cdaf62700b70..703a26772202 100644 --- a/azurerm/internal/services/cdn/cdn_profile_resource.go +++ b/azurerm/internal/services/cdn/cdn_profile_resource.go @@ -23,7 +23,24 @@ import ( ) func resourceArmCdnProfile() *schema.Resource { - tmpResource := &schema.Resource{ + return &schema.Resource{ + Create: resourceArmCdnProfileCreate, + Read: resourceArmCdnProfileRead, + Update: resourceArmCdnProfileUpdate, + Delete: resourceArmCdnProfileDelete, + + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(30 * time.Minute), + Read: schema.DefaultTimeout(5 * time.Minute), + Update: schema.DefaultTimeout(30 * time.Minute), + Delete: schema.DefaultTimeout(30 * time.Minute), + }, + + Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { + _, err := parse.CdnProfileID(id) + return err + }), + Schema: map[string]*schema.Schema{ "name": { Type: schema.TypeString, @@ -51,32 +68,11 @@ func resourceArmCdnProfile() *schema.Resource { "tags": tags.Schema(), }, - } - - return &schema.Resource{ - Create: resourceArmCdnProfileCreate, - Read: resourceArmCdnProfileRead, - Update: resourceArmCdnProfileUpdate, - Delete: resourceArmCdnProfileDelete, - - Timeouts: &schema.ResourceTimeout{ - Create: schema.DefaultTimeout(30 * time.Minute), - Read: schema.DefaultTimeout(5 * time.Minute), - Update: schema.DefaultTimeout(30 * time.Minute), - Delete: schema.DefaultTimeout(30 * time.Minute), - }, - - Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { - _, err := parse.CdnProfileID(id) - return err - }), - - Schema: tmpResource.Schema, SchemaVersion: 1, StateUpgraders: []schema.StateUpgrader{ { - Type: tmpResource.CoreConfigSchema().ImpliedType(), + Type: migration.CdnProfileV0Schema().CoreConfigSchema().ImpliedType(), Upgrade: migration.CdnProfileV0ToV1, Version: 0, }, diff --git a/azurerm/internal/services/cdn/migration/cdn_endpoint.go b/azurerm/internal/services/cdn/migration/cdn_endpoint.go index 8e9f0c6b11e9..f705ae78a9ba 100644 --- a/azurerm/internal/services/cdn/migration/cdn_endpoint.go +++ b/azurerm/internal/services/cdn/migration/cdn_endpoint.go @@ -3,16 +3,12 @@ package migration import ( "log" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/location" + "github.com/Azure/azure-sdk-for-go/services/cdn/mgmt/2019-04-15/cdn" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" - "github.com/hashicorp/terraform-plugin-sdk/helper/validation" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cdn/deliveryruleactions" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cdn/deliveryruleconditions" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cdn/parse" ) @@ -25,9 +21,19 @@ func CdnEndpointV0Schema() *schema.Resource { ForceNew: true, }, - "location": azure.SchemaLocation(), + "location": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + StateFunc: location.StateFunc, + DiffSuppressFunc: location.DiffSuppressFunc, + }, - "resource_group_name": azure.SchemaResourceGroupName(), + "resource_group_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, "profile_name": { Type: schema.TypeString, @@ -97,12 +103,6 @@ func CdnEndpointV0Schema() *schema.Resource { Type: schema.TypeString, Optional: true, Default: string(cdn.IgnoreQueryString), - ValidateFunc: validation.StringInSlice([]string{ - string(cdn.BypassCaching), - string(cdn.IgnoreQueryString), - string(cdn.NotSet), - string(cdn.UseQueryString), - }, false), }, "content_types_to_compress": { @@ -137,12 +137,8 @@ func CdnEndpointV0Schema() *schema.Resource { Required: true, }, "action": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validation.StringInSlice([]string{ - string(cdn.Allow), - string(cdn.Block), - }, true), + Type: schema.TypeString, + Required: true, DiffSuppressFunc: suppress.CaseDifference, }, "country_codes": { @@ -157,15 +153,8 @@ func CdnEndpointV0Schema() *schema.Resource { }, "optimization_type": { - Type: schema.TypeString, - Optional: true, - ValidateFunc: validation.StringInSlice([]string{ - string(cdn.DynamicSiteAcceleration), - string(cdn.GeneralMediaStreaming), - string(cdn.GeneralWebDelivery), - string(cdn.LargeFileDownload), - string(cdn.VideoOnDemandMediaStreaming), - }, true), + Type: schema.TypeString, + Optional: true, DiffSuppressFunc: suppress.CaseDifference, }, @@ -174,7 +163,6 @@ func CdnEndpointV0Schema() *schema.Resource { Computed: true, }, - // This is a point-in-time copy paste of the return value of `endpointGlobalDeliveryRule()` used in V1 schema "global_delivery_rule": { Type: schema.TypeList, Optional: true, @@ -185,194 +173,814 @@ func CdnEndpointV0Schema() *schema.Resource { Type: schema.TypeList, Optional: true, MaxItems: 1, - Elem: deliveryruleactions.CacheExpiration(), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "behavior": { + Type: schema.TypeString, + Required: true, + }, + + "duration": { + Type: schema.TypeString, + Optional: true, + }, + }, + }, }, "cache_key_query_string_action": { Type: schema.TypeList, Optional: true, MaxItems: 1, - Elem: deliveryruleactions.CacheKeyQueryString(), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "behavior": { + Type: schema.TypeString, + Required: true, + }, + + "parameters": { + Type: schema.TypeString, + Optional: true, + }, + }, + }, }, "modify_request_header_action": { Type: schema.TypeList, Optional: true, - Elem: deliveryruleactions.ModifyRequestHeader(), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "action": { + Type: schema.TypeString, + Required: true, + }, + + "name": { + Type: schema.TypeString, + Required: true, + }, + + "value": { + Type: schema.TypeString, + Optional: true, + }, + }, + }, }, "modify_response_header_action": { Type: schema.TypeList, Optional: true, - Elem: deliveryruleactions.ModifyResponseHeader(), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "action": { + Type: schema.TypeString, + Required: true, + }, + + "name": { + Type: schema.TypeString, + Required: true, + }, + + "value": { + Type: schema.TypeString, + Optional: true, + }, + }, + }, }, "url_redirect_action": { Type: schema.TypeList, Optional: true, MaxItems: 1, - Elem: deliveryruleactions.URLRedirect(), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "redirect_type": { + Type: schema.TypeString, + Required: true, + }, + + "protocol": { + Type: schema.TypeString, + Optional: true, + Default: string(cdn.MatchRequest), + }, + + "hostname": { + Type: schema.TypeString, + Optional: true, + }, + + "path": { + Type: schema.TypeString, + Optional: true, + }, + + "query_string": { + Type: schema.TypeString, + Optional: true, + }, + + "fragment": { + Type: schema.TypeString, + Optional: true, + }, + }, + }, }, "url_rewrite_action": { Type: schema.TypeList, Optional: true, MaxItems: 1, - Elem: deliveryruleactions.URLRewrite(), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "source_pattern": { + Type: schema.TypeString, + Required: true, + }, + + "destination": { + Type: schema.TypeString, + Required: true, + }, + + "preserve_unmatched_path": { + Type: schema.TypeBool, + Optional: true, + Default: true, + }, + }, + }, }, }, }, }, - // This is a point-in-time copy paste of the return value of `endpointDeliveryRule()` used in V1 schema "delivery_rule": { Type: schema.TypeList, Optional: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "name": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validate.EndpointDeliveryRuleName(), + Type: schema.TypeString, + Required: true, }, "order": { - Type: schema.TypeInt, - Required: true, - ValidateFunc: validation.IntAtLeast(1), + Type: schema.TypeInt, + Required: true, }, "cookies_condition": { Type: schema.TypeList, Optional: true, - Elem: deliveryruleconditions.Cookies(), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "selector": { + Type: schema.TypeString, + Required: true, + }, + + "operator": { + Type: schema.TypeString, + Required: true, + }, + + "negate_condition": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + + "match_values": { + Type: schema.TypeSet, + Required: true, + MinItems: 1, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + + "transforms": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, }, "http_version_condition": { Type: schema.TypeList, Optional: true, - Elem: deliveryruleconditions.HTTPVersion(), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "operator": { + Type: schema.TypeString, + Optional: true, + Default: "Equal", + }, + + "negate_condition": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + + "match_values": { + Type: schema.TypeSet, + Required: true, + MinItems: 1, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, }, "device_condition": { Type: schema.TypeList, Optional: true, MaxItems: 1, - Elem: deliveryruleconditions.Device(), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "operator": { + Type: schema.TypeString, + Optional: true, + Default: "Equal", + }, + + "negate_condition": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + + "match_values": { + Type: schema.TypeSet, + Required: true, + MinItems: 1, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, }, "post_arg_condition": { Type: schema.TypeList, Optional: true, - Elem: deliveryruleconditions.PostArg(), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "selector": { + Type: schema.TypeString, + Required: true, + }, + + "operator": { + Type: schema.TypeString, + Required: true, + }, + + "negate_condition": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + + "match_values": { + Type: schema.TypeSet, + Required: true, + MinItems: 1, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + + "transforms": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, }, "query_string_condition": { Type: schema.TypeList, Optional: true, - Elem: deliveryruleconditions.QueryString(), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "operator": { + Type: schema.TypeString, + Required: true, + }, + + "negate_condition": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + + "match_values": { + Type: schema.TypeSet, + Required: true, + MinItems: 1, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + + "transforms": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, }, "remote_address_condition": { Type: schema.TypeList, Optional: true, - Elem: deliveryruleconditions.RemoteAddress(), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "operator": { + Type: schema.TypeString, + Required: true, + }, + + "negate_condition": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + + "match_values": { + Type: schema.TypeSet, + Required: true, + MinItems: 1, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, }, "request_body_condition": { Type: schema.TypeList, Optional: true, - Elem: deliveryruleconditions.RequestBody(), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "operator": { + Type: schema.TypeString, + Required: true, + }, + + "negate_condition": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + + "match_values": { + Type: schema.TypeSet, + Required: true, + MinItems: 1, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + + "transforms": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, }, "request_header_condition": { Type: schema.TypeList, Optional: true, - Elem: deliveryruleconditions.RequestHeader(), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "selector": { + Type: schema.TypeString, + Required: true, + }, + + "operator": { + Type: schema.TypeString, + Required: true, + }, + + "negate_condition": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + + "match_values": { + Type: schema.TypeSet, + Required: true, + MinItems: 1, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + + "transforms": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, }, "request_method_condition": { Type: schema.TypeList, Optional: true, MaxItems: 1, - Elem: deliveryruleconditions.RequestMethod(), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "operator": { + Type: schema.TypeString, + Optional: true, + Default: "Equal", + }, + + "negate_condition": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + + "match_values": { + Type: schema.TypeSet, + Required: true, + MinItems: 1, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, }, "request_scheme_condition": { Type: schema.TypeList, Optional: true, MaxItems: 1, - Elem: deliveryruleconditions.RequestScheme(), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "operator": { + Type: schema.TypeString, + Optional: true, + Default: "Equal", + }, + + "negate_condition": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + + "match_values": { + Type: schema.TypeSet, + Required: true, + MinItems: 1, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, }, "request_uri_condition": { Type: schema.TypeList, Optional: true, - Elem: deliveryruleconditions.RequestURI(), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "operator": { + Type: schema.TypeString, + Required: true, + }, + + "negate_condition": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + + "match_values": { + Type: schema.TypeSet, + Required: true, + MinItems: 1, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + + "transforms": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, }, "url_file_extension_condition": { Type: schema.TypeList, Optional: true, - Elem: deliveryruleconditions.URLFileExtension(), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "operator": { + Type: schema.TypeString, + Required: true, + }, + + "negate_condition": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + + "match_values": { + Type: schema.TypeSet, + Required: true, + MinItems: 1, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + + "transforms": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, }, "url_file_name_condition": { Type: schema.TypeList, Optional: true, - Elem: deliveryruleconditions.URLFileName(), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "operator": { + Type: schema.TypeString, + Required: true, + }, + + "negate_condition": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + + "match_values": { + Type: schema.TypeSet, + Required: true, + MinItems: 1, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + + "transforms": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, }, "url_path_condition": { Type: schema.TypeList, Optional: true, - Elem: deliveryruleconditions.URLPath(), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "operator": { + Type: schema.TypeString, + Required: true, + }, + + "negate_condition": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + + "match_values": { + Type: schema.TypeSet, + Required: true, + MinItems: 1, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + + "transforms": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, }, "cache_expiration_action": { Type: schema.TypeList, Optional: true, MaxItems: 1, - Elem: deliveryruleactions.CacheExpiration(), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "behavior": { + Type: schema.TypeString, + Required: true, + }, + + "duration": { + Type: schema.TypeString, + Optional: true, + }, + }, + }, }, "cache_key_query_string_action": { Type: schema.TypeList, Optional: true, MaxItems: 1, - Elem: deliveryruleactions.CacheKeyQueryString(), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "behavior": { + Type: schema.TypeString, + Required: true, + }, + + "parameters": { + Type: schema.TypeString, + Optional: true, + }, + }, + }, }, "modify_request_header_action": { Type: schema.TypeList, Optional: true, - Elem: deliveryruleactions.ModifyRequestHeader(), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "action": { + Type: schema.TypeString, + Required: true, + }, + + "name": { + Type: schema.TypeString, + Required: true, + }, + + "value": { + Type: schema.TypeString, + Optional: true, + }, + }, + }, }, "modify_response_header_action": { Type: schema.TypeList, Optional: true, - Elem: deliveryruleactions.ModifyResponseHeader(), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "action": { + Type: schema.TypeString, + Required: true, + }, + + "name": { + Type: schema.TypeString, + Required: true, + }, + + "value": { + Type: schema.TypeString, + Optional: true, + }, + }, + }, }, "url_redirect_action": { Type: schema.TypeList, Optional: true, MaxItems: 1, - Elem: deliveryruleactions.URLRedirect(), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "redirect_type": { + Type: schema.TypeString, + Required: true, + }, + + "protocol": { + Type: schema.TypeString, + Optional: true, + Default: string(cdn.MatchRequest), + }, + + "hostname": { + Type: schema.TypeString, + Optional: true, + }, + + "path": { + Type: schema.TypeString, + Optional: true, + }, + + "query_string": { + Type: schema.TypeString, + Optional: true, + }, + + "fragment": { + Type: schema.TypeString, + Optional: true, + }, + }, + }, }, "url_rewrite_action": { Type: schema.TypeList, Optional: true, MaxItems: 1, - Elem: deliveryruleactions.URLRewrite(), + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "source_pattern": { + Type: schema.TypeString, + Required: true, + }, + + "destination": { + Type: schema.TypeString, + Required: true, + }, + + "preserve_unmatched_path": { + Type: schema.TypeBool, + Optional: true, + Default: true, + }, + }, + }, }, }, }, }, - "tags": tags.Schema(), + "tags": { + Type: schema.TypeMap, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, }, } } diff --git a/azurerm/internal/services/cdn/migration/cdn_profile.go b/azurerm/internal/services/cdn/migration/cdn_profile.go index 4283d081bc30..58d208968551 100644 --- a/azurerm/internal/services/cdn/migration/cdn_profile.go +++ b/azurerm/internal/services/cdn/migration/cdn_profile.go @@ -3,10 +3,55 @@ package migration import ( "log" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/location" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cdn/parse" ) +func CdnProfileV0Schema() *schema.Resource { + return &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "location": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + StateFunc: location.StateFunc, + DiffSuppressFunc: location.DiffSuppressFunc, + }, + + "resource_group_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "sku": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + DiffSuppressFunc: suppress.CaseDifference, + }, + + "tags": { + Type: schema.TypeMap, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + } +} + func CdnProfileV0ToV1(rawState map[string]interface{}, _ interface{}) (map[string]interface{}, error) { // old // /subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.Cdn/profiles/{profileName} From 5ddc3d5f4f52f5390678617d0ab89ec553b606dd Mon Sep 17 00:00:00 2001 From: magodo Date: Mon, 31 Aug 2020 09:47:05 +0800 Subject: [PATCH 09/10] rename param for `subscriptionId` to be consistent --- azurerm/internal/services/cdn/parse/cdn_endpoint.go | 2 +- azurerm/internal/services/cdn/parse/cdn_profile.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/azurerm/internal/services/cdn/parse/cdn_endpoint.go b/azurerm/internal/services/cdn/parse/cdn_endpoint.go index 012a40c35d1d..490491cbba8d 100644 --- a/azurerm/internal/services/cdn/parse/cdn_endpoint.go +++ b/azurerm/internal/services/cdn/parse/cdn_endpoint.go @@ -45,7 +45,7 @@ func CdnEndpointID(input string) (*CdnEndpointId, error) { return &endpoint, nil } -func (id CdnEndpointId) ID(subscription string) string { +func (id CdnEndpointId) ID(subscriptionId string) string { base := NewCdnProfileID(id.ResourceGroup, id.ProfileName).ID(subscription) return fmt.Sprintf("%s/endpoints/%s", base, id.Name) } diff --git a/azurerm/internal/services/cdn/parse/cdn_profile.go b/azurerm/internal/services/cdn/parse/cdn_profile.go index f3c097f0c59d..f844c60f3d0d 100644 --- a/azurerm/internal/services/cdn/parse/cdn_profile.go +++ b/azurerm/internal/services/cdn/parse/cdn_profile.go @@ -39,7 +39,7 @@ func CdnProfileID(input string) (*CdnProfileId, error) { return &profile, nil } -func (id CdnProfileId) ID(subscription string) string { +func (id CdnProfileId) ID(subscriptionId string) string { return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Cdn/profiles/%s", subscription, id.ResourceGroup, id.Name) } From dff6dac8dcc32039a451b92d1818fcf11f14b595 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Mon, 31 Aug 2020 13:37:46 +0200 Subject: [PATCH 10/10] fixing the build --- azurerm/internal/services/cdn/parse/cdn_endpoint.go | 2 +- azurerm/internal/services/cdn/parse/cdn_profile.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/azurerm/internal/services/cdn/parse/cdn_endpoint.go b/azurerm/internal/services/cdn/parse/cdn_endpoint.go index 490491cbba8d..225066302df9 100644 --- a/azurerm/internal/services/cdn/parse/cdn_endpoint.go +++ b/azurerm/internal/services/cdn/parse/cdn_endpoint.go @@ -46,6 +46,6 @@ func CdnEndpointID(input string) (*CdnEndpointId, error) { } func (id CdnEndpointId) ID(subscriptionId string) string { - base := NewCdnProfileID(id.ResourceGroup, id.ProfileName).ID(subscription) + base := NewCdnProfileID(id.ResourceGroup, id.ProfileName).ID(subscriptionId) return fmt.Sprintf("%s/endpoints/%s", base, id.Name) } diff --git a/azurerm/internal/services/cdn/parse/cdn_profile.go b/azurerm/internal/services/cdn/parse/cdn_profile.go index f844c60f3d0d..479a57e1e9cf 100644 --- a/azurerm/internal/services/cdn/parse/cdn_profile.go +++ b/azurerm/internal/services/cdn/parse/cdn_profile.go @@ -41,5 +41,5 @@ func CdnProfileID(input string) (*CdnProfileId, error) { func (id CdnProfileId) ID(subscriptionId string) string { return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Cdn/profiles/%s", - subscription, id.ResourceGroup, id.Name) + subscriptionId, id.ResourceGroup, id.Name) }