6
6
7
7
package com .offbytwo .jenkins ;
8
8
9
- import java .io .IOException ;
10
- import java .net .URI ;
11
- import java .util .List ;
12
- import java .util .Map ;
13
-
14
- import javax .xml .bind .JAXBException ;
15
-
16
- import org .apache .http .HttpStatus ;
17
- import org .apache .http .client .HttpResponseException ;
18
- import org .apache .http .entity .ContentType ;
19
- import org .dom4j .DocumentException ;
20
- import org .slf4j .Logger ;
21
- import org .slf4j .LoggerFactory ;
22
-
23
9
import com .google .common .base .Function ;
24
10
import com .google .common .base .Optional ;
25
11
import com .google .common .collect .ImmutableMap ;
26
12
import com .google .common .collect .Maps ;
27
13
import com .offbytwo .jenkins .client .JenkinsHttpClient ;
28
14
import com .offbytwo .jenkins .client .util .EncodingUtils ;
29
15
import com .offbytwo .jenkins .helper .JenkinsVersion ;
30
- import com .offbytwo .jenkins .model .Build ;
31
- import com .offbytwo .jenkins .model .Computer ;
32
- import com .offbytwo .jenkins .model .ComputerSet ;
33
- import com .offbytwo .jenkins .model .FolderJob ;
34
- import com .offbytwo .jenkins .model .Job ;
35
- import com .offbytwo .jenkins .model .JobConfiguration ;
36
- import com .offbytwo .jenkins .model .JobWithDetails ;
37
- import com .offbytwo .jenkins .model .LabelWithDetails ;
38
- import com .offbytwo .jenkins .model .MainView ;
39
- import com .offbytwo .jenkins .model .MavenJobWithDetails ;
40
- import com .offbytwo .jenkins .model .PluginManager ;
41
- import com .offbytwo .jenkins .model .Queue ;
42
- import com .offbytwo .jenkins .model .QueueItem ;
43
- import com .offbytwo .jenkins .model .QueueReference ;
44
- import com .offbytwo .jenkins .model .View ;
16
+ import com .offbytwo .jenkins .model .*;
17
+ import com .offbytwo .jenkins .model .credentials .Credential ;
18
+ import com .offbytwo .jenkins .model .credentials .CredentialManager ;
19
+ import org .apache .commons .collections .CollectionUtils ;
20
+ import org .apache .commons .collections .Predicate ;
21
+ import org .apache .http .HttpStatus ;
22
+ import org .apache .http .client .HttpResponseException ;
23
+ import org .apache .http .entity .ContentType ;
24
+ import org .dom4j .DocumentException ;
25
+ import org .slf4j .Logger ;
26
+ import org .slf4j .LoggerFactory ;
27
+
28
+ import javax .xml .bind .JAXBException ;
29
+ import java .io .IOException ;
30
+ import java .net .URI ;
31
+ import java .rmi .server .ExportException ;
32
+ import java .util .List ;
33
+ import java .util .Map ;
45
34
46
35
/**
47
36
* The main starting point for interacting with a Jenkins server.
@@ -51,6 +40,8 @@ public class JenkinsServer {
51
40
52
41
private final JenkinsHttpClient client ;
53
42
43
+ private CredentialManager credentialManager ;
44
+
54
45
/**
55
46
* Create a new Jenkins server reference given only the server address
56
47
*
@@ -917,4 +908,83 @@ private String toViewBaseUrl(FolderJob folder, String name) {
917
908
return toBaseUrl (folder ) + "view/" + EncodingUtils .encode (name );
918
909
}
919
910
911
+ /**
912
+ * List the credentials from the Jenkins server.
913
+ * @return a hash map of the credentials. The key is the id of each credential.
914
+ * @throws IOException
915
+ */
916
+ public Map <String , Credential > listCredentials () throws IOException {
917
+ return this .getCredentialManager ().listCredentials ();
918
+ }
919
+
920
+ /**
921
+ * Create the given credential
922
+ * @param credential a credential instance
923
+ * @param crumbFlag
924
+ * @throws IOException
925
+ */
926
+ public void createCredential (Credential credential , boolean crumbFlag ) throws IOException {
927
+ this .getCredentialManager ().createCredential (credential , crumbFlag );
928
+ }
929
+
930
+ /**
931
+ * Update an existing credential
932
+ * @param credentialId the id of the credential
933
+ * @param credential the updated credential instance
934
+ * @param crumbFlag
935
+ * @throws IOException
936
+ */
937
+ public void updateCredential (String credentialId , Credential credential , boolean crumbFlag ) throws IOException {
938
+ this .getCredentialManager ().updateCredential (credentialId , credential , crumbFlag );
939
+ }
940
+
941
+ /**
942
+ * Delete an existing credential
943
+ * @param credentialId the id of the credential to delete
944
+ * @param crumbFlag
945
+ * @throws IOException
946
+ */
947
+ public void deleteCredential (String credentialId , boolean crumbFlag ) throws IOException {
948
+ this .getCredentialManager ().deleteCredential (credentialId , crumbFlag );
949
+ }
950
+
951
+ /**
952
+ * Return the credentialManager instance. Will initialise it if it's never used before.
953
+ * @return the credentialManager instance
954
+ * @throws IOException
955
+ */
956
+ private CredentialManager getCredentialManager () throws IOException {
957
+ if (this .credentialManager == null ) {
958
+ Plugin credentialPlugin = findPluginWithName ("credentials" );
959
+ if (credentialPlugin == null ) {
960
+ throw new ExportException ("credential plugin is not installed" );
961
+ }
962
+ String version = credentialPlugin .getVersion ();
963
+ this .credentialManager = new CredentialManager (version , this .client );
964
+ }
965
+ return this .credentialManager ;
966
+ }
967
+
968
+ /**
969
+ * Find a plugin that matches the given short name
970
+ * @param pluginShortName the short name of the plugin to find
971
+ * @return the pluin object that is found. Can be null if no match found.
972
+ * @throws IOException
973
+ */
974
+ public Plugin findPluginWithName (final String pluginShortName ) throws IOException {
975
+ List <Plugin > plugins = this .getPluginManager ().getPlugins ();
976
+ Object foundPlugin = CollectionUtils .find (plugins , new Predicate () {
977
+ @ Override
978
+ public boolean evaluate (Object o ) {
979
+ Plugin p = (Plugin ) o ;
980
+ if (p .getShortName ().equalsIgnoreCase (pluginShortName )) {
981
+ return true ;
982
+ } else {
983
+ return false ;
984
+ }
985
+ }
986
+ });
987
+
988
+ return foundPlugin == null ? null : (Plugin ) foundPlugin ;
989
+ }
920
990
}
0 commit comments