@Override protected Account editAccount(GoogleAccount account) { account.setJsonPath(isSet(jsonPath) ? jsonPath : account.getJsonPath()); account.setProject(isSet(project) ? project : account.getProject()); account.setAlphaListed(alphaListed != null ? alphaListed : account.isAlphaListed()); account.setUserDataFile(userDataFile != null ? userDataFile : account.getUserDataFile()); try { account.setImageProjects( updateStringList(account.getImageProjects(), imageProjects, addImageProject, removeImageProject)); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Set either --image-projects or --[add/remove]-image-project"); } try { account.setRegions( updateStringList(account.getRegions(), regions, addRegion, removeRegion)); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Set either --regions or --[add/remove]-region"); } return account; } }
@Override public void validate(ConfigProblemSetBuilder p, GoogleAccount n) { DaemonTaskHandler.message("Validating " + n.getNodeName() + " with " + GoogleAccountValidator.class.getSimpleName()); GoogleNamedAccountCredentials credentials = n.getNamedAccountCredentials(halyardVersion, p); if (credentials == null) { return; Compute compute = credentials.getCompute(); compute.projects().get(n.getProject()).execute(); for (String imageProject : n.getImageProjects()) { compute.projects().get(imageProject).execute(); p.addProblem(Severity.ERROR, "Failed to load project \"" + n.getProject() + "\": " + e.getMessage() + "."); if (!StringUtils.isEmpty(n.getUserDataFile())) { userDataFile = ValidatingFileReader.contents(p, n.getUserDataFile());
@Override protected Account buildAccount(String accountName) { GoogleAccount account = (GoogleAccount) new GoogleAccount().setName(accountName); account = (GoogleAccount) account.setJsonPath(jsonPath) .setProject(project); account.setAlphaListed(alphaListed) .setImageProjects(imageProjects) .setUserDataFile(userDataFile) .setRegions(regions); return account; }
@JsonIgnore public GoogleNamedAccountCredentials getNamedAccountCredentials(String version, ConfigProblemSetBuilder p) { String jsonKey = null; if (!StringUtils.isEmpty(getJsonPath())) { jsonKey = ValidatingFileReader.contents(p, getJsonPath()); if (jsonKey == null) { return null; } else if (jsonKey.isEmpty()) { p.addProblem(Problem.Severity.WARNING, "The supplied credentials file is empty."); } } if (StringUtils.isEmpty(getProject())) { p.addProblem(Problem.Severity.ERROR, "No google project supplied."); return null; } try { return new GoogleNamedAccountCredentials.Builder() .jsonKey(jsonKey) .project(getProject()) .computeVersion(isAlphaListed() ? ComputeVersion.ALPHA : ComputeVersion.DEFAULT) .imageProjects(getImageProjects()) .applicationName("halyard " + version) .liveLookupsEnabled(false) .build(); } catch (Exception e) { p.addProblem(Problem.Severity.ERROR, "Error instantiating Google credentials: " + e.getMessage() + ".") .setRemediation("Do the provided credentials have access to project " + getProject() + "?"); return null; } }
static String getInstanceIp(AccountDeploymentDetails<GoogleAccount> details, String instanceName) { Compute compute = getCompute(details); Instance instance = null; try { instance = compute.instances().get(details.getAccount().getProject(), "us-central1-f", instanceName).execute(); } catch (IOException e) { throw new HalException(FATAL, "Unable to get instance " + instanceName); } return instance.getNetworkInterfaces() .stream() .map(i -> i.getAccessConfigs().stream() .map(AccessConfig::getNatIP) .filter(ip -> !StringUtils.isEmpty(ip)) .findFirst() ).filter(Optional::isPresent) .map(Optional::get) .findFirst() .orElseThrow(() -> new HalException(FATAL, "No public IP associated with" + instanceName)); }
static Compute getCompute(AccountDeploymentDetails<GoogleAccount> details) { ConfigProblemSetBuilder problemSetBuilder = new ConfigProblemSetBuilder(null); GoogleNamedAccountCredentials credentials = details.getAccount().getNamedAccountCredentials("", problemSetBuilder); if (credentials == null) { throw new HalException(problemSetBuilder.build().getProblems()); } return credentials.getCompute(); }
@Override protected Account emptyAccount() { return new GoogleAccount(); } }
String accountName = account.getName();
@JsonIgnore public GoogleNamedAccountCredentials getNamedAccountCredentials(String version, ConfigProblemSetBuilder p) { String jsonKey = null; if (!StringUtils.isEmpty(getJsonPath())) { jsonKey = ValidatingFileReader.contents(p, getJsonPath()); if (jsonKey == null) { return null; } else if (jsonKey.isEmpty()) { p.addProblem(Problem.Severity.WARNING, "The supplied credentials file is empty."); } } if (StringUtils.isEmpty(getProject())) { p.addProblem(Problem.Severity.ERROR, "No google project supplied."); return null; } try { return new GoogleNamedAccountCredentials.Builder() .jsonKey(jsonKey) .project(getProject()) .computeVersion(isAlphaListed() ? ComputeVersion.ALPHA : ComputeVersion.DEFAULT) .imageProjects(getImageProjects()) .applicationName("halyard " + version) .liveLookupsEnabled(false) .build(); } catch (Exception e) { p.addProblem(Problem.Severity.ERROR, "Error instantiating Google credentials: " + e.getMessage() + ".") .setRemediation("Do the provided credentials have access to project " + getProject() + "?"); return null; } }
static String getInstanceIp(AccountDeploymentDetails<GoogleAccount> details, String instanceName) { Compute compute = getCompute(details); Instance instance = null; try { instance = compute.instances().get(details.getAccount().getProject(), "us-central1-f", instanceName).execute(); } catch (IOException e) { throw new HalException(FATAL, "Unable to get instance " + instanceName); } return instance.getNetworkInterfaces() .stream() .map(i -> i.getAccessConfigs().stream() .map(AccessConfig::getNatIP) .filter(ip -> !StringUtils.isEmpty(ip)) .findFirst() ).filter(Optional::isPresent) .map(Optional::get) .findFirst() .orElseThrow(() -> new HalException(FATAL, "No public IP associated with" + instanceName)); }
static Compute getCompute(AccountDeploymentDetails<GoogleAccount> details) { ConfigProblemSetBuilder problemSetBuilder = new ConfigProblemSetBuilder(null); GoogleNamedAccountCredentials credentials = details.getAccount().getNamedAccountCredentials("", problemSetBuilder); if (credentials == null) { throw new HalException(problemSetBuilder.build().getProblems()); } return credentials.getCompute(); }
@Override protected Account emptyAccount() { return new GoogleAccount(); } }
String accountName = account.getName();
@Override protected Account editAccount(GoogleAccount account) { account.setJsonPath(isSet(jsonPath) ? jsonPath : account.getJsonPath()); account.setProject(isSet(project) ? project : account.getProject()); account.setAlphaListed(alphaListed != null ? alphaListed : account.isAlphaListed()); try { account.setImageProjects( updateStringList(account.getImageProjects(), imageProjects, addImageProject, removeImageProject)); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Set either --image-projects or --[add/remove]-image-project"); } try { account.setRegions( updateStringList(account.getRegions(), regions, addRegion, removeRegion)); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Set either --regions or --[add/remove]-region"); } return account; } }
@Override public void validate(ConfigProblemSetBuilder p, GoogleAccount n) { DaemonTaskHandler.message("Validating " + n.getNodeName() + " with " + GoogleAccountValidator.class.getSimpleName()); GoogleNamedAccountCredentials credentials = n.getNamedAccountCredentials(halyardVersion, p); if (credentials == null) { return; Compute compute = credentials.getCompute(); compute.projects().get(n.getProject()).execute(); for (String imageProject : n.getImageProjects()) { compute.projects().get(imageProject).execute(); p.addProblem(Severity.ERROR, "Failed to load project \"" + n.getProject() + "\": " + e.getMessage() + "."); if (!StringUtils.isEmpty(n.getUserDataFile())) { userDataFile = ValidatingFileReader.contents(p, n.getUserDataFile());
static public void resize(AccountDeploymentDetails<GoogleAccount> details, String zone, String managedInstaceGroupName, int targetSize) { Compute compute = getCompute(details); try { compute.instanceGroupManagers().resize(details.getAccount().getProject(), zone, managedInstaceGroupName, targetSize); } catch (IOException e) { throw new HalException(FATAL, "Unable to resize instance group manager " + managedInstaceGroupName + ": " + e.getMessage(), e); } }
@Override protected Account buildAccount(String accountName) { GoogleAccount account = (GoogleAccount) new GoogleAccount().setName(accountName); account = (GoogleAccount) account.setJsonPath(jsonPath) .setProject(project); account.setAlphaListed(alphaListed) .setImageProjects(imageProjects) .setUserDataFile(userDataFile) .setRegions(regions); return account; }
static public void resize(AccountDeploymentDetails<GoogleAccount> details, String zone, String managedInstaceGroupName, int targetSize) { Compute compute = getCompute(details); try { compute.instanceGroupManagers().resize(details.getAccount().getProject(), zone, managedInstaceGroupName, targetSize); } catch (IOException e) { throw new HalException(FATAL, "Unable to resize instance group manager " + managedInstaceGroupName + ": " + e.getMessage(), e); } }
static String defaultServiceAccount(AccountDeploymentDetails<GoogleAccount> details) { GoogleAccount account = details.getAccount(); String project = account.getProject(); Compute compute = getCompute(details); try { return compute.projects().get(project).execute().getDefaultServiceAccount(); } catch (IOException e) { throw new HalException(FATAL, "Unable to get default compute service account"); } }
static String defaultServiceAccount(AccountDeploymentDetails<GoogleAccount> details) { GoogleAccount account = details.getAccount(); String project = account.getProject(); Compute compute = getCompute(details); try { return compute.projects().get(project).execute().getDefaultServiceAccount(); } catch (IOException e) { throw new HalException(FATAL, "Unable to get default compute service account"); } }