数组
数组是一种用于保存数据列表的数据类型。
💡 常用数组 Usage
例如,数组属性类型可被用于来存储任何数据列表:
- 被用于的软件包
- 依赖关系
- 徽章
在live demo 这个示例中,我们可以看到 Monitor Tooling
数组属性。
应用程序接口定义
{
"myArrayProp": {
"title": "My array",
"icon": "My icon",
"description": "My array property",
"type": "array",
"default": [1, 2, 3]
}
}
Check out Port's API reference to learn more.
Terraform 定义
resource "port_blueprint" "myBlueprint" {
# ...blueprint properties
properties = {
array_props = {
"myArrayProp" = {
title = "My array"
required = true
}
}
}
}
要设置数组属性的类型,需要使用
<type>_items
属性类型。例如,要设置字符串数组,需要使用 string_items
属性类型。resource "port_blueprint" "myBlueprint" {
# ...blueprint properties
properties = {
array_props = {
"myArrayProp" = {
title = "My array"
required = true
string_items = {} # You can also set here default values
}
}
}
}
我们目前支持以下类型的数组项: string_items
, number_items
, boolean_items
, object_items
。
Pulumi 的定义
- Python
- TypeScript
- JavaScript
- GO
"""A Python Pulumi program"""
import pulumi
from port_pulumi import Blueprint,BlueprintPropertiesArgs,BlueprintPropertyArgs
blueprint = Blueprint(
"myBlueprint",
identifier="myBlueprint",
title="My Blueprint",
properties=BlueprintPropertiesArgs(
array_props={
"myArrayProp": BlueprintPropertyArgs(
title="My array", required=True,
)
}
),
relations=[]
)
import * as pulumi from "@pulumi/pulumi";
import * as port from "@port-labs/port";
export const blueprint = new port.Blueprint("myBlueprint", {
identifier: "myBlueprint",
title: "My Blueprint",
properties: {
array_props: {
myArrayProp: {
title: "My array",
required: true,
},
},
},
});
"use strict";
const pulumi = require("@pulumi/pulumi");
const port = require("@port-labs/port");
const entity = new port.Blueprint("myBlueprint", {
title: "My Blueprint",
identifier: "myBlueprint",
properties: {
array_props: {
myArrayProp: {
title: "My array",
required: true,
},
},
},
relations: [],
});
exports.title = entity.title;
package main
import (
"github.com/port-labs/pulumi-port/sdk/go/port"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
blueprint, err := port.NewBlueprint(ctx, "myBlueprint", &port.BlueprintArgs{
Identifier: pulumi.String("myBlueprint"),
Title: pulumi.String("My Blueprint"),
Properties: port.BlueprintPropertiesArgs{
ArrayProps: port.BlueprintPropertiesArrayPropsMap{
"myArrayProp": port.BlueprintPropertyArgs{
Title: pulumi.String("My array"),
Required: pulumi.Bool(true),
},
},
},
})
ctx.Export("blueprint", blueprint.Title)
if err != nil {
return err
}
return nil
})
}
验证数组
数组验证支持以下操作符:
- 最小项目
- 最大项目
- 唯一项目
数组验证遵循 JSON 模式模型,请参阅JSON schema docs 了解所有可用的验证。
- Basic
- Terraform
{
"myArrayProp": {
"title": "My array",
"icon": "My icon",
"description": "My array property",
"type": "array",
"minItems": 0,
"maxItems": 5,
"uniqueItems": false
}
}
resource "port_blueprint" "myBlueprint" {
# ...blueprint properties
properties = {
array_props = {
"myArrayProp" = {
title = "My array"
required = true
min_items = 0
max_items = 5
}
}
}
}