Skip to content

Commit

Permalink
Add entry for ApiManagement.Policy to default resources state (#3688)
Browse files Browse the repository at this point in the history
Resolves #1729.

There's only one global API Management Policy, which can be updated but not created.

Tested locally with the following program, slightly modified from the
user's example in #1729.
- `pulumi up` without this change fails with
`azure-native:apimanagement:Policy (policy): error: cannot create
already existing subresource
'/subscriptions/123/resourceGroups/rg-test-apim7a7ad090/providers/Microsoft.ApiManagement/service/apimtest122f07ff/policies/policy'`
  -  `pulumi up` and `pulumi down` succeed with this change.

Here's a program to create the parent apiManagementService then test
creating and destroying a Policy. When creating the Policy, the provider
will do a GET to check for existance first which can be used to see the
initial state.

```csharp
using Pulumi;
using Resources = Pulumi.AzureNative.Resources;
using ApiManagement = Pulumi.AzureNative.ApiManagement;

return await Deployment.RunAsync(() =>
{
    // Create an Azure Resource Group
    var resourceGroup = new Resources.ResourceGroup("rg-test-apim");

    // Create an API Management Service
    var apiManagementService = new ApiManagement.ApiManagementService(
        "apimtest",
        new ApiManagement.ApiManagementServiceArgs
        {
            Location = resourceGroup.Location,
            PublisherEmail = "[email protected]",
            PublisherName = "Publisher",
            ResourceGroupName = resourceGroup.Name,
            Sku = new ApiManagement.Inputs.ApiManagementServiceSkuPropertiesArgs
            {
                Capacity = 0,
                Name = "Consumption",
            },
        });

    // Update the contents of the global policy.
    var policy = new ApiManagement.Policy(
        "policy",
        new ApiManagement.PolicyArgs
        {
            Format = "xml",
            ResourceGroupName = resourceGroup.Name,
            ServiceName = apiManagementService.Name,
            Value = "<!-- Custom Test Policy -->\r\n<policies>\r\n  <inbound>\r\n  </inbound>\r\n  <backend>\r\n    <forward-request />\r\n  </backend>\r\n  <outbound />\r\n  <on-error />\r\n</policies>",
        }
    );
});
```

Note that [in the earliest preview API version, the property names are
different](https://github.com/Azure/azure-rest-api-specs/blob/0410d404c68289cb1737d06bba92133bb84b515c/specification/apimanagement/resource-manager/Microsoft.ApiManagement/preview/2018-06-01-preview/definitions.json#L3097),
hence two entries in the map.
  • Loading branch information
thomas11 authored Nov 7, 2024
1 parent 6cc6318 commit 169f2e2
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions provider/pkg/openapi/defaults/defaultResourcesState.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,34 @@ var defaultResourcesStateRaw = map[string]map[string]interface{}{
"/{scope}/providers/Microsoft.Resources/tags/default": {
"properties": map[string]string{},
},
// https://github.com/pulumi/pulumi-azure-native/issues/1729
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/policies/{policyId}": {
"format": "xml",
"value": "<!--\r\n IMPORTANT:\r\n - Policy elements can appear only within the <inbound>, <outbound>, <backend> section elements.\r\n - Only the <forward-request> policy element can appear within the <backend> section element.\r\n - To apply a policy to the incoming request (before it is forwarded to the backend service), place a corresponding policy element within the <inbound> section element.\r\n - To apply a policy to the outgoing response (before it is sent back to the caller), place a corresponding policy element within the <outbound> section element.\r\n - To add a policy position the cursor at the desired insertion point and click on the round button associated with the policy.\r\n - To remove a policy, delete the corresponding policy statement from the policy document.\r\n - Policies are applied in the order of their appearance, from the top down.\r\n-->\r\n<policies>\r\n\t<inbound></inbound>\r\n\t<backend>\r\n\t\t<forward-request />\r\n\t</backend>\r\n\t<outbound></outbound>\r\n</policies>",
},
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/policies/{policyId}?version=2018-06-01-preview": {
"contentFormat": "xml",
"policyContent": "",
},
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.AppPlatform/Spring/{serviceName}/monitoringSettings/default": {
"properties": map[string]interface{}{
"traceEnabled": false,
"appInsightsInstrumentationKey": nil,
"appInsightsSamplingRate": 10.0,
},
},
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.AppPlatform/Spring/{serviceName}/configServers/default": {
"properties": map[string]interface{}{
"configServer": nil,
},
},
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.AppPlatform/Spring/{serviceName}/buildServices/{buildServiceName}/agentPools/{agentPoolName}": {
"properties": map[string]interface{}{
"poolSize": map[string]interface{}{
"name": "S1",
},
},
},
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DBForMariaDB/servers/{serverName}/configurations/{configurationName}": {
"source": "system-default",
},
Expand Down Expand Up @@ -149,25 +177,6 @@ var defaultResourcesStateRaw = map[string]map[string]interface{}{
"swiftSupported": true,
},
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{name}/sourcecontrols/web": {},
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.AppPlatform/Spring/{serviceName}/monitoringSettings/default": {
"properties": map[string]interface{}{
"traceEnabled": false,
"appInsightsInstrumentationKey": nil,
"appInsightsSamplingRate": 10.0,
},
},
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.AppPlatform/Spring/{serviceName}/configServers/default": {
"properties": map[string]interface{}{
"configServer": nil,
},
},
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.AppPlatform/Spring/{serviceName}/buildServices/{buildServiceName}/agentPools/{agentPoolName}": {
"properties": map[string]interface{}{
"poolSize": map[string]interface{}{
"name": "S1",
},
},
},
// https://learn.microsoft.com/en-us/azure/templates/microsoft.web/sites/basicpublishingcredentialspolicies-ftp?pivots=deployment-language-arm-template#csmpublishingcredentialspoliciesentityproperties-1
// We set the default value to true, as this is the observed behavior when the resource is created.
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{name}/basicPublishingCredentialsPolicies/ftp": {
Expand Down

0 comments on commit 169f2e2

Please sign in to comment.