Is it possible to store an array-of-objects in my ...
# general
m
Is it possible to store an array-of-objects in my Pulumi configuration, and if so, how would I access that from code?
g
If i understand you correctly, TS and C# example. 🙂 (havent tested it, though it should be something like this if im not misstaken to much)
Copy code
config:
  myproject:servers:
    - name: "Server1"
      ip: "192.168.1.1"
      location: "New York"
    - name: "Server2"
      ip: "192.168.1.2"
      location: "London"
    - name: "Server3"
      ip: "192.168.1.3"
      location: "Tokyo"
Typescript
Copy code
import * as pulumi from "@pulumi/pulumi";

const config = new pulumi.Config();
const servers: { name: string, ip: string, location: string }[] = config.getObject("servers") || [];

servers.forEach((server, index) => {
    console.log(`Server ${index + 1}:`);
    console.log(`  Name: ${server.name}`);
    console.log(`  IP: ${server.ip}`);
    console.log(`  Location: ${server.location}`);
});
C#
Copy code
public class Server
{
    public string Name { get; set; }
    public string Ip { get; set; }
    public string Location { get; set; }
}

var config = new Config();
var servers = config.GetObject<List<Server>>("servers") ?? new List<Server>();

foreach (var server in servers)
{
            Console.WriteLine($"Server: {server.Name}, IP: {server.Ip}, Location: {server.Location}");
}
m
Ah, so
getObject
can get arrays as well?
At this time, configuration specifications are not supported for structured configuration.
This is unfortunate 😕
g
I'm assuming from your answer that you are using TS as language. I dont have much more than that, ugly way around would be to use a string as JSON and than parse that.
Copy code
config:
  myproject:servers: '[{}]'
Sorry man dont have much more.