Remove the need for the :config: part of a config bag
We now unify new Config("package") and new Config("package:config"), printing a warning when the new Config("package:config") form is used and pointing consumers towards just new Config("package") I've updated our examples to use the newer syntax, but I've added a test ot the langhost to ensure both forms work. Fixes #923
This commit is contained in:
parent
81a273c7bb
commit
1515889a40
|
@ -70,7 +70,7 @@ class Div extends dynamic.Resource {
|
|||
}
|
||||
|
||||
let run = async () => {
|
||||
let config = new pulumi.Config("simple:config");
|
||||
let config = new pulumi.Config("simple");
|
||||
|
||||
let w = Number(config.require("w")), x = Number(config.require("x")), y = Number(config.require("y"));
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
import { Config } from "@pulumi/pulumi";
|
||||
|
||||
let config = new Config("minimal:config");
|
||||
let config = new Config("minimal");
|
||||
console.log(`Hello, ${config.require("name")}!`);
|
||||
console.log(`Psst, ${config.require("secret")}`);
|
||||
|
||||
|
|
|
@ -148,11 +148,8 @@ export function main(args: string[]): void {
|
|||
stopEarly: true,
|
||||
});
|
||||
|
||||
// If any config variables are present, parse and set them, so the subsequent accesses are fast.
|
||||
const envObject: {[key: string]: string} = runtime.getConfigEnv();
|
||||
for (const key of Object.keys(envObject)) {
|
||||
runtime.setConfig(key, envObject[key]);
|
||||
}
|
||||
// Load configuration passed from the language plugin
|
||||
runtime.loadConfig();
|
||||
|
||||
// If there is a --project=p, and/or a --stack=s, use them in the options.
|
||||
const project: string | undefined = argv["project"];
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
|
||||
|
||||
import { RunError } from "./errors";
|
||||
import * as log from "./log";
|
||||
import * as runtime from "./runtime";
|
||||
|
||||
/**
|
||||
|
@ -17,6 +18,16 @@ export class Config {
|
|||
public readonly name: string;
|
||||
|
||||
constructor(name: string) {
|
||||
// To ease migration of code that already does new Config("<package>:config"), treat this as if
|
||||
// just new Config("<package>") was called.
|
||||
if (name.endsWith(":config")) {
|
||||
const newName = name.replace(/:config$/, "");
|
||||
log.warn("`:config` is no longer required at the end of configuration " +
|
||||
"bag names and support will be removed in a future version, please " +
|
||||
"use new Config(\"%s\") instead.", newName);
|
||||
name = newName;
|
||||
}
|
||||
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
|
||||
|
||||
/**
|
||||
* configEnvKey is the environment variable key for configuration that we will check in the event that a
|
||||
* configuration variable is missing. Explicit overrides take precedence.
|
||||
* configEnvKey is the environment variable key that the language plugin uses to set configuration values.
|
||||
*/
|
||||
export const configEnvKey = "PULUMI_CONFIG";
|
||||
const configEnvKey = "PULUMI_CONFIG";
|
||||
|
||||
const config: {[key: string]: string} = {};
|
||||
|
||||
|
@ -12,7 +11,7 @@ const config: {[key: string]: string} = {};
|
|||
* setConfig sets a configuration variable.
|
||||
*/
|
||||
export function setConfig(k: string, v: string): void {
|
||||
config[k] = v;
|
||||
config[cleanKey(k)] = v;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -24,48 +23,35 @@ export function getConfig(k: string): string | undefined {
|
|||
return config[k];
|
||||
}
|
||||
|
||||
// If there is a specific PULUMI_CONFIG_<k> variable, use it.
|
||||
const envKey: string = getConfigEnvKey(k);
|
||||
if (process.env.hasOwnProperty(envKey)) {
|
||||
return process.env[envKey];
|
||||
}
|
||||
|
||||
// If the config hasn't been set, but there is a process-wide PULUMI_CONFIG envvar, use it.
|
||||
const envObject: {[key: string]: string} = getConfigEnv();
|
||||
if (envObject.hasOwnProperty(k)) {
|
||||
return envObject[k];
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* getConfigEnvKey returns a scrubbed environment variable key, PULUMI_CONFIG_<k>, that can be used for
|
||||
* setting explicit varaibles. This is unlike PULUMI_CONFIG which is just a JSON-serialized bag.
|
||||
* loadConfig populates the runtime.config object based on configuration set in the environment.
|
||||
*/
|
||||
export function getConfigEnvKey(key: string): string {
|
||||
let envkey: string = "";
|
||||
for (const c of key) {
|
||||
if (c === "_" || (c >= "A" && c <= "Z") || (c >= "0" && c <= "9")) {
|
||||
envkey += c;
|
||||
}
|
||||
else if (c >= "a" && c <= "z") {
|
||||
envkey += c.toUpperCase();
|
||||
}
|
||||
else {
|
||||
envkey += "_";
|
||||
export function loadConfig() {
|
||||
const envConfig = process.env.PULUMI_CONFIG;
|
||||
if (envConfig) {
|
||||
const envObject: {[key: string]: string} = JSON.parse(envConfig);
|
||||
for (const key of Object.keys(envObject)) {
|
||||
setConfig(key, envObject[key]);
|
||||
}
|
||||
}
|
||||
return `${configEnvKey}_${envkey}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* getConfigEnv returns the environment map that will be used for config checking when variables aren't set.
|
||||
* cleanKey takes a configuration key, and if it is of the form "<string>:config:<string>" removes the ":config:"
|
||||
* portion. Previously, our keys always had the string ":config:" in them, and we'd like to remove it. However, the
|
||||
* language host needs to continue to set it so we can be compatable with older versions of our packages. Once we
|
||||
* stop supporting older packages, we can change the language host to not add this :config: thing and remove this
|
||||
* function.
|
||||
*/
|
||||
export function getConfigEnv(): {[key: string]: string} {
|
||||
const envConfig = process.env.PULUMI_CONFIG;
|
||||
if (envConfig) {
|
||||
return JSON.parse(envConfig);
|
||||
function cleanKey(key: string): string {
|
||||
const idx = key.indexOf(":");
|
||||
|
||||
if (idx > 0 && key.startsWith("config:", idx + 1)) {
|
||||
return key.substring(0, idx) + ":" + key.substring(idx + 1 + "config:".length);
|
||||
}
|
||||
return {};
|
||||
|
||||
return key;
|
||||
}
|
||||
|
|
|
@ -6,10 +6,18 @@ const pulumi = require("../../../../../");
|
|||
assert.equal(pulumi.getProject(), "runtimeSettingsProject");
|
||||
assert.equal(pulumi.getStack(), "runtimeSettingsStack");
|
||||
|
||||
const config = new pulumi.Config("myBag:config");
|
||||
const config = new pulumi.Config("myBag");
|
||||
assert.equal(config.getNumber("A"), 42);
|
||||
assert.equal(config.requireNumber("A"), 42);
|
||||
assert.equal(config.get("bbbb"), "a string o' b's");
|
||||
assert.equal(config.require("bbbb"), "a string o' b's");
|
||||
assert.equal(config.get("missingC"), undefined);
|
||||
|
||||
// ensure the older format works as well
|
||||
const configOld = new pulumi.Config("myBag:config");
|
||||
assert.equal(configOld.getNumber("A"), 42);
|
||||
assert.equal(configOld.requireNumber("A"), 42);
|
||||
assert.equal(configOld.get("bbbb"), "a string o' b's");
|
||||
assert.equal(configOld.require("bbbb"), "a string o' b's");
|
||||
assert.equal(configOld.get("missingC"), undefined);
|
||||
|
||||
|
|
Loading…
Reference in a new issue