From 2ace0b02340601b3ef2cc237cb20192d5032bb92 Mon Sep 17 00:00:00 2001 From: evanboyle Date: Wed, 16 Sep 2020 08:20:46 -0700 Subject: [PATCH] StackSettings --- .../tests/automation/stackSettings.spec.ts | 74 +++++++++++++++++++ sdk/nodejs/tsconfig.json | 1 + sdk/nodejs/x/automation/stackSettings.ts | 65 +++++++++++++++- 3 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 sdk/nodejs/tests/automation/stackSettings.spec.ts diff --git a/sdk/nodejs/tests/automation/stackSettings.spec.ts b/sdk/nodejs/tests/automation/stackSettings.spec.ts new file mode 100644 index 000000000..e4bfc8c1e --- /dev/null +++ b/sdk/nodejs/tests/automation/stackSettings.spec.ts @@ -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); + }) +}) \ No newline at end of file diff --git a/sdk/nodejs/tsconfig.json b/sdk/nodejs/tsconfig.json index 8bf7a30c1..7e9a3d7b7 100644 --- a/sdk/nodejs/tsconfig.json +++ b/sdk/nodejs/tsconfig.json @@ -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" ] } diff --git a/sdk/nodejs/x/automation/stackSettings.ts b/sdk/nodejs/x/automation/stackSettings.ts index 022396626..cbe6726c7 100644 --- a/sdk/nodejs/x/automation/stackSettings.ts +++ b/sdk/nodejs/x/automation/stackSettings.ts @@ -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 = 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 + } } \ No newline at end of file