early-musician-41645
06/07/2019, 11:20 PMcreate
method that works when I do pulumi up
.
The create
method makes a POST
to a server API, and then it gets back a result object with some data about the created resource, e.g. id
, owner
, etc.
I'd like to be able to do something like:
console.log(myNewResource.id);
However, I'm not sure how to expose the returned values in the resource's state.
Any pointers or examples?const schedule = new gitlab.ScheduledPipeline("test-pipeline", {
description: "Testy test test schedule2",
ref: "master",
cron: "0 1 * * 5",
cron_timezone: "UTC",
active: true,
variables: [{
key: "testvar",
value: "fooval",
}],
},
);
console.log(`Schedule ID: ${schedule.id}`);
How do I get the schedule
object to store the ID returned in the POST response from gitlab?const gitlabScheduledPipelineProvider: pulumi.dynamic.ResourceProvider = {
async create(inputs: Schedule) {
let postData: Schedule = <Schedule>{
description: inputs.description,
ref: inputs.ref,
cron: inputs.cron,
cron_timezone: inputs.cron_timezone,
active: inputs.active,
variables: inputs.variables,
};
let rest: rm.RestClient = new rm.RestClient('gitlab-client', baseUrl);
let res: rm.IRestResponse<Schedule> = await rest.create<Schedule>(pipelineSchedulesPath, postData);
return { id: "foo", outs: {}};
},
}
export class ScheduledPipeline extends pulumi.dynamic.Resource {
public readonly id!: pulumi.Output<number>;
constructor(name: string, props: Schedule, opts?: pulumi.CustomResourceOptions) {
super(gitlabScheduledPipelineProvider, name, props, opts);
}
}
id
propertyclever-sunset-76585
06/07/2019, 11:48 PMnot sure how to hook up theAny property returned from thepropertyid
create
function in the outs
property will be set in the corresponding class member of your dynamic.Resource
class.
What you have here: https://pulumi-community.slack.com/archives/C84L4E3N1/p1559949818250800?thread_ts=1559949621.250200&cid=C84L4E3N1, should allow you to access that id
already. You shouldn’t need the !
. As long as you are setting an id
in the result of the create
function, it’ll be persisted by the engine. In addition, you can also return other output properties, by adding them to the outs
property of the return value of create
.early-musician-41645
06/10/2019, 7:53 PMid
was already a property. What I really meant was "some arbitrary property that I'd like to store"clever-sunset-76585
06/10/2019, 10:36 PMcreate
function of your pulumi.dynamic.ResourceProvider
implementation, be sure to set that in the outs
property. For example,
...
async create(.....) {
return { id: "....", outs: { someOutput: "simpleValue", anotherOutput2: { key: "value", key2: true, key3: 5 } }
}
...
pulumi.dynamic.Resource
(assuming this is all in TypeScript rather than plain JS), you can declare these:
export interface SomeOutputType {
key: pulumi.Output<string>;
key2: pulumi.Output<boolean>;
key3: pulumi.Output<number>;
}
export class MyResource extends pulumi.dynamic.Resource{
public readonly someOutput: pulumi.Output<string>;
public readonly anotherOutput2: pulumi.Output<SomeOutputType>;
constructor(.....) {
super(......);
}
}
early-musician-41645
06/11/2019, 10:42 PM