Is doable to use a generic type (T ) on a resource...
# golang
f
Is doable to use a generic type (T ) on a resource args field?. for example, when creating a Route:
Copy code
_, err = ec2.NewRoute(ctx, "my-route", &ec2.RouteArgs{
		RouteTableId:         routeTable.ID(),
		DestinationCidrBlock: pulumi.String("0.0.0.0/0"),
		GatewayId:            pulumi.String("igw-xxxf0axxx"), 
	})
On the GatewayId field, to have the type T or accept a generic type?. I am trying to don't repeat a lot of the same definitions to create routes. Some of my routes require TransitGatewayId or NatGatewayId instead. Do I have to create every single route repeating all the code and changing that field one by one?
m
You can have a loop, example of subnet creation I’m using, it creates 1 subnet / AZ for both private and public type of subnets:
Copy code
for _, subnetType := range subnetTypes {

			for _, az := range azs.Names {
				var mapPublicIp pulumi.Bool
				if subnetType == "private" {
					mapPublicIp = pulumi.Bool(false)
				} else {
					mapPublicIp = pulumi.Bool(true)
				}
				subnetName := fmt.Sprintf("%s-%s-%s", component, subnetType, az)
				subnet, err := ec2.NewSubnet(ctx, subnetName, &ec2.SubnetArgs{
					AvailabilityZone:    pulumi.String(az),
					VpcId:               vpc.ID(),
					CidrBlock:           pulumi.String(subnetCIDRBlocks[subnetCount].CIDR()),
					MapPublicIpOnLaunch: mapPublicIp,
					Tags: pulumi.StringMap{
						"Name":                   pulumi.String(subnetName),
						"<http://kubernetes.io/role/elb|kubernetes.io/role/elb>": pulumi.String("1"),
					},
				})
				if err != nil {
					return err
				}

				subnetIDs[subnetType] = append(subnetIDs[subnetType], subnet.ID())

				subnetCount = subnetCount + 1
			}

		}
f
I got some loops already for other resources but having it for routes is a bit different as every route is completely different and has different fields depending on the route table, destination and target. I went with using methods for a set of struct with encompass multiple use cases. The generic implementation I tried did not work as for now v1.19 this feature is not implemented in Go yet. Maybe on v1.20.