Skip to content

Commit 20786a9

Browse files
authored
Use Jenkins project-wide formatter configuration (#366)
1 parent 8c9c0ae commit 20786a9

25 files changed

+3206
-3468
lines changed

pom.xml

Lines changed: 126 additions & 169 deletions
Large diffs are not rendered by default.

src/main/java/com/google/jenkins/plugins/k8sengine/ClusterUtil.java

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -23,58 +23,58 @@
2323

2424
/** Utility functions for converting between {@link Cluster}s and their String representations. */
2525
class ClusterUtil {
26-
/**
27-
* Given a GKE {@link Cluster} return a String representation containing the name and location.
28-
*
29-
* @param cluster The {@link Cluster} object to extract values from.
30-
* @return A String of the form "name (location)" based on the given cluster's properties.
31-
*/
32-
static String toNameAndLocation(Cluster cluster) {
33-
Preconditions.checkNotNull(cluster);
34-
return toNameAndLocation(cluster.getName(), cluster.getLocation());
35-
}
26+
/**
27+
* Given a GKE {@link Cluster} return a String representation containing the name and location.
28+
*
29+
* @param cluster The {@link Cluster} object to extract values from.
30+
* @return A String of the form "name (location)" based on the given cluster's properties.
31+
*/
32+
static String toNameAndLocation(Cluster cluster) {
33+
Preconditions.checkNotNull(cluster);
34+
return toNameAndLocation(cluster.getName(), cluster.getLocation());
35+
}
3636

37-
/**
38-
* Given a name and location for a cluster, return the combined String representation.
39-
*
40-
* @param name A non-empty cluster name
41-
* @param location A non-empty GCP resource location.
42-
* @return A String of the form "name (location)".
43-
*/
44-
static String toNameAndLocation(String name, String location) {
45-
Preconditions.checkArgument(!Strings.isNullOrEmpty(name));
46-
Preconditions.checkArgument(!Strings.isNullOrEmpty(location));
47-
return String.format("%s (%s)", name, location);
48-
}
37+
/**
38+
* Given a name and location for a cluster, return the combined String representation.
39+
*
40+
* @param name A non-empty cluster name
41+
* @param location A non-empty GCP resource location.
42+
* @return A String of the form "name (location)".
43+
*/
44+
static String toNameAndLocation(String name, String location) {
45+
Preconditions.checkArgument(!Strings.isNullOrEmpty(name));
46+
Preconditions.checkArgument(!Strings.isNullOrEmpty(location));
47+
return String.format("%s (%s)", name, location);
48+
}
4949

50-
/**
51-
* Only used for mocking the {@link
52-
* com.google.cloud.graphite.platforms.plugin.client.ContainerClient}. Constructs a {@link
53-
* Cluster} from the given nameAndLocation value.
54-
*
55-
* @param nameAndLocation A non-empty String of the form "name (location)"
56-
* @return A cluster with the name and location properties from the provided nameAndLocation.
57-
*/
58-
@VisibleForTesting
59-
static Cluster fromNameAndLocation(String nameAndLocation) {
60-
Preconditions.checkArgument(!Strings.isNullOrEmpty(nameAndLocation));
61-
String[] values = valuesFromNameAndLocation(nameAndLocation);
62-
return new Cluster().setName(values[0]).setLocation(values[1]);
63-
}
50+
/**
51+
* Only used for mocking the {@link
52+
* com.google.cloud.graphite.platforms.plugin.client.ContainerClient}. Constructs a {@link
53+
* Cluster} from the given nameAndLocation value.
54+
*
55+
* @param nameAndLocation A non-empty String of the form "name (location)"
56+
* @return A cluster with the name and location properties from the provided nameAndLocation.
57+
*/
58+
@VisibleForTesting
59+
static Cluster fromNameAndLocation(String nameAndLocation) {
60+
Preconditions.checkArgument(!Strings.isNullOrEmpty(nameAndLocation));
61+
String[] values = valuesFromNameAndLocation(nameAndLocation);
62+
return new Cluster().setName(values[0]).setLocation(values[1]);
63+
}
6464

65-
/**
66-
* Extracts the individual values from a combined nameAndLocation String.
67-
*
68-
* @param nameAndLocation A non-empty String of the form "name (location)"
69-
* @return The String array {name, location} from the provided value.
70-
*/
71-
static String[] valuesFromNameAndLocation(String nameAndLocation) {
72-
Preconditions.checkArgument(!Strings.isNullOrEmpty(nameAndLocation));
73-
String[] clusters = nameAndLocation.split(" [(]");
74-
if (clusters.length != 2) {
75-
throw new IllegalArgumentException("nameAndLocation should be of the form 'name (location)'");
65+
/**
66+
* Extracts the individual values from a combined nameAndLocation String.
67+
*
68+
* @param nameAndLocation A non-empty String of the form "name (location)"
69+
* @return The String array {name, location} from the provided value.
70+
*/
71+
static String[] valuesFromNameAndLocation(String nameAndLocation) {
72+
Preconditions.checkArgument(!Strings.isNullOrEmpty(nameAndLocation));
73+
String[] clusters = nameAndLocation.split(" [(]");
74+
if (clusters.length != 2) {
75+
throw new IllegalArgumentException("nameAndLocation should be of the form 'name (location)'");
76+
}
77+
clusters[1] = clusters[1].substring(0, clusters[1].length() - 1);
78+
return clusters;
7679
}
77-
clusters[1] = clusters[1].substring(0, clusters[1].length() - 1);
78-
return clusters;
79-
}
8080
}

src/main/java/com/google/jenkins/plugins/k8sengine/CredentialsUtil.java

Lines changed: 104 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -35,128 +35,120 @@
3535

3636
/** Provides a library of utility functions for credentials-related work. */
3737
public class CredentialsUtil {
38-
/**
39-
* Get the Google Robot Credentials for the given credentialsId.
40-
*
41-
* @param itemGroup A handle to the Jenkins instance. Must be non-null.
42-
* @param domainRequirements A list of domain requirements. Must be non-null.
43-
* @param credentialsId The ID of the GoogleRobotCredentials to be retrieved from Jenkins and
44-
* utilized for authorization. Must be non-empty or non-null and exist in credentials store.
45-
* @return Google Robot Credential for the given credentialsId.
46-
* @throws AbortException If there was an issue retrieving the Google Robot Credentials.
47-
*/
48-
public static GoogleRobotCredentials getRobotCredentials(
49-
ItemGroup itemGroup,
50-
ImmutableList<DomainRequirement> domainRequirements,
51-
String credentialsId)
52-
throws AbortException {
53-
Preconditions.checkNotNull(itemGroup);
54-
Preconditions.checkNotNull(domainRequirements);
55-
Preconditions.checkArgument(!Strings.isNullOrEmpty(credentialsId));
38+
/**
39+
* Get the Google Robot Credentials for the given credentialsId.
40+
*
41+
* @param itemGroup A handle to the Jenkins instance. Must be non-null.
42+
* @param domainRequirements A list of domain requirements. Must be non-null.
43+
* @param credentialsId The ID of the GoogleRobotCredentials to be retrieved from Jenkins and
44+
* utilized for authorization. Must be non-empty or non-null and exist in credentials store.
45+
* @return Google Robot Credential for the given credentialsId.
46+
* @throws AbortException If there was an issue retrieving the Google Robot Credentials.
47+
*/
48+
public static GoogleRobotCredentials getRobotCredentials(
49+
ItemGroup itemGroup, ImmutableList<DomainRequirement> domainRequirements, String credentialsId)
50+
throws AbortException {
51+
Preconditions.checkNotNull(itemGroup);
52+
Preconditions.checkNotNull(domainRequirements);
53+
Preconditions.checkArgument(!Strings.isNullOrEmpty(credentialsId));
5654

57-
GoogleRobotCredentials robotCreds =
58-
CredentialsMatchers.firstOrNull(
59-
CredentialsProvider.lookupCredentials(
60-
GoogleRobotCredentials.class, itemGroup, ACL.SYSTEM, domainRequirements),
61-
CredentialsMatchers.withId(credentialsId));
55+
GoogleRobotCredentials robotCreds = CredentialsMatchers.firstOrNull(
56+
CredentialsProvider.lookupCredentials(
57+
GoogleRobotCredentials.class, itemGroup, ACL.SYSTEM, domainRequirements),
58+
CredentialsMatchers.withId(credentialsId));
6259

63-
if (robotCreds == null) {
64-
throw new AbortException(Messages.ClientFactory_FailedToRetrieveCredentials(credentialsId));
60+
if (robotCreds == null) {
61+
throw new AbortException(Messages.ClientFactory_FailedToRetrieveCredentials(credentialsId));
62+
}
63+
64+
return robotCreds;
6565
}
6666

67-
return robotCreds;
68-
}
67+
/**
68+
* Get the Credential from the Google robot credentials for GKE access.
69+
*
70+
* @param robotCreds Google Robot Credential for desired service account.
71+
* @return Google Credential for the service account.
72+
* @throws AbortException if there was an error initializing HTTP transport.
73+
*/
74+
public static Credential getGoogleCredential(GoogleRobotCredentials robotCreds) throws AbortException {
75+
Credential credential;
76+
try {
77+
credential = robotCreds.getGoogleCredential(new ContainerScopeRequirement());
78+
} catch (GeneralSecurityException gse) {
79+
throw new AbortException(Messages.ClientFactory_FailedToInitializeHTTPTransport(gse.getMessage()));
80+
}
6981

70-
/**
71-
* Get the Credential from the Google robot credentials for GKE access.
72-
*
73-
* @param robotCreds Google Robot Credential for desired service account.
74-
* @return Google Credential for the service account.
75-
* @throws AbortException if there was an error initializing HTTP transport.
76-
*/
77-
public static Credential getGoogleCredential(GoogleRobotCredentials robotCreds)
78-
throws AbortException {
79-
Credential credential;
80-
try {
81-
credential = robotCreds.getGoogleCredential(new ContainerScopeRequirement());
82-
} catch (GeneralSecurityException gse) {
83-
throw new AbortException(
84-
Messages.ClientFactory_FailedToInitializeHTTPTransport(gse.getMessage()));
82+
return credential;
8583
}
8684

87-
return credential;
88-
}
89-
90-
/**
91-
* Given a credentialsId and Jenkins context, returns the access token.
92-
*
93-
* @param itemGroup A handle to the Jenkins instance. Must be non-null.
94-
* @param credentialsId The service account credential's id. Must be non-null.
95-
* @return Access token from OAuth to allow kubectl to interact with the cluster.
96-
* @throws IOException If an error occurred fetching the access token.
97-
*/
98-
static String getAccessToken(ItemGroup itemGroup, String credentialsId) throws IOException {
99-
Preconditions.checkArgument(!Strings.isNullOrEmpty(credentialsId));
100-
Preconditions.checkNotNull(itemGroup);
101-
GoogleRobotCredentials robotCreds =
102-
getRobotCredentials(itemGroup, ImmutableList.of(), credentialsId);
85+
/**
86+
* Given a credentialsId and Jenkins context, returns the access token.
87+
*
88+
* @param itemGroup A handle to the Jenkins instance. Must be non-null.
89+
* @param credentialsId The service account credential's id. Must be non-null.
90+
* @return Access token from OAuth to allow kubectl to interact with the cluster.
91+
* @throws IOException If an error occurred fetching the access token.
92+
*/
93+
static String getAccessToken(ItemGroup itemGroup, String credentialsId) throws IOException {
94+
Preconditions.checkArgument(!Strings.isNullOrEmpty(credentialsId));
95+
Preconditions.checkNotNull(itemGroup);
96+
GoogleRobotCredentials robotCreds = getRobotCredentials(itemGroup, ImmutableList.of(), credentialsId);
10397

104-
Credential googleCredential = getGoogleCredential(robotCreds);
105-
return getAccessToken(googleCredential);
106-
}
107-
/**
108-
* Wrapper to get access token for service account with this credentialsId. Uses Jenkins.get() as
109-
* context.
110-
*
111-
* @param credentialsId The service account credential's id. Must be non-null.
112-
* @return Access token from OAuth to allow kubectl to interact with the cluster.
113-
* @throws IOException If an error occurred fetching the access token.
114-
*/
115-
static String getAccessToken(String credentialsId) throws IOException {
116-
return getAccessToken(Jenkins.get(), credentialsId);
117-
}
98+
Credential googleCredential = getGoogleCredential(robotCreds);
99+
return getAccessToken(googleCredential);
100+
}
101+
/**
102+
* Wrapper to get access token for service account with this credentialsId. Uses Jenkins.get() as
103+
* context.
104+
*
105+
* @param credentialsId The service account credential's id. Must be non-null.
106+
* @return Access token from OAuth to allow kubectl to interact with the cluster.
107+
* @throws IOException If an error occurred fetching the access token.
108+
*/
109+
static String getAccessToken(String credentialsId) throws IOException {
110+
return getAccessToken(Jenkins.get(), credentialsId);
111+
}
118112

119-
/**
120-
* Given the Google Credential, retrieve the access token.
121-
*
122-
* @param googleCredential Google Credential to get an access token. Must be non-null.
123-
* @return Access token from OAuth to allow kubectl to interact with the cluster.
124-
* @throws IOException If an error occured fetching the access token.
125-
*/
126-
static String getAccessToken(Credential googleCredential) throws IOException {
127-
Preconditions.checkNotNull(googleCredential);
113+
/**
114+
* Given the Google Credential, retrieve the access token.
115+
*
116+
* @param googleCredential Google Credential to get an access token. Must be non-null.
117+
* @return Access token from OAuth to allow kubectl to interact with the cluster.
118+
* @throws IOException If an error occured fetching the access token.
119+
*/
120+
static String getAccessToken(Credential googleCredential) throws IOException {
121+
Preconditions.checkNotNull(googleCredential);
128122

129-
googleCredential.refreshToken();
130-
return googleCredential.getAccessToken();
131-
}
123+
googleCredential.refreshToken();
124+
return googleCredential.getAccessToken();
125+
}
132126

133-
/**
134-
* Given a credentialsId and Jenkins context, return the default project ID for the service
135-
* account credentials specified.
136-
*
137-
* @param itemGroup A handle to the Jenkins instance. Must be non-null.
138-
* @param credentialsId The service account credential's ID. Must be non-null.
139-
* @return The project ID specified when creating the credential in the Jenkins credential store.
140-
* @throws AbortException If an error occurred fetching the credential.
141-
*/
142-
static String getDefaultProjectId(ItemGroup itemGroup, String credentialsId)
143-
throws AbortException {
144-
Preconditions.checkArgument(!Strings.isNullOrEmpty(credentialsId));
145-
Preconditions.checkNotNull(itemGroup);
146-
GoogleRobotCredentials robotCreds =
147-
getRobotCredentials(itemGroup, ImmutableList.of(), credentialsId);
148-
return Strings.isNullOrEmpty(robotCreds.getProjectId()) ? "" : robotCreds.getProjectId();
149-
}
127+
/**
128+
* Given a credentialsId and Jenkins context, return the default project ID for the service
129+
* account credentials specified.
130+
*
131+
* @param itemGroup A handle to the Jenkins instance. Must be non-null.
132+
* @param credentialsId The service account credential's ID. Must be non-null.
133+
* @return The project ID specified when creating the credential in the Jenkins credential store.
134+
* @throws AbortException If an error occurred fetching the credential.
135+
*/
136+
static String getDefaultProjectId(ItemGroup itemGroup, String credentialsId) throws AbortException {
137+
Preconditions.checkArgument(!Strings.isNullOrEmpty(credentialsId));
138+
Preconditions.checkNotNull(itemGroup);
139+
GoogleRobotCredentials robotCreds = getRobotCredentials(itemGroup, ImmutableList.of(), credentialsId);
140+
return Strings.isNullOrEmpty(robotCreds.getProjectId()) ? "" : robotCreds.getProjectId();
141+
}
150142

151-
/**
152-
* Wrapper to get the default project ID for a credential using Jenkins.get() as the context.
153-
*
154-
* @param credentialsId The service account credential's ID. Must be non-null.
155-
* @return The project ID specified when creating the credential in the Jenkins credential store.
156-
* @throws AbortException If an error occurred fetching the credential.
157-
*/
158-
static String getDefaultProjectId(String credentialsId) throws AbortException {
159-
Preconditions.checkArgument(!Strings.isNullOrEmpty(credentialsId));
160-
return getDefaultProjectId(Jenkins.get(), credentialsId);
161-
}
143+
/**
144+
* Wrapper to get the default project ID for a credential using Jenkins.get() as the context.
145+
*
146+
* @param credentialsId The service account credential's ID. Must be non-null.
147+
* @return The project ID specified when creating the credential in the Jenkins credential store.
148+
* @throws AbortException If an error occurred fetching the credential.
149+
*/
150+
static String getDefaultProjectId(String credentialsId) throws AbortException {
151+
Preconditions.checkArgument(!Strings.isNullOrEmpty(credentialsId));
152+
return getDefaultProjectId(Jenkins.get(), credentialsId);
153+
}
162154
}

0 commit comments

Comments
 (0)