字符串
字符串是一种原语数据类型,被用于来保存文本数据。
💡 常用字符串 Usage
字符串属性类型可被用于来存储任何基于文本的数据,例如:
- 镜像标签;
- 可变密钥
- 提交 SHA;
- 文件名;
- 等等。
在live demo 这个示例中,我们可以看到 Language
字符串属性。
应用程序接口定义
- Basic
- Enum
- Array
- Enum Array
{
"myStringProp": {
"title": "My string",
"icon": "My icon",
"description": "My string property",
"type": "string",
"default": "My default"
}
}
{
"myStringEnum": {
"title": "My string enum",
"icon": "My icon",
"description": "My string enum",
"type": "string",
"enum": ["my-option-1", "my-option-2"],
"enumColors": {
"my-option-1": "red",
"my-option-2": "green"
}
}
}
{
"myStringArray": {
"title": "My string array",
"icon": "My icon",
"description": "My string array",
"type": "array",
"items": {
"type": "string"
}
}
}
{
"myStringArray": {
"title": "My string enum array",
"icon": "My icon",
"description": "My string enum array",
"type": "array",
"items": {
"type": "string",
"enum": ["my-option-1", "my-option-2"],
"enumColors": {
"my-option-1": "red",
"my-option-2": "green"
}
}
}
}
Check out Port's API reference to learn more.
Terraform 定义
- Basic
- Enum
- Array
resource "port_blueprint" "myBlueprint" {
# ...blueprint properties
properties = {
string_props = {
"myStringProp" = {
title = "My string"
required = false
}
}
}
}
resource "port_blueprint" "myBlueprint" {
# ...blueprint properties
properties = {
string_props = {
"myStringProp" = {
title = "My string"
required = false
enum = ["my-option-1", "my-option-2"]
enum_colors = {
"my-option-1" = "red"
"my-option-2" = "green"
}
}
}
}
}
resource "port_blueprint" "myBlueprint" {
# ...blueprint properties
properties = {
array_props = {
"myStringArray" = {
title = "My string array"
string_items = {} # Pass an empty object only sets the type to string
}
"myStringArrayWithDefault" = {
title = "My string array with default"
string_items = {
default = ["my-default-1", "my-default-2"]
}
}
}
}
}
Pulumi 的定义
- Basic
- Enum
- Array - coming soon
- Python
- TypeScript
- JavaScript
- GO
"""A Python Pulumi program"""
import pulumi
from port_pulumi import Blueprint,BlueprintPropertiesArgs,BlueprintPropertiesStringPropsArgs
blueprint = Blueprint(
"myBlueprint",
identifier="myBlueprint",
title="My Blueprint",
properties=BlueprintPropertiesArgs(
string_props={
"myStringProp": BlueprintPropertiesStringPropsArgs(
title="My string", required=False
)
),
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: {
stringProps: {
myStringProp: {
title: "My string",
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: {
stringProps: {
myStringProp: {
title: "My string",
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{
StringProps: port.BlueprintPropertiesStringPropsMap{
"myStringProp": &port.BlueprintPropertiesStringPropsArgs{
Title: pulumi.String("My String"),
Required: pulumi.Bool(true),
},
},
},
})
ctx.Export("blueprint", blueprint.Title)
if err != nil {
return err
}
return nil
})
}
- Python
- TypeScript
- JavaScript
- GO
"""A Python Pulumi program"""
import pulumi
from port_pulumi import Blueprint,BlueprintPropertiesArgs
blueprint = Blueprint(
"myBlueprint",
identifier="myBlueprint",
title="My Blueprint",
properties=BlueprintPropertiesArgs(
string_props={
"myStringProp": {
"title": "My String",
"required": True,
"enum": ["my-option-1", "my-option-2"],
"enum_colors": {
"my-option-1": "red",
"my-option-2": "green"
}
}
}
),
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: {
stringProps: {
myStringProp: {
title: "My String",
required: true,
enums: ["my-option-1", "my-option-2"],
enumColors: {
"my-option-1": "red",
"my-option-2": "green",
},
},
},
},
});
"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: {
stringProps: {
myStringProp: {
title: "My String",
required: true,
enums: ["my-option-1", "my-option-2"],
enumColors: {
"my-option-1": "red",
"my-option-2": "green",
},
},
},
},
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{
StringProps: port.BlueprintPropertiesStringPropsMap{
"myStringProp": port.BlueprintPropertiesStringPropsArgs{
Title: pulumi.String("My String"),
Required: pulumi.Bool(false),
Type: pulumi.String("string"),
Enums: pulumi.StringArray{
pulumi.String("my-option-1"),
pulumi.String("my-option-2"),
},
EnumColors: pulumi.StringMap{
"my-option-1": pulumi.String("red"),
"my-option-2": pulumi.String("green"),
},
},
},
},
})
ctx.Export("blueprint", blueprint.Title)
if err != nil {
return err
}
return nil
})
}
验证字符串
字符串验证支持以下操作符:
minLength
- 执行最小字符串长度;maxLength
- 执行最大字符串长度;pattern
- 执行 Regex 模式。
- Basic
- Array
- Terraform
- Pulumi - coming soon
{
"myStringProp": {
"title": "My string",
"icon": "My icon",
"description": "My string property",
"type": "string",
"minLength": 1,
"maxLength": 32,
"pattern": "^[a-zA-Z0-9-]*-service$"
}
}
{
"myStringArray": {
"title": "My string array",
"icon": "My icon",
"description": "My string array",
"type": "array",
"items": {
"type": "string",
"minLength": 1,
"maxLength": 32,
"pattern": "^[a-zA-Z0-9-]*-service$"
}
}
}
resource "port_blueprint" "myBlueprint" {
# ...blueprint properties
properties = {
string_props = {
"myStringProp" = {
title = "My string"
required = false
min_length = 1
max_length = 32
pattern = "^[a-zA-Z0-9-]*-service$"
}
}
}
}