Azure APIM -通过Terraform部署架构

jqjz2hbq  于 2023-03-19  发布在  其他
关注(0)|答案(1)|浏览(157)

我希望通过Terraform将架构部署到Azure APIM。以下是基于TF Documentation的TF代码和SO上的其他示例,例如question

resource "azurerm_api_management_api_schema" "api_schema" {
  api_name            = var.api.name
  api_management_name = var.apim_name
  resource_group_name = var.resource_group_name
  schema_id           = "schema01"
  content_type        = "application/vnd.oai.openapi.components+json"
  value               = file("schema.json")
}

下面是schema.json文件的内容

{
  "properties": {
    "contentType": "application/vnd.oai.openapi.components+json",
    "document": {
      "components": {
        "schemas": {
          "Thing": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "nullable": true
              }
            },
            "additionalProperties": false
          },
          "ThingArray": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Thing"
            }
          }
        }
      }
    }
  }
}

当我运行Terraform时,没有错误。但是,门户中也没有可见的定义:

如果我使用Azure API来获取架构,我会得到以下响应-它的值似乎是字符串而非纯JSON:

{
    "id": "***REDACTED***",
    "type": "Microsoft.ApiManagement/service/apis/schemas",
    "name": "schema01",
    "properties": {
        "contentType": "application/vnd.oai.openapi.components+json",
        "document": {
            "value": "{\r\n  \"properties\": {\r\n    \"contentType\": \"application\/vnd.oai.openapi.components+json\",\r\n    \"document\": {\r\n      \"components\": {\r\n        \"schemas\": {\r\n          \"Thing\": {\r\n            \"type\": \"object\",\r\n            \"properties\": {\r\n              \"id\": {\r\n                \"type\": \"string\",\r\n                \"nullable\": true\r\n              }\r\n            },\r\n            \"additionalProperties\": false\r\n          },\r\n          \"ThingArray\": {\r\n            \"type\": \"array\",\r\n            \"items\": {\r\n              \"$ref\": \"#\/components\/schemas\/Thing\"\r\n            }\r\n          }\r\n        }\r\n      }\r\n    }\r\n  }\r\n}"
        }
    }
}

如果我使用API通过PUT方法更新模式,并使用相同的schema.json作为有效负载,那么值看起来是正确的(都是纯JSON):

{
    "id": "***REDACTED***",
    "type": "Microsoft.ApiManagement/service/apis/schemas",
    "name": "schema01",
    "properties": {
        "contentType": "application/vnd.oai.openapi.components+json",
        "document": {
            "components": {
                "schemas": {
                    "Thing": {
                        "type": "object",
                        "properties": {
                            "id": {
                                "type": "string",
                                "nullable": true
                            }
                        },
                        "additionalProperties": false
                    },
                    "ThingArray": {
                        "type": "array",
                        "items": {
                            "$ref": "#/components/schemas/Thing"
                        }
                    }
                }
            }
        }
    }
}

...定义将显示在门户中:

有人知道我哪里做错了吗?
谢谢

yjghlzjz

yjghlzjz1#

我运行了以下terraform脚本,但定义在Azure APIM资源中的Azure门户中不可见。

terraform {

required_providers {

azurerm =  {

source = "hashicorp/azurerm"

version = "3.8.0"

}

}

}

  

provider  "azurerm" {

subscription_id =  "<subscription-id>"

tenant_id =  "<tenant-id>"

client_id =  "<client-id>"

client_secret =  "<client-secret>"

features {}

}

  

data  "azurerm_resource_group"  "rg" {

name =  "siliconrg098"

}

  
  

data  "azurerm_api_management"  "apim_service" {

name =  "siliconapim"

resource_group_name = data.azurerm_resource_group.rg.name

}

  
  

# Configure the Microsoft Azure Provider

# provider "azurerm" {

# features {}

# }

  

resource  "azurerm_api_management_api"  "sample-api" {

name =  "Test-2"

resource_group_name = data.azurerm_resource_group.rg.name

api_management_name = data.azurerm_api_management.apim_service.name

revision =  "1"

display_name =  "API-1"

path =  "API"

protocols =  ["https", "http"]

description =  "example"

}

  
  

resource  "azurerm_api_management_api_schema"  "example" {

api_name =  azurerm_api_management_api.sample-api.name

api_management_name =  azurerm_api_management_api.sample-api.api_management_name

resource_group_name =  azurerm_api_management_api.sample-api.resource_group_name

schema_id =  "example-schema"

content_type =  "application/vnd.oai.openapi.components+json"

value =  <<JSON

{

{

"properties": {

"contentType": "application/vnd.oai.openapi.components+json",

"document": {

"components": {

"schemas": {

"Thing": {

"type": "object",

"properties": {

"id": {

"type": "string",

"nullable": true

}

},

"additionalProperties": false

},

"ThingArray": {

"type": "array",

"items": {

"$ref": "#/components/schemas/Thing"

}

}

}

}

}

}

}

JSON

}

输出:-

在APIM资源中没有看到任何定义,如下所示:-

此外,当我检查我的Azure APIM资源中的API架构时,它与您在API调用中获得的内容类似,请参阅以下内容:-

为了创建定义,我使用了适当的Open API架构和以下terraform代码:-

terraform {

required_providers {

azurerm  =  {

source = "hashicorp/azurerm"

version = "=3.0.0"

}

}

}

  

provider  "azurerm" {

subscription_id  =  "<subscription-id>"

tenant_id  =  "<tenant-id>"

client_id  =  "<client-id>"

client_secret  =  "<client-secret>"

features {}

}

  

data  "azurerm_resource_group"  "rg" {

name  =  "siliconrg098"

}

  

data  "azurerm_api_management"  "apim_service" {

name  =  "siliconapim"

resource_group_name  =  data.azurerm_resource_group.rg.name

}

  

resource  "azurerm_api_management_api"  "example" {

name  =  "example-api"

resource_group_name  =  data.azurerm_resource_group.rg.name

api_management_name  =  data.azurerm_api_management.apim_service.name

display_name  =  "Example API"

path  =  "example"

protocols  =  ["https"]

revision  =  "1"

  

import {

content_format  =  "swagger-json"

content_value  =  <<EOF

{

"swagger": "2.0",

"info": {

"version": "1.0.0",

"title": "Example API"

},

"paths": {},

"definitions": {

"Thing": {

"type": "object",

"properties": {

"id": {

"type": "string"

}

}

},

"ThingArray": {

"type": "array",

"items": {

"$ref": "#/definitions/Thing"

}

}

}

}

EOF

}

}

APIM定义Thing和Thing数组已成功创建,如下所示:-
输出:-

相关问题