Skip to content

Commit

Permalink
refactor(gcb): Further clean-up of GCB config (#742)
Browse files Browse the repository at this point in the history
Given that we are enforcing a non-null account name and project
downstream, let's enforce them at object creation to make things
cleaner.

This commit also removes the subscriptionName field; it is
part of the config but not needed by igor, so there's no sense
mapping it.

Finally, make jsonKey an optional to better reflect that it is
optional (instead of treating an empty string as a sentinel
value).

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
ezimanyi and mergify[bot] authored May 12, 2020
1 parent 67f3fdb commit 75f28bd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.netflix.spinnaker.igor.config;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.netflix.spinnaker.fiat.model.resources.Permissions;
import com.netflix.spinnaker.igor.model.BuildServiceProvider;
Expand Down Expand Up @@ -43,25 +44,22 @@ public List<BuildService> getGcbBuildServices() {
@NonnullByDefault
@Value
public static final class Account implements BuildService {
private static final String ERROR_TEMPLATE = "Missing required field for GCB account %s: %s";

private final String name;
private final String project;
private final String subscriptionName;
private final String jsonKey;
private final Optional<String> jsonKey;
private final Permissions permissions;

@Builder
@ConstructorBinding
@ParametersAreNullableByDefault
public Account(
String name,
String project,
String subscriptionName,
String jsonKey,
Permissions.Builder permissions) {
this.name = Strings.nullToEmpty(name);
this.project = Strings.nullToEmpty(project);
this.subscriptionName = Strings.nullToEmpty(subscriptionName);
this.jsonKey = Strings.nullToEmpty(jsonKey);
public Account(String name, String project, String jsonKey, Permissions.Builder permissions) {
this.name = Preconditions.checkNotNull(Strings.emptyToNull(name), ERROR_TEMPLATE, "", "name");
this.project =
Preconditions.checkNotNull(
Strings.emptyToNull(project), ERROR_TEMPLATE, this.name, "project");
this.jsonKey = Optional.ofNullable(Strings.emptyToNull(jsonKey));
this.permissions =
Optional.ofNullable(permissions)
.map(Permissions.Builder::build)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ GoogleCloudBuildAccount build(GoogleCloudBuildProperties.Account account) {
}

private GoogleCredentials getCredentials(GoogleCloudBuildProperties.Account account) {
if (account.getJsonKey().isEmpty()) {
return credentialService.getApplicationDefault();
if (account.getJsonKey().isPresent()) {
return credentialService.getFromKey(account.getJsonKey().get());
} else {
return credentialService.getFromKey(account.getJsonKey());
return credentialService.getApplicationDefault();
}
}
}

0 comments on commit 75f28bd

Please sign in to comment.