I'm trying to pass the tags as a custom object class to Pulumi as an argument because I can set the Name property on a needed basis. The problem is that it doesn't work. Due to the lack of JS/TS knowledge, I don't understand what it wants from me.
Could anyone give me a clue?
import * as aws from "@pulumi/aws";
class Common_tags_test {
Name!: string;
Owner: string = 'test';
ManagedBy: string = 'Pulumi';
constructor() {}
set_name(name: string) {
this.Name = name
}
}
// const common_tags = {
// Owner: 'test',
// ManagedBy: 'Pulumi'
// };
const main_vpc = new aws.ec2.Vpc("Test_VPC", {
cidrBlock: "10.10.0.0/16",
// tags: {...common_tags, ...{Name: 'Test_VPC'}}
tags: new Common_tags_test()
});
Error:
TSError: ⨯ Unable to compile TypeScript:
index.ts(29,3): error TS2322: Type 'Common_tags_test' is not assignable to type '{ [key: string]: Input<string>; } | Promise<{ [key: string]: Input<string>; }> | OutputInstance<{ [key: string]: Input<string>; }> | undefined'.
Type 'Common_tags_test' is not assignable to type '{ [key: string]: Input<string>; }'.
Index signature is missing in type 'Common_tags_test'.
``
Because typescript allows for strongly typed properties, the tags
property isn't expecting a type of Common_tags_test
there.
You had the right idea before, doing something like
import * as aws from "@pulumi/aws";
const baseTags = {
Owner: "test",
ManagedBy: "Pulumi"
};
const vpc = new aws.ec2.Vpc("vpc", {
tags: {
...baseTags,
Name: "my-vpc"
},
cidrBlock: "10.10.0.0/"
})
If you really wanted to use a class you could do the following:
import * as aws from "@pulumi/aws";
class CommonTags {
Name!: string;
Owner: string = "test";
ManagedBy: string = "Pulumi";
constructor() {}
setTags(name: string) :any {
return {
Name: name,
Owner: this.Owner,
ManagedBy: this.ManagedBy
}
}
}
let mytags = new CommonTags();
const vpc = new aws.ec2.Vpc("vpc", {
tags: {
...mytags.setTags("My-VPC")
},
cidrBlock: "10.10.0.0/16",
});
Personally, I'd use the first way. It's still possible to re-use them by putting them in a module and sharing that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.