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

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

How to configure "All APIs" policy using Bicep?

问题

I configured APIM in Bicep like this:

  1. resource apiManagement 'Microsoft.ApiManagement/service@2019-12-01' = {
  2. name: appName
  3. location: location
  4. sku: {
  5. name: 'Developer'
  6. capacity: 1
  7. }
  8. identity: {
  9. type: 'SystemAssigned'
  10. }
  11. properties: {
  12. publisherEmail: publisherEmail
  13. publisherName: publisherName
  14. }
  15. resource policy 'policies@2021-01-01-preview' = {
  16. name: 'allApis'
  17. properties: {
  18. value: allOperationsPolicy
  19. format: 'xml'
  20. }
  21. }
  22. }

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

  1. resource apiRead 'Microsoft.ApiManagement/service/apis@2021-01-01-preview' = {
  2. parent: apiManagement
  3. name: 'api-read'
  4. properties: {
  5. displayName: 'API Read'
  6. subscriptionRequired: true
  7. path: 'read'
  8. protocols: [
  9. 'https'
  10. ]
  11. isCurrent: true
  12. authenticationSettings: {
  13. oAuth2: {
  14. authorizationServerId: authServer.name
  15. }
  16. }
  17. }
  18. resource policyAllOperationsRead 'policies@2021-01-01-preview' = {
  19. name: 'policy'
  20. properties: {
  21. value: myPolicyHere
  22. format: 'xml'
  23. }
  24. }
  25. }

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

I have tried like this:

  1. resource apiManagement 'Microsoft.ApiManagement/service@2019-12-01' = {
  2. name: appName
  3. location: location
  4. sku: {
  5. name: 'Developer'
  6. capacity: 1
  7. }
  8. identity: {
  9. type: 'SystemAssigned'
  10. }
  11. properties: {
  12. publisherEmail: publisherEmail
  13. publisherName: publisherName
  14. }
  15. }
  16. resource policy 'Microsoft.ApiManagement/service/apis/policies@2021-01-01-preview' = {
  17. parent: apiManagement
  18. name: 'allApis'
  19. properties: {
  20. value: allOperationsPolicy
  21. format: 'xml'
  22. }
  23. }

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:

  1. resource apiManagement 'Microsoft.ApiManagement/service@2019-12-01' = {
  2. name: appName
  3. location: location
  4. sku: {
  5. name: 'Developer'
  6. capacity: 1
  7. }
  8. identity: {
  9. type: 'SystemAssigned'
  10. }
  11. properties: {
  12. publisherEmail: publisherEmail
  13. publisherName: publisherName
  14. }
  15. resource policy 'policies@2021-01-01-preview' = {
  16. name: 'allApis'
  17. properties: {
  18. value: allOperationsPolicy
  19. format: 'xml'
  20. }
  21. }
  22. }

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

  1. resource apiRead 'Microsoft.ApiManagement/service/apis@2021-01-01-preview' = {
  2. parent: apiManagement
  3. name: 'api-read'
  4. properties: {
  5. displayName: 'API Read'
  6. subscriptionRequired: true
  7. path: 'read'
  8. protocols: [
  9. 'https'
  10. ]
  11. isCurrent: true
  12. authenticationSettings: {
  13. oAuth2: {
  14. authorizationServerId: authServer.name
  15. }
  16. }
  17. }
  18. resource policyAllOperationsRead 'policies@2021-01-01-preview' = {
  19. name: 'policy'
  20. properties: {
  21. value: myPolicyHere
  22. format: 'xml'
  23. }
  24. }
  25. }

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:

  1. resource apiManagement 'Microsoft.ApiManagement/service@2019-12-01' = {
  2. name: appName
  3. location: location
  4. sku: {
  5. name: 'Developer'
  6. capacity: 1
  7. }
  8. identity: {
  9. type: 'SystemAssigned'
  10. }
  11. properties: {
  12. publisherEmail: publisherEmail
  13. publisherName: publisherName
  14. }
  15. }
  16. resource policy 'Microsoft.ApiManagement/service/apis/policies@2021-01-01-preview' = {
  17. parent: apiManagement
  18. name: 'allApis'
  19. properties: {
  20. value: allOperationsPolicy
  21. format: 'xml'
  22. }
  23. }

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

以下是翻译好的部分:

  1. 在我尝试之后,我能够这样做:
  2. resource apiManagement 'Microsoft.ApiManagement/service@2019-12-01' = {
  3. name: appName
  4. location: location
  5. sku: {
  6. name: 'Developer'
  7. capacity: 1
  8. }
  9. identity: {
  10. type: 'SystemAssigned'
  11. }
  12. properties: {
  13. publisherEmail: publisherEmail
  14. publisherName: publisherName
  15. }
  16. resource policy 'policies@2021-01-01-preview' = {
  17. name: 'policy'
  18. properties: {
  19. value: allOperationsPolicy
  20. format: 'xml'
  21. }
  22. }
  23. }
  24. 请注意,name 只能是 "policy"
英文:

After all I was able to do it like that:

  1. resource apiManagement 'Microsoft.ApiManagement/service@2019-12-01' = {
  2. name: appName
  3. location: location
  4. sku: {
  5. name: 'Developer'
  6. capacity: 1
  7. }
  8. identity: {
  9. type: 'SystemAssigned'
  10. }
  11. properties: {
  12. publisherEmail: publisherEmail
  13. publisherName: publisherName
  14. }
  15. resource policy 'policies@2021-01-01-preview' = {
  16. name: 'policy'
  17. properties: {
  18. value: allOperationsPolicy
  19. format: 'xml'
  20. }
  21. }
  22. }

Note that name can only be "policy"

答案2

得分: 0

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

  1. resource policy 'Microsoft.ApiManagement/service/policies@2021-01-01-preview' = {
  2. parent: apiManagement
  3. name: 'allApis'
  4. properties: {
  5. value: allOperationsPolicy
  6. format: 'xml'
  7. }
  8. }
英文:

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

  1. resource policy 'Microsoft.ApiManagement/service/policies@2021-01-01-preview' = {
  2. parent: apiManagement
  3. name: 'allApis'
  4. properties: {
  5. value: allOperationsPolicy
  6. format: 'xml'
  7. }
  8. }

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:

确定