Skip to content

Commit

Permalink
Add apiextensions.CustomResource#get
Browse files Browse the repository at this point in the history
Fixes #323.
  • Loading branch information
hausdorff committed Dec 22, 2018
1 parent 2edde48 commit af97f8f
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 12 deletions.
60 changes: 58 additions & 2 deletions pkg/gen/nodejs-templates/provider.ts.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,54 @@ export namespace apiextensions {
* fields required across all CRDs.
*/
export interface CustomResourceArgs {
apiVersion: pulumi.Input<string>,
kind: pulumi.Input<string>
/**
* APIVersion defines the versioned schema of this representation of an object. Servers should
* convert recognized schemas to the latest internal value, and may reject unrecognized
* values. More info:
* https://git.k8s.io/community/contributors/devel/api-conventions.md#resources
*/
apiVersion: pulumi.Input<string>;
/**
* Kind is a string value representing the REST resource this object represents. Servers may
* infer this from the endpoint the client submits requests to. Cannot be updated. In
* CamelCase. More info:
* https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds
*/
kind: pulumi.Input<string>;
/**
* Standard object metadata; More info:
* https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata.
*/
metadata?: pulumi.Input<inputApi.meta.v1.ObjectMeta>;
[othersFields: string]: pulumi.Input<any>;
}

/**
* CustomResourceGetOptions uniquely identifies a Kubernetes CustomResource, primarily for use
* in supplied to `apiextensions.CustomResource#get`.
*/
export interface CustomResourceGetOptions {
/**
* apiVersion is the API version of the apiExtensions.CustomResource we wish to select,
* as specified by the CustomResourceDefinition that defines it on the API server.
*/
apiVersion: pulumi.Input<string>;
/**
* kind is the kind of the apiextensions.CustomResource we wish to select, as specified by
* the CustomResourceDefinition that defines it on the API server.
*/
kind: pulumi.Input<string>
/**
* An ID for the Kubernetes resource to retrive. Takes the form <namespace>/<name> or
* <name>.
*/
id: pulumi.Input<pulumi.ID>;
}

/**
* CustomResource represents an instance of a CustomResourceDefinition (CRD). For example, the
* CoreOS Prometheus operator exposes a CRD `monitoring.coreos.com/ServiceMonitor`; to
Expand Down Expand Up @@ -325,6 +367,20 @@ export namespace apiextensions {
*/
public readonly metadata: pulumi.Output<outputApi.meta.v1.ObjectMeta>;
/**
* Get the state of an existing `CustomResource`, as identified by `id`.
* Typically this ID is of the form <namespace>/<name>; if <namespace> is omitted, then (per
* Kubernetes convention) the ID becomes default/<name>.
*
* Pulumi will keep track of this resource using `name` as the Pulumi ID.
*
* @param name _Unique_ name used to register this resource with Pulumi.
* @param opts Uniquely specifies a CustomResource to select.
*/
public static get(name: string, opts: CustomResourceGetOptions): CustomResource {
return new CustomResource(name, {apiVersion: opts.apiVersion, kind: opts.kind}, { id: opts.id });
}

public getInputs(): CustomResourceArgs { return this.__inputs; }
private readonly __inputs: CustomResourceArgs;

Expand Down
60 changes: 58 additions & 2 deletions sdk/nodejs/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3306,12 +3306,54 @@ export namespace apiextensions {
* fields required across all CRDs.
*/
export interface CustomResourceArgs {
apiVersion: pulumi.Input<string>,
kind: pulumi.Input<string>
/**
* APIVersion defines the versioned schema of this representation of an object. Servers should
* convert recognized schemas to the latest internal value, and may reject unrecognized
* values. More info:
* https://git.k8s.io/community/contributors/devel/api-conventions.md#resources
*/
apiVersion: pulumi.Input<string>;

/**
* Kind is a string value representing the REST resource this object represents. Servers may
* infer this from the endpoint the client submits requests to. Cannot be updated. In
* CamelCase. More info:
* https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds
*/
kind: pulumi.Input<string>;

/**
* Standard object metadata; More info:
* https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata.
*/
metadata?: pulumi.Input<inputApi.meta.v1.ObjectMeta>;
[othersFields: string]: pulumi.Input<any>;
}

/**
* CustomResourceGetOptions uniquely identifies a Kubernetes CustomResource, primarily for use
* in supplied to `apiextensions.CustomResource#get`.
*/
export interface CustomResourceGetOptions {
/**
* apiVersion is the API version of the apiExtensions.CustomResource we wish to select,
* as specified by the CustomResourceDefinition that defines it on the API server.
*/
apiVersion: pulumi.Input<string>;

/**
* kind is the kind of the apiextensions.CustomResource we wish to select, as specified by
* the CustomResourceDefinition that defines it on the API server.
*/
kind: pulumi.Input<string>

/**
* An ID for the Kubernetes resource to retrive. Takes the form <namespace>/<name> or
* <name>.
*/
id: pulumi.Input<pulumi.ID>;
}

/**
* CustomResource represents an instance of a CustomResourceDefinition (CRD). For example, the
* CoreOS Prometheus operator exposes a CRD `monitoring.coreos.com/ServiceMonitor`; to
Expand Down Expand Up @@ -3341,6 +3383,20 @@ export namespace apiextensions {
*/
public readonly metadata: pulumi.Output<outputApi.meta.v1.ObjectMeta>;

/**
* Get the state of an existing `CustomResource`, as identified by `id`.
* Typically this ID is of the form <namespace>/<name>; if <namespace> is omitted, then (per
* Kubernetes convention) the ID becomes default/<name>.
*
* Pulumi will keep track of this resource using `name` as the Pulumi ID.
*
* @param name _Unique_ name used to register this resource with Pulumi.
* @param opts Uniquely specifies a CustomResource to select.
*/
public static get(name: string, opts: CustomResourceGetOptions): CustomResource {
return new CustomResource(name, {apiVersion: opts.apiVersion, kind: opts.kind}, { id: opts.id });
}

public getInputs(): CustomResourceArgs { return this.__inputs; }
private readonly __inputs: CustomResourceArgs;

Expand Down
26 changes: 19 additions & 7 deletions tests/integration/get/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
"github.com/stretchr/testify/assert"
)

var step1Name interface{}

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

Expand All @@ -29,24 +27,38 @@ func TestGet(t *testing.T) {
Quick: true,
ExtraRuntimeValidation: func(t *testing.T, stackInfo integration.RuntimeValidationStackInfo) {
assert.NotNil(t, stackInfo.Deployment)
assert.Equal(t, 3, len(stackInfo.Deployment.Resources))
assert.Equal(t, 6, len(stackInfo.Deployment.Resources))

tests.SortResourcesByURN(stackInfo)

stackRes := stackInfo.Deployment.Resources[2]
stackRes := stackInfo.Deployment.Resources[5]
assert.Equal(t, resource.RootStackType, stackRes.URN.Type())

provRes := stackInfo.Deployment.Resources[1]
provRes := stackInfo.Deployment.Resources[4]
assert.True(t, providers.IsProviderType(provRes.URN.Type()))

//
// Assert we can use .get to retrieve the Kubernetes dashboard Service.
//

pod := stackInfo.Deployment.Resources[0]
pod := stackInfo.Deployment.Resources[1]
assert.Equal(t, "kube-dashboard", string(pod.URN.Name()))
step1Name, _ = openapi.Pluck(pod.Outputs, "metadata", "name")
step1Name, _ := openapi.Pluck(pod.Outputs, "metadata", "name")
assert.Equal(t, "kubernetes", step1Name.(string))

//
// Assert we can use .get to retrieve CRDs.
//

crd := stackInfo.Deployment.Resources[0]
assert.Equal(t, "crontab", string(crd.URN.Name()))

ct1 := stackInfo.Deployment.Resources[2]
assert.Equal(t, "my-new-cron-object", string(ct1.URN.Name()))

ct2 := stackInfo.Deployment.Resources[3]
assert.Equal(t, "my-new-cron-object-get", string(ct2.URN.Name()))

},
})
}
38 changes: 37 additions & 1 deletion tests/integration/get/step1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,40 @@ import * as k8s from "@pulumi/kubernetes";
// `get`s the Kubernetes Dashboard, which is deployed by default in minikube.
//

const dashboard = k8s.core.v1.Service.get("kube-dashboard", "kubernetes");
k8s.core.v1.Service.get("kube-dashboard", "kubernetes");

//
// Create a CustomResourceDefinition, a CustomResource, and then `.get` it.
//

const ct = new k8s.apiextensions.v1beta1.CustomResourceDefinition("crontab", {
metadata: { name: "crontabs.stable.example.com" },
spec: {
group: "stable.example.com",
version: "v1",
scope: "Namespaced",
names: {
plural: "crontabs",
singular: "crontab",
kind: "CronTab",
shortNames: ["ct"]
}
}
});

new k8s.apiextensions.CustomResource(
"my-new-cron-object",
{
apiVersion: "stable.example.com/v1",
kind: "CronTab",
metadata: { name: "my-new-cron-object" },
spec: { cronSpec: "* * * * */5", image: "my-awesome-cron-image" }
},
{ dependsOn: ct }
);

const ctObj = k8s.apiextensions.CustomResource.get("my-new-cron-object-get", {
apiVersion: "stable.example.com/v1",
kind: "CronTab",
id: "my-new-cron-object"
});

0 comments on commit af97f8f

Please sign in to comment.