Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_api_management - deprecate and remove policy block in 4.0 #26305

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
networkValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/network/validate"
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
}
}
Expand Down Expand Up @@ -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)
}
}
}
}
Expand Down Expand Up @@ -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))
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
Loading