StackSettings

This commit is contained in:
evanboyle 2020-09-16 08:20:46 -07:00 committed by Evan Boyle
parent faafb6b157
commit 2ace0b0234
3 changed files with 139 additions and 1 deletions

View file

@ -0,0 +1,74 @@
// Copyright 2016-2020, 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.
import * as assert from "assert";
import { StackSettings } from "../../x/automation/stackSettings";
describe("StackSettings", () => {
it("parses JSON", () => {
const testStack = {
secretsProvider: "abc",
config: {
plain: "plain",
secure: {
secure: "secret"
},
},
};
const result = StackSettings.fromJSON(testStack)
assert.equal(result.secretsProvider, "abc");
assert.equal(result.config!["plain"].value, "plain");
assert.equal(result.config!["secure"].secure, "secret");
})
it("JSON serializes", () => {
const testStack = {
secretsProvider: "abc",
config: {
plain: "plain",
secure: {
secure: "secret"
},
},
};
const result = StackSettings.fromJSON(testStack)
const expected = `{"secretsProvider":"abc","config":{"plain":"plain","secure":{"secure":"secret"}}}`;
assert.equal(JSON.stringify(result), expected)
})
it("parses YAML", () => {
const stackYaml = `secretsProvider: abc\nconfig:\n plain: plain\n secure:\n secure: secret\n`;
const result = StackSettings.fromYAML(stackYaml)
assert.equal(result.secretsProvider, "abc");
assert.equal(result.config!["plain"].value, "plain");
assert.equal(result.config!["secure"].secure, "secret");
})
it("YAML serializes", () => {
const testStack = {
secretsProvider: "abc",
config: {
plain: "plain",
secure: {
secure: "secret"
},
},
};
const result = StackSettings.fromJSON(testStack).toYAML();
const expected = `secretsProvider: abc\nconfig:\n plain: plain\n secure:\n secure: secret\n`;
assert.equal(result, expected);
})
})

View file

@ -82,6 +82,7 @@
"tests/runtime/props.spec.ts",
"tests/runtime/langhost/run.spec.ts",
"tests/automation/projectSettings.spec.ts",
"tests/automation/stackSettings.spec.ts",
"tests/automation/localWorkspace.spec.ts"
]
}

View file

@ -1,3 +1,66 @@
import * as yaml from "js-yaml";
export class StackSettings {
secretsProvider?: string
encryptedKey?: string
encryptionSalt?: string
config?: {[key: string]: StackSettingsConfigValue}
public static fromJSON(obj: any) {
const stack = new StackSettings();
if (obj.config) {
Object.keys(obj.config).forEach(k => {
obj.config[k] = StackSettingsConfigValue.fromJSON(obj.config![k])
})
}
stack.secretsProvider = obj.secretsProvider;
stack.encryptedKey = obj.encryptedKey;
stack.encryptionSalt = obj.encryptionSalt;
stack.config = obj.config
return stack
}
public static fromYAML(text: string) {
const res = yaml.safeLoad(text, { json: true });
return StackSettings.fromJSON(res);
}
toYAML(): string {
const copy = <StackSettings>Object.assign({}, this);
if (copy.config) {
Object.keys(copy.config).forEach(k => {
copy.config![k] = copy.config![k].toJSON()
})
}
return yaml.safeDump(copy, { skipInvalid: true });
}
}
export class StackSettingsConfigValue {
value?: string
secure?: string
public static fromJSON(obj: any) {
const config = new StackSettingsConfigValue();
if (typeof obj === "string") {
config.value = obj;
}
else {
config.secure = obj.secure
}
if (!config.value && !config.secure) {
throw new Error("could not deserialize invalid StackSettingsConfigValue object")
}
return config;
}
toJSON(): any{
if (this.secure){
return {
secure: this.secure
};
}
return this.value
}
}