From 95b3b2297de38503a2b8e32a0b2050cd03a8a22e Mon Sep 17 00:00:00 2001 From: Steph Date: Wed, 12 Jun 2024 10:38:20 +0200 Subject: [PATCH] deprecate and remove block in 4.0 --- .../apimanagement/api_management_resource.go | 137 ++++++++++-------- .../api_management_resource_test.go | 4 + website/docs/r/api_management.html.markdown | 10 -- 3 files changed, 78 insertions(+), 73 deletions(-) diff --git a/internal/services/apimanagement/api_management_resource.go b/internal/services/apimanagement/api_management_resource.go index 33a51077a207..061cd1d7c188 100644 --- a/internal/services/apimanagement/api_management_resource.go +++ b/internal/services/apimanagement/api_management_resource.go @@ -33,6 +33,7 @@ import ( "github.com/hashicorp/terraform-provider-azurerm/helpers/tf" "github.com/hashicorp/terraform-provider-azurerm/helpers/validate" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" + "github.com/hashicorp/terraform-provider-azurerm/internal/features" "github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/schemaz" apimValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/validate" networkValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/network/validate" @@ -100,7 +101,7 @@ func resourceApiManagementService() *pluginsdk.Resource { } func resourceApiManagementSchema() map[string]*pluginsdk.Schema { - return map[string]*pluginsdk.Schema{ + schema := map[string]*pluginsdk.Schema{ "name": schemaz.SchemaApiManagementName(), "resource_group_name": commonschema.ResourceGroupName(), @@ -463,32 +464,6 @@ func resourceApiManagementSchema() map[string]*pluginsdk.Schema { }, }, - // lintignore:XS003 - "policy": { - Type: pluginsdk.TypeList, - Optional: true, - Computed: true, - MaxItems: 1, - ConfigMode: pluginsdk.SchemaConfigModeAttr, - Elem: &pluginsdk.Resource{ - Schema: map[string]*pluginsdk.Schema{ - "xml_content": { - Type: pluginsdk.TypeString, - Optional: true, - Computed: true, - ConflictsWith: []string{"policy.0.xml_link"}, - DiffSuppressFunc: XmlWithDotNetInterpolationsDiffSuppress, - }, - - "xml_link": { - Type: pluginsdk.TypeString, - Optional: true, - ConflictsWith: []string{"policy.0.xml_content"}, - }, - }, - }, - }, - "sign_in": { Type: pluginsdk.TypeList, Optional: true, @@ -664,6 +639,36 @@ func resourceApiManagementSchema() map[string]*pluginsdk.Schema { "tags": commonschema.Tags(), } + + if !features.FourPointOhBeta() { + schema["policy"] = &pluginsdk.Schema{ + Type: pluginsdk.TypeList, + Optional: true, + Computed: true, + MaxItems: 1, + ConfigMode: pluginsdk.SchemaConfigModeAttr, + Deprecated: "The `policy` block has been superseded by the resource `azurerm_api_management_policy` and will be removed in v4.0 of the AzureRM Provider", + Elem: &pluginsdk.Resource{ + Schema: map[string]*pluginsdk.Schema{ + "xml_content": { + Type: pluginsdk.TypeString, + Optional: true, + Computed: true, + ConflictsWith: []string{"policy.0.xml_link"}, + DiffSuppressFunc: XmlWithDotNetInterpolationsDiffSuppress, + }, + + "xml_link": { + Type: pluginsdk.TypeString, + Optional: true, + ConflictsWith: []string{"policy.0.xml_content"}, + }, + }, + }, + } + } + + return schema } func resourceApiManagementServiceCreate(d *pluginsdk.ResourceData, meta interface{}) error { @@ -938,26 +943,28 @@ func resourceApiManagementServiceCreate(d *pluginsdk.ResourceData, meta interfac } } - policyClient := meta.(*clients.Client).ApiManagement.PolicyClient - policiesRaw := d.Get("policy").([]interface{}) - policyContract, err := expandApiManagementPolicies(policiesRaw) - if err != nil { - return err - } + if !features.FourPointOhBeta() { + policyClient := meta.(*clients.Client).ApiManagement.PolicyClient + policiesRaw := d.Get("policy").([]interface{}) + policyContract, err := expandApiManagementPolicies(policiesRaw) + if err != nil { + return err + } - if _, ok := d.GetOk("policy"); ok { - policyServiceId := policy.NewServiceID(subscriptionId, id.ResourceGroupName, id.ServiceName) - // remove the existing policy - if delResp, err := policyClient.Delete(ctx, policyServiceId, policy.DeleteOperationOptions{}); err != nil { - if !response.WasNotFound(delResp.HttpResponse) { - return fmt.Errorf("removing Policies from %s: %+v", id, err) + if _, ok := d.GetOk("policy"); ok { + policyServiceId := policy.NewServiceID(subscriptionId, id.ResourceGroupName, id.ServiceName) + // remove the existing policy + if delResp, err := policyClient.Delete(ctx, policyServiceId, policy.DeleteOperationOptions{}); err != nil { + if !response.WasNotFound(delResp.HttpResponse) { + return fmt.Errorf("removing Policies from %s: %+v", id, err) + } } - } - // then add the new one, if it exists - if policyContract != nil { - if _, err := policyClient.CreateOrUpdate(ctx, policyServiceId, *policyContract, policy.CreateOrUpdateOperationOptions{}); err != nil { - return fmt.Errorf(" setting Policies for %s: %+v", id, err) + // then add the new one, if it exists + if policyContract != nil { + if _, err := policyClient.CreateOrUpdate(ctx, policyServiceId, *policyContract, policy.CreateOrUpdateOperationOptions{}); err != nil { + return fmt.Errorf(" setting Policies for %s: %+v", id, err) + } } } } @@ -1187,26 +1194,28 @@ func resourceApiManagementServiceUpdate(d *pluginsdk.ResourceData, meta interfac } } - if d.HasChange("policy") { - policyClient := meta.(*clients.Client).ApiManagement.PolicyClient - policiesRaw := d.Get("policy").([]interface{}) - policyContract, err := expandApiManagementPolicies(policiesRaw) - if err != nil { - return err - } + if !features.FourPointOhBeta() { + if d.HasChange("policy") { + policyClient := meta.(*clients.Client).ApiManagement.PolicyClient + policiesRaw := d.Get("policy").([]interface{}) + policyContract, err := expandApiManagementPolicies(policiesRaw) + if err != nil { + return err + } - policyServiceId := policy.NewServiceID(subscriptionId, id.ResourceGroupName, id.ServiceName) - // remove the existing policy - if delResp, err := policyClient.Delete(ctx, policyServiceId, policy.DeleteOperationOptions{}); err != nil { - if !response.WasNotFound(delResp.HttpResponse) { - return fmt.Errorf("removing Policies from %s: %+v", id, err) + policyServiceId := policy.NewServiceID(subscriptionId, id.ResourceGroupName, id.ServiceName) + // remove the existing policy + if delResp, err := policyClient.Delete(ctx, policyServiceId, policy.DeleteOperationOptions{}); err != nil { + if !response.WasNotFound(delResp.HttpResponse) { + return fmt.Errorf("removing Policies from %s: %+v", id, err) + } } - } - // then add the new one, if it exists - if policyContract != nil { - if _, err := policyClient.CreateOrUpdate(ctx, policyServiceId, *policyContract, policy.CreateOrUpdateOperationOptions{}); err != nil { - return fmt.Errorf(" setting Policies for %s: %+v", id, err) + // then add the new one, if it exists + if policyContract != nil { + if _, err := policyClient.CreateOrUpdate(ctx, policyServiceId, *policyContract, policy.CreateOrUpdateOperationOptions{}); err != nil { + return fmt.Errorf(" setting Policies for %s: %+v", id, err) + } } } } @@ -1335,8 +1344,10 @@ func resourceApiManagementServiceRead(d *pluginsdk.ResourceData, meta interface{ return fmt.Errorf("setting `sku_name`: %+v", err) } - if err := d.Set("policy", flattenApiManagementPolicies(d, policy.Model)); err != nil { - return fmt.Errorf("setting `policy`: %+v", err) + if !features.FourPointOhBeta() { + if err := d.Set("policy", flattenApiManagementPolicies(d, policy.Model)); err != nil { + return fmt.Errorf("setting `policy`: %+v", err) + } } d.Set("zones", zones.FlattenUntyped(model.Zones)) diff --git a/internal/services/apimanagement/api_management_resource_test.go b/internal/services/apimanagement/api_management_resource_test.go index 5f22f69e7ac6..8c1810f0cf82 100644 --- a/internal/services/apimanagement/api_management_resource_test.go +++ b/internal/services/apimanagement/api_management_resource_test.go @@ -18,6 +18,7 @@ import ( "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/testclient" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" + "github.com/hashicorp/terraform-provider-azurerm/internal/features" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" ) @@ -250,6 +251,9 @@ func TestAccApiManagement_delegationSettings(t *testing.T) { } func TestAccApiManagement_policy(t *testing.T) { + if features.FourPointOhBeta() { + t.Skip("Skipping since `policy` has been deprecated and removed in 4.0") + } data := acceptance.BuildTestData(t, "azurerm_api_management", "test") r := ApiManagementResource{} diff --git a/website/docs/r/api_management.html.markdown b/website/docs/r/api_management.html.markdown index 0a17990cdce5..3028d8fdb7d7 100644 --- a/website/docs/r/api_management.html.markdown +++ b/website/docs/r/api_management.html.markdown @@ -81,8 +81,6 @@ The following arguments are supported: * `notification_sender_email` - (Optional) Email address from which the notification will be sent. -* `policy` - (Optional) A `policy` block as defined below. - * `protocols` - (Optional) A `protocols` block as defined below. * `security` - (Optional) A `security` block as defined below. @@ -197,14 +195,6 @@ A `management`, `portal`, `developer_portal` and `scm` block supports the follow --- -A `policy` block supports the following: - -* `xml_content` - (Optional) The XML Content for this Policy. - -* `xml_link` - (Optional) A link to an API Management Policy XML Document, which must be publicly available. - ---- - A `proxy` block supports the following: * `default_ssl_binding` - (Optional) Is the certificate associated with this Hostname the Default SSL Certificate? This is used when an SNI header isn't specified by a client. Defaults to `false`.