I'm building a dynamic provider and a class to man...
# general
e
I'm building a dynamic provider and a class to manage in pulumi. I've implemented a
create
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?
e.g.
Copy code
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?
Right now my provider looks like this:
Copy code
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: {}};
  },
}
and the ScheduledPipeline is:
Copy code
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);
  }
}
not sure how to hook up the
id
property
c
Here’s a recent example I wrote with some documentation that may help you: https://github.com/pulumi/examples/blob/master/azure-ts-dynamicresource/cdnCustomDomain.ts In the example, I make API calls to add a custom domain to an Azure CDN endpoint. The example shows how you can declare outputs in your custom resource, so that you can access them outside the resource just like any other regular resource from one of the cloud providers.
not sure how to hook up the
id
property
Any property returned from the
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&amp;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
.
e
I realized later that
id
was already a property. What I really meant was "some arbitrary property that I'd like to store"
c
Then in your
create
function of your
pulumi.dynamic.ResourceProvider
implementation, be sure to set that in the
outs
property. For example,
Copy code
...
async create(.....) {
  return { id: "....", outs: { someOutput: "simpleValue", anotherOutput2: { key: "value", key2: true, key3: 5 } }
}
...
Then in your implementation of
pulumi.dynamic.Resource
(assuming this is all in TypeScript rather than plain JS), you can declare these:
Copy code
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(......);
  }
}
e
That works, thanks 👍
👏 1