Skip to content

Commit

Permalink
deprecate and remove block in 4.0 (#26305)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephybun authored Jun 12, 2024
1 parent 75666bd commit 14a1f4c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 73 deletions.
137 changes: 74 additions & 63 deletions internal/services/apimanagement/api_management_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand Down Expand Up @@ -99,7 +100,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(),
Expand Down Expand Up @@ -462,32 +463,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,
Expand Down Expand Up @@ -663,6 +638,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 {
Expand Down Expand Up @@ -937,26 +942,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)
}
}
}
}
Expand Down Expand Up @@ -1186,26 +1193,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)
}
}
}
}
Expand Down Expand Up @@ -1334,8 +1343,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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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{}

Expand Down
10 changes: 0 additions & 10 deletions website/docs/r/api_management.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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`.
Expand Down

0 comments on commit 14a1f4c

Please sign in to comment.