如何使用Bicep配置“所有API”策略?

huangapple go评论65阅读模式
英文:

How to configure "All APIs" policy using Bicep?

问题

I configured APIM in Bicep like this:

resource apiManagement 'Microsoft.ApiManagement/service@2019-12-01' = {
  name: appName
  location: location
  sku: {
    name: 'Developer'
    capacity: 1
  }
  identity: {
    type: 'SystemAssigned'
  }
  properties: {
    publisherEmail: publisherEmail
    publisherName: publisherName
  }
  resource policy 'policies@2021-01-01-preview' = {
    name: 'allApis'
    properties: {
      value: allOperationsPolicy
      format: 'xml'
    }
  }
}

And I can create 'api-read' API with some policy using syntax like this:

resource apiRead 'Microsoft.ApiManagement/service/apis@2021-01-01-preview' = {
  parent: apiManagement
  name: 'api-read'
  properties: {
    displayName: 'API Read'
    subscriptionRequired: true
    path: 'read'
    protocols: [
      'https'
    ]
    isCurrent: true
    authenticationSettings: {
      oAuth2: {
        authorizationServerId: authServer.name
      }
    }
  }
  resource policyAllOperationsRead 'policies@2021-01-01-preview' = {
    name: 'policy'
    properties: {
      value: myPolicyHere
      format: 'xml'
    }
  }
}

But how can I add policy to All API's (global policy) using Bicep, to have it here?

I have tried like this:

resource apiManagement 'Microsoft.ApiManagement/service@2019-12-01' = {
  name: appName
  location: location
  sku: {
    name: 'Developer'
    capacity: 1
  }
  identity: {
    type: 'SystemAssigned'
  }
  properties: {
    publisherEmail: publisherEmail
    publisherName: publisherName
  }
}

resource policy 'Microsoft.ApiManagement/service/apis/policies@2021-01-01-preview' = {
  parent: apiManagement
  name: 'allApis'
  properties: {
    value: allOperationsPolicy
    format: 'xml'
  }
}

But I got an error: "The property 'parent' expected a value of type 'Microsoft.ApiManagement/service/apis' but the provided value is of type 'Microsoft.ApiManagement/service@2019-12-01'."

英文:

I configured APIM in Bicep like this:

resource apiManagement 'Microsoft.ApiManagement/service@2019-12-01' = {
  name: appName
  location: location
  sku: {
    name: 'Developer'
    capacity: 1
  }
  identity: {
    type: 'SystemAssigned'
  }
  properties: {
    publisherEmail: publisherEmail
    publisherName: publisherName
  }
  resource policy 'policies@2021-01-01-preview' = {
    name: 'allApis'
    properties: {
      value: allOperationsPolicy
      format: 'xml'
    }
  }
}

And I can create 'api-read' API with some policy using syntax like this:

resource apiRead 'Microsoft.ApiManagement/service/apis@2021-01-01-preview' = {
  parent: apiManagement
  name: 'api-read'
  properties: {
    displayName: 'API Read'
    subscriptionRequired: true
    path: 'read'
    protocols: [
      'https'
    ]
    isCurrent: true
    authenticationSettings: {
      oAuth2: {
        authorizationServerId: authServer.name
      }
    }
  }
  resource policyAllOperationsRead 'policies@2021-01-01-preview' = {
    name: 'policy'
    properties: {
      value: myPolicyHere
      format: 'xml'
    }
  }
}

But how can I add policy to All API's (global policy) using Bicep, to have it here?

如何使用Bicep配置“所有API”策略?

I have tried like this:

resource apiManagement 'Microsoft.ApiManagement/service@2019-12-01' = {
  name: appName
  location: location
  sku: {
    name: 'Developer'
    capacity: 1
  }
  identity: {
    type: 'SystemAssigned'
  }
  properties: {
    publisherEmail: publisherEmail
    publisherName: publisherName
  }
  
}

resource policy 'Microsoft.ApiManagement/service/apis/policies@2021-01-01-preview' = {
  parent: apiManagement
  name: 'allApis'
  properties: {
    value: allOperationsPolicy
    format: 'xml'
  }
}

But I got error:
> The property "parent" expected a value of type "Microsoft.ApiManagement/service/apis" but the provided value is of type "Microsoft.ApiManagement/service@2019-12-01"

答案1

得分: 1

以下是翻译好的部分:

在我尝试之后,我能够这样做:

    resource apiManagement 'Microsoft.ApiManagement/service@2019-12-01' = {
      name: appName
      location: location
      sku: {
        name: 'Developer'
        capacity: 1
      }
      identity: {
        type: 'SystemAssigned'
      }
      properties: {
        publisherEmail: publisherEmail
        publisherName: publisherName
      }
      resource policy 'policies@2021-01-01-preview' = {
        name: 'policy'
        properties: {
          value: allOperationsPolicy
          format: 'xml'
        }
      }
    }

请注意,name 只能是 "policy"
英文:

After all I was able to do it like that:

resource apiManagement 'Microsoft.ApiManagement/service@2019-12-01' = {
  name: appName
  location: location
  sku: {
    name: 'Developer'
    capacity: 1
  }
  identity: {
    type: 'SystemAssigned'
  }
  properties: {
    publisherEmail: publisherEmail
    publisherName: publisherName
  }
  resource policy 'policies@2021-01-01-preview' = {
    name: 'policy'
    properties: {
      value: allOperationsPolicy
      format: 'xml'
    }
  }
}

Note that name can only be "policy"

答案2

得分: 0

查看文档,可以像这样为所有 API 添加策略:

resource policy 'Microsoft.ApiManagement/service/policies@2021-01-01-preview' = {
  parent: apiManagement
  name: 'allApis'
  properties: {
    value: allOperationsPolicy
    format: 'xml'
  }
}
英文:

Looking at the documentation, policies can be added for all apis like that:

resource policy 'Microsoft.ApiManagement/service/policies@2021-01-01-preview' = {
  parent: apiManagement
  name: 'allApis'
  properties: {
    value: allOperationsPolicy
    format: 'xml'
  }
}

huangapple
  • 本文由 发表于 2023年3月7日 21:33:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定