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 Dec 21, 2018
1 parent 49b9ce5 commit 5d2963e
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 4 deletions.
3 changes: 2 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,8 @@
"glob": "^7.1.2",
"@types/glob": "^5.0.35",
"mocha": "^5.2.0",
"@types/mocha": "^5.2.5"
"@types/mocha": "^5.2.5",
"sync-request": "^6.0.0"
},
"devDependencies": {
"typescript": "^2.6.2",
Expand Down
34 changes: 33 additions & 1 deletion pkg/gen/nodejs-templates/provider.ts.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,47 @@ 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 http from "http";
import * as https from "https";
import * as syncreq from "sync-request";

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,7 +199,14 @@ 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 fileId = config && config.file || name
let text = "";
if (fileId.startsWith("http://") || fileId.startsWith("https://")) {
text = syncreq.default("GET", fileId).getBody().toString()
} else {
text = fs.readFileSync(fileId).toString();
}

const objs = jsyaml.safeLoadAll(text);
this.resources = pulumi.output(parseYamlDocument({
objs: objs,
Expand Down
3 changes: 2 additions & 1 deletion sdk/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"glob": "^7.1.2",
"@types/glob": "^5.0.35",
"mocha": "^5.2.0",
"@types/mocha": "^5.2.5"
"@types/mocha": "^5.2.5",
"sync-request": "^6.0.0"
},
"devDependencies": {
"typescript": "^2.6.2",
Expand Down
34 changes: 33 additions & 1 deletion sdk/nodejs/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,47 @@ 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 http from "http";
import * as https from "https";
import * as syncreq from "sync-request";

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,7 +2247,14 @@ 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 fileId = config && config.file || name
let text = "";
if (fileId.startsWith("http://") || fileId.startsWith("https://")) {
text = syncreq.default("GET", fileId).getBody().toString()
} else {
text = fs.readFileSync(fileId).toString();
}

const objs = jsyaml.safeLoadAll(text);
this.resources = pulumi.output(parseYamlDocument({
objs: objs,
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://raw.githubusercontent.com/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 5d2963e

Please sign in to comment.