samedi 23 décembre 2017

How can I represent key with list of values for each environment type in an Enum?

I have two environment PROD and STAGING. In prod environment we have three datacenters ABC, DEF and PQR and staging has one datacenter CORP. Each datacenter has few machines and I have constant defined for them as shown below:

// NOTE: I can have more machines in each dc in future
public static final ImmutableList<String> ABC_SERVERS = ImmutableList.of("tcp://machineA:8081", "tcp://machineA:8082");
public static final ImmutableList<String> DEF_SERVERS = ImmutableList.of("tcp://machineB:8081", "tcp://machineB:8082");
public static final ImmutableList<String> PQR_SERVERS = ImmutableList.of("tcp://machineC:8081", "tcp://machineC:8082");

public static final ImmutableList<String> STAGING_SERVERS = ImmutableList.of("tcp://machineJ:8087","tcp://machineJ:8088");

Now I have another constant defined in the same class which groups by DC to List of machines for each environment type.

public static final ImmutableMap<Datacenter, ImmutableList<String>> PROD_SERVERS_BY_DC =
  ImmutableMap.<Datacenter, ImmutableList<String>>builder()
      .put(Datacenter.ABC, ABC_SERVERS).put(Datacenter.DEF, DEF_SERVERS)
      .put(Datacenter.PQR, PQR_SERVERS).build();

public static final ImmutableMap<Datacenter, ImmutableList<String>> STAGING_SERVERS_BY_DC =
  ImmutableMap.<Datacenter, ImmutableList<String>>builder()
      .put(Datacenter.CORP, STAGING_SERVERS).build();

Now in some other class, basis on what environment I am in (Utils.isProd()), I get either PROD_SERVERS_BY_DC or STAGING_SERVERS_BY_DC.

Map<Datacenter, ImmutableList<String>> machinesByDC = Utils.isProd() ? Utils.PROD_SERVERS_BY_DC : Utils.STAGING_SERVERS_BY_DC;

Now I think this can be represented in much better way in some sort of Enum instead of having constants defined like I have above but I am not able to figure out how can I do that? I started off with this but got confuse on how can I have single key for each DC and then multiple values as List of machines for that DC and then I need to group them by environment as well.

// got confuse on how can I make key (DC) and list of values (machines) for each environment type.
public enum DCoMachines {
  abc(tcp://machineA:8081", "tcp://machineA:8082"), 

  private final String machines;


}

Aucun commentaire:

Enregistrer un commentaire