All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.heroku.sdk.deploy.ConfigVars Maven / Gradle / Ivy

There is a newer version: 3.0.7
Show newest version
package com.heroku.sdk.deploy;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.heroku.api.HerokuAPI;

public class ConfigVars {

  private Deployer deployer;

  private HerokuAPI api;

  public ConfigVars(Deployer deployer, String apiKey) {
    this.deployer = deployer;
    this.api = new HerokuAPI(apiKey);
  }

  public void merge(Map configVars) throws Exception {
    Map existingConfigVars = getConfigVars();
    deployer.logDebug("Heroku existing config variables: " + existingConfigVars.keySet());

    Map newConfigVars = new HashMap();
    for (String key : configVars.keySet()) {
      newConfigVars.putAll(addConfigVar(key, configVars.get(key), existingConfigVars));
    }
    setConfigVars(newConfigVars);
  }

  protected Map getConfigVars() throws Exception {
    return this.api.listConfig(deployer.getName());
  }

  protected void setConfigVars(Map configVars) throws IOException {
    if (!configVars.isEmpty()) {
      api.updateConfig(deployer.getName(), configVars);
    }
  }

  private Map addConfigVar(String key, String value, Map existingConfigVars) {
    return addConfigVar(key, value, existingConfigVars, true);
  }

  private Map addConfigVar(String key, String value, Map existingConfigVars, Boolean force) {
    Map m = new HashMap();
    if (!existingConfigVars.containsKey(key) || (!value.equals(existingConfigVars.get(key)) && force)) {
      m.put(key, value);
    }
    return m;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy