From e48927cfc546543949dc9659dad9a8f4adc18f0f Mon Sep 17 00:00:00 2001 From: dbiggs Date: Thu, 13 May 2021 15:21:26 -0400 Subject: [PATCH] ApiServices: Use retry support for most GET, PUT, POST and DELETE api calls Useful for retrying any api calls that fail for infra reasons. Added base class for Ssl actions. Added support for saving a cert to a pem file. Testing Done: Manual testing. Bug Number: none Reviewed by: trivial Run Pipeline: NO Former-commit-id: 245b298b6ae164f12156d185c3be8631a64f9c1a --- .../vmware/config/section/CommitConfig.java | 2 +- .../com/vmware/config/section/VcdConfig.java | 12 ++++ .../com/vmware/action/base/BaseSslAction.java | 63 +++++++++++++++++++ .../CreateSelfSignedCertForUrl.java | 4 +- .../filesystem/LoadCertFromKeystore.java | 55 ++++++++++++++++ .../vmware/action/filesystem/SaveCert.java | 53 ++-------------- .../java/com/vmware/action/vcd/DeleteVMs.java | 45 +++++++++++++ .../java/com/vmware/action/vcd/LoadVapps.java | 2 +- .../mapping/GenerateActionConfigMappings.java | 1 + .../main/resources/configValueMappings.json | 28 ++++++++- core/src/main/resources/internalConfig.json | 7 ++- .../java/com/vmware/AbstractRestService.java | 26 ++++---- .../java/com/vmware/buildweb/Buildweb.java | 6 +- .../main/java/com/vmware/gitlab/Gitlab.java | 18 +++--- .../java/com/vmware/http/HttpConnection.java | 4 -- .../main/java/com/vmware/jenkins/Jenkins.java | 27 ++++---- .../src/main/java/com/vmware/jira/Jira.java | 20 +++--- .../com/vmware/reviewboard/ReviewBoard.java | 26 ++++---- .../src/main/java/com/vmware/slack/Slack.java | 2 +- .../main/java/com/vmware/trello/Trello.java | 18 +++--- .../src/main/java/com/vmware/vcd/Vcd.java | 23 ++++--- .../vmware/vcd/domain/QueryResultVMType.java | 9 ++- .../java/com/vmware/util/StringUtils.java | 3 +- workflow.jar.REMOVED.git-id | 2 +- 24 files changed, 307 insertions(+), 149 deletions(-) create mode 100644 core/src/main/java/com/vmware/action/base/BaseSslAction.java create mode 100644 core/src/main/java/com/vmware/action/filesystem/LoadCertFromKeystore.java create mode 100644 core/src/main/java/com/vmware/action/vcd/DeleteVMs.java diff --git a/config/src/main/java/com/vmware/config/section/CommitConfig.java b/config/src/main/java/com/vmware/config/section/CommitConfig.java index a3e6ace7..a8fd8084 100644 --- a/config/src/main/java/com/vmware/config/section/CommitConfig.java +++ b/config/src/main/java/com/vmware/config/section/CommitConfig.java @@ -31,7 +31,7 @@ public class CommitConfig { @ConfigurableProperty(help = "Label for no bug number") public String noBugNumberLabel; - @ConfigurableProperty(help = "Label for trivial reviewer") + @ConfigurableProperty(help = "Label for trivial reviewer", commandLine = "--trivial-reviewer-label") public String trivialReviewerLabel; @ConfigurableProperty(help = "Label for no reviewer") diff --git a/config/src/main/java/com/vmware/config/section/VcdConfig.java b/config/src/main/java/com/vmware/config/section/VcdConfig.java index c468549a..de2620ae 100644 --- a/config/src/main/java/com/vmware/config/section/VcdConfig.java +++ b/config/src/main/java/com/vmware/config/section/VcdConfig.java @@ -1,8 +1,10 @@ package com.vmware.config.section; +import java.util.Arrays; import java.util.List; import com.vmware.config.ConfigurableProperty; +import com.vmware.util.StringUtils; public class VcdConfig { @ConfigurableProperty(help = "Url for Vcloud Director") @@ -58,4 +60,14 @@ public class VcdConfig { @ConfigurableProperty(commandLine = "--use-owned-vapps-only", help = "Only use a Vapp owned by the user, no file based Vapps") public boolean useOwnedVappsOnly; + + @ConfigurableProperty(commandLine = "--query-filter", help = "Optional filter to use when querying Vapps or VMs") + public String queryFilter; + + public String[] queryFilters() { + if (StringUtils.isEmpty(queryFilter)) { + return new String[0]; + } + return new String[] {queryFilter}; + } } diff --git a/core/src/main/java/com/vmware/action/base/BaseSslAction.java b/core/src/main/java/com/vmware/action/base/BaseSslAction.java new file mode 100644 index 00000000..e9616e9e --- /dev/null +++ b/core/src/main/java/com/vmware/action/base/BaseSslAction.java @@ -0,0 +1,63 @@ +package com.vmware.action.base; + +import java.io.File; +import java.io.FileInputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.Provider; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; + +import com.vmware.action.BaseAction; +import com.vmware.config.WorkflowConfig; +import com.vmware.util.StringUtils; +import com.vmware.util.exception.FatalException; + +public abstract class BaseSslAction extends BaseAction { + public BaseSslAction(WorkflowConfig config) { + super(config); + } + + protected KeyStore loadKeyStore(File keystoreFile) { + List keystoreTypes = new ArrayList<>(Arrays.asList("JCEKS", "PKCS12", "JKS")); + if (sslConfig.additionalKeystoreTypes != null) { + keystoreTypes.addAll(sslConfig.additionalKeystoreTypes.keySet()); + } + return keystoreTypes.stream().map(keystoreType -> { + try { + KeyStore privateKS = createKeyStore(keystoreType); + privateKS.load(new FileInputStream(keystoreFile), sslConfig.keystorePassword.toCharArray()); + log.info("Loaded keystore {} of type {}", keystoreFile.getAbsolutePath(), keystoreType); + return privateKS; + } catch (Throwable t) { + log.debug("Failed with keystore type {}\n{}", keystoreType, StringUtils.exceptionAsString(t)); + return null; + } + }).filter(Objects::nonNull).findFirst().orElseThrow(() -> new FatalException("Unable to load keystore file {}", + keystoreFile.getPath(), sslConfig.keystorePassword)); + } + + protected KeyStore createKeyStore(String keystoreType) + throws MalformedURLException, ClassNotFoundException, KeyStoreException, IllegalAccessException, InstantiationException { + if (sslConfig.additionalKeystoreTypes == null || !sslConfig.additionalKeystoreTypes.containsKey(keystoreType)) { + return KeyStore.getInstance(keystoreType); + } + List providerInfo = sslConfig.additionalKeystoreTypes.get(keystoreType); + + failIfTrue(providerInfo == null || providerInfo.size() != 2, "Invalid value " + providerInfo + " for keystore " + keystoreType); + String filePath = replacementVariables.replaceVariablesInValue(providerInfo.get(1)); + File providerJarFile = new File(filePath); + failIfTrue(!providerJarFile.exists(), "file " + providerJarFile.getAbsolutePath() + " path not set"); + + log.debug("Using jar file {} for provider {}", providerJarFile.getAbsolutePath(), providerInfo.get(0)); + URL jarUrl = new URL("jar:file:" + providerJarFile.getAbsolutePath() + "!/"); + URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] {jarUrl}, getClass().getClassLoader()); + Provider provider = (Provider) classLoader.loadClass(providerInfo.get(0)).newInstance(); + return KeyStore.getInstance(keystoreType, provider); + } +} diff --git a/core/src/main/java/com/vmware/action/filesystem/CreateSelfSignedCertForUrl.java b/core/src/main/java/com/vmware/action/filesystem/CreateSelfSignedCertForUrl.java index 89aae169..e4bbd512 100644 --- a/core/src/main/java/com/vmware/action/filesystem/CreateSelfSignedCertForUrl.java +++ b/core/src/main/java/com/vmware/action/filesystem/CreateSelfSignedCertForUrl.java @@ -21,15 +21,13 @@ import com.vmware.util.exception.FatalException; import com.vmware.util.logging.LogLevel; -import static com.vmware.util.StringUtils.convertToPem; +import static com.vmware.util.StringUtils.LINE_SEPARATOR; import static com.vmware.util.StringUtils.convertToPem; @ActionDescription(value = "Created a self signed cert. The host of the specified url is used as the CN value.", configFlagsToExcludeFromCompleter = "--keystore-type") public class CreateSelfSignedCertForUrl extends BaseAction { - public final static String LINE_SEPARATOR = System.getProperty("line.separator"); - public CreateSelfSignedCertForUrl(WorkflowConfig config) { super(config); super.addFailWorkflowIfBlankProperties("sourceUrl"); diff --git a/core/src/main/java/com/vmware/action/filesystem/LoadCertFromKeystore.java b/core/src/main/java/com/vmware/action/filesystem/LoadCertFromKeystore.java new file mode 100644 index 00000000..24a1265e --- /dev/null +++ b/core/src/main/java/com/vmware/action/filesystem/LoadCertFromKeystore.java @@ -0,0 +1,55 @@ +package com.vmware.action.filesystem; + +import java.io.File; +import java.io.FileInputStream; +import java.security.Key; +import java.security.KeyPair; +import java.security.KeyStore; +import java.security.cert.Certificate; +import java.security.cert.X509Certificate; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; + +import com.vmware.action.BaseAction; +import com.vmware.action.base.BaseSslAction; +import com.vmware.config.ActionDescription; +import com.vmware.config.WorkflowConfig; +import com.vmware.util.IOUtils; +import com.vmware.util.StringUtils; +import com.vmware.util.exception.FatalException; + +import static com.vmware.util.StringUtils.LINE_SEPARATOR; + +@ActionDescription("Loads a certificate from a keystore") +public class LoadCertFromKeystore extends BaseSslAction { + public LoadCertFromKeystore(WorkflowConfig config) { + super(config); + super.addFailWorkflowIfBlankProperties("sourceFile", "keystoreAlias", "keystoreAliasPassword", "keystorePassword"); + } + + @Override + protected void failWorkflowIfConditionNotMet() { + super.failWorkflowIfConditionNotMet(); + super.failIfTrue(!new File(fileSystemConfig.sourceFile).exists(), "Source file " + fileSystemConfig.sourceFile + " does not exist"); + } + + @Override + public void process() { + try { + log.info("Loading alias {} from keystore {}", sslConfig.keystoreAlias, fileSystemConfig.sourceFile); + KeyStore keystore = loadKeyStore(new File(fileSystemConfig.sourceFile)); + Key privateKey = keystore.getKey(sslConfig.keystoreAlias, sslConfig.keystoreAliasPassword.toCharArray()); + Certificate cert = keystore.getCertificate(sslConfig.keystoreAlias); + + fileSystemConfig.fileData = StringUtils.convertToPem(privateKey) + LINE_SEPARATOR + StringUtils.convertToPem(cert); + } catch (Exception e) { + if (e instanceof RuntimeException) { + throw (RuntimeException) e; + } else { + throw new RuntimeException(e); + } + } + } +} diff --git a/core/src/main/java/com/vmware/action/filesystem/SaveCert.java b/core/src/main/java/com/vmware/action/filesystem/SaveCert.java index 7a7e0ab0..26b5393b 100644 --- a/core/src/main/java/com/vmware/action/filesystem/SaveCert.java +++ b/core/src/main/java/com/vmware/action/filesystem/SaveCert.java @@ -2,12 +2,8 @@ import java.io.ByteArrayInputStream; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLClassLoader; import java.nio.charset.StandardCharsets; import java.security.AlgorithmParameters; import java.security.InvalidKeyException; @@ -18,7 +14,6 @@ import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; -import java.security.Provider; import java.security.PublicKey; import java.security.SecureRandom; import java.security.SignatureException; @@ -30,12 +25,8 @@ import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.RSAPublicKeySpec; -import java.util.ArrayList; -import java.util.Arrays; import java.util.Base64; import java.util.Calendar; -import java.util.List; -import java.util.Objects; import java.util.Random; import javax.crypto.Cipher; @@ -45,12 +36,11 @@ import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.PBEParameterSpec; -import com.vmware.action.BaseAction; +import com.vmware.action.base.BaseSslAction; import com.vmware.config.ActionDescription; import com.vmware.config.WorkflowConfig; import com.vmware.util.IOUtils; import com.vmware.util.StringUtils; -import com.vmware.util.exception.FatalException; import static com.vmware.util.StringUtils.BEGIN_PRIVATE_KEY; import static com.vmware.util.StringUtils.END_PRIVATE_KEY; @@ -59,10 +49,10 @@ @ActionDescription(value = "Saves the loaded cert. If keystore password is set, cert is saved to a keystore, otherwise as pem files.", configFlagsToAlwaysExcludeFromCompleter = {"--cipher-salt-length", "--new-keystore-type"}) -public class SaveCert extends BaseAction { +public class SaveCert extends BaseSslAction { public SaveCert(WorkflowConfig config) { super(config); - super.addSkipActionIfBlankProperties("fileData", "destinationFile", "keystoreAlias", "keystorePassword"); + super.addSkipActionIfBlankProperties("fileData", "destinationFile", "keystoreAliasPassword"); } @Override @@ -112,6 +102,7 @@ private void addCertToKeyStoreFile() throws ClassNotFoundException, KeyStoreException, IllegalAccessException, InstantiationException, CertificateException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException, InvalidKeySpecException, IOException { failIfEmpty("keystoreAliasPassword", "keystoreAliasPassword not set"); + failIfEmpty("keystoreAlias", "keystoreAlias not set"); File keystoreFile = new File(fileSystemConfig.destinationFile); KeyStore privateKS; if (keystoreFile.exists()) { @@ -134,43 +125,7 @@ private void addCertToKeyStoreFile() } - private KeyStore loadKeyStore(File keystoreFile) { - List keystoreTypes = new ArrayList<>(Arrays.asList("JCEKS", "PKCS12", "JKS")); - if (sslConfig.additionalKeystoreTypes != null) { - keystoreTypes.addAll(sslConfig.additionalKeystoreTypes.keySet()); - } - return keystoreTypes.stream().map(keystoreType -> { - try { - KeyStore privateKS = createKeyStore(keystoreType); - privateKS.load(new FileInputStream(keystoreFile), sslConfig.keystorePassword.toCharArray()); - log.info("Loaded keystore {} of type {}", keystoreFile.getAbsolutePath(), keystoreType); - return privateKS; - } catch (Throwable t) { - log.debug("Failed with keystore type {}\n{}", keystoreType, StringUtils.exceptionAsString(t)); - return null; - } - }).filter(Objects::nonNull).findFirst().orElseThrow(() -> new FatalException("Unable to load keystore file {}", - keystoreFile.getPath(), sslConfig.keystorePassword)); - } - private KeyStore createKeyStore(String keystoreType) - throws MalformedURLException, ClassNotFoundException, KeyStoreException, IllegalAccessException, InstantiationException { - if (sslConfig.additionalKeystoreTypes == null || !sslConfig.additionalKeystoreTypes.containsKey(keystoreType)) { - return KeyStore.getInstance(keystoreType); - } - List providerInfo = sslConfig.additionalKeystoreTypes.get(keystoreType); - - failIfTrue(providerInfo == null || providerInfo.size() != 2, "Invalid value " + providerInfo + " for keystore " + keystoreType); - String filePath = replacementVariables.replaceVariablesInValue(providerInfo.get(1)); - File providerJarFile = new File(filePath); - failIfTrue(!providerJarFile.exists(), "file " + providerJarFile.getAbsolutePath() + " path not set"); - - log.debug("Using jar file {} for provider {}", providerJarFile.getAbsolutePath(), providerInfo.get(0)); - URL jarUrl = new URL("jar:file:" + providerJarFile.getAbsolutePath() + "!/"); - URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] {jarUrl}, getClass().getClassLoader()); - Provider provider = (Provider) classLoader.loadClass(providerInfo.get(0)).newInstance(); - return KeyStore.getInstance(keystoreType, provider); - } private X509Certificate createCertificate() throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException { String certOutput = StringUtils.findStringWithStartAndEnd(fileSystemConfig.fileData, BEGIN_CERT, END_CERT); diff --git a/core/src/main/java/com/vmware/action/vcd/DeleteVMs.java b/core/src/main/java/com/vmware/action/vcd/DeleteVMs.java new file mode 100644 index 00000000..22a45537 --- /dev/null +++ b/core/src/main/java/com/vmware/action/vcd/DeleteVMs.java @@ -0,0 +1,45 @@ +package com.vmware.action.vcd; + +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +import com.vmware.action.BaseAction; +import com.vmware.config.ActionDescription; +import com.vmware.config.WorkflowConfig; +import com.vmware.util.CollectionUtils; +import com.vmware.util.input.InputUtils; +import com.vmware.vcd.Vcd; +import com.vmware.vcd.domain.QueryResultVMType; +import com.vmware.vcd.domain.QueryResultVMsType; +import com.vmware.vcd.domain.TaskType; + +@ActionDescription("Action for bulk deleting of VMs") +public class DeleteVMs extends BaseAction { + public DeleteVMs(WorkflowConfig config) { + super(config); + } + + @Override + public void process() { + Vcd vcd = serviceLocator.getVcd(); + QueryResultVMsType vmRecords = vcd.queryVms(vcdConfig.queryFilters()); + if (CollectionUtils.isEmpty(vmRecords.record)) { + log.info("No VMs found"); + return; + } + List choices = vmRecords.record.stream().map(QueryResultVMType::getLabel).collect(Collectors.toList()); + List vmIndexes = InputUtils.readSelections(choices, "Select VMs to delete", false); + + String vmsToDelete = vmIndexes.stream().map(choices::get).collect(Collectors.joining(",")); + log.info("VMs {} will be deleted", vmsToDelete); + String confirmation = InputUtils.readValueUntilNotBlank("Delete (Y/N)"); + if ("Y".equalsIgnoreCase(confirmation)) { + vmIndexes.stream().map(i -> vmRecords.record.get(i)).forEach(vm -> { + log.info("Deleting VM {}", vm.getLabel()); + TaskType deleteTask = serviceLocator.getVcd().deleteResource(vm.getSelfLink(), true); + vcd.waitForTaskToComplete(deleteTask.href, 1, TimeUnit.MINUTES); + }); + } + } +} diff --git a/core/src/main/java/com/vmware/action/vcd/LoadVapps.java b/core/src/main/java/com/vmware/action/vcd/LoadVapps.java index b8412456..e4f7f9f9 100644 --- a/core/src/main/java/com/vmware/action/vcd/LoadVapps.java +++ b/core/src/main/java/com/vmware/action/vcd/LoadVapps.java @@ -39,7 +39,7 @@ public void checkIfActionShouldBeSkipped() { @Override public void process() { - QueryResultVappsType vappRecords = serviceLocator.getVcd().queryVapps(); + QueryResultVappsType vappRecords = serviceLocator.getVcd().queryVapps(vcdConfig.queryFilters()); vappRecords.record.forEach(this::populatedPoweredOnVmCount); List vapps = new ArrayList<>(); vapps.addAll(parseVappJsonFiles()); diff --git a/core/src/main/java/com/vmware/mapping/GenerateActionConfigMappings.java b/core/src/main/java/com/vmware/mapping/GenerateActionConfigMappings.java index 20048cd8..48a0685c 100644 --- a/core/src/main/java/com/vmware/mapping/GenerateActionConfigMappings.java +++ b/core/src/main/java/com/vmware/mapping/GenerateActionConfigMappings.java @@ -56,6 +56,7 @@ public class GenerateActionConfigMappings { methodNameToConfigValueMappings.put("trackingBranchPath", singletonList("trackingBranch")); methodNameToConfigValueMappings.put("commandLineSite", Arrays.asList("sshHost", "sshPort", "sshUsername", "sshPassword")); methodNameToConfigValueMappings.put("determineBuildwebBranch", Arrays.asList("buildwebBranch", "useGitTrackingBranch")); + methodNameToConfigValueMappings.put("queryFilters", singletonList("queryFilter")); } private static final Logger log = LoggerFactory.getLogger(GenerateActionConfigMappings.class); diff --git a/core/src/main/resources/configValueMappings.json b/core/src/main/resources/configValueMappings.json index 57007625..721ff8dd 100644 --- a/core/src/main/resources/configValueMappings.json +++ b/core/src/main/resources/configValueMappings.json @@ -199,6 +199,7 @@ "--use-owned-vapps-only" ], "LoadVapps": [ + "--query-filter", "--username", "--vapp-json-file", "--use-owned-vapps-only" @@ -243,6 +244,9 @@ "--tracking-branch", "--parent" ], + "ExitIfTrivialCommit": [ + "--trivial-reviewer-label" + ], "ParseText": [ "--output-variable-name", "--regex", @@ -272,6 +276,9 @@ "--wait-time", "--wait-for-action" ], + "SetReviewedByAsTrivial": [ + "--trivial-reviewer-label" + ], "MarkBugAsResolved": [ "--username" ], @@ -381,10 +388,14 @@ ], "GenerateGitCommitStats": [ "--author-emails", + "--trivial-reviewer-label", "--last-number-of-days", "--line-count-ranges", "--file-count-ranges" ], + "BaseSslAction": [ + "--keystore-password" + ], "OpenVcdTenantApp": [ "--vcd-tenant" ], @@ -400,6 +411,12 @@ "--source-file", "--database-schema-name" ], + "LoadCertFromKeystore": [ + "--file-data", + "--keystore-alias-password", + "--keystore-alias", + "--source-file" + ], "LoadIssues": [ "--use-epics", "--use-fix-version", @@ -450,6 +467,9 @@ "--tracking-branch", "--sync-to-branch-latest" ], + "BaseCommitWithReviewAction": [ + "--trivial-reviewer-label" + ], "LoadPerforcePatchFile": [ "--diff-file" ], @@ -459,6 +479,7 @@ "--use-patch-command" ], "BaseSetReviewersList": [ + "--trivial-reviewer-label", "--always-include-review-url" ], "ExecuteSqlStatement": [ @@ -468,6 +489,10 @@ "--database-url-pattern", "--database-url" ], + "DeleteVMs": [ + "--query-filter", + "--username" + ], "SyncCardsWithJiraIssues": [ "--keep-missing-cards" ], @@ -534,7 +559,8 @@ ], "BaseSetUsersList": [ "--search-by-usernames-only", - "--username" + "--username", + "--trivial-reviewer-label" ], "PushToTrackingBranch": [ "--tracking-branch" diff --git a/core/src/main/resources/internalConfig.json b/core/src/main/resources/internalConfig.json index 0539c8a3..2e662180 100644 --- a/core/src/main/resources/internalConfig.json +++ b/core/src/main/resources/internalConfig.json @@ -974,10 +974,11 @@ "DisplayCommitsForIssues" ], "testEncryption": [ - "EncryptProperty&&--cipher-key=bG9uZ2VyUGFzc3dvcmQxMg==&&--property-value=sampleData&&--output-variable-name=encryptedData", + "EncryptProperty&&--cipher-key=bG9uZ2VyUGFzc3dvcmQxMg==&&--property-value=$encryptionValue&&--output-variable-name=encryptedData", "DecryptProperty&&--cipher-key=bG9uZ2VyUGFzc3dvcmQxMg==&&--property-value=$encryptedData&&--output-variable-name=decryptedData", - "ExitIfValueDoesNotMatch&&--input-text=$decryptedData&&--property-value=sampleData", - "DisplayInfo&&--input-text=Values matched for sampleData!" + "ExitIfValueDoesNotMatch&&--input-text=$decryptedData&&--property-value=$encryptionValue", + "DisplayInfo&&--input-text=Successfully encrypted and descrypted value $encryptionValue", + "--VencryptionValue=sampleTestData" ] }, "supportingWorkflows": [ diff --git a/serviceApis/src/main/java/com/vmware/AbstractRestService.java b/serviceApis/src/main/java/com/vmware/AbstractRestService.java index d2329587..e3f128e6 100644 --- a/serviceApis/src/main/java/com/vmware/AbstractRestService.java +++ b/serviceApis/src/main/java/com/vmware/AbstractRestService.java @@ -10,10 +10,7 @@ import com.vmware.util.ThreadUtils; import com.vmware.util.exception.RuntimeIOException; -import java.io.IOException; -import java.net.SocketTimeoutException; import java.net.URI; -import java.net.URISyntaxException; import java.util.Collections; import java.util.List; import java.util.concurrent.TimeUnit; @@ -35,21 +32,21 @@ public boolean isBaseUriTrusted() { return connection.isUriTrusted(URI.create(baseUrl)); } - protected void optimisticPost(String url, Object requestBody, RequestParam... params) { - optimisticPost(url, null, requestBody, params); + protected void post(String url, Object requestBody, RequestParam... params) { + post(url, null, requestBody, params); } /** * WIll try first to post the request. * If that fails, will then try to authenticate and re post the request. */ - protected T optimisticPost(String url, Class responseConversionClass, Object requestBody, RequestParam... params) { - return optimisticPost(url, responseConversionClass, requestBody, Collections.emptyList(), params); + protected T post(String url, Class responseConversionClass, Object requestBody, RequestParam... params) { + return post(url, responseConversionClass, requestBody, Collections.emptyList(), params); } - protected T optimisticPost(String url, Class responseConversionClass, Object requestBody, - List> allowedExceptionTypes, RequestParam... params) { + protected T post(String url, Class responseConversionClass, Object requestBody, + List> allowedExceptionTypes, RequestParam... params) { try { return connection.post(url, responseConversionClass, requestBody, params); } catch (NotAuthorizedException e) { @@ -73,7 +70,7 @@ protected T optimisticPost(String url, Class responseConversionClass, Obj * WIll try first to get the request. * If that fails, will then try to authenticate and re get the request. */ - protected T optimisticGet(String url, Class responseConversionClass, RequestParam... params) { + protected T get(String url, Class responseConversionClass, RequestParam... params) { try { return connection.get(url, responseConversionClass, params); } catch (NotAuthorizedException e) { @@ -91,12 +88,12 @@ protected T optimisticGet(String url, Class responseConversionClass, Requ } } - protected T optimisticPut(String url, Class responseConversionClass, Object requestBody, RequestParam... params) { - return optimisticPut(url, responseConversionClass, requestBody, Collections.emptyList(), params); + protected T put(String url, Class responseConversionClass, Object requestBody, RequestParam... params) { + return put(url, responseConversionClass, requestBody, Collections.emptyList(), params); } - protected T optimisticPut(String url, Class responseConversionClass, Object requestBody, List> allowedExceptions, - RequestParam... params) { + protected T put(String url, Class responseConversionClass, Object requestBody, List> allowedExceptions, + RequestParam... params) { try { return connection.put(url, responseConversionClass, requestBody, params); } catch (NotAuthorizedException e) { @@ -115,5 +112,4 @@ protected T optimisticPut(String url, Class responseConversionClass, Obje return connection.put(url, requestBody, params); } } - } diff --git a/serviceApis/src/main/java/com/vmware/buildweb/Buildweb.java b/serviceApis/src/main/java/com/vmware/buildweb/Buildweb.java index dafe798d..d4348e0f 100644 --- a/serviceApis/src/main/java/com/vmware/buildweb/Buildweb.java +++ b/serviceApis/src/main/java/com/vmware/buildweb/Buildweb.java @@ -38,7 +38,7 @@ public BuildwebBuild getSandboxBuild(String id) { String[] idParts = id.split("-"); String buildType = idParts.length == 2 ? idParts[0] : "sb"; String idForBuild = idParts.length == 2 ? idParts[1] : id; - return connection.get(addRelativePaths(baseUrl, buildType, "build", idForBuild), BuildwebBuild.class); + return get(addRelativePaths(baseUrl, buildType, "build", idForBuild), BuildwebBuild.class); } public void logOutputForBuilds(ReviewRequestDraft draft, int linesToShow, BuildStatus... results) { @@ -64,7 +64,7 @@ public String getLogsUrl(String buildId) { if (build.buildStatus == BuildStatus.STARTING) { return null; } - BuildMachines machines = connection.get(addRelativePaths(baseUrl, build.buildMachinesUrl), BuildMachines.class); + BuildMachines machines = get(addRelativePaths(baseUrl, build.buildMachinesUrl), BuildMachines.class); BuildMachine buildMachine = machines.realBuildMachine(); String logsUrl; if (build.buildStatus == BuildStatus.BUILDING) { @@ -93,7 +93,7 @@ protected String urlUsedInBuilds() { protected BuildStatus getResultForBuild(String url) { BuildwebId buildwebId = new BuildwebId(MatcherUtils.singleMatchExpected(url, "/(\\w\\w/\\d++)")); String buildApiUrl = baseUrl + buildwebId.buildApiPath(); - BuildwebBuild build = optimisticGet(buildApiUrl, BuildwebBuild.class); + BuildwebBuild build = get(buildApiUrl, BuildwebBuild.class); return build.buildStatus; } diff --git a/serviceApis/src/main/java/com/vmware/gitlab/Gitlab.java b/serviceApis/src/main/java/com/vmware/gitlab/Gitlab.java index b69b8644..281b7773 100644 --- a/serviceApis/src/main/java/com/vmware/gitlab/Gitlab.java +++ b/serviceApis/src/main/java/com/vmware/gitlab/Gitlab.java @@ -33,24 +33,24 @@ public Gitlab(String baseUrl, String username) { } public MergeRequest[] getMergeRequests(String state) { - return optimisticGet(apiUrl +"/merge_requests?state=" + state, MergeRequest[].class); + return get(apiUrl +"/merge_requests?state=" + state, MergeRequest[].class); } public MergeRequest createMergeRequest(MergeRequest mergeRequest) { - return optimisticPost(mergeRequestsUrl(mergeRequest.targetProjectId), MergeRequest.class, mergeRequest); + return post(mergeRequestsUrl(mergeRequest.targetProjectId), MergeRequest.class, mergeRequest); } public MergeRequest getMergeRequest(int projectId, int mergeRequestId) { - return optimisticGet(mergeRequestUrl(projectId, mergeRequestId), MergeRequest.class); + return get(mergeRequestUrl(projectId, mergeRequestId), MergeRequest.class); } public MergeRequestApprovals getMergeRequestApprovals(int projectId, int mergeRequestId) { - return optimisticGet(mergeRequestUrl(projectId, mergeRequestId) + "/approvals", MergeRequestApprovals.class); + return get(mergeRequestUrl(projectId, mergeRequestId) + "/approvals", MergeRequestApprovals.class); } public MergeRequestApprovals approveMergeRequest(int projectId, int mergeRequestId) { try { - return optimisticPost(mergeRequestUrl(projectId, mergeRequestId) + "/approve", MergeRequestApprovals.class, null, + return post(mergeRequestUrl(projectId, mergeRequestId) + "/approve", MergeRequestApprovals.class, null, Collections.singletonList(NotAuthorizedException.class)); } catch (NotAuthorizedException nae) { throw new FatalException(nae, "Not authorized to approve merge request {} for project {}", mergeRequestId, projectId); @@ -61,7 +61,7 @@ public MergeRequestApprovals approveMergeRequest(int projectId, int mergeRequest public void acceptMergeRequest(MergeRequest mergeRequest) { try { MergeAcceptRequest acceptRequest = new MergeAcceptRequest(mergeRequest); - String response = optimisticPut(mergeRequestUrl(mergeRequest.projectId, mergeRequest.iid) + "/merge", String.class, acceptRequest, + String response = put(mergeRequestUrl(mergeRequest.projectId, mergeRequest.iid) + "/merge", String.class, acceptRequest, Collections.singletonList(NotAuthorizedException.class)); log.debug(response); } catch (NotAuthorizedException nae) { @@ -71,7 +71,7 @@ public void acceptMergeRequest(MergeRequest mergeRequest) { public void rebaseMergeRequest(int projectId, int mergeRequestId) { try { - String response = optimisticPut(mergeRequestUrl(projectId, mergeRequestId) + "/rebase", String.class, null, + String response = put(mergeRequestUrl(projectId, mergeRequestId) + "/rebase", String.class, null, Collections.singletonList(ForbiddenException.class)); log.debug("Rebase response: {}", response); } catch (ForbiddenException fe) { @@ -80,12 +80,12 @@ public void rebaseMergeRequest(int projectId, int mergeRequestId) { } public MergeRequest updateMergeRequest(MergeRequest mergeRequest) { - return optimisticPut(mergeRequestUrl(mergeRequest.projectId, mergeRequest.iid), MergeRequest.class, mergeRequest); + return put(mergeRequestUrl(mergeRequest.projectId, mergeRequest.iid), MergeRequest.class, mergeRequest); } @Override protected void checkAuthenticationAgainstServer() { - optimisticGet(apiUrl + "/version", String.class); + get(apiUrl + "/version", String.class); } @Override diff --git a/serviceApis/src/main/java/com/vmware/http/HttpConnection.java b/serviceApis/src/main/java/com/vmware/http/HttpConnection.java index e5312f5a..0969069b 100644 --- a/serviceApis/src/main/java/com/vmware/http/HttpConnection.java +++ b/serviceApis/src/main/java/com/vmware/http/HttpConnection.java @@ -114,10 +114,6 @@ public void resetParams() { requestParams.reset(); } - public T get(String url, Class responseConversionClass, List params) { - return get(url, responseConversionClass, params.toArray(new RequestParam[params.size()])); - } - public T get(String url, Class responseConversionClass, RequestParam... params) { setupConnection(url, GET, params); return handleServerResponse(responseConversionClass, GET, params); diff --git a/serviceApis/src/main/java/com/vmware/jenkins/Jenkins.java b/serviceApis/src/main/java/com/vmware/jenkins/Jenkins.java index fd198157..348be029 100644 --- a/serviceApis/src/main/java/com/vmware/jenkins/Jenkins.java +++ b/serviceApis/src/main/java/com/vmware/jenkins/Jenkins.java @@ -7,7 +7,6 @@ import com.vmware.http.credentials.UsernamePasswordCredentials; import com.vmware.http.exception.InternalServerException; import com.vmware.http.exception.NotAuthorizedException; -import com.vmware.http.exception.NotFoundException; import com.vmware.http.request.RequestHeader; import com.vmware.http.request.RequestParam; import com.vmware.http.request.body.RequestBodyHandling; @@ -62,30 +61,30 @@ public Jenkins(String serverUrl, final String username, boolean usesCsrf, boolea public HomePage getHomePage() { if (homePage == null) { - homePage = connection.get(apiUrl, HomePage.class); + homePage = get(apiUrl, HomePage.class); } return homePage; } public void invokeJob(Job jobToInvoke) { - optimisticPost(jobToInvoke.getBuildWithParametersUrl(), null); + post(jobToInvoke.getBuildWithParametersUrl(), null); } public void invokeJobWithParameters(Job jobToInvoke, JobParameters params) { - optimisticPost(jobToInvoke.getBuildWithParametersUrl(), params.toMap()); + post(jobToInvoke.getBuildWithParametersUrl(), params.toMap()); } public JobView getFullViewDetails(String viewUrl) { - return optimisticGet(UrlUtils.addRelativePaths(viewUrl, "api/json?depth=1"), JobView.class); + return get(UrlUtils.addRelativePaths(viewUrl, "api/json?depth=1"), JobView.class); } public Job getJobDetails(Job jobToInvoke) { - return optimisticGet(jobToInvoke.getInfoUrl(), Job.class); + return get(jobToInvoke.getInfoUrl(), Job.class); } public JobBuild getJobBuildDetails(JobBuild jobBuild) { - return optimisticGet(jobBuild.getJenkinsInfoUrl(), JobBuild.class); + return get(jobBuild.getJenkinsInfoUrl(), JobBuild.class); } public JobBuild getJobBuildDetails(String jobName, int buildNumber) { @@ -94,14 +93,14 @@ public JobBuild getJobBuildDetails(String jobName, int buildNumber) { } public TestResults getJobBuildTestResults(JobBuild jobBuild) { - TestResults results = optimisticGet(jobBuild.getTestReportsApiUrl(), TestResults.class); + TestResults results = get(jobBuild.getTestReportsApiUrl(), TestResults.class); results.setBuild(jobBuild); return results; } public void abortJobBuild(JobBuild jobBuildToAbort) { log.info("Aborting build {}", jobBuildToAbort.url); - optimisticPost(jobBuildToAbort.stopUrl(), null); + post(jobBuildToAbort.stopUrl(), null); jobBuildToAbort.status = BuildStatus.ABORTED; } @@ -162,20 +161,20 @@ protected void loginManually() { } private JobBuild getJobBuildDetails(String jobBuildUrl) { - return optimisticGet(jobBuildUrl, JobBuild.class); + return get(jobBuildUrl, JobBuild.class); } @Override - protected T optimisticPost(String url, Class responseConversionClass, Object param, RequestParam... params) { + protected T post(String url, Class responseConversionClass, Object param, RequestParam... params) { if (usesCsrf) { - CsrfCrumb csrfCrumb = super.optimisticGet(super.baseUrl + "crumbIssuer/api/json", CsrfCrumb.class); + CsrfCrumb csrfCrumb = super.get(super.baseUrl + "crumbIssuer/api/json", CsrfCrumb.class); RequestHeader csrfHeader = new RequestHeader(csrfCrumb.crumbRequestField, csrfCrumb.crumb); List paramList = new ArrayList<>(Arrays.asList(params)); paramList.add(csrfHeader); connection.setUseSessionCookies(true); - return super.optimisticPost(url, responseConversionClass, param, paramList.toArray(new RequestParam[0])); + return super.post(url, responseConversionClass, param, paramList.toArray(new RequestParam[0])); } else { - return super.optimisticPost(url, responseConversionClass, param, params); + return super.post(url, responseConversionClass, param, params); } } diff --git a/serviceApis/src/main/java/com/vmware/jira/Jira.java b/serviceApis/src/main/java/com/vmware/jira/Jira.java index 66495c10..9ff896c0 100644 --- a/serviceApis/src/main/java/com/vmware/jira/Jira.java +++ b/serviceApis/src/main/java/com/vmware/jira/Jira.java @@ -65,7 +65,7 @@ public Jira(String jiraUrl, String username, Map customFieldName public List getRecentBoardItems() { List recentItems = new ArrayList(); String url = legacyApiUrl + "menus/greenhopper_menu?inAdminMode=false"; - MenuSection[] sections = connection.get(url, MenuSections.class).sections; + MenuSection[] sections = get(url, MenuSections.class).sections; if (sections.length == 0) { return recentItems; } @@ -81,12 +81,12 @@ public List getRecentBoardItems() { public RapidView getRapidView(String viewId) { String url = greenhopperUrl + "xboard/plan/backlog/data.json"; - RapidView rapidView = connection.get(url, RapidView.class, new UrlParam("rapidViewId", viewId)); + RapidView rapidView = get(url, RapidView.class, new UrlParam("rapidViewId", viewId)); return rapidView; } public Issue getIssueByKey(String key) { - return connection.get(urlBaseForKey(key), Issue.class); + return get(urlBaseForKey(key), Issue.class); } public Issue getIssueWithoutException(String key) { @@ -99,7 +99,7 @@ public Issue getIssueWithoutException(String key) { } public IssuesResponse searchForIssues(SearchRequest searchRequest) { - return connection.post(searchUrl, IssuesResponse.class, searchRequest); + return post(searchUrl, IssuesResponse.class, searchRequest); } public IssuesResponse getOpenTasksForUser() { @@ -108,7 +108,7 @@ public IssuesResponse getOpenTasksForUser() { String jql = String.format("issuetype in (%s,subTaskIssueTypes()) AND status in (%s) AND assignee=%s", issueTypesToGet, allowedStatuses, escapeUsername(getUsername())); - IssuesResponse response = connection.get(searchUrl, IssuesResponse.class, new UrlParam("jql", jql)); + IssuesResponse response = get(searchUrl, IssuesResponse.class, new UrlParam("jql", jql)); log.debug("{} tasks found", response.issues.length); return response; } @@ -116,7 +116,7 @@ public IssuesResponse getOpenTasksForUser() { public IssuesResponse getIssuesForUser(IssueStatusDefinition status, IssueResolutionDefinition resolution) { String jql = String.format("status=%s AND resolution=%s AND assignee=%s", status.getValue(), resolution != null ? resolution.getValue() : null, escapeUsername(getUsername())); - IssuesResponse response = connection.get(searchUrl, IssuesResponse.class, new UrlParam("jql", jql)); + IssuesResponse response = get(searchUrl, IssuesResponse.class, new UrlParam("jql", jql)); log.debug("{} tasks found", response.issues.length); return response; } @@ -127,11 +127,11 @@ public IssuesResponse getCreatedTasksForUser() { String jql = String.format("issuetype in (%s,subTaskIssueTypes()) AND status in (%s) AND reporter in (%s)", issueTypesToGet, allowedStatuses, escapeUsername(getUsername())); - return connection.get(searchUrl, IssuesResponse.class, new UrlParam("jql", jql)); + return get(searchUrl, IssuesResponse.class, new UrlParam("jql", jql)); } public IssueTransitions getAllowedTransitions(String key) { - IssueTransitions transitions = connection.get(urlBaseForKey(key) + "transitions", IssueTransitions.class); + IssueTransitions transitions = get(urlBaseForKey(key) + "transitions", IssueTransitions.class); if (transitions == null) { transitions = new IssueTransitions(); } @@ -148,11 +148,11 @@ public void transitionIssue(IssueTransition transition, IssueResolutionDefinitio if (resolution != null) { updateIssue.fields.resolution = new IssueResolution(resolution); } - connection.post(urlBaseForKey(transition.issueId) + "transitions", updateIssue); + post(urlBaseForKey(transition.issueId) + "transitions", updateIssue); } public Issue createIssue(Issue issue) { - return connection.post(apiUrl + "issue", Issue.class, issue); + return post(apiUrl + "issue", Issue.class, issue); } public void updateIssue(Issue issue) { diff --git a/serviceApis/src/main/java/com/vmware/reviewboard/ReviewBoard.java b/serviceApis/src/main/java/com/vmware/reviewboard/ReviewBoard.java index 203d30b0..981bddb9 100644 --- a/serviceApis/src/main/java/com/vmware/reviewboard/ReviewBoard.java +++ b/serviceApis/src/main/java/com/vmware/reviewboard/ReviewBoard.java @@ -64,7 +64,7 @@ public RootList getRootLinkList() { public ReviewRequests getReviewRequests(ReviewRequestStatus status) { Link reviewRequestLink = getRootLinkList().getReviewRequestsLink(); - return connection.get(reviewRequestLink.getHref(), ReviewRequests.class, + return get(reviewRequestLink.getHref(), ReviewRequests.class, new UrlParam("from-user", getUsername()), new UrlParam("status", status.name())); } @@ -81,7 +81,7 @@ public ReviewRequest[] getOpenReviewRequestsWithSubmittedComment() { public ReviewRequests getOpenReviewRequestsWithShipIts() { Link reviewRequestLink = getRootLinkList().getReviewRequestsLink(); - return connection.get(reviewRequestLink.getHref(), ReviewRequests.class, new UrlParam("from-user", getUsername()), + return get(reviewRequestLink.getHref(), ReviewRequests.class, new UrlParam("from-user", getUsername()), new UrlParam("status", pending.name()), new UrlParam("ship-it", "1")); } @@ -92,18 +92,18 @@ public List searchUsersMatchingText(Link usersLink, String textToMat if (!searchByUsernameOnly) { params.add(new UrlParam("fullname", "1")); } - return connection.get(usersLink.getHref(), ReviewUsersResponse.class, params).users; + return get(usersLink.getHref(), ReviewUsersResponse.class, params.toArray(new RequestParam[0])).users; } public int getFilesCountForReviewRequestDiff(Link filesLink) { - return connection.get(filesLink.getHref(), ResultsCount.class, new UrlParam("counts-only", "1")).count; + return get(filesLink.getHref(), ResultsCount.class, new UrlParam("counts-only", "1")).count; } public ReviewRequests getReviewRequestsWithShipItsForGroups(String groupNames, Date fromDate) { SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss"); String formattedDate = formatter.format(fromDate); Link reviewRequestLink = getRootLinkList().getReviewRequestsLink(); - return connection.get(reviewRequestLink.getHref(), ReviewRequests.class, + return get(reviewRequestLink.getHref(), ReviewRequests.class, new UrlParam("to-groups", groupNames), new UrlParam("max-results", "200"), new UrlParam("time-added-from", formattedDate), new UrlParam("ship-it", "1"), new UrlParam("status", all.name())); @@ -115,7 +115,7 @@ public ReviewRequest getReviewRequestById(Integer id) { } Link reviewRequestLink = getRootLinkList().getReviewRequestsLink(); reviewRequestLink.addPathParam(String.valueOf(id)); - return connection.get(reviewRequestLink.getHref(), ReviewRequestResponse.class).review_request; + return get(reviewRequestLink.getHref(), ReviewRequestResponse.class).review_request; } public ReviewRequest createReviewRequest(String repository) { @@ -134,7 +134,7 @@ public ReviewRequest createReviewRequestFromDraft(ReviewRequestDraft reviewReque } public ReviewRequestDraft getReviewRequestDraft(Link draftLink) { - return connection.get(draftLink.getHref(), ReviewRequestDraftResponse.class).draft; + return get(draftLink.getHref(), ReviewRequestDraftResponse.class).draft; } public ReviewRequestDraft getReviewRequestDraftWithExceptionHandling(Link draftLink) { @@ -164,27 +164,27 @@ public void updateReviewRequest(ReviewRequest reviewRequest) { } public void createReviewRequestDiff(Link diffLink, DiffToUpload diffToCreate) { - connection.post(diffLink.getHref(), diffToCreate); + post(diffLink.getHref(), diffToCreate); } public void createUserReview(ReviewRequest reviewRequest, UserReview review) { - connection.post(reviewRequest.getReviewsLink().getHref(), review); + post(reviewRequest.getReviewsLink().getHref(), review); } public UserReview[] getReviewsForReviewRequest(Link reviewsLink) { - return connection.get(reviewsLink.getHref(), UserReviewsResponse.class).reviews; + return get(reviewsLink.getHref(), UserReviewsResponse.class).reviews; } public ReviewRequestDiff[] getDiffsForReviewRequest(Link diffsLink) { - return connection.get(diffsLink.getHref(), ReviewRequestDiffsResponse.class).diffs; + return get(diffsLink.getHref(), ReviewRequestDiffsResponse.class).diffs; } public Repository getRepository(Link repositoryLink) { - return connection.get(repositoryLink.getHref(), RepositoryResponse.class).repository; + return get(repositoryLink.getHref(), RepositoryResponse.class).repository; } public String getDiffData(Link diffLink) { - String diffData = connection.get(diffLink.getHref(), String.class, anAcceptHeader("text/x-patch")); + String diffData = get(diffLink.getHref(), String.class, anAcceptHeader("text/x-patch")); // need to add in a trailing newline for git apply to work correctly diffData += "\n"; return diffData; diff --git a/serviceApis/src/main/java/com/vmware/slack/Slack.java b/serviceApis/src/main/java/com/vmware/slack/Slack.java index d97182cf..969c7293 100644 --- a/serviceApis/src/main/java/com/vmware/slack/Slack.java +++ b/serviceApis/src/main/java/com/vmware/slack/Slack.java @@ -25,7 +25,7 @@ public void sendMessage(String channel, String text) { message.channel = channel; message.text = text; - SlackResponse response = optimisticPost(apiUrl + "/chat.postMessage", SlackResponse.class, message); + SlackResponse response = post(apiUrl + "/chat.postMessage", SlackResponse.class, message); checkResponse(response); } diff --git a/serviceApis/src/main/java/com/vmware/trello/Trello.java b/serviceApis/src/main/java/com/vmware/trello/Trello.java index e16394cc..6c4e0435 100644 --- a/serviceApis/src/main/java/com/vmware/trello/Trello.java +++ b/serviceApis/src/main/java/com/vmware/trello/Trello.java @@ -59,11 +59,11 @@ public Trello(String trelloUrl) { } public Member getTrelloMember(String memberId) { - return connection.get(apiUrl + "members/" + memberId, Member.class); + return get(apiUrl + "members/" + memberId, Member.class); } public Board createBoard(Board boardToCreate) { - return connection.post(apiUrl + "boards", Board.class, boardToCreate); + return post(apiUrl + "boards", Board.class, boardToCreate); } public void closeBoard(Board board) { @@ -72,7 +72,7 @@ public void closeBoard(Board board) { } public Swimlane createSwimlane(Swimlane swimlaneToCreate) { - return connection.post(apiUrl + "lists", Swimlane.class, swimlaneToCreate); + return post(apiUrl + "lists", Swimlane.class, swimlaneToCreate); } public void closeSwimlane(Swimlane swimlane) { @@ -86,7 +86,7 @@ public void moveSwimlane(Swimlane swimlane, String position) { } public Card createCard(Card cardToCreate) { - return connection.post(apiUrl + "cards", Card.class, cardToCreate); + return post(apiUrl + "cards", Card.class, cardToCreate); } public void deleteCard(Card cardToDelete) { @@ -95,23 +95,23 @@ public void deleteCard(Card cardToDelete) { } public Board[] getOpenBoardsForUser() { - return connection.get(apiUrl + "members/me/boards", Board[].class, + return get(apiUrl + "members/me/boards", Board[].class, new UrlParam("filter", "open")); } public Swimlane[] getSwimlanesForBoard(Board board) { String url = String.format("%sboards/%s/lists", apiUrl, board.id); - return connection.get(url, Swimlane[].class); + return get(url, Swimlane[].class); } public Card[] getCardsForSwimlane(Swimlane swimlane) { String url = String.format("%slists/%s/cards", apiUrl, swimlane.id); - return connection.get(url, Card[].class); + return get(url, Card[].class); } public Card[] getCardsForBoard(Board board) { String url = String.format("%sboards/%s/cards", apiUrl, board.id); - return connection.get(url, Card[].class); + return get(url, Card[].class); } public void createDefaultSwimlanesIfNeeded(Board board, List storyPointValues) { @@ -211,7 +211,7 @@ private List scrapeAuthInfoFromUI(String apiTokenPage) { authorizeParams.add(new UrlParam("scope", "read,write")); authorizeParams.add(new UrlParam("expiration", "30days")); authorizeParams.add(new UrlParam("name", "Workflow Tools")); - String authorizeResponseText = connection.get(authorizeUrl, String.class, authorizeParams); + String authorizeResponseText = connection.get(authorizeUrl, String.class, authorizeParams.toArray(new RequestParam[0])); String requestKey = findMatchForPattern(authorizeResponseText, "name=\"requestKey\" value=\"(\\w+)\""); String signature = findMatchForPattern(authorizeResponseText, "name=\"signature\" value=\"([\\w/]+)\""); diff --git a/serviceApis/src/main/java/com/vmware/vcd/Vcd.java b/serviceApis/src/main/java/com/vmware/vcd/Vcd.java index c65436fc..7cbedede 100644 --- a/serviceApis/src/main/java/com/vmware/vcd/Vcd.java +++ b/serviceApis/src/main/java/com/vmware/vcd/Vcd.java @@ -43,7 +43,6 @@ * Represents the Vmware Vcloud Director Api */ public class Vcd extends AbstractRestService { - public static String AUTHORIZATION_HEADER = "x-vcloud-authorization"; private String apiVersion; private String vcdOrg; @@ -73,11 +72,11 @@ public Vcd(String vcdUrl, String apiVersion, String username, String vcdOrg) { } public Map getResourceAsMap(String resourcePath, String acceptType) { - return optimisticGet(UrlUtils.addRelativePaths(apiUrl, resourcePath), Map.class, anAcceptHeader(acceptType + "+json;version=" + apiVersion)); + return get(UrlUtils.addRelativePaths(apiUrl, resourcePath), Map.class, anAcceptHeader(acceptType + "+json;version=" + apiVersion)); } public Map updateResourceFromMap(String resourcePath, Map resourceValue, String contentType, String acceptType) { - return optimisticPut(UrlUtils.addRelativePaths(apiUrl, resourcePath), Map.class, resourceValue, + return put(UrlUtils.addRelativePaths(apiUrl, resourcePath), Map.class, resourceValue, aContentTypeHeader(contentType + "+json;version=" + apiVersion), anAcceptHeader(acceptType + "+json;version=" + apiVersion)); } @@ -90,19 +89,19 @@ public TaskType updateResource(LinkType link, ResourceType resourceType) { } public T getResource(LinkType link, Class resourceTypeClass) { - return optimisticGet(link.href, resourceTypeClass, acceptHeader(resourceTypeClass)); + return get(link.href, resourceTypeClass, acceptHeader(resourceTypeClass)); } public QueryResultVappType queryVappById(String id) { - QueryResultVappsType vapps = query("vApp", QueryResultVappsType.class, true, "id==" + id); + QueryResultVappsType vapps = queryVapps("id==" + id); if (vapps.record == null || vapps.record.isEmpty()) { throw new FatalException("Failed to find vapp for id " + id); } return vapps.record.get(0); } - public QueryResultVappsType queryVapps() { - QueryResultVappsType vapps = query("vApp", QueryResultVappsType.class, true); + public QueryResultVappsType queryVapps(String... filters) { + QueryResultVappsType vapps = query("vApp", QueryResultVappsType.class, true, filters); if (vapps.record != null) { String username = getUsername(); vapps.record.forEach(vapp -> vapp.setOwnedByWorkflowUser(username.equalsIgnoreCase(vapp.ownerName))); @@ -114,7 +113,11 @@ public QueryResultVappsType queryVapps() { } public QueryResultVMsType queryVmsForVapp(String vappId) { - return query("vm", QueryResultVMsType.class, false, "container==" + vappId); + return queryVms("container==" + vappId); + } + + public QueryResultVMsType queryVms(String... filters) { + return query("vm", QueryResultVMsType.class, true, filters); } public MetaDatasType getVappMetaData(LinkType metadataLink) { @@ -126,7 +129,7 @@ public T query(String queryType, Class responseTypeC if (filterValues.length > 0) { queryUrl += "&filter=" + Arrays.stream(filterValues).filter(Objects::nonNull).collect(Collectors.joining(";")); } - return optimisticGet(queryUrl, responseTypeClass, acceptHeader(responseTypeClass)); + return get(queryUrl, responseTypeClass, acceptHeader(responseTypeClass)); } public void waitForTaskToComplete(String taskHref, int amount, TimeUnit timeUnit) { @@ -134,7 +137,7 @@ public void waitForTaskToComplete(String taskHref, int amount, TimeUnit timeUnit long waitTimeInMilliseconds = timeUnit.toMillis(amount); while (elapsedTimeInMilliseconds < waitTimeInMilliseconds) { - TaskType task = optimisticGet(taskHref, TaskType.class, taskAcceptHeader()); + TaskType task = get(taskHref, TaskType.class, taskAcceptHeader()); log.info("Task: {}, status: {}", task.operation, task.status); if ("SUCCESS".equalsIgnoreCase(task.status)) { return; diff --git a/serviceApis/src/main/java/com/vmware/vcd/domain/QueryResultVMType.java b/serviceApis/src/main/java/com/vmware/vcd/domain/QueryResultVMType.java index 93448c9c..0b7cca56 100644 --- a/serviceApis/src/main/java/com/vmware/vcd/domain/QueryResultVMType.java +++ b/serviceApis/src/main/java/com/vmware/vcd/domain/QueryResultVMType.java @@ -1,6 +1,8 @@ package com.vmware.vcd.domain; -public class QueryResultVMType extends ResourceType { +import com.vmware.util.input.InputListSelection; + +public class QueryResultVMType extends ResourceType implements InputListSelection { public String container; public String containerName; public String status; @@ -8,4 +10,9 @@ public class QueryResultVMType extends ResourceType { public boolean isPoweredOn() { return "POWERED_ON".equalsIgnoreCase(status); } + + @Override + public String getLabel() { + return name + "(" + containerName + ") " + status; + } } diff --git a/utils/src/main/java/com/vmware/util/StringUtils.java b/utils/src/main/java/com/vmware/util/StringUtils.java index 1f0983a3..bd0bf0d4 100644 --- a/utils/src/main/java/com/vmware/util/StringUtils.java +++ b/utils/src/main/java/com/vmware/util/StringUtils.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; +import java.security.Key; import java.security.PrivateKey; import java.security.cert.Certificate; import java.security.cert.CertificateEncodingException; @@ -351,7 +352,7 @@ public static String convertToPem(final Certificate certificate) throws Certific return convertToPem(certificate.getEncoded(), BEGIN_CERT, END_CERT); } - public static String convertToPem(final PrivateKey key) { + public static String convertToPem(final Key key) { return convertToPem(key.getEncoded(), BEGIN_PRIVATE_KEY, END_PRIVATE_KEY); } diff --git a/workflow.jar.REMOVED.git-id b/workflow.jar.REMOVED.git-id index cbd6c53c..8de1c07b 100644 --- a/workflow.jar.REMOVED.git-id +++ b/workflow.jar.REMOVED.git-id @@ -1 +1 @@ -34c47a99e906a75c9d8bb58774607c52ba8ddf21 \ No newline at end of file +40bf091e3d2b76d92ed7dae97c5cf97adbbf1588 \ No newline at end of file