-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDevOpsConfiguration.java
More file actions
518 lines (440 loc) · 17.2 KB
/
DevOpsConfiguration.java
File metadata and controls
518 lines (440 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
package io.jenkins.plugins.config;
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.AbstractIdCredentialsListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import hudson.Extension;
import hudson.model.ItemGroup;
import hudson.security.ACL;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import io.jenkins.plugins.utils.CommUtils;
import io.jenkins.plugins.utils.DevOpsConstants;
import io.jenkins.plugins.utils.GenericUtils;
import jenkins.model.GlobalConfiguration;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.acegisecurity.Authentication;
import org.jenkinsci.Symbol;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import javax.annotation.Nonnull;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
@Extension
@Symbol("snDevOpsConfig")
public class DevOpsConfiguration extends GlobalConfiguration {
private static final Logger LOGGER = Logger.getLogger(DevOpsConfiguration.class.getName());
private boolean snDevopsEnabled;
private String instanceUrl;
private String apiVersion;
private String toolId;
private String snArtifactToolId; // Skipping validation for Artifact tool Id as it is an optional parameter.
private boolean debug;
private String logLevel;
private String credentialsId;
private String user;
private String pwd;
private boolean trackCheck;
private boolean trackPullRequestPipelinesCheck;
public DevOpsConfiguration() {
load();
// To handle upgrade case
if (this.logLevel == null)
this.logLevel = (this.debug) ? "info" : "off";
GenericUtils.configureLogger(this.logLevel);
}
@Override
public boolean configure(StaplerRequest req, JSONObject formData) throws FormException {
final JSONObject snDevOpsJSON = formData.getJSONObject("snDevopsEnabled");
if ((snDevOpsJSON != null) && !(snDevOpsJSON.isNullObject())) {
if (snDevOpsJSON.isEmpty()) {
reIntialize();
} else {
this.snDevopsEnabled = true;
this.instanceUrl = snDevOpsJSON.getString("instanceUrl");
this.toolId = snDevOpsJSON.getString("toolId");
this.snArtifactToolId = snDevOpsJSON.getString("snArtifactToolId");
this.apiVersion = snDevOpsJSON.getString("apiVersion");
this.credentialsId = snDevOpsJSON.getString("credentialsId");
this.logLevel = snDevOpsJSON.getString("logLevel");
GenericUtils.configureLogger(this.logLevel);
this.user = null;
this.pwd = null;
this.trackCheck = snDevOpsJSON.getBoolean("trackCheck");
this.trackPullRequestPipelinesCheck = snDevOpsJSON.getBoolean("trackPullRequestPipelinesCheck");
}
} else {
this.snDevopsEnabled = false;
}
this.save();
return super.configure(req, formData);
}
private void reIntialize() {
this.snDevopsEnabled = false;
this.instanceUrl = null;
this.toolId = null;
this.snArtifactToolId = null;
this.apiVersion = null;
this.debug = false;
this.logLevel = "off";
this.user = null;
this.pwd = null;
this.credentialsId = null;
this.trackCheck = false;
this.trackPullRequestPipelinesCheck = false;
}
@Nonnull
public static DevOpsConfiguration get() {
return (DevOpsConfiguration) GlobalConfiguration.all().getInstance(DevOpsConfiguration.class);
}
@Restricted(NoExternalUse.class)
public static @Nonnull DevOpsConfiguration getOrDie() {
DevOpsConfiguration config = DevOpsConfiguration.get();
if (config == null) {
throw new IllegalStateException(
"DevOpsConfiguration instance is missing. Probably the Jenkins instance is not fully loaded at this time.");
}
return config;
}
public boolean isSnDevopsEnabled() {
return snDevopsEnabled;
}
public String getInstanceUrl() {
return instanceUrl;
}
public String getApiVersion() {
return apiVersion;
}
public boolean isTrackCheck() {
return trackCheck;
}
public boolean isTrackPullRequestPipelinesCheck() {
return trackPullRequestPipelinesCheck;
}
public String getUser() {
// To ensure backward compatibility we are using saved user details
if (!GenericUtils.isEmpty(this.user)) {
return this.user;
}
StandardUsernamePasswordCredentials sc = getCredentials(this.credentialsId);
if (sc != null) {
return sc.getUsername();
}
return null;
}
public String getPwd() {
// To ensure backward compatibility we are using saved user details
if (!GenericUtils.isEmpty(this.pwd)) {
return this.pwd;
}
StandardUsernamePasswordCredentials sc = getCredentials(this.credentialsId);
if (sc != null && sc.getPassword() != null) {
return sc.getPassword().getPlainText();
}
return null;
}
public StandardUsernamePasswordCredentials getCredentials(String credentialsId) {
DomainRequirement dr = null;
ItemGroup itemGroup = null;
Authentication authentication = null;
List<StandardUsernamePasswordCredentials> lc = CredentialsProvider
.lookupCredentials(StandardUsernamePasswordCredentials.class, itemGroup, authentication, dr);
for (int i = 0; i < lc.size(); i++) {
StandardUsernamePasswordCredentials sc = lc.get(i);
if (sc.getId().equals(credentialsId)) {
return sc;
}
}
return null;
}
public String getToolId() {
return toolId;
}
public String getSnArtifactToolId() {
return snArtifactToolId;
}
public String getLogLevel() {
return logLevel;
}
public FormValidation doCheckInstanceUrl(@QueryParameter("instanceUrl") String snInstanceUrl)
throws IOException, ServletException {
if (GenericUtils.isEmpty(snInstanceUrl))
return FormValidation.error("Please provide a valid instance URL");
if (snInstanceUrl.length() > 0 && !snInstanceUrl.startsWith("http"))
return FormValidation.error("URL must start with http/https");
if (snInstanceUrl.length() > 9 && !GenericUtils.checkUrlValid(snInstanceUrl))
return FormValidation.error("Invalid URL");
return FormValidation.ok();
}
public FormValidation doCheckApiVersion(@QueryParameter("apiVersion") String snApiVersion)
throws IOException, ServletException {
if (GenericUtils.isEmpty(snApiVersion))
return FormValidation.error("Please provide an api version");
return FormValidation.ok();
}
public FormValidation doCheckCredentialsId(@QueryParameter("credentialsId") String credentialsId)
throws IOException, ServletException {
List<DomainRequirement> drl = null;
ItemGroup itemGroup = null;
Authentication authentication = null;
if (GenericUtils.isEmpty(credentialsId))
return FormValidation.error("Please choose a credential!");
if (CredentialsProvider.listCredentials(StandardUsernamePasswordCredentials.class, itemGroup, authentication,
drl, CredentialsMatchers.withId(credentialsId)).isEmpty())
return FormValidation.error("Cannot find currently selected credentials");
return FormValidation.ok();
}
public FormValidation doCheckToolId(@QueryParameter("toolId") String snToolId)
throws IOException, ServletException {
if (GenericUtils.isEmpty(snToolId))
return FormValidation.error("Please provide a valid tool ID");
return FormValidation.ok();
}
// Skipping validation for Artifact tool Id as it is an optional parameter.
public FormValidation doTestConnection(@QueryParameter("instanceUrl") String instanceUrl,
@QueryParameter("apiVersion") String apiVersion, @QueryParameter("toolId") String toolId,
@QueryParameter("credentialsId") String credentialsId) throws IOException, ServletException {
List<DomainRequirement> drl = null;
ItemGroup itemGroup = null;
Authentication authentication = null;
if (GenericUtils.isEmpty(instanceUrl))
return FormValidation.error("Please provide the url!");
if (GenericUtils.isEmpty(credentialsId))
return FormValidation.error("Please choose a credential!");
if (CredentialsProvider.listCredentials(StandardUsernamePasswordCredentials.class, itemGroup, authentication,
drl, CredentialsMatchers.withId(credentialsId)).isEmpty())
return FormValidation.error("Cannot find currently selected credentials");
if (GenericUtils.isEmpty(toolId))
return FormValidation.error("Invalid tool id!");
if (GenericUtils.isEmpty(apiVersion))
return FormValidation.error("Invalid API Version!");
String changeControlUrl = getChangeControlUrl(instanceUrl, apiVersion);
LOGGER.log(Level.INFO, "changeControlUrl ->" + changeControlUrl);
if (GenericUtils.isEmpty(changeControlUrl) || !GenericUtils.checkUrlValid(changeControlUrl)) {
return FormValidation.error("Invalid URL");
}
StandardUsernamePasswordCredentials credentials = getCredentials(credentialsId);
String user = null;
String pwd = null;
if (credentials != null) {
user = credentials.getUsername();
if (credentials.getPassword() != null) {
pwd = credentials.getPassword().getPlainText();
}
}
JSONObject params = new JSONObject();
params.put(DevOpsConstants.TOOL_ID_ATTR.toString(), toolId);
params.put(DevOpsConstants.TEST_CONNECTION_ATTR.toString(), "true");
params.put(DevOpsConstants.TOOL_TYPE_ATTR.toString(), DevOpsConstants.TOOL_TYPE.toString());
try {
String result = GenericUtils.parseResponseResult(
CommUtils.call("GET", changeControlUrl, params, null, user, pwd, null, null),
DevOpsConstants.TEST_CONNECTION_RESPONSE_ATTR.toString());
if (result != null && result.equalsIgnoreCase("OK"))
return FormValidation.ok("Connection successful!");
else
throw new Exception("Connection failed!");
} catch (Exception e) {
return FormValidation.error("Client error : " + e.getMessage());
}
}
private String getTrimmedUrl(String url) {
return GenericUtils.isNotEmpty(url) ? url.endsWith("/") ? url.substring(0, url.length() - 1) : url : null;
}
private String getChangeControlUrl(String instanceUrl, String apiVersion) {
return GenericUtils.isNotEmpty(instanceUrl)
? String.format("%s/api/sn_devops/%s/devops/orchestration" + "/changeControl",
getTrimmedUrl(instanceUrl), apiVersion)
: null;
}
private String getChangeInfoUrl(String instanceUrl, String apiVersion) {
return GenericUtils.isNotEmpty(instanceUrl)
? String.format("%s/api/sn_devops/%s/devops/orchestration/changeInfo",
getTrimmedUrl(instanceUrl), apiVersion)
: null;
}
private String getTrackingUrl(String instanceUrl, String apiVersion) {
return GenericUtils.isNotEmpty(instanceUrl)
? String.format("%s/api/sn_devops/%s/devops/orchestration" + "/pipelineInfo",
getTrimmedUrl(instanceUrl), apiVersion)
: null;
}
public String getTrackingUrl() {
return getTrackingUrl(getInstanceUrl(), getApiVersion());
}
// change control url
public String getChangeControlUrl() {
return getChangeControlUrl(getInstanceUrl(), getApiVersion());
}
// change Info url
public String getChangeInfoUrl() {
return getChangeInfoUrl(getInstanceUrl(), getApiVersion());
}
public String getCallbackUrl() {
return GenericUtils.isNotEmpty(getInstanceUrl())
? String.format("%s/api/sn_devops/%s/devops/orchestration" + "/callback",
getTrimmedUrl(getInstanceUrl()), getApiVersion())
: null;
}
// mapping url
public String getMappingUrl() {
return GenericUtils.isNotEmpty(getInstanceUrl())
? String.format("%s/api/sn_devops/%s/devops/orchestration" + "/stepMapping",
getTrimmedUrl(getInstanceUrl()), getApiVersion())
: null;
}
// notification url
public String getNotificationUrl() {
return GenericUtils.isNotEmpty(getInstanceUrl())
? String.format("%s/api/sn_devops/%s/devops/tool/orchestration", getTrimmedUrl(getInstanceUrl()),
getApiVersion())
: null;
}
public String getTestUrl() {
return GenericUtils.isNotEmpty(getInstanceUrl())
? String.format("%s/api/sn_devops/%s/devops/tool/test", getTrimmedUrl(getInstanceUrl()),
getApiVersion())
: null;
}
// artifact registration url
public String getArtifactRegistrationUrl() {
return GenericUtils.isNotEmpty(getInstanceUrl())
? String.format("%s/api/sn_devops/%s/devops/artifact/registration", getTrimmedUrl(getInstanceUrl()),
getApiVersion())
: null;
}
// artifact create package url
public String getArtifactCreatePackageUrl() {
return GenericUtils.isNotEmpty(getInstanceUrl())
? String.format("%s/api/sn_devops/%s/devops/package/registration", getTrimmedUrl(getInstanceUrl()),
getApiVersion())
: null;
}
public ListBoxModel doFillCredentialsIdItems(@QueryParameter String credentialsId) {
if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
return new StandardListBoxModel().includeCurrentValue(credentialsId);
}
credentialsId = GenericUtils.isEmpty(credentialsId) ? this.credentialsId : credentialsId;
AbstractIdCredentialsListBoxModel<StandardListBoxModel, StandardCredentials> options = new StandardListBoxModel()
.includeEmptyValue().includeAs(ACL.SYSTEM, Jenkins.get(), StandardUsernamePasswordCredentials.class)
.includeCurrentValue(credentialsId);
for (ListBoxModel.Option option : options) {
if (option.value.equals(credentialsId)) {
option.selected = true;
}
}
return options;
}
public String getCDMChangeSetCreationURL() {
return GenericUtils.isNotEmpty(getInstanceUrl())
? String.format("%s/api/sn_cdm/changesets/create", getTrimmedUrl(getInstanceUrl()))
: null;
}
public String getCDMUploadToComponentURL() {
return GenericUtils.isNotEmpty(getInstanceUrl())
? String.format("%s/api/sn_cdm/applications/uploads/components", getTrimmedUrl(getInstanceUrl()))
: null;
}
public String getCDMUploadToDeployableURL() {
return GenericUtils.isNotEmpty(getInstanceUrl())
? String.format("%s/api/sn_cdm/applications/uploads/deployables", getTrimmedUrl(getInstanceUrl()))
: null;
}
public String getCDMUploadToCollectionURL() {
return GenericUtils.isNotEmpty(getInstanceUrl())
? String.format("%s/api/sn_cdm/applications/uploads/collections", getTrimmedUrl(getInstanceUrl()))
: null;
}
public String getUploadStatusURL() {
return GenericUtils.isNotEmpty(getInstanceUrl())
? String.format("%s/api/sn_cdm/applications/upload-status/", getTrimmedUrl(getInstanceUrl()))
: null;
}
public String getSnapshotStatusURL() {
return GenericUtils.isNotEmpty(getInstanceUrl())
? String.format("%s/api/now/table/sn_cdm_snapshot", getTrimmedUrl(getInstanceUrl()))
: null;
}
public String getPublishSnapshotURL(String snapshotId) {
return GenericUtils.isNotEmpty(getInstanceUrl())
? String.format("%s/api/sn_cdm/snapshots/%s/publish", getTrimmedUrl(getInstanceUrl()),
snapshotId)
: null;
}
public String getExportRequestURL() {
return GenericUtils.isNotEmpty(getInstanceUrl())
? String.format("%s/api/sn_cdm/applications/deployables/exports", getTrimmedUrl(getInstanceUrl()))
: null;
}
public String getExportConfigStatusURL(String exportId) {
return GenericUtils.isNotEmpty(getInstanceUrl())
? String.format("%s/api/sn_cdm/applications/deployables/exports/"+exportId+"/status", getTrimmedUrl(getInstanceUrl()))
: null;
}
public String getExportConfigDataURL(String exportId) {
return GenericUtils.isNotEmpty(getInstanceUrl())
? String.format("%s/api/sn_cdm/applications/deployables/exports/"+exportId+"/content", getTrimmedUrl(getInstanceUrl()))
: null;
}
public String getImpactedDeployableURL(String changesetId) {
return GenericUtils.isNotEmpty(getInstanceUrl())
? String.format("%s/api/sn_cdm/changesets/%s/impacted-deployables", getTrimmedUrl(getInstanceUrl()),
changesetId)
: null;
}
public String getPipelineRegisterURL() {
return GenericUtils.isNotEmpty(getInstanceUrl())
? String.format("%s/api/sn_devops/%s/devops/config/updatePipeline", getTrimmedUrl(getInstanceUrl()),
getApiVersion())
: null;
}
public ListBoxModel doFillLogLevelItems(@QueryParameter String logLevel) {
ListBoxModel options = new ListBoxModel();
options.add("inherit");
options.add("off");
options.add("severe");
options.add("warning");
options.add("info");
options.add("config");
options.add("fine");
options.add("finer");
options.add("finest");
options.add("all");
for (ListBoxModel.Option option : options) {
if (option.value.equals(logLevel)) {
option.selected = true;
}
}
return options;
}
public String getValidateSnapshotURL(String snapshotId) {
return GenericUtils.isNotEmpty(getInstanceUrl())
? String.format("%s/api/sn_cdm/snapshots/%s/validate", getTrimmedUrl(getInstanceUrl()),
snapshotId)
: null;
}
public String getChangesetURL() {
return GenericUtils.isNotEmpty(getInstanceUrl())
? String.format("%s/api/now/table/sn_cdm_changeset", getTrimmedUrl(getInstanceUrl()))
: null;
}
public String getValidAppURL() {
return GenericUtils.isNotEmpty(getInstanceUrl())
? String.format("%s/api/now/table/sn_cdm_application", getTrimmedUrl(getInstanceUrl()))
: null;
}
public String getPolicyValidationURL() {
return GenericUtils.isNotEmpty(getInstanceUrl())
? String.format("%s/api/now/table/sn_cdm_policy_validation_result", getTrimmedUrl(getInstanceUrl()))
: null;
}
}