Allow eliding name in pulumi.Config .ctor

When this argument is not provided, we'll default to the value of
pulumi.getProject(). This is what you want for application level code
anyway and it matches the CLI behavior where if you don't qualify a
key with a package we use the name of the current project.

Fixes #1581
This commit is contained in:
Matt Ellis 2018-08-02 15:27:37 -07:00
parent 5eb78af779
commit 153729683a
5 changed files with 112 additions and 34 deletions

View file

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

View file

@ -28,6 +28,10 @@ type Config struct {
// New creates a new config bag with the given context and namespace.
func New(ctx *pulumi.Context, namespace string) *Config {
if namespace == "" {
namespace = ctx.Project()
}
return &Config{ctx: ctx, namespace: namespace}
}

View file

@ -14,6 +14,7 @@
import * as util from "util";
import { RunError } from "./errors";
import { getProject } from "./metadata";
import { getConfig } from "./runtime";
/**
@ -25,11 +26,16 @@ import { getConfig } from "./runtime";
*/
export class Config {
/**
* name is the configuration bag's logical name and uniquely identifies it.
* name is the configuration bag's logical name and uniquely identifies it. The default is the name of the current
* project.
*/
public readonly name: string;
constructor(name: string) {
constructor(name?: string) {
if (name === undefined) {
name = getProject();
}
if (name.endsWith(":config")) {
name = name.replace(/:config$/, "");
}

View file

@ -4822,10 +4822,12 @@ Module './bin/index.js' is a 'deployment only' module. In general these cannot b
expectText: `exports.handler = __f0;
var __testConfig_proto = {};
var __config = {["test:TestingKey1"]: "TestingValue1", ["test:TestingKey2"]: "TestingValue2", ["pkg:a"]: "foo", ["pkg:bar"]: "b", ["pkg:baz"]: "baz", ["otherpkg:a"]: "babble", ["otherpkg:nothere"]: "bazzle", ["pkg:boolf"]: "false", ["pkg:boolt"]: "true", ["pkg:num"]: "42.333", ["pkg:array"]: "[ 0, false, 2, \\"foo\\" ]", ["pkg:struct"]: "{ \\"foo\\": \\"bar\\", \\"mim\\": [] }"};
var __options = {project: undefined};
var __runtime = {getConfig: __getConfig, getProject: __0_getProject};
var __metadata_1 = {getProject: __getProject};
__f1.prototype = __testConfig_proto;
Object.defineProperty(__testConfig_proto, "constructor", { configurable: true, writable: true, value: __f1 });
var __config = {["test:TestingKey1"]: "TestingValue1", ["test:TestingKey2"]: "TestingValue2", ["pkg:a"]: "foo", ["pkg:bar"]: "b", ["pkg:baz"]: "baz", ["otherpkg:a"]: "babble", ["otherpkg:nothere"]: "bazzle", ["pkg:boolf"]: "false", ["pkg:boolt"]: "true", ["pkg:num"]: "42.333", ["pkg:array"]: "[ 0, false, 2, \\"foo\\" ]", ["pkg:struct"]: "{ \\"foo\\": \\"bar\\", \\"mim\\": [] }"};
var __runtime_1 = {getConfig: __getConfig};
Object.defineProperty(__testConfig_proto, "get", { configurable: true, writable: true, value: __f2 });
__f5.isInstance = __f6;
(...)
@ -4842,21 +4844,6 @@ Object.defineProperty(__testConfig_proto, "fullKey", { configurable: true, writa
var __testConfig = Object.create(__testConfig_proto);
__testConfig.name = "test";
function __f1() {
return (function() {
with({ }) {
return function /*constructor*/(name) {
if (name.endsWith(":config")) {
name = name.replace(/:config$/, "");
}
this.name = name;
};
}
}).apply(undefined, undefined).apply(this, arguments);
}
function __getConfig() {
return (function() {
with({ config: __config, getConfig: __getConfig }) {
@ -4869,9 +4856,55 @@ return function /*getConfig*/(k) {
}).apply(undefined, undefined).apply(this, arguments);
}
function __0_getProject() {
return (function() {
with({ options: __options, getProject: __0_getProject }) {
return function /*getProject*/() {
return options.project;
};
}
}).apply(undefined, undefined).apply(this, arguments);
}
function __getProject() {
return (function() {
with({ runtime: __runtime, getProject: __getProject }) {
return function /*getProject*/() {
const project = runtime.getProject();
if (project) {
return project;
}
throw new Error("Project unknown; are you using the Pulumi CLI?");
};
}
}).apply(undefined, undefined).apply(this, arguments);
}
function __f1() {
return (function() {
with({ metadata_1: __metadata_1 }) {
return function /*constructor*/(name) {
if (name === undefined) {
name = metadata_1.getProject();
}
if (name.endsWith(":config")) {
name = name.replace(/:config$/, "");
}
this.name = name;
};
}
}).apply(undefined, undefined).apply(this, arguments);
}
function __f2() {
return (function() {
with({ runtime_1: __runtime_1 }) {
with({ runtime_1: __runtime }) {
return function /*get*/(key) {
return runtime_1.getConfig(this.fullKey(key));
@ -4902,10 +4935,12 @@ return function () { const v = testConfig.get("TestingKey1"); console.log(v); };
func: function () { const v = new pulumi.Config("test").get("TestingKey2"); console.log(v); },
expectText: `exports.handler = __f0;
var __options = {project: undefined};
var __config = {["test:TestingKey1"]: "TestingValue1", ["test:TestingKey2"]: "TestingValue2", ["pkg:a"]: "foo", ["pkg:bar"]: "b", ["pkg:baz"]: "baz", ["otherpkg:a"]: "babble", ["otherpkg:nothere"]: "bazzle", ["pkg:boolf"]: "false", ["pkg:boolt"]: "true", ["pkg:num"]: "42.333", ["pkg:array"]: "[ 0, false, 2, \\"foo\\" ]", ["pkg:struct"]: "{ \\"foo\\": \\"bar\\", \\"mim\\": [] }"};
var __runtime = {getProject: __0_getProject, getConfig: __getConfig};
var __metadata_1 = {getProject: __getProject};
var __f1_prototype = {};
Object.defineProperty(__f1_prototype, "constructor", { configurable: true, writable: true, value: __f1 });
var __config = {["test:TestingKey1"]: "TestingValue1", ["test:TestingKey2"]: "TestingValue2", ["pkg:a"]: "foo", ["pkg:bar"]: "b", ["pkg:baz"]: "baz", ["otherpkg:a"]: "babble", ["otherpkg:nothere"]: "bazzle", ["pkg:boolf"]: "false", ["pkg:boolt"]: "true", ["pkg:num"]: "42.333", ["pkg:array"]: "[ 0, false, 2, \\"foo\\" ]", ["pkg:struct"]: "{ \\"foo\\": \\"bar\\", \\"mim\\": [] }"};
var __runtime_1 = {getConfig: __getConfig};
Object.defineProperty(__f1_prototype, "get", { configurable: true, writable: true, value: __f2 });
__f5.isInstance = __f6;
(...)
@ -4922,16 +4957,13 @@ Object.defineProperty(__f1_prototype, "fullKey", { configurable: true, writable:
__f1.prototype = __f1_prototype;
var __pulumi = {Config: __f1};
function __f1() {
function __0_getProject() {
return (function() {
with({ }) {
with({ options: __options, getProject: __0_getProject }) {
return function /*constructor*/(name) {
if (name.endsWith(":config")) {
name = name.replace(/:config$/, "");
}
this.name = name;
};
return function /*getProject*/() {
return options.project;
};
}
}).apply(undefined, undefined).apply(this, arguments);
@ -4949,9 +4981,43 @@ return function /*getConfig*/(k) {
}).apply(undefined, undefined).apply(this, arguments);
}
function __getProject() {
return (function() {
with({ runtime: __runtime, getProject: __getProject }) {
return function /*getProject*/() {
const project = runtime.getProject();
if (project) {
return project;
}
throw new Error("Project unknown; are you using the Pulumi CLI?");
};
}
}).apply(undefined, undefined).apply(this, arguments);
}
function __f1() {
return (function() {
with({ metadata_1: __metadata_1 }) {
return function /*constructor*/(name) {
if (name === undefined) {
name = metadata_1.getProject();
}
if (name.endsWith(":config")) {
name = name.replace(/:config$/, "");
}
this.name = name;
};
}
}).apply(undefined, undefined).apply(this, arguments);
}
function __f2() {
return (function() {
with({ runtime_1: __runtime_1 }) {
with({ runtime_1: __runtime }) {
return function /*get*/(key) {
return runtime_1.getConfig(this.fullKey(key));

View file

@ -20,6 +20,7 @@ import six
from . import errors
from .runtime.config import get_config
from .metadata import get_project
class Config(object):
"""
@ -31,11 +32,12 @@ class Config(object):
"""
def __init__(self, name):
if not name:
raise TypeError('Missing name argument')
name = get_project()
if not isinstance(name, six.string_types):
raise TypeError('Expected name to be a string')
self.name = name
"""The configuration bag's logical name that uniquely identifies it."""
"""The configuration bag's logical name that uniquely identifies it. The default is the name of the current
project."""
def get(self, key):
"""