This change adds support for lists and maps in config. We now allow lists/maps (and nested structures) in `Pulumi.<stack>.yaml` (or `Pulumi.<stack>.json`; yes, we currently support that). For example: ```yaml config: proj:blah: - a - b - c proj:hello: world proj:outer: inner: value proj:servers: - port: 80 ``` While such structures could be specified in the `.yaml` file manually, we support setting values in maps/lists from the command line. As always, you can specify single values with: ```shell $ pulumi config set hello world ``` Which results in the following YAML: ```yaml proj:hello world ``` And single value secrets via: ```shell $ pulumi config set --secret token shhh ``` Which results in the following YAML: ```yaml proj:token: secure: v1:VZAhuroR69FkEPTk:isKafsoZVMWA9pQayGzbWNynww== ``` Values in a list can be set from the command line using the new `--path` flag, which indicates the config key contains a path to a property in a map or list: ```shell $ pulumi config set --path names[0] a $ pulumi config set --path names[1] b $ pulumi config set --path names[2] c ``` Which results in: ```yaml proj:names - a - b - c ``` Values can be obtained similarly: ```shell $ pulumi config get --path names[1] b ``` Or setting values in a map: ```shell $ pulumi config set --path outer.inner value ``` Which results in: ```yaml proj:outer: inner: value ``` Of course, setting values in nested structures is supported: ```shell $ pulumi config set --path servers[0].port 80 ``` Which results in: ```yaml proj:servers: - port: 80 ``` If you want to include a period in the name of a property, it can be specified as: ``` $ pulumi config set --path 'nested["foo.bar"]' baz ``` Which results in: ```yaml proj:nested: foo.bar: baz ``` Examples of valid paths: - root - root.nested - 'root["nested"]' - root.double.nest - 'root["double"].nest' - 'root["double"]["nest"]' - root.array[0] - root.array[100] - root.array[0].nested - root.array[0][1].nested - root.nested.array[0].double[1] - 'root["key with \"escaped\" quotes"]' - 'root["key with a ."]' - '["root key with \"escaped\" quotes"].nested' - '["root key with a ."][100]' Note: paths that contain quotes can be surrounded by single quotes. When setting values with `--path`, if the value is `"false"` or `"true"`, it will be saved as the boolean value, and if it is convertible to an integer, it will be saved as an integer. Secure values are supported in lists/maps as well: ```shell $ pulumi config set --path --secret tokens[0] shh ``` Will result in: ```yaml proj:tokens: - secure: v1:wpZRCe36sFg1RxwG:WzPeQrCn4n+m4Ks8ps15MxvFXg== ``` Note: maps of length 1 with a key of “secure” and string value are reserved for storing secret values. Attempting to create such a value manually will result in an error: ```shell $ pulumi config set --path parent.secure foo error: "secure" key in maps of length 1 are reserved ``` **Accessing config values from the command line with JSON** ```shell $ pulumi config --json ``` Will output: ```json { "proj:hello": { "value": "world", "secret": false, "object": false }, "proj:names": { "value": "[\"a\",\"b\",\"c\"]", "secret": false, "object": true, "objectValue": [ "a", "b", "c" ] }, "proj:nested": { "value": "{\"foo.bar\":\"baz\"}", "secret": false, "object": true, "objectValue": { "foo.bar": "baz" } }, "proj:outer": { "value": "{\"inner\":\"value\"}", "secret": false, "object": true, "objectValue": { "inner": "value" } }, "proj:servers": { "value": "[{\"port\":80}]", "secret": false, "object": true, "objectValue": [ { "port": 80 } ] }, "proj:token": { "secret": true, "object": false }, "proj:tokens": { "secret": true, "object": true } } ``` If the value is a map or list, `"object"` will be `true`. `"value"` will contain the object as serialized JSON and a new `"objectValue"` property will be available containing the value of the object. If the object contains any secret values, `"secret"` will be `true`, and just like with scalar values, the value will not be outputted unless `--show-secrets` is specified. **Accessing config values from Pulumi programs** Map/list values are available to Pulumi programs as serialized JSON, so the existing `getObject`/`requireObject`/`getSecretObject`/`requireSecretObject` functions can be used to retrieve such values, e.g.: ```typescript import * as pulumi from "@pulumi/pulumi"; interface Server { port: number; } const config = new pulumi.Config(); const names = config.requireObject<string[]>("names"); for (const n of names) { console.log(n); } const servers = config.requireObject<Server[]>("servers"); for (const s of servers) { console.log(s.port); } ```
238 lines
7.7 KiB
Go
238 lines
7.7 KiB
Go
// Copyright 2016-2018, Pulumi Corporation.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package workspace
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/user"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/pulumi/pulumi/pkg/encoding"
|
|
"github.com/pulumi/pulumi/pkg/tokens"
|
|
"github.com/pulumi/pulumi/pkg/util/fsutil"
|
|
)
|
|
|
|
const (
|
|
// BackupDir is the name of the folder where backup stack information is stored.
|
|
BackupDir = "backups"
|
|
// BookkeepingDir is the name of our bookkeeping folder, we store state here (like .git for git).
|
|
BookkeepingDir = ".pulumi"
|
|
// ConfigDir is the name of the folder that holds local configuration information.
|
|
ConfigDir = "config"
|
|
// GitDir is the name of the folder git uses to store information.
|
|
GitDir = ".git"
|
|
// HistoryDir is the name of the directory that holds historical information for projects.
|
|
HistoryDir = "history"
|
|
// PluginDir is the name of the directory containing plugins.
|
|
PluginDir = "plugins"
|
|
// PolicyDir is the name of the directory that holds policy packs.
|
|
PolicyDir = "policies"
|
|
// StackDir is the name of the directory that holds stack information for projects.
|
|
StackDir = "stacks"
|
|
// TemplateDir is the name of the directory containing templates.
|
|
TemplateDir = "templates"
|
|
// TemplatePolicyDir is the name of the directory containing templates for Policy Packs.
|
|
TemplatePolicyDir = "templates-policy"
|
|
// WorkspaceDir is the name of the directory that holds workspace information for projects.
|
|
WorkspaceDir = "workspaces"
|
|
|
|
// IgnoreFile is the name of the file that we use to control what to upload to the service.
|
|
IgnoreFile = ".pulumiignore"
|
|
|
|
// ProjectFile is the base name of a project file.
|
|
ProjectFile = "Pulumi"
|
|
// RepoFile is the name of the file that holds information specific to the entire repository.
|
|
RepoFile = "settings.json"
|
|
// WorkspaceFile is the name of the file that holds workspace information.
|
|
WorkspaceFile = "workspace.json"
|
|
// CachedVersionFile is the name of the file we use to store when we last checked if the CLI was out of date
|
|
CachedVersionFile = ".cachedVersionInfo"
|
|
|
|
// PulumiHomeEnvVar is a path to the '.pulumi' folder with plugins, access token, etc.
|
|
// The folder can have any name, not necessarily '.pulumi'.
|
|
// It defaults to the '<user's home>/.pulumi' if not specified.
|
|
PulumiHomeEnvVar = "PULUMI_HOME"
|
|
|
|
// PolicyPackFile is the base name of a Pulumi policy pack file.
|
|
PolicyPackFile = "PulumiPolicy"
|
|
)
|
|
|
|
// DetectProjectPath locates the closest project from the current working directory, or an error if not found.
|
|
func DetectProjectPath() (string, error) {
|
|
dir, err := os.Getwd()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
path, err := DetectProjectPathFrom(dir)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return path, nil
|
|
}
|
|
|
|
// DetectProjectStackPath returns the name of the file to store stack specific project settings in. We place stack
|
|
// specific settings next to the Pulumi.yaml file, named like: Pulumi.<stack-name>.yaml
|
|
func DetectProjectStackPath(stackName tokens.QName) (string, error) {
|
|
proj, projPath, err := DetectProjectAndPath()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return filepath.Join(filepath.Dir(projPath), proj.Config, fmt.Sprintf("%s.%s%s", ProjectFile, qnameFileName(stackName),
|
|
filepath.Ext(projPath))), nil
|
|
}
|
|
|
|
// DetectProjectPathFrom locates the closest project from the given path, searching "upwards" in the directory
|
|
// hierarchy. If no project is found, an empty path is returned.
|
|
func DetectProjectPathFrom(path string) (string, error) {
|
|
return fsutil.WalkUp(path, isProject, func(s string) bool {
|
|
return true
|
|
})
|
|
}
|
|
|
|
// DetectPolicyPackPathFrom locates the closest Pulumi policy project from the given path,
|
|
// searching "upwards" in the directory hierarchy. If no project is found, an empty path is
|
|
// returned.
|
|
func DetectPolicyPackPathFrom(path string) (string, error) {
|
|
return fsutil.WalkUp(path, isPolicyPack, func(s string) bool {
|
|
return true
|
|
})
|
|
}
|
|
|
|
// DetectProject loads the closest project from the current working directory, or an error if not found.
|
|
func DetectProject() (*Project, error) {
|
|
proj, _, err := DetectProjectAndPath()
|
|
return proj, err
|
|
}
|
|
|
|
func DetectProjectStack(stackName tokens.QName) (*ProjectStack, error) {
|
|
path, err := DetectProjectStackPath(stackName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return LoadProjectStack(path)
|
|
}
|
|
|
|
// DetectProjectAndPath loads the closest package from the current working directory, or an error if not found. It
|
|
// also returns the path where the package was found.
|
|
func DetectProjectAndPath() (*Project, string, error) {
|
|
path, err := DetectProjectPath()
|
|
if err != nil {
|
|
return nil, "", err
|
|
} else if path == "" {
|
|
return nil, "", errors.Errorf("no Pulumi project found in the current working directory")
|
|
}
|
|
|
|
proj, err := LoadProject(path)
|
|
return proj, path, err
|
|
}
|
|
|
|
// SaveProject saves the project file on top of the existing one, using the standard location.
|
|
func SaveProject(proj *Project) error {
|
|
path, err := DetectProjectPath()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return proj.Save(path)
|
|
}
|
|
|
|
func SaveProjectStack(stackName tokens.QName, stack *ProjectStack) error {
|
|
path, err := DetectProjectStackPath(stackName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return stack.Save(path)
|
|
}
|
|
|
|
// isProject returns true if the path references what appears to be a valid project. If problems are detected -- like
|
|
// an incorrect extension -- they are logged to the provided diag.Sink (if non-nil).
|
|
func isProject(path string) bool {
|
|
return isMarkupFile(path, ProjectFile)
|
|
}
|
|
|
|
// isPolicyPack returns true if the path references what appears to be a valid policy pack project.
|
|
// If problems are detected -- like an incorrect extension -- they are logged to the provided
|
|
// diag.Sink (if non-nil).
|
|
func isPolicyPack(path string) bool {
|
|
return isMarkupFile(path, PolicyPackFile)
|
|
}
|
|
|
|
func isMarkupFile(path string, expect string) bool {
|
|
info, err := os.Stat(path)
|
|
if err != nil || info.IsDir() {
|
|
// Missing files and directories can't be markup files.
|
|
return false
|
|
}
|
|
|
|
// Ensure the base name is expected.
|
|
name := info.Name()
|
|
ext := filepath.Ext(name)
|
|
base := strings.TrimSuffix(name, ext)
|
|
if base != expect {
|
|
return false
|
|
}
|
|
|
|
// Check all supported extensions.
|
|
for _, mext := range encoding.Exts {
|
|
if name == expect+mext {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// GetCachedVersionFilePath returns the location where the CLI caches information from pulumi.com on the newest
|
|
// available version of the CLI
|
|
func GetCachedVersionFilePath() (string, error) {
|
|
return GetPulumiPath(CachedVersionFile)
|
|
}
|
|
|
|
// GetPulumiHomeDir returns the path of the '.pulumi' folder where Pulumi puts its artifacts.
|
|
func GetPulumiHomeDir() (string, error) {
|
|
// Allow the folder we use to be overridden by an environment variable
|
|
dir := os.Getenv(PulumiHomeEnvVar)
|
|
if dir != "" {
|
|
return dir, nil
|
|
}
|
|
|
|
// Otherwise, use the current user's home dir + .pulumi
|
|
user, err := user.Current()
|
|
if err != nil {
|
|
return "", errors.Wrapf(err, "getting current user")
|
|
}
|
|
|
|
return filepath.Join(user.HomeDir, BookkeepingDir), nil
|
|
}
|
|
|
|
// GetPulumiPath returns the path to a file or directory under the '.pulumi' folder. It joins the path of
|
|
// the '.pulumi' folder with elements passed as arguments.
|
|
func GetPulumiPath(elem ...string) (string, error) {
|
|
homeDir, err := GetPulumiHomeDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return filepath.Join(append([]string{homeDir}, elem...)...), nil
|
|
}
|