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

Use the correct ID for api_management_policy #15060

Merged
merged 6 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 2.94.0 (Unreleased)

UPGRADE NOTES:
* `azurerm_api_management_policy` - resources that were created with v2.92.0 will be marked as tainted due to a [bug](https://github.com/hashicorp/terraform-provider-azurerm/issues/15042). This version addresses the underlying issue, but the actual resource needs to either be untainted (via `terraform untaint`) or allow terraform to delete the resource and create it again.
koikonom marked this conversation as resolved.
Show resolved Hide resolved

FEATURES:

**New Data Source:** `azurerm_linux_function_app` [GH-15009]
Expand All @@ -11,6 +14,7 @@ ENHANCEMENTS:

BUG FIXES:

* `azurerm_api_management_policy` - use the correct ID for api_management_policy [GH-15060]
koikonom marked this conversation as resolved.
Show resolved Hide resolved
* `azurerm_bastion_host` - Fix crash by adding nil check for `copy_paste_enabled` [GH-15074]
* `azurerm_dev_test_lab` - fix the unexpected diff on `key_vault_id` [GH-15054]
* `azurerm_subscription_cost_management_export` - fix the update method by sending the ETag when updating a cost management export [GH-15017]
Expand Down
16 changes: 12 additions & 4 deletions internal/services/apimanagement/api_management_policy_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2021-08-01/apimanagement"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/migration"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
Expand All @@ -24,6 +25,11 @@ func resourceApiManagementPolicy() *pluginsdk.Resource {
// TODO: replace this with an importer which validates the ID during import
Importer: pluginsdk.DefaultImporter(),

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

Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Expand Down Expand Up @@ -64,12 +70,12 @@ func resourceApiManagementPolicyCreateUpdate(d *pluginsdk.ResourceData, meta int
defer cancel()

apiManagementID := d.Get("api_management_id").(string)
id, err := parse.ApiManagementID(apiManagementID)
apiMgmtId, err := parse.ApiManagementID(apiManagementID)
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
serviceName := id.ServiceName
resourceGroup := apiMgmtId.ResourceGroup
serviceName := apiMgmtId.ServiceName

/*
Other resources would have a check for d.IsNewResource() at this location, and would error out using `tf.ImportAsExistsError` if the resource already existed.
Expand Down Expand Up @@ -105,10 +111,12 @@ func resourceApiManagementPolicyCreateUpdate(d *pluginsdk.ResourceData, meta int
return fmt.Errorf("Either `xml_content` or `xml_link` must be set")
}

if _, err := client.CreateOrUpdate(ctx, resourceGroup, serviceName, parameters, ""); err != nil {
policyResp, err := client.CreateOrUpdate(ctx, resourceGroup, serviceName, parameters, "")
if err != nil {
return fmt.Errorf("creating or updating Policy (Resource Group %q / API Management Service %q): %+v", resourceGroup, serviceName, err)
}

id := parse.NewPolicyID(apiMgmtId.SubscriptionId, apiMgmtId.ResourceGroup, apiMgmtId.ServiceName, utils.NormalizeNilableString(policyResp.Name))
koikonom marked this conversation as resolved.
Show resolved Hide resolved
d.SetId(id.ID())

return resourceApiManagementPolicyRead(d, meta)
Expand Down
61 changes: 61 additions & 0 deletions internal/services/apimanagement/migration/policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package migration

import (
"context"
"fmt"
"html"

"github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2021-08-01/apimanagement"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

var _ pluginsdk.StateUpgrade = ApiManagementApiPolicyV0ToV1{}

type ApiManagementApiPolicyV0ToV1 struct{}

func (ApiManagementApiPolicyV0ToV1) UpgradeFunc() pluginsdk.StateUpgraderFunc {
return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
apiMgmtId, err := parse.ApiManagementID(rawState["id"].(string))
if err != nil {
return rawState, nil
}
id := parse.NewPolicyID(apiMgmtId.SubscriptionId, apiMgmtId.ResourceGroup, apiMgmtId.ServiceName, "policy")
rawState["id"] = id.ID()

client := meta.(*clients.Client).ApiManagement.PolicyClient
resp, err := client.Get(ctx, id.ResourceGroup, id.ServiceName, apimanagement.PolicyExportFormatXML)
if err != nil {
return nil, fmt.Errorf("making Read request for API Management Policy (Resource Group %q / API Management Service %q / API %q): %+v", id.ResourceGroup, id.ServiceName, id.Name, err)
}

if properties := resp.PolicyContractProperties; properties != nil {
// when you submit an `xml_link` to the API, the API downloads this link and stores it as `xml_content`
// as such there is no way to set `xml_link` and we'll let Terraform handle it
rawState["xml_content"] = html.UnescapeString(*properties.Value)
}
return rawState, nil
}
}

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

"xml_content": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
},

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