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:
Matt Ellis 2018-03-02 11:36:07 -08:00
parent 81a273c7bb
commit 1515889a40
6 changed files with 46 additions and 44 deletions

View file

@ -70,7 +70,7 @@ class Div extends dynamic.Resource {
} }
let run = async () => { 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")); let w = Number(config.require("w")), x = Number(config.require("x")), y = Number(config.require("y"));

View file

@ -2,7 +2,7 @@
import { Config } from "@pulumi/pulumi"; import { Config } from "@pulumi/pulumi";
let config = new Config("minimal:config"); let config = new Config("minimal");
console.log(`Hello, ${config.require("name")}!`); console.log(`Hello, ${config.require("name")}!`);
console.log(`Psst, ${config.require("secret")}`); console.log(`Psst, ${config.require("secret")}`);

View file

@ -148,11 +148,8 @@ export function main(args: string[]): void {
stopEarly: true, stopEarly: true,
}); });
// If any config variables are present, parse and set them, so the subsequent accesses are fast. // Load configuration passed from the language plugin
const envObject: {[key: string]: string} = runtime.getConfigEnv(); runtime.loadConfig();
for (const key of Object.keys(envObject)) {
runtime.setConfig(key, envObject[key]);
}
// If there is a --project=p, and/or a --stack=s, use them in the options. // If there is a --project=p, and/or a --stack=s, use them in the options.
const project: string | undefined = argv["project"]; const project: string | undefined = argv["project"];

View file

@ -1,6 +1,7 @@
// Copyright 2016-2017, Pulumi Corporation. All rights reserved. // Copyright 2016-2017, Pulumi Corporation. All rights reserved.
import { RunError } from "./errors"; import { RunError } from "./errors";
import * as log from "./log";
import * as runtime from "./runtime"; import * as runtime from "./runtime";
/** /**
@ -17,6 +18,16 @@ export class Config {
public readonly name: string; public readonly name: string;
constructor(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; this.name = name;
} }

View file

@ -1,10 +1,9 @@
// Copyright 2016-2017, Pulumi Corporation. All rights reserved. // 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 * configEnvKey is the environment variable key that the language plugin uses to set configuration values.
* configuration variable is missing. Explicit overrides take precedence.
*/ */
export const configEnvKey = "PULUMI_CONFIG"; const configEnvKey = "PULUMI_CONFIG";
const config: {[key: string]: string} = {}; const config: {[key: string]: string} = {};
@ -12,7 +11,7 @@ const config: {[key: string]: string} = {};
* setConfig sets a configuration variable. * setConfig sets a configuration variable.
*/ */
export function setConfig(k: string, v: string): void { 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]; 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; return undefined;
} }
/** /**
* getConfigEnvKey returns a scrubbed environment variable key, PULUMI_CONFIG_<k>, that can be used for * loadConfig populates the runtime.config object based on configuration set in the environment.
* setting explicit varaibles. This is unlike PULUMI_CONFIG which is just a JSON-serialized bag.
*/ */
export function getConfigEnvKey(key: string): string { export function loadConfig() {
let envkey: string = ""; const envConfig = process.env.PULUMI_CONFIG;
for (const c of key) { if (envConfig) {
if (c === "_" || (c >= "A" && c <= "Z") || (c >= "0" && c <= "9")) { const envObject: {[key: string]: string} = JSON.parse(envConfig);
envkey += c; for (const key of Object.keys(envObject)) {
} setConfig(key, envObject[key]);
else if (c >= "a" && c <= "z") {
envkey += c.toUpperCase();
}
else {
envkey += "_";
} }
} }
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} { function cleanKey(key: string): string {
const envConfig = process.env.PULUMI_CONFIG; const idx = key.indexOf(":");
if (envConfig) {
return JSON.parse(envConfig); if (idx > 0 && key.startsWith("config:", idx + 1)) {
return key.substring(0, idx) + ":" + key.substring(idx + 1 + "config:".length);
} }
return {};
return key;
} }

View file

@ -6,10 +6,18 @@ const pulumi = require("../../../../../");
assert.equal(pulumi.getProject(), "runtimeSettingsProject"); assert.equal(pulumi.getProject(), "runtimeSettingsProject");
assert.equal(pulumi.getStack(), "runtimeSettingsStack"); 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.getNumber("A"), 42);
assert.equal(config.requireNumber("A"), 42); assert.equal(config.requireNumber("A"), 42);
assert.equal(config.get("bbbb"), "a string o' b's"); assert.equal(config.get("bbbb"), "a string o' b's");
assert.equal(config.require("bbbb"), "a string o' b's"); assert.equal(config.require("bbbb"), "a string o' b's");
assert.equal(config.get("missingC"), undefined); 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);