From cafcb368e2f184e87a788f73a9121234c39cfd3e Mon Sep 17 00:00:00 2001 From: mateii Date: Wed, 23 Nov 2022 11:07:45 +0200 Subject: [PATCH 01/16] first structure of openshift --- .../utils/GetAuthorizationTokenV2.java | 19 ++ .../services/AuthorizationTokenV2Impl.java | 14 ++ cs-openshift/license.template | 12 + cs-openshift/pom.xml | 236 ++++++++++++++++++ .../redhat/actions/GetTokenAction.java | 112 +++++++++ .../redhat/services/OpenshiftService.java | 24 ++ .../content/redhat/utils/Constants.java | 52 ++++ .../content/redhat/utils/Descriptions.java | 47 ++++ .../content/redhat/utils/Outputs.java | 30 +++ cs-openshift/src/pom.xml | 230 +++++++++++++++++ 10 files changed, 776 insertions(+) create mode 100644 cs-openshift/license.template create mode 100644 cs-openshift/pom.xml create mode 100644 cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetTokenAction.java create mode 100644 cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java create mode 100644 cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java create mode 100644 cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java create mode 100644 cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Outputs.java create mode 100644 cs-openshift/src/pom.xml diff --git a/cs-office-365/src/main/java/io/cloudslang/content/office365/actions/utils/GetAuthorizationTokenV2.java b/cs-office-365/src/main/java/io/cloudslang/content/office365/actions/utils/GetAuthorizationTokenV2.java index ea4de73acb..3c5cef2be3 100644 --- a/cs-office-365/src/main/java/io/cloudslang/content/office365/actions/utils/GetAuthorizationTokenV2.java +++ b/cs-office-365/src/main/java/io/cloudslang/content/office365/actions/utils/GetAuthorizationTokenV2.java @@ -1,3 +1,17 @@ +/* + * (c) Copyright 2019 Micro Focus, L.P. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License v2.0 which accompany this distribution. + * + * The Apache License is available at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ /* * (c) Copyright 2022 Micro Focus, L.P. * All rights reserved. This program and the accompanying materials @@ -88,6 +102,7 @@ public class GetAuthorizationTokenV2 { public Map execute(@Param(value = LOGIN_TYPE, description = LOGIN_TYPE_DESC) String loginType, @Param(value = CLIENT_ID, required = true, description = CLIENT_ID_DESC) String clientId, @Param(value = CLIENT_SECRET, encrypted = true, description = CLIENT_SECRET_DESC) String clientSecret, + @Param(value = RESOURCE, description = RESOURCES_DESC) String resource, @Param(value = USERNAME, description = USERNAME_DESC) String username, @Param(value = PASSWORD, encrypted = true, description = PASSWORD_DESC) String password, @Param(value = LOGIN_AUTHORITY, required = true, description = LOGIN_AUTHORITY_DESC) String loginAuthority, @@ -111,6 +126,10 @@ public Map execute(@Param(value = LOGIN_TYPE, description = LOGI return getFailureResultsMap(StringUtilities.join(exceptionMessages, NEW_LINE)); } + if(!resource.isEmpty()){ + scope = resource; + } + try { final IAuthenticationResult result = AuthorizationTokenV2Impl.getToken(AuthorizationTokenInputs.builder() .loginType(loginType) diff --git a/cs-office-365/src/main/java/io/cloudslang/content/office365/services/AuthorizationTokenV2Impl.java b/cs-office-365/src/main/java/io/cloudslang/content/office365/services/AuthorizationTokenV2Impl.java index 4d6aaae209..fe407c8cee 100644 --- a/cs-office-365/src/main/java/io/cloudslang/content/office365/services/AuthorizationTokenV2Impl.java +++ b/cs-office-365/src/main/java/io/cloudslang/content/office365/services/AuthorizationTokenV2Impl.java @@ -1,3 +1,17 @@ +/* + * (c) Copyright 2019 Micro Focus, L.P. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License v2.0 which accompany this distribution. + * + * The Apache License is available at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ /* * (c) Copyright 2022 Micro Focus, L.P. * All rights reserved. This program and the accompanying materials diff --git a/cs-openshift/license.template b/cs-openshift/license.template new file mode 100644 index 0000000000..7592b89f18 --- /dev/null +++ b/cs-openshift/license.template @@ -0,0 +1,12 @@ +* (c) Copyright ${copyright.year} Micro Focus, L.P. +* All rights reserved. This program and the accompanying materials +* are made available under the terms of the Apache License v2.0 which accompany this distribution. +* +* The Apache License is available at +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. \ No newline at end of file diff --git a/cs-openshift/pom.xml b/cs-openshift/pom.xml new file mode 100644 index 0000000000..5d0b0f6fd1 --- /dev/null +++ b/cs-openshift/pom.xml @@ -0,0 +1,236 @@ + + + + 4.0.0 + + io.cloudslang.content + cs-openshift + 0.0.1-SNAPSHOT + jar + + ${project.groupId}:${project.artifactId} + Openshift + https://github.com/CloudSlang/cs-actions + + + + The Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + + + + + scm:git:https://CloudSlang/cs-actions.git + scm:git:git@github.com:CloudSlang/cs-actions.git + https://github.com/CloudSlang/cs-actions.git + master + + + + + ossrh + Maven Central staging repository + https://oss.sonatype.org/service/local/staging/deploy/maven2/ + + + ossrh + Maven Central snapshots repository + https://oss.sonatype.org/content/repositories/snapshots/ + + + + + + 3.1 + 2.5.2 + 2.10.4 + 3.0 + 3.0.1 + 2.8.2 + 1.6 + + 1.10.7 + 0.0.5 + 0.1.74 + + UTF-8 + 1.8 + 1.8 + true + overwritten_by_ci + + + + + + + com.hp.score.sdk + score-content-sdk + ${score-content-sdk.version} + + + io.cloudslang.content + cs-commons + ${cs-commons.version} + + + io.cloudslang.content + cs-httpclient-commons + 0.0.1-SNAPSHOT + compile + + + + + + + + + org.apache.maven.plugins + maven-source-plugin + ${maven-source-plugin.version} + + + attach-sources + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + ${maven-javadoc-plugin.version} + + + attach-javadocs + + jar + + + + + -Xdoclint:none + + + + org.apache.maven.plugins + maven-deploy-plugin + ${maven-deploy-plugin.version} + + + org.apache.maven.plugins + maven-release-plugin + ${maven-release-plugin.version} + + + + + + + com.mycila + license-maven-plugin + 3.0 + +
${project.basedir}/license.template
+ + ${project.basedir}/header-definitions.xml + + + **/*.java + pom.xml + + + 2022 + + false + + cs-actions-java-header + XML_STYLE + +
+ + + process-resources + + format + + + +
+ + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + +
+
+ + + + gpg + + + + org.apache.maven.plugins + maven-gpg-plugin + ${maven-gpg-plugin.version} + + + sign-artifacts + verify + + sign + + + + + + + + + deploy-internal + + + + org.apache.maven.plugins + maven-deploy-plugin + ${maven-deploy-plugin.version} + + + custom-deployment-to-nexus + + deploy + + deploy + + ${custom.nexus} + + + + + + + + + +
\ No newline at end of file diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetTokenAction.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetTokenAction.java new file mode 100644 index 0000000000..887ea054ae --- /dev/null +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetTokenAction.java @@ -0,0 +1,112 @@ +/* + * (c) Copyright 2022 Micro Focus, L.P. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License v2.0 which accompany this distribution. + * + * The Apache License is available at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package io.cloudslang.content.redhat.actions; + +import com.hp.oo.sdk.content.annotations.Action; +import com.hp.oo.sdk.content.annotations.Output; +import com.hp.oo.sdk.content.annotations.Param; +import com.hp.oo.sdk.content.annotations.Response; +import com.hp.oo.sdk.content.plugin.GlobalSessionObject; +import com.hp.oo.sdk.content.plugin.SerializableSessionObject; +import io.cloudslang.content.constants.ReturnCodes; +import io.cloudslang.content.httpclient.actions.HttpClientGetAction; +import io.cloudslang.content.utils.OutputUtilities; + +import java.util.Map; + +import static com.hp.oo.sdk.content.plugin.ActionMetadata.MatchType.COMPARE_EQUAL; +import static com.hp.oo.sdk.content.plugin.ActionMetadata.ResponseType.ERROR; +import static com.hp.oo.sdk.content.plugin.ActionMetadata.ResponseType.RESOLVED; +import static io.cloudslang.content.constants.OutputNames.*; +import static io.cloudslang.content.constants.ResponseNames.FAILURE; +import static io.cloudslang.content.constants.ResponseNames.SUCCESS; +import static io.cloudslang.content.httpclient.utils.Descriptions.HTTPClient.SESSION_CONNECTION_POOL_DESC; +import static io.cloudslang.content.httpclient.utils.Descriptions.HTTPClient.SESSION_COOKIES_DESC; +import static io.cloudslang.content.redhat.services.OpenshiftService.processHttpResult; +import static io.cloudslang.content.redhat.utils.Constants.CommonConstants.*; +import static io.cloudslang.content.redhat.utils.Constants.CommonConstants.HOST; +import static io.cloudslang.content.redhat.utils.Constants.CommonConstants.PASSWORD; +import static io.cloudslang.content.redhat.utils.Constants.CommonConstants.USERNAME; +import static io.cloudslang.content.redhat.utils.Descriptions.Common.RETURN_CODE_DESC; +import static io.cloudslang.content.redhat.utils.Descriptions.GetTokenAction.*; +import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.AUTH_TOKEN; +import static org.apache.commons.lang3.StringUtils.*; + +public class GetTokenAction { + @Action(name = GET_TOKEN_NAME, + description = "DESC", + outputs = { + @Output(value = RETURN_RESULT, description = RETURN_RESULT_DESC), + @Output(value = RETURN_CODE, description = RETURN_CODE_DESC), + @Output(value = AUTH_TOKEN, description = AUTH_TOKEN_DESC), + @Output(value = EXCEPTION, description = EXCEPTION_DESC) + }, + responses = { + @Response(text = SUCCESS, field = RETURN_CODE, value = ReturnCodes.SUCCESS, matchType = COMPARE_EQUAL, responseType = RESOLVED, description = SUCCESS_DESC), + @Response(text = FAILURE, field = RETURN_CODE, value = ReturnCodes.FAILURE, matchType = COMPARE_EQUAL, responseType = ERROR, description = FAILURE_DESC) + }) + public Map execute(@Param(value = HOST, description = HOST_DESC) String host, + @Param(value = USERNAME, description = USERNAME_DESC) String username, + @Param(value = PASSWORD, encrypted = true, description = PASSWORD_DESC) String password, + @Param(value = TRUST_ALL_ROOTS, description = TRUST_ALL_ROOTS_DESCRIPTION) String trustAllRoots, + @Param(value = X509_HOSTNAME_VERIFIER, description = X509_HOSTNAME_VERIFIER_DESCRIPTION) String x509HostnameVerifier, + @Param(value = SESSION_COOKIES, description = SESSION_COOKIES_DESC) SerializableSessionObject sessionCookies, + @Param(value = SESSION_CONNECTION_POOL, description = SESSION_CONNECTION_POOL_DESC) GlobalSessionObject sessionConnectionPool) { + try { + Map result = new HttpClientGetAction().execute( + host, + "basic", + username, + password, + "false", + "web-proxy.eu.softwaregrp.net", + "8080", + "", + "", + "", + "", + trustAllRoots, + x509HostnameVerifier, + "", + "", + "", + "", + "", + "", + "", + "false", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + sessionCookies, + sessionConnectionPool); + + processHttpResult(result); + return result; + } catch (Exception exception) { + return OutputUtilities.getFailureResultsMap(exception); + } + } + +} diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java new file mode 100644 index 0000000000..46fa7917a2 --- /dev/null +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java @@ -0,0 +1,24 @@ +package io.cloudslang.content.redhat.services; + +import org.apache.commons.lang3.StringUtils; + +import java.util.Map; + +import static io.cloudslang.content.constants.OutputNames.RETURN_CODE; +import static io.cloudslang.content.constants.OutputNames.RETURN_RESULT; +import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.EXCEPTION; +import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.STATUS_CODE; + +public class OpenshiftService { + + public static void processHttpResult(Map httpResults) { + + String statusCode = httpResults.get(STATUS_CODE); + + if (StringUtils.isEmpty(statusCode) || Integer.parseInt(statusCode) < 200 || Integer.parseInt(statusCode) >= 300) { + if(StringUtils.isEmpty(httpResults.get(EXCEPTION))) + httpResults.put(EXCEPTION, httpResults.get(RETURN_RESULT)); + httpResults.put(RETURN_CODE, "-1"); + } + } +} diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java new file mode 100644 index 0000000000..17cbddf47a --- /dev/null +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java @@ -0,0 +1,52 @@ +/* + * (c) Copyright 2022 Micro Focus + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License v2.0 which accompany this distribution. + * + * The Apache License is available at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +*/ +/* + * (c) Copyright 2022 Micro Focus + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License v2.0 which accompany this distribution. + * + * The Apache License is available at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.cloudslang.content.redhat.utils; + +public final class Constants { + + public static final class CommonConstants { + + //Inputs + public static final String HOST = "hostname"; + public static final String USERNAME = "username"; + public static final String PASSWORD = "password"; + public static final String TRUST_ALL_ROOTS = "trustAllRoots"; + public static final String X509_HOSTNAME_VERIFIER = "x509HostnameVerifier"; + public static final String SESSION_COOKIES = "httpClientCookieSession"; + public static final String SESSION_CONNECTION_POOL = "httpClientPoolingConnectionManager"; + + // + public static final String BASIC = "BASIC"; + public static final String APPLICATION_JSON = "application/json"; + public static final String CONTENT_TYPE = "Content-Type:"; + + } + +} + diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java new file mode 100644 index 0000000000..00957657cf --- /dev/null +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java @@ -0,0 +1,47 @@ +/* + * (c) Copyright 2022 Micro Focus, L.P. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License v2.0 which accompany this distribution. + * + * The Apache License is available at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.cloudslang.content.redhat.utils; + +public class Descriptions { + public static class Common{ + public static final String RETURN_CODE_DESC = "0 if success, -1 if failure."; + } + public static class GetTokenAction { + + public static final String GET_TOKEN_NAME = "Get Token"; + public static final String RETURN_RESULT_DESC = "The authorization token for Openshift."; + public static final String AUTH_TOKEN_DESC = "Generated authentication token."; + public static final String EXCEPTION_DESC = "An error message in case there was an error while generating the " + + "token."; + public static final String TRUST_ALL_ROOTS_DESCRIPTION = "Specifies whether to enable weak security over SSL/TSL. " + + "A certificate is trusted even if no trusted certification authority issued it."; + public static final String X509_HOSTNAME_VERIFIER_DESCRIPTION = "Specifies the way the server hostname must match a domain name in " + + "the subject's Common Name (CN) or subjectAltName field of the X.509 certificate. Set this to " + + "\"allow_all\" to skip any checking. For the value \"browser_compatible\" the hostname verifier " + + "works the same way as Curl and Firefox. The hostname must match either the first CN, or any of " + + "the subject-alts. A wildcard can occur in the CN, and in any of the subject-alts. The only " + + "difference between \"browser_compatible\" and \"strict\" is that a wildcard (such as \"*.foo.com\") " + + "with \"browser_compatible\" matches all subdomains, including \"a.b.foo.com\"."; + public static final String USERNAME_DESC = "The name of the user who is logging in to Openshift."; + public static final String PASSWORD_DESC = "The password used by the user to log in to Openshift."; + public static final String HOST_DESC = "HOST."; + + public static final String SUCCESS_DESC = "Token generated successfully."; + public static final String FAILURE_DESC = "There was an error while trying to retrieve token."; + + + } +} diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Outputs.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Outputs.java new file mode 100644 index 0000000000..df2f581597 --- /dev/null +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Outputs.java @@ -0,0 +1,30 @@ +/* + * (c) Copyright 2021 Micro Focus, L.P. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License v2.0 which accompany this distribution. + * + * The Apache License is available at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.cloudslang.content.redhat.utils; + +import io.cloudslang.content.constants.OutputNames; + +public final class Outputs extends OutputNames { + + public static class OutputNames { + + public static final String STATUS_CODE = "statusCode"; + public static final String EXCEPTION = "exception"; + public static final String AUTH_TOKEN = "authToken"; + + } +} + diff --git a/cs-openshift/src/pom.xml b/cs-openshift/src/pom.xml new file mode 100644 index 0000000000..150dbe2bad --- /dev/null +++ b/cs-openshift/src/pom.xml @@ -0,0 +1,230 @@ + + + + 4.0.0 + + io.cloudslang.content + cs-openshift + 1.0.0-SNAPSHOT + jar + + ${project.groupId}:${project.artifactId} + Openshift + https://github.com/CloudSlang/cs-actions + + + + The Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + + + + + scm:git:https://CloudSlang/cs-actions.git + scm:git:git@github.com:CloudSlang/cs-actions.git + https://github.com/CloudSlang/cs-actions.git + master + + + + + ossrh + Maven Central staging repository + https://oss.sonatype.org/service/local/staging/deploy/maven2/ + + + ossrh + Maven Central snapshots repository + https://oss.sonatype.org/content/repositories/snapshots/ + + + + + + 3.1 + 2.5.2 + 2.10.4 + 3.0 + 3.0.1 + 2.8.2 + 1.6 + + 1.10.7 + 0.0.5 + 0.1.74 + + UTF-8 + 1.8 + 1.8 + true + overwritten_by_ci + + + + + + + com.hp.score.sdk + score-content-sdk + ${score-content-sdk.version} + + + io.cloudslang.content + cs-commons + ${cs-commons.version} + + + + + + + + + org.apache.maven.plugins + maven-source-plugin + ${maven-source-plugin.version} + + + attach-sources + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + ${maven-javadoc-plugin.version} + + + attach-javadocs + + jar + + + + + -Xdoclint:none + + + + org.apache.maven.plugins + maven-deploy-plugin + ${maven-deploy-plugin.version} + + + org.apache.maven.plugins + maven-release-plugin + ${maven-release-plugin.version} + + + + + + + com.mycila + license-maven-plugin + 3.0 + +
${project.basedir}/license.template
+ + ${project.basedir}/header-definitions.xml + + + **/*.java + pom.xml + + + 2022 + + false + + cs-actions-java-header + XML_STYLE + +
+ + + process-resources + + format + + + +
+ + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + +
+
+ + + + gpg + + + + org.apache.maven.plugins + maven-gpg-plugin + ${maven-gpg-plugin.version} + + + sign-artifacts + verify + + sign + + + + + + + + + deploy-internal + + + + org.apache.maven.plugins + maven-deploy-plugin + ${maven-deploy-plugin.version} + + + custom-deployment-to-nexus + + deploy + + deploy + + ${custom.nexus} + + + + + + + + + +
\ No newline at end of file From 80999c3fdc80e90944c4c1cdb4e3c90cf23c4bdd Mon Sep 17 00:00:00 2001 From: mateii Date: Thu, 24 Nov 2022 16:54:20 +0200 Subject: [PATCH 02/16] first structure of openshift --- cs-httpclient-commons/pom.xml | 2 +- cs-openshift/pom.xml | 9 +- .../redhat/actions/GetTokenAction.java | 28 ++- cs-openshift/src/pom.xml | 230 ------------------ pom.xml | 1 + 5 files changed, 27 insertions(+), 243 deletions(-) delete mode 100644 cs-openshift/src/pom.xml diff --git a/cs-httpclient-commons/pom.xml b/cs-httpclient-commons/pom.xml index aebbd76060..a5d6bfa932 100644 --- a/cs-httpclient-commons/pom.xml +++ b/cs-httpclient-commons/pom.xml @@ -21,7 +21,7 @@ io.cloudslang.content cs-httpclient-commons - 0.0.1-SNAPSHOT + 0.0.2-SNAPSHOT jar ${project.groupId}:${project.artifactId} diff --git a/cs-openshift/pom.xml b/cs-openshift/pom.xml index 5d0b0f6fd1..5d1466c742 100644 --- a/cs-openshift/pom.xml +++ b/cs-openshift/pom.xml @@ -91,10 +91,17 @@ io.cloudslang.content cs-httpclient-commons - 0.0.1-SNAPSHOT + 0.0.2-SNAPSHOT compile + + + org.jsoup + jsoup + 1.15.3 + + diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetTokenAction.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetTokenAction.java index 887ea054ae..a649ad6179 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetTokenAction.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetTokenAction.java @@ -25,7 +25,9 @@ import io.cloudslang.content.constants.ReturnCodes; import io.cloudslang.content.httpclient.actions.HttpClientGetAction; import io.cloudslang.content.utils.OutputUtilities; +import org.apache.commons.codec.binary.Base64; +import java.nio.charset.StandardCharsets; import java.util.Map; import static com.hp.oo.sdk.content.plugin.ActionMetadata.MatchType.COMPARE_EQUAL; @@ -67,35 +69,39 @@ public Map execute(@Param(value = HOST, description = HOST_DESC) @Param(value = SESSION_COOKIES, description = SESSION_COOKIES_DESC) SerializableSessionObject sessionCookies, @Param(value = SESSION_CONNECTION_POOL, description = SESSION_CONNECTION_POOL_DESC) GlobalSessionObject sessionConnectionPool) { try { + + String auth = "kubeadmin" + ":" + "XFaTK-6qDEQ-HsBEH-H29gk"; + byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(StandardCharsets.ISO_8859_1)); + Map result = new HttpClientGetAction().execute( - host, - "basic", - username, - password, - "false", - "web-proxy.eu.softwaregrp.net", - "8080", + "https://oauth-openshift.apps.oscbtp448t.swinfra.net/oauth/authorize", + "Anonymous", "", "", + "true", "", "", - trustAllRoots, - x509HostnameVerifier, - "", "", "", "", "", + trustAllRoots, + x509HostnameVerifier, "", "", - "false", "", "", "", "", "", + "true", + "true", + "Authorization: Basic " + new String(encodedAuth), "", "", + "response_type=code&client_id=openshift-browser-client", + "true", + "false", "", "", "", diff --git a/cs-openshift/src/pom.xml b/cs-openshift/src/pom.xml deleted file mode 100644 index 150dbe2bad..0000000000 --- a/cs-openshift/src/pom.xml +++ /dev/null @@ -1,230 +0,0 @@ - - - - 4.0.0 - - io.cloudslang.content - cs-openshift - 1.0.0-SNAPSHOT - jar - - ${project.groupId}:${project.artifactId} - Openshift - https://github.com/CloudSlang/cs-actions - - - - The Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt - - - - - scm:git:https://CloudSlang/cs-actions.git - scm:git:git@github.com:CloudSlang/cs-actions.git - https://github.com/CloudSlang/cs-actions.git - master - - - - - ossrh - Maven Central staging repository - https://oss.sonatype.org/service/local/staging/deploy/maven2/ - - - ossrh - Maven Central snapshots repository - https://oss.sonatype.org/content/repositories/snapshots/ - - - - - - 3.1 - 2.5.2 - 2.10.4 - 3.0 - 3.0.1 - 2.8.2 - 1.6 - - 1.10.7 - 0.0.5 - 0.1.74 - - UTF-8 - 1.8 - 1.8 - true - overwritten_by_ci - - - - - - - com.hp.score.sdk - score-content-sdk - ${score-content-sdk.version} - - - io.cloudslang.content - cs-commons - ${cs-commons.version} - - - - - - - - - org.apache.maven.plugins - maven-source-plugin - ${maven-source-plugin.version} - - - attach-sources - - jar-no-fork - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - ${maven-javadoc-plugin.version} - - - attach-javadocs - - jar - - - - - -Xdoclint:none - - - - org.apache.maven.plugins - maven-deploy-plugin - ${maven-deploy-plugin.version} - - - org.apache.maven.plugins - maven-release-plugin - ${maven-release-plugin.version} - - - - - - - com.mycila - license-maven-plugin - 3.0 - -
${project.basedir}/license.template
- - ${project.basedir}/header-definitions.xml - - - **/*.java - pom.xml - - - 2022 - - false - - cs-actions-java-header - XML_STYLE - -
- - - process-resources - - format - - - -
- - org.apache.maven.plugins - maven-compiler-plugin - - 8 - 8 - - -
-
- - - - gpg - - - - org.apache.maven.plugins - maven-gpg-plugin - ${maven-gpg-plugin.version} - - - sign-artifacts - verify - - sign - - - - - - - - - deploy-internal - - - - org.apache.maven.plugins - maven-deploy-plugin - ${maven-deploy-plugin.version} - - - custom-deployment-to-nexus - - deploy - - deploy - - ${custom.nexus} - - - - - - - - - -
\ No newline at end of file diff --git a/pom.xml b/pom.xml index 03a9ad3b63..f711267340 100644 --- a/pom.xml +++ b/pom.xml @@ -359,6 +359,7 @@ cs-winrm cs-xml cs-cyberark + cs-openshift From 443607ec58344b87945ac682c53be8aa4ba3fe0e Mon Sep 17 00:00:00 2001 From: mateii Date: Thu, 24 Nov 2022 23:18:18 +0200 Subject: [PATCH 03/16] first structure of openshift --- cs-openshift/header-definitions.xml | 15 +++++++++++++++ .../content/redhat/actions/GetTokenAction.java | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 cs-openshift/header-definitions.xml diff --git a/cs-openshift/header-definitions.xml b/cs-openshift/header-definitions.xml new file mode 100644 index 0000000000..0f34784aaa --- /dev/null +++ b/cs-openshift/header-definitions.xml @@ -0,0 +1,15 @@ + + + + /* + + */ + + /* + */ + true + false + true + false + + diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetTokenAction.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetTokenAction.java index a649ad6179..c5efd519a7 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetTokenAction.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetTokenAction.java @@ -70,7 +70,7 @@ public Map execute(@Param(value = HOST, description = HOST_DESC) @Param(value = SESSION_CONNECTION_POOL, description = SESSION_CONNECTION_POOL_DESC) GlobalSessionObject sessionConnectionPool) { try { - String auth = "kubeadmin" + ":" + "XFaTK-6qDEQ-HsBEH-H29gk"; + String auth = username + ":" + password; byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(StandardCharsets.ISO_8859_1)); Map result = new HttpClientGetAction().execute( From 64727e02a56bfcc5162e87213cfc90406d83e615 Mon Sep 17 00:00:00 2001 From: AlexandruPresecan <79699589+AlexandruPresecan@users.noreply.github.com> Date: Thu, 8 Dec 2022 13:24:12 +0200 Subject: [PATCH 04/16] Openshift delete deployment (#1110) * added delete deployment operation * common inputs and constants --- .../redhat/actions/DeleteDeployment.java | 122 ++++++++++++++++++ .../redhat/actions/GetTokenAction.java | 11 +- .../content/redhat/utils/Constants.java | 25 ++++ .../content/redhat/utils/Descriptions.java | 82 ++++++++++-- 4 files changed, 223 insertions(+), 17 deletions(-) create mode 100644 cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/DeleteDeployment.java diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/DeleteDeployment.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/DeleteDeployment.java new file mode 100644 index 0000000000..2c0294c253 --- /dev/null +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/DeleteDeployment.java @@ -0,0 +1,122 @@ +/* + * (c) Copyright 2022 Micro Focus, L.P. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License v2.0 which accompany this distribution. + * + * The Apache License is available at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package io.cloudslang.content.redhat.actions; + +import com.hp.oo.sdk.content.annotations.Action; +import com.hp.oo.sdk.content.annotations.Output; +import com.hp.oo.sdk.content.annotations.Param; +import com.hp.oo.sdk.content.annotations.Response; +import com.hp.oo.sdk.content.plugin.GlobalSessionObject; +import com.hp.oo.sdk.content.plugin.SerializableSessionObject; +import com.sun.xml.internal.ws.api.model.wsdl.WSDLBoundOperation; +import io.cloudslang.content.constants.ReturnCodes; +import io.cloudslang.content.httpclient.actions.HttpClientDeleteAction; +import io.cloudslang.content.utils.OutputUtilities; + +import java.util.Map; + +import static com.hp.oo.sdk.content.plugin.ActionMetadata.MatchType.COMPARE_EQUAL; +import static com.hp.oo.sdk.content.plugin.ActionMetadata.ResponseType.ERROR; +import static com.hp.oo.sdk.content.plugin.ActionMetadata.ResponseType.RESOLVED; +import static io.cloudslang.content.constants.OutputNames.*; +import static io.cloudslang.content.constants.ResponseNames.FAILURE; +import static io.cloudslang.content.constants.ResponseNames.SUCCESS; +import static io.cloudslang.content.httpclient.utils.Descriptions.HTTPClient.SESSION_CONNECTION_POOL_DESC; +import static io.cloudslang.content.httpclient.utils.Descriptions.HTTPClient.SESSION_COOKIES_DESC; +import static io.cloudslang.content.redhat.services.OpenshiftService.processHttpResult; +import static io.cloudslang.content.redhat.utils.Constants.CommonConstants.*; +import static io.cloudslang.content.redhat.utils.Descriptions.Common.*; +import static io.cloudslang.content.redhat.utils.Descriptions.DeleteDeployment.*; +import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.AUTH_TOKEN; + +public class DeleteDeployment { + @Action(name = DELETE_DEPLOYMENT_NAME, + description = DELETE_DEPLOYMENT_DESC, + outputs = { + @Output(value = RETURN_RESULT, description = RETURN_RESULT_DESC), + @Output(value = RETURN_CODE, description = RETURN_CODE_DESC), + @Output(value = EXCEPTION, description = EXCEPTION_DESC) + }, + responses = { + @Response(text = SUCCESS, field = RETURN_CODE, value = ReturnCodes.SUCCESS, matchType = COMPARE_EQUAL, responseType = RESOLVED, description = SUCCESS_DESC), + @Response(text = FAILURE, field = RETURN_CODE, value = ReturnCodes.FAILURE, matchType = COMPARE_EQUAL, responseType = ERROR, description = FAILURE_DESC) + }) + public Map execute(@Param(value = HOST, required = true, description = HOST_DESC) String host, + @Param(value = AUTH_TOKEN, required = true, description = AUTH_TOKEN_DESC) String authToken, + @Param(value = NAMESPACE, required = true, description = NAMESPACE_DESC) String namespace, + @Param(value = DEPLOYMENT, required = true, description = DEPLOYMENT_DESC) String deployment, + + @Param(value = PROXY_HOST, description = PROXY_HOST_DESC) String proxyHost, + @Param(value = PROXY_PORT, description = PROXY_PORT_DESC) String proxyPort, + @Param(value = PROXY_USERNAME, description = PROXY_USERNAME_DESC) String proxyUsername, + @Param(value = PROXY_PASSWORD, description = PROXY_PASSWORD_DESC) String proxyPassword, + @Param(value = TLS_VERSION, description = TLS_VERSION_DESC) String tlsVersion, + @Param(value = ALLOWED_CIPHERS, description = ALLOWED_CIPHERS_DESC) String allowedCyphers, + @Param(value = TRUST_ALL_ROOTS, description = TRUST_ALL_ROOTS_DESC) String trustAllRoots, + @Param(value = X509_HOSTNAME_VERIFIER, description = X509_HOSTNAME_VERIFIER_DESC) String x509HostnameVerifier, + @Param(value = TRUST_KEYSTORE, description = TRUST_KEYSTORE_DESC) String trustKeystore, + @Param(value = TRUST_PASSWORD, description = TRUST_PASSWORD_DESC) String trustPassword, + @Param(value = KEYSTORE, description = KEYSTORE_DESC) String keystore, + @Param(value = KEYSTORE_PASSWORD, description = KEYSTORE_PASSWORD_DESC) String keystorePassword, + @Param(value = CONNECT_TIMEOUT, description = CONNECT_TIMEOUT_DESC) String connectTimeout, + @Param(value = EXECUTION_TIMEOUT, description = EXECUTION_TIMEOUT_DESC) String executionTimeout, + @Param(value = KEEP_ALIVE, description = KEEP_ALIVE_DESC) String keepAlive, + @Param(value = CONNECTIONS_MAX_PER_ROUTE, description = CONNECTIONS_MAX_PER_ROUTE_DESC) String connectionsMaxPerRoute, + @Param(value = CONNECTIONS_MAX_TOTAL, description = CONNECTIONS_MAX_TOTAL_DESC) String connectionsMaxTotal, + @Param(value = SESSION_COOKIES, description = SESSION_COOKIES_DESC) SerializableSessionObject sessionCookies, + @Param(value = SESSION_CONNECTION_POOL, description = SESSION_CONNECTION_POOL_DESC) GlobalSessionObject sessionConnectionPool) { + try { + + Map result = new HttpClientDeleteAction().execute( + host + APPS_V1_NAMESPACES + namespace + DEPLOYMENTS + deployment, + ANONYMOUS, + EMPTY_STRING, + EMPTY_STRING, + TRUE, + proxyHost, + proxyPort, + proxyUsername, + proxyPassword, + tlsVersion, + allowedCyphers, + trustAllRoots, + x509HostnameVerifier, + trustKeystore, + trustPassword, + keystore, + keystorePassword, + keepAlive, + connectionsMaxPerRoute, + connectionsMaxTotal, + TRUE, + AUTHORIZATION_BEARER + authToken, + EMPTY_STRING, + EMPTY_STRING, + connectTimeout, + EMPTY_STRING, + executionTimeout, + sessionCookies, + sessionConnectionPool); + + processHttpResult(result); + return result; + } catch (Exception exception) { + return OutputUtilities.getFailureResultsMap(exception); + } + } + +} diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetTokenAction.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetTokenAction.java index c5efd519a7..947f9567b0 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetTokenAction.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetTokenAction.java @@ -40,13 +40,10 @@ import static io.cloudslang.content.httpclient.utils.Descriptions.HTTPClient.SESSION_COOKIES_DESC; import static io.cloudslang.content.redhat.services.OpenshiftService.processHttpResult; import static io.cloudslang.content.redhat.utils.Constants.CommonConstants.*; -import static io.cloudslang.content.redhat.utils.Constants.CommonConstants.HOST; -import static io.cloudslang.content.redhat.utils.Constants.CommonConstants.PASSWORD; -import static io.cloudslang.content.redhat.utils.Constants.CommonConstants.USERNAME; -import static io.cloudslang.content.redhat.utils.Descriptions.Common.RETURN_CODE_DESC; +import static io.cloudslang.content.redhat.utils.Descriptions.Common.*; +import static io.cloudslang.content.redhat.utils.Descriptions.GetTokenAction.AUTH_TOKEN_DESC; import static io.cloudslang.content.redhat.utils.Descriptions.GetTokenAction.*; import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.AUTH_TOKEN; -import static org.apache.commons.lang3.StringUtils.*; public class GetTokenAction { @Action(name = GET_TOKEN_NAME, @@ -64,8 +61,8 @@ public class GetTokenAction { public Map execute(@Param(value = HOST, description = HOST_DESC) String host, @Param(value = USERNAME, description = USERNAME_DESC) String username, @Param(value = PASSWORD, encrypted = true, description = PASSWORD_DESC) String password, - @Param(value = TRUST_ALL_ROOTS, description = TRUST_ALL_ROOTS_DESCRIPTION) String trustAllRoots, - @Param(value = X509_HOSTNAME_VERIFIER, description = X509_HOSTNAME_VERIFIER_DESCRIPTION) String x509HostnameVerifier, + @Param(value = TRUST_ALL_ROOTS, description = TRUST_ALL_ROOTS_DESC) String trustAllRoots, + @Param(value = X509_HOSTNAME_VERIFIER, description = X509_HOSTNAME_VERIFIER_DESC) String x509HostnameVerifier, @Param(value = SESSION_COOKIES, description = SESSION_COOKIES_DESC) SerializableSessionObject sessionCookies, @Param(value = SESSION_CONNECTION_POOL, description = SESSION_CONNECTION_POOL_DESC) GlobalSessionObject sessionConnectionPool) { try { diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java index 17cbddf47a..215e8070f0 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java @@ -36,8 +36,25 @@ public static final class CommonConstants { public static final String HOST = "hostname"; public static final String USERNAME = "username"; public static final String PASSWORD = "password"; + public static final String NAMESPACE = "namespace"; + public static final String DEPLOYMENT = "deployment"; + public static final String PROXY_HOST = "proxyHost"; + public static final String PROXY_PORT = "proxyPort"; + public static final String PROXY_USERNAME = "proxyUsername"; + public static final String PROXY_PASSWORD = "proxyPassword"; + public static final String TLS_VERSION = "tlsVersion"; + public static final String ALLOWED_CIPHERS = "allowedCiphers"; public static final String TRUST_ALL_ROOTS = "trustAllRoots"; public static final String X509_HOSTNAME_VERIFIER = "x509HostnameVerifier"; + public static final String TRUST_KEYSTORE = "trustKeystore"; + public static final String TRUST_PASSWORD = "trustPassword"; + public static final String KEYSTORE = "keystore"; + public static final String KEYSTORE_PASSWORD = "keystorePassword"; + public static final String CONNECT_TIMEOUT = "connectTimeout"; + public static final String EXECUTION_TIMEOUT = "executionTimeout"; + public static final String KEEP_ALIVE = "keepAlive"; + public static final String CONNECTIONS_MAX_PER_ROUTE = "connectionsMaxPerRoute"; + public static final String CONNECTIONS_MAX_TOTAL = "connectionsMaxTotal"; public static final String SESSION_COOKIES = "httpClientCookieSession"; public static final String SESSION_CONNECTION_POOL = "httpClientPoolingConnectionManager"; @@ -45,6 +62,14 @@ public static final class CommonConstants { public static final String BASIC = "BASIC"; public static final String APPLICATION_JSON = "application/json"; public static final String CONTENT_TYPE = "Content-Type:"; + public static final String TRUE = "true"; + public static final String EMPTY_STRING = ""; + public static final String AUTHORIZATION_BEARER = "Authorization: Bearer "; + public static final String ANONYMOUS = "Anonymous"; + + //apis + public static final String APPS_V1_NAMESPACES = "/apis/apps/v1/namespaces/"; + public static final String DEPLOYMENTS = "/deployments/"; } diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java index 00957657cf..74a5381eba 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java @@ -16,32 +16,94 @@ package io.cloudslang.content.redhat.utils; public class Descriptions { + public static class Common{ + public static final String RETURN_CODE_DESC = "0 if success, -1 if failure."; - } - public static class GetTokenAction { - public static final String GET_TOKEN_NAME = "Get Token"; - public static final String RETURN_RESULT_DESC = "The authorization token for Openshift."; - public static final String AUTH_TOKEN_DESC = "Generated authentication token."; - public static final String EXCEPTION_DESC = "An error message in case there was an error while generating the " + - "token."; - public static final String TRUST_ALL_ROOTS_DESCRIPTION = "Specifies whether to enable weak security over SSL/TSL. " + + public static final String HOST_DESC = "HOST."; + public static final String AUTH_TOKEN_DESC = "Token used to authenticate to the Openshift environment."; + public static final String PROXY_HOST_DESC = "The proxy server used to access the web site."; + public static final String PROXY_PORT_DESC = "The proxy server port." + + "Default value: 8080."; + public static final String PROXY_USERNAME_DESC = "The username used when connecting to the proxy."; + public static final String PROXY_PASSWORD_DESC = "The proxy server password associated with the 'proxyUsername'" + + " input value."; + public static final String TLS_VERSION_DESC= "The version of TLS to use. The value of this input will be ignored if 'protocol'" + + "is set to 'HTTP'. This capability is provided “as is”, please see product documentation for further information." + + "Valid values: TLSv1, TLSv1.1, TLSv1.2. \n" + + "Default value: TLSv1.2. \n"; + public static final String ALLOWED_CIPHERS_DESC= "A list of ciphers to use. The value of this input will be ignored " + + "if 'tlsVersion' does " + + "not contain 'TLSv1.2'. This capability is provided “as is”, please see product documentation for further security considerations." + + "In order to connect successfully to the target host, it should accept at least one of the following ciphers. If this is not the case, it is " + + "the user's responsibility to configure the host accordingly or to update the list of allowed ciphers. \n" + + "Default value: TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, " + + "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256, TLS_DHE_RSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, " + + "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, " + + "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLS_RSA_WITH_AES_256_GCM_SHA384, TLS_RSA_WITH_AES_256_CBC_SHA256, " + + "TLS_RSA_WITH_AES_128_CBC_SHA256."; + public static final String TRUST_ALL_ROOTS_DESC= "Specifies whether to enable weak security over SSL/TSL. " + "A certificate is trusted even if no trusted certification authority issued it."; - public static final String X509_HOSTNAME_VERIFIER_DESCRIPTION = "Specifies the way the server hostname must match a domain name in " + + public static final String X509_HOSTNAME_VERIFIER_DESC = "Specifies the way the server hostname must match a domain name in " + "the subject's Common Name (CN) or subjectAltName field of the X.509 certificate. Set this to " + "\"allow_all\" to skip any checking. For the value \"browser_compatible\" the hostname verifier " + "works the same way as Curl and Firefox. The hostname must match either the first CN, or any of " + "the subject-alts. A wildcard can occur in the CN, and in any of the subject-alts. The only " + "difference between \"browser_compatible\" and \"strict\" is that a wildcard (such as \"*.foo.com\") " + "with \"browser_compatible\" matches all subdomains, including \"a.b.foo.com\"."; + public static final String TRUST_KEYSTORE_DESC = "The pathname of the Java TrustStore file. This contains " + + "certificates from other parties that you expect to communicate with, or from Certificate Authorities" + + " that you trust to identify other parties. If the protocol (specified by the 'url') is not 'https' " + + "or if trustAllRoots is 'true' this input is ignored. \n " + + "Format: Java KeyStore (JKS)"; + public static final String TRUST_PASSWORD_DESC = "The password associated with the TrustStore file. If " + + "trustAllRoots is false and trustKeystore is empty, trustPassword default will be supplied."; + public static final String KEYSTORE_DESC = "The pathname of the Java KeyStore file. You only need this if " + + "the server requires client authentication. If the protocol (specified by the 'url') is not 'https' " + + "or if trustAllRoots is 'true' this input is ignored. Format: Java KeyStore (JKS)"; + public static final String KEYSTORE_PASSWORD_DESC = "The password associated with the KeyStore file. If " + + "trustAllRoots is false and keystore is empty, keystorePassword default will be supplied."; + public static final String CONNECT_TIMEOUT_DESC = "The time to wait for a connection to be established, " + + "in seconds. A timeout value of '0' represents an infinite timeout."; + public static final String EXECUTION_TIMEOUT_DESC = "The amount of time (in seconds) to allow the client to complete the execution " + + "of an API call. A value of '0' disables this feature. \n" + + "Default: 60 \n"; + public static final String KEEP_ALIVE_DESC = "Specifies whether to create a shared connection that will be " + + "used in subsequent calls. If keepAlive is false, the already open connection will be used and after" + + "execution it will close it."; + public static final String CONNECTIONS_MAX_PER_ROUTE_DESC = "The maximum limit of connections on a per route basis."; + public static final String CONNECTIONS_MAX_TOTAL_DESC = "The maximum limit of connections in total."; + + } + + public static class GetTokenAction { + + public static final String GET_TOKEN_NAME = "Get Token"; + public static final String RETURN_RESULT_DESC = "The authorization token for Openshift."; + public static final String AUTH_TOKEN_DESC = "Generated authentication token."; + public static final String EXCEPTION_DESC = "An error message in case there was an error while generating the " + + "token."; public static final String USERNAME_DESC = "The name of the user who is logging in to Openshift."; public static final String PASSWORD_DESC = "The password used by the user to log in to Openshift."; - public static final String HOST_DESC = "HOST."; public static final String SUCCESS_DESC = "Token generated successfully."; public static final String FAILURE_DESC = "There was an error while trying to retrieve token."; + } + + public static class DeleteDeployment { + + public static final String DELETE_DEPLOYMENT_NAME = "Delete Deployment"; + public static final String DELETE_DEPLOYMENT_DESC = "Deletes the deployment from namespace."; + + public static final String NAMESPACE_DESC = "Namespace to delete the deployment from."; + public static final String DEPLOYMENT_DESC = "Name of the deployment to delete."; + public static final String RETURN_RESULT_DESC = "The deployment was successfully deleted."; + public static final String EXCEPTION_DESC = "An error message in case there was an error while deleting the deployment."; + + public static final String SUCCESS_DESC = "Deployment was successfully deleted."; + public static final String FAILURE_DESC = "There was an error while trying to delete the deployment."; } } From f3e5326c4a98aa7f64209e98db3d9e8d646a31ca Mon Sep 17 00:00:00 2001 From: AlexandruPresecan <79699589+AlexandruPresecan@users.noreply.github.com> Date: Fri, 9 Dec 2022 12:53:55 +0200 Subject: [PATCH 05/16] added status code description and output for delete deployment (#1113) --- .../cloudslang/content/redhat/actions/DeleteDeployment.java | 5 +++-- .../io/cloudslang/content/redhat/utils/Descriptions.java | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/DeleteDeployment.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/DeleteDeployment.java index 2c0294c253..9f640f5f79 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/DeleteDeployment.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/DeleteDeployment.java @@ -22,7 +22,6 @@ import com.hp.oo.sdk.content.annotations.Response; import com.hp.oo.sdk.content.plugin.GlobalSessionObject; import com.hp.oo.sdk.content.plugin.SerializableSessionObject; -import com.sun.xml.internal.ws.api.model.wsdl.WSDLBoundOperation; import io.cloudslang.content.constants.ReturnCodes; import io.cloudslang.content.httpclient.actions.HttpClientDeleteAction; import io.cloudslang.content.utils.OutputUtilities; @@ -42,6 +41,7 @@ import static io.cloudslang.content.redhat.utils.Descriptions.Common.*; import static io.cloudslang.content.redhat.utils.Descriptions.DeleteDeployment.*; import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.AUTH_TOKEN; +import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.STATUS_CODE; public class DeleteDeployment { @Action(name = DELETE_DEPLOYMENT_NAME, @@ -49,7 +49,8 @@ public class DeleteDeployment { outputs = { @Output(value = RETURN_RESULT, description = RETURN_RESULT_DESC), @Output(value = RETURN_CODE, description = RETURN_CODE_DESC), - @Output(value = EXCEPTION, description = EXCEPTION_DESC) + @Output(value = EXCEPTION, description = EXCEPTION_DESC), + @Output(value = STATUS_CODE, description = STATUS_CODE_DESC) }, responses = { @Response(text = SUCCESS, field = RETURN_CODE, value = ReturnCodes.SUCCESS, matchType = COMPARE_EQUAL, responseType = RESOLVED, description = SUCCESS_DESC), diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java index 74a5381eba..66bd78da8a 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java @@ -20,6 +20,7 @@ public class Descriptions { public static class Common{ public static final String RETURN_CODE_DESC = "0 if success, -1 if failure."; + public static final String STATUS_CODE_DESC = "The HTTP status code for Openshift API request."; public static final String HOST_DESC = "HOST."; public static final String AUTH_TOKEN_DESC = "Token used to authenticate to the Openshift environment."; From 487f0b517b83deb3680525cc6f5cee2b20025f82 Mon Sep 17 00:00:00 2001 From: TodereanLenuta <77394653+TodereanLenuta@users.noreply.github.com> Date: Fri, 9 Dec 2022 20:29:56 +0200 Subject: [PATCH 06/16] Get Deployment Status - action. (#1111) * Get Deployment Status - action. * Get Deployment Status - action (implement feedback from PO). --- cs-openshift/pom.xml | 12 ++ .../actions/GetDeploymentStatusAction.java | 145 ++++++++++++++++++ .../redhat/services/OpenshiftService.java | 80 +++++++++- .../content/redhat/utils/Constants.java | 3 +- .../content/redhat/utils/Descriptions.java | 52 ++++++- .../content/redhat/utils/Outputs.java | 12 ++ 6 files changed, 296 insertions(+), 8 deletions(-) create mode 100644 cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetDeploymentStatusAction.java diff --git a/cs-openshift/pom.xml b/cs-openshift/pom.xml index 5d1466c742..303347966e 100644 --- a/cs-openshift/pom.xml +++ b/cs-openshift/pom.xml @@ -102,6 +102,18 @@ 1.15.3 + + com.google.code.gson + gson + 2.10 + + + com.jayway.jsonpath + json-path + 2.7.0 + compile + + diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetDeploymentStatusAction.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetDeploymentStatusAction.java new file mode 100644 index 0000000000..875a27046e --- /dev/null +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetDeploymentStatusAction.java @@ -0,0 +1,145 @@ +package io.cloudslang.content.redhat.actions; + +import com.hp.oo.sdk.content.annotations.Action; +import com.hp.oo.sdk.content.annotations.Output; +import com.hp.oo.sdk.content.annotations.Param; +import com.hp.oo.sdk.content.annotations.Response; +import com.hp.oo.sdk.content.plugin.ActionMetadata.MatchType; +import com.hp.oo.sdk.content.plugin.ActionMetadata.ResponseType; +import com.hp.oo.sdk.content.plugin.GlobalSessionObject; +import com.hp.oo.sdk.content.plugin.SerializableSessionObject; +import io.cloudslang.content.constants.OutputNames; +import io.cloudslang.content.constants.ResponseNames; +import io.cloudslang.content.constants.ReturnCodes; +import io.cloudslang.content.httpclient.actions.HttpClientGetAction; +import io.cloudslang.content.utils.OutputUtilities; + +import static io.cloudslang.content.redhat.services.OpenshiftService.processHttpGetDeploymentStatusResult; +import static io.cloudslang.content.redhat.utils.Constants.CommonConstants.*; + +import java.util.Map; + +import static io.cloudslang.content.redhat.utils.Descriptions.GetDeploymentStatus.FAILURE_DESC; +import static io.cloudslang.content.redhat.utils.Descriptions.GetDeploymentStatus.SUCCESS_DESC; +import static io.cloudslang.content.redhat.utils.Descriptions.GetDeploymentStatus.RETURN_RESULT_DESC; +import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.*; +import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.EXCEPTION; +import static org.apache.commons.lang3.StringUtils.EMPTY; + +import static io.cloudslang.content.constants.OutputNames.*; +import static io.cloudslang.content.redhat.utils.Descriptions.Common.*; +import static io.cloudslang.content.redhat.utils.Descriptions.GetDeploymentStatus.*; + +import static io.cloudslang.content.httpclient.utils.Descriptions.HTTPClient.SESSION_CONNECTION_POOL_DESC; +import static io.cloudslang.content.httpclient.utils.Descriptions.HTTPClient.SESSION_COOKIES_DESC; +import static io.cloudslang.content.httpclient.utils.Inputs.HTTPInputs.SESSION_CONNECTION_POOL; +import static io.cloudslang.content.httpclient.utils.Inputs.HTTPInputs.SESSION_COOKIES; + +public class GetDeploymentStatusAction { + + @Action(name = GET_DEPLOYMENT_STATUS, + description = GET_DEPLOYMENT_STATUS_DESC, + outputs = { + //Common outputs + @Output(value = RETURN_RESULT, description = RETURN_RESULT_DESC), + @Output(value = STATUS_CODE, description = STATUS_CODE_DESC), + @Output(value = RETURN_CODE, description = RETURN_CODE_DESC), + @Output(value = EXCEPTION, description = EXCEPTION_DESC), + //Specific outputs - general + @Output(value = DOCUMENT_OUTPUT, description = DOCUMENT_OUTPUT_DESC), + + @Output(value = KIND_OUTPUT, description = KIND_OUTPUT_DESC), + @Output(value = NAME_OUTPUT, description = NAME_OUTPUT_DESC), + @Output(value = NAMESPACE_OUTPUT, description = NAMESPACE_OUTPUT_DESC), + @Output(value = UID_OUTPUT, description = UID_OUTPUT_DESC), + //Specific outputs - general + @Output(value = OBSERVED_GENERATION_OUTPUT, description = OBSERVED_GENERATION_OUTPUT_DESC), + @Output(value = REPLICAS_OUTPUT, description = REPLICAS_OUTPUT_DESC), + @Output(value = UPDATED_REPLICAS_OUTPUT, description = UPDATED_REPLICAS_OUTPUT_DESC), + @Output(value = UNAVAILABLE_REPLICAS_OUTPUT, description = UNAVAILABLE_REPLICAS_OUTPUT_DESC), + @Output(value = CONDITIONS_OUTPUT, description = CONDITIONS_OUTPUT_DESC) + }, + responses = { + @Response(text = ResponseNames.SUCCESS, field = OutputNames.RETURN_CODE, value = ReturnCodes.SUCCESS, + matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.RESOLVED, description = SUCCESS_DESC), + @Response(text = ResponseNames.FAILURE, field = OutputNames.RETURN_CODE, value = ReturnCodes.FAILURE, + matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.ERROR, description = FAILURE_DESC) + }) + + public Map execute( + //Common Inputs + @Param(value = HOST, description = HOST_DESC, required = true) String host, + @Param(value = AUTH_TOKEN, description = AUTH_TOKEN_DESC, required = true) String authToken, + //Specific inputs + @Param(value = NAME, description = NAME_DESC) String name, + @Param(value = NAMESPACE, description = NAMESPACE_DESC) String namespace, + //Common Inputs + @Param(value = PROXY_HOST, description = PROXY_HOST_DESC) String proxyHost, + @Param(value = PROXY_PORT, description = PROXY_PORT_DESC) String proxyPort, + @Param(value = PROXY_USERNAME, description = PROXY_USERNAME_DESC) String proxyUsername, + @Param(value = PROXY_PASSWORD, encrypted = true, description = PROXY_PASSWORD_DESC) String proxyPassword, + @Param(value = TLS_VERSION, description = TLS_VERSION_DESC) String tlsVersion, + @Param(value = ALLOWED_CIPHERS, description = ALLOWED_CIPHERS_DESC) String allowedCiphers, + @Param(value = TRUST_ALL_ROOTS, description = TRUST_ALL_ROOTS_DESC) String trustAllRoots, + @Param(value = X509_HOSTNAME_VERIFIER, description = X509_HOSTNAME_VERIFIER_DESC) String x509HostnameVerifier, + @Param(value = TRUST_KEYSTORE, description = TRUST_KEYSTORE_DESC) String trustKeystore, + @Param(value = TRUST_PASSWORD, encrypted = true, description = TRUST_PASSWORD_DESC) String trustPassword, + @Param(value = KEYSTORE, description = KEYSTORE_DESC) String keystore, + @Param(value = KEYSTORE_PASSWORD, encrypted = true, description = KEYSTORE_PASSWORD_DESC) String keystorePassword, + @Param(value = CONNECT_TIMEOUT, description = CONNECT_TIMEOUT_DESC) String connectTimeout, + @Param(value = EXECUTION_TIMEOUT, description = EXECUTION_TIMEOUT_DESC) String executionTimeout, + @Param(value = KEEP_ALIVE, description = KEEP_ALIVE_DESC) String keepAlive, + @Param(value = CONNECTIONS_MAX_PER_ROUTE, description = CONNECTIONS_MAX_PER_ROUTE_DESC) String connectionsMaxPerRoute, + @Param(value = CONNECTIONS_MAX_TOTAL, description = CONNECTIONS_MAX_TOTAL_DESC) String connectionsMaxTotal, + @Param(value = SESSION_COOKIES, description = SESSION_COOKIES_DESC) SerializableSessionObject sessionCookies, + @Param(value = SESSION_CONNECTION_POOL, description = SESSION_CONNECTION_POOL_DESC) GlobalSessionObject sessionConnectionPool) { + + try { + + Map result = new HttpClientGetAction().execute( + host + GET_DEPLOYMENT_STATUS_ENDPOINT_1 + namespace + + GET_DEPLOYMENT_STATUS_ENDPOINT_2 + name + + GET_DEPLOYMENT_STATUS_ENDPOINT_3, + ANONYMOUS, + EMPTY, + EMPTY, + EMPTY, + proxyHost, + proxyPort, + proxyUsername, + proxyPassword, + tlsVersion, + allowedCiphers, + trustAllRoots, + x509HostnameVerifier, + trustKeystore, + trustPassword, + keystore, + keystorePassword, + keepAlive, + connectionsMaxPerRoute, + connectionsMaxTotal, + EMPTY, + EMPTY, + AUTHORIZATION_BEARER + authToken, + EMPTY, + EMPTY, + EMPTY, + EMPTY, + EMPTY, + connectTimeout, + EMPTY, + executionTimeout, + sessionCookies, + sessionConnectionPool + ); + + processHttpGetDeploymentStatusResult(result); + return result; + + } catch (Exception exception) { + return OutputUtilities.getFailureResultsMap(exception); + } + } + +} diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java index 46fa7917a2..6e3965aee8 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java @@ -1,13 +1,19 @@ package io.cloudslang.content.redhat.services; +import com.google.gson.JsonPrimitive; +import com.jayway.jsonpath.JsonPath; import org.apache.commons.lang3.StringUtils; +import java.util.List; import java.util.Map; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + import static io.cloudslang.content.constants.OutputNames.RETURN_CODE; import static io.cloudslang.content.constants.OutputNames.RETURN_RESULT; -import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.EXCEPTION; -import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.STATUS_CODE; +import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.*; +import static io.cloudslang.content.redhat.utils.Descriptions.GetDeploymentStatus.RETURN_RESULT_MESSAGE_DESC; public class OpenshiftService { @@ -16,7 +22,75 @@ public static void processHttpResult(Map httpResults) { String statusCode = httpResults.get(STATUS_CODE); if (StringUtils.isEmpty(statusCode) || Integer.parseInt(statusCode) < 200 || Integer.parseInt(statusCode) >= 300) { - if(StringUtils.isEmpty(httpResults.get(EXCEPTION))) + if (StringUtils.isEmpty(httpResults.get(EXCEPTION))) + httpResults.put(EXCEPTION, httpResults.get(RETURN_RESULT)); + httpResults.put(RETURN_CODE, "-1"); + } + } + + public static void processHttpGetDeploymentStatusResult(Map httpResults) { + + //Process the return result output + String returnResult = httpResults.get(RETURN_RESULT); + + if (!(returnResult.isEmpty())) { + httpResults.put(DOCUMENT_OUTPUT, returnResult); + + JsonObject jsonResponse = ((new JsonParser()).parse(httpResults.get(RETURN_RESULT))).getAsJsonObject(); + + //Kind output + JsonPrimitive tmpResponse = (JsonPrimitive) jsonResponse.get(KIND_OUTPUT); + httpResults.put(KIND_OUTPUT, tmpResponse.toString()); + + //Name outputs + String namePath = "$.metadata.name"; + String namePathResponse = JsonPath.read(jsonResponse.toString(), namePath); + httpResults.put(NAME_OUTPUT, namePathResponse); + + //Namespace outputs + String namespacePath = "$.metadata.namespace"; + String namespacePathResponse = JsonPath.read(jsonResponse.toString(), namespacePath); + httpResults.put(NAMESPACE_OUTPUT, namespacePathResponse); + + //Uid outputs + String uidPath = "$.metadata.uid"; + String uidPathResponse = JsonPath.read(jsonResponse.toString(), uidPath); + httpResults.put(UID_OUTPUT, uidPathResponse); + + //ObservedGeneration output + String observedGenerationPath = "$.status.observedGeneration"; + Integer observedGenerationPathResponse = JsonPath.read(jsonResponse.toString(), observedGenerationPath); + httpResults.put(OBSERVED_GENERATION_OUTPUT, observedGenerationPathResponse.toString()); + + //Replicas output + String replicasPath = "$.status.replicas"; + Integer replicasPathResponse = JsonPath.read(jsonResponse.toString(), replicasPath); + httpResults.put(REPLICAS_OUTPUT, replicasPathResponse.toString()); + + //UpdatedReplicas output + String updatedReplicasPath = "$.status.updatedReplicas"; + Integer updatedReplicasPathResponse = JsonPath.read(jsonResponse.toString(), updatedReplicasPath); + httpResults.put(UPDATED_REPLICAS_OUTPUT, updatedReplicasPathResponse.toString()); + + //UnavailableReplicas output + String unavailableReplicasPath = "$.status.unavailableReplicas"; + Integer unavailableReplicasPathResponse = JsonPath.read(jsonResponse.toString(), unavailableReplicasPath); + httpResults.put(UNAVAILABLE_REPLICAS_OUTPUT, unavailableReplicasPathResponse.toString()); + + //Conditions output + String conditionsPath = "$.status.conditions"; + List conditionsPathResponse = JsonPath.read(jsonResponse.toString(), conditionsPath); + httpResults.put(CONDITIONS_OUTPUT, conditionsPathResponse.toString()); + + httpResults.put(RETURN_RESULT, RETURN_RESULT_MESSAGE_DESC); + } + + + //Process HTTP status code + String statusCode = httpResults.get(STATUS_CODE); + + if (StringUtils.isEmpty(statusCode) || Integer.parseInt(statusCode) < 200 || Integer.parseInt(statusCode) >= 300) { + if (StringUtils.isEmpty(httpResults.get(EXCEPTION))) httpResults.put(EXCEPTION, httpResults.get(RETURN_RESULT)); httpResults.put(RETURN_CODE, "-1"); } diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java index 215e8070f0..37e46d6262 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java @@ -33,9 +33,10 @@ public final class Constants { public static final class CommonConstants { //Inputs - public static final String HOST = "hostname"; + public static final String HOST = "host"; public static final String USERNAME = "username"; public static final String PASSWORD = "password"; + public static final String NAME = "name"; public static final String NAMESPACE = "namespace"; public static final String DEPLOYMENT = "deployment"; public static final String PROXY_HOST = "proxyHost"; diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java index 66bd78da8a..a099cef394 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java @@ -17,7 +17,7 @@ public class Descriptions { - public static class Common{ + public static class Common { public static final String RETURN_CODE_DESC = "0 if success, -1 if failure."; public static final String STATUS_CODE_DESC = "The HTTP status code for Openshift API request."; @@ -30,11 +30,11 @@ public static class Common{ public static final String PROXY_USERNAME_DESC = "The username used when connecting to the proxy."; public static final String PROXY_PASSWORD_DESC = "The proxy server password associated with the 'proxyUsername'" + " input value."; - public static final String TLS_VERSION_DESC= "The version of TLS to use. The value of this input will be ignored if 'protocol'" + + public static final String TLS_VERSION_DESC = "The version of TLS to use. The value of this input will be ignored if 'protocol'" + "is set to 'HTTP'. This capability is provided “as is”, please see product documentation for further information." + "Valid values: TLSv1, TLSv1.1, TLSv1.2. \n" + "Default value: TLSv1.2. \n"; - public static final String ALLOWED_CIPHERS_DESC= "A list of ciphers to use. The value of this input will be ignored " + + public static final String ALLOWED_CIPHERS_DESC = "A list of ciphers to use. The value of this input will be ignored " + "if 'tlsVersion' does " + "not contain 'TLSv1.2'. This capability is provided “as is”, please see product documentation for further security considerations." + "In order to connect successfully to the target host, it should accept at least one of the following ciphers. If this is not the case, it is " + @@ -44,7 +44,7 @@ public static class Common{ "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, " + "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLS_RSA_WITH_AES_256_GCM_SHA384, TLS_RSA_WITH_AES_256_CBC_SHA256, " + "TLS_RSA_WITH_AES_128_CBC_SHA256."; - public static final String TRUST_ALL_ROOTS_DESC= "Specifies whether to enable weak security over SSL/TSL. " + + public static final String TRUST_ALL_ROOTS_DESC = "Specifies whether to enable weak security over SSL/TSL. " + "A certificate is trusted even if no trusted certification authority issued it."; public static final String X509_HOSTNAME_VERIFIER_DESC = "Specifies the way the server hostname must match a domain name in " + "the subject's Common Name (CN) or subjectAltName field of the X.509 certificate. Set this to " + @@ -107,4 +107,48 @@ public static class DeleteDeployment { public static final String FAILURE_DESC = "There was an error while trying to delete the deployment."; } + + public static class GetDeploymentStatus { + + public static final String GET_DEPLOYMENT_STATUS = "Get Deployment Status"; + public static final String GET_DEPLOYMENT_STATUS_DESC = "Read the status of the specified deployment."; + + public static final String NAME_DESC = "The name of the deployment."; + public static final String NAMESPACE_DESC = "The object name and auth scope, such as for teams and projects."; + + //Outputs + public static final String RETURN_RESULT_DESC = "A suggestive message both for the case of success and for the " + + "case of failure."; + public static final String RETURN_RESULT_MESSAGE_DESC = "The request was made successfully, please analyze the " + + "entire response generated by the API call in the document output as a json format."; + public static final String STATUS_CODE_DESC = "The status code of the request."; + public static final String EXCEPTION_DESC = "An error message in case there was an error while reading the " + + "deployment status."; + + public static final String SUCCESS_DESC = "The request to read the status of the specified deployment was made " + + "successfully."; + public static final String FAILURE_DESC = "There was an error while trying to get the status of the deployment."; + + //Specific outputs + + //Get Deployment Status + public static final String NAME_OUTPUT_DESC = "The deployment name."; + public static final String NAMESPACE_OUTPUT_DESC = "The deployment namespace."; + public static final String UID_OUTPUT_DESC = "The deployment uid."; + public static final String KIND_OUTPUT_DESC = "The deployment kind."; + public static final String OBSERVED_GENERATION_OUTPUT_DESC = "The observedGeneration status property of the deployment."; + public static final String REPLICAS_OUTPUT_DESC = "The replicas status property of the deployment."; + public static final String UPDATED_REPLICAS_OUTPUT_DESC = "The updatedReplicas status property of the deployment."; + public static final String UNAVAILABLE_REPLICAS_OUTPUT_DESC = "The unavailableReplicas status property of the deployment."; + public static final String CONDITIONS_OUTPUT_DESC = "The conditions status properties of the deployment in the json format."; + + public static final String DOCUMENT_OUTPUT_DESC = "All the information related to a specific deployment in the " + + "json format."; + + //API + public static final String GET_DEPLOYMENT_STATUS_ENDPOINT_1 = "/apis/apps/v1/namespaces/"; + public static final String GET_DEPLOYMENT_STATUS_ENDPOINT_2 = "/deployments/"; + public static final String GET_DEPLOYMENT_STATUS_ENDPOINT_3 = "/status"; + + } } diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Outputs.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Outputs.java index df2f581597..6936b6468a 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Outputs.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Outputs.java @@ -25,6 +25,18 @@ public static class OutputNames { public static final String EXCEPTION = "exception"; public static final String AUTH_TOKEN = "authToken"; + //Get Deployment Status + public static final String NAME_OUTPUT = "name"; + public static final String DOCUMENT_OUTPUT = "document"; + public static final String NAMESPACE_OUTPUT = "namespace"; + public static final String UID_OUTPUT = "uid"; + public static final String KIND_OUTPUT = "kind"; + public static final String OBSERVED_GENERATION_OUTPUT = "observedGeneration"; + public static final String REPLICAS_OUTPUT = "replicas"; + public static final String UPDATED_REPLICAS_OUTPUT = "updatedReplicas"; + public static final String UNAVAILABLE_REPLICAS_OUTPUT = "unavailableReplicas"; + public static final String CONDITIONS_OUTPUT = "conditions"; + } } From 19f6e80cb2b182f2ad26e3e5f30c50fd7ac76873 Mon Sep 17 00:00:00 2001 From: TodereanLenuta Date: Fri, 9 Dec 2022 20:32:51 +0200 Subject: [PATCH 07/16] Added the STATUS CODE DESC import (Get Deployment Status) --- .../content/redhat/actions/GetDeploymentStatusAction.java | 1 + 1 file changed, 1 insertion(+) diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetDeploymentStatusAction.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetDeploymentStatusAction.java index 875a27046e..aca8b5801e 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetDeploymentStatusAction.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetDeploymentStatusAction.java @@ -19,6 +19,7 @@ import java.util.Map; +import static io.cloudslang.content.redhat.utils.Descriptions.Common.STATUS_CODE_DESC; import static io.cloudslang.content.redhat.utils.Descriptions.GetDeploymentStatus.FAILURE_DESC; import static io.cloudslang.content.redhat.utils.Descriptions.GetDeploymentStatus.SUCCESS_DESC; import static io.cloudslang.content.redhat.utils.Descriptions.GetDeploymentStatus.RETURN_RESULT_DESC; From e752668f0aedabcadc6b66153abce36def087095 Mon Sep 17 00:00:00 2001 From: ioana-matei <41365169+ioana-matei@users.noreply.github.com> Date: Mon, 12 Dec 2022 14:05:46 +0200 Subject: [PATCH 08/16] Cs openshift token (#1114) * first structure of openshift for token * openshift get token --- cs-openshift/pom.xml | 32 +- .../redhat/actions/GetTokenAction.java | 111 ++++-- .../content/redhat/entities/HttpInput.java | 347 ++++++++++++++++++ .../redhat/services/OpenshiftService.java | 77 ++++ .../content/redhat/utils/Constants.java | 14 + .../content/redhat/utils/Descriptions.java | 1 + 6 files changed, 532 insertions(+), 50 deletions(-) create mode 100644 cs-openshift/src/main/java/io/cloudslang/content/redhat/entities/HttpInput.java diff --git a/cs-openshift/pom.xml b/cs-openshift/pom.xml index 303347966e..415106e8be 100644 --- a/cs-openshift/pom.xml +++ b/cs-openshift/pom.xml @@ -1,20 +1,20 @@ - + 4.0.0 diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetTokenAction.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetTokenAction.java index 947f9567b0..ea2615dae6 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetTokenAction.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetTokenAction.java @@ -24,6 +24,7 @@ import com.hp.oo.sdk.content.plugin.SerializableSessionObject; import io.cloudslang.content.constants.ReturnCodes; import io.cloudslang.content.httpclient.actions.HttpClientGetAction; +import io.cloudslang.content.redhat.entities.HttpInput; import io.cloudslang.content.utils.OutputUtilities; import org.apache.commons.codec.binary.Base64; @@ -38,6 +39,8 @@ import static io.cloudslang.content.constants.ResponseNames.SUCCESS; import static io.cloudslang.content.httpclient.utils.Descriptions.HTTPClient.SESSION_CONNECTION_POOL_DESC; import static io.cloudslang.content.httpclient.utils.Descriptions.HTTPClient.SESSION_COOKIES_DESC; +import static io.cloudslang.content.redhat.entities.HttpInput.builder; +import static io.cloudslang.content.redhat.services.OpenshiftService.processAuthTokenResult; import static io.cloudslang.content.redhat.services.OpenshiftService.processHttpResult; import static io.cloudslang.content.redhat.utils.Constants.CommonConstants.*; import static io.cloudslang.content.redhat.utils.Descriptions.Common.*; @@ -47,7 +50,7 @@ public class GetTokenAction { @Action(name = GET_TOKEN_NAME, - description = "DESC", + description = GET_TOKEN_NAME_DESC, outputs = { @Output(value = RETURN_RESULT, description = RETURN_RESULT_DESC), @Output(value = RETURN_CODE, description = RETURN_CODE_DESC), @@ -61,51 +64,91 @@ public class GetTokenAction { public Map execute(@Param(value = HOST, description = HOST_DESC) String host, @Param(value = USERNAME, description = USERNAME_DESC) String username, @Param(value = PASSWORD, encrypted = true, description = PASSWORD_DESC) String password, + + @Param(value = PROXY_HOST, description = PROXY_HOST_DESC) String proxyHost, + @Param(value = PROXY_PORT, description = PROXY_PORT_DESC) String proxyPort, + @Param(value = PROXY_USERNAME, description = PROXY_USERNAME_DESC) String proxyUsername, + @Param(value = PROXY_PASSWORD, description = PROXY_PASSWORD_DESC) String proxyPassword, + @Param(value = TLS_VERSION, description = TLS_VERSION_DESC) String tlsVersion, + @Param(value = ALLOWED_CIPHERS, description = ALLOWED_CIPHERS_DESC) String allowedCyphers, @Param(value = TRUST_ALL_ROOTS, description = TRUST_ALL_ROOTS_DESC) String trustAllRoots, @Param(value = X509_HOSTNAME_VERIFIER, description = X509_HOSTNAME_VERIFIER_DESC) String x509HostnameVerifier, + @Param(value = TRUST_KEYSTORE, description = TRUST_KEYSTORE_DESC) String trustKeystore, + @Param(value = TRUST_PASSWORD, description = TRUST_PASSWORD_DESC) String trustPassword, + @Param(value = KEYSTORE, description = KEYSTORE_DESC) String keystore, + @Param(value = KEYSTORE_PASSWORD, description = KEYSTORE_PASSWORD_DESC) String keystorePassword, + @Param(value = CONNECT_TIMEOUT, description = CONNECT_TIMEOUT_DESC) String connectTimeout, + @Param(value = EXECUTION_TIMEOUT, description = EXECUTION_TIMEOUT_DESC) String executionTimeout, + @Param(value = KEEP_ALIVE, description = KEEP_ALIVE_DESC) String keepAlive, + @Param(value = CONNECTIONS_MAX_PER_ROUTE, description = CONNECTIONS_MAX_PER_ROUTE_DESC) String connectionsMaxPerRoute, + @Param(value = CONNECTIONS_MAX_TOTAL, description = CONNECTIONS_MAX_TOTAL_DESC) String connectionsMaxTotal, @Param(value = SESSION_COOKIES, description = SESSION_COOKIES_DESC) SerializableSessionObject sessionCookies, @Param(value = SESSION_CONNECTION_POOL, description = SESSION_CONNECTION_POOL_DESC) GlobalSessionObject sessionConnectionPool) { try { - String auth = username + ":" + password; + String auth = username + COLON_PUNCTUATION + password; byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(StandardCharsets.ISO_8859_1)); + HttpInput input = HttpInput.builder() + .host(host) + .proxyHost(proxyHost) + .proxyPassword(proxyPassword) + .proxyPort(proxyPort) + .proxyPassword(proxyPassword) + .connectionsMaxPerRoute(connectionsMaxPerRoute) + .keystorePassword(keystorePassword) + .proxyUsername(proxyUsername) + .allowedCyphers(allowedCyphers) + .trustKeystore(trustKeystore) + .connectionsMaxTotal(connectionsMaxTotal) + .keepAlive(keepAlive) + .keystore(keystore) + .tlsVersion(tlsVersion) + .trustAllRoots(trustAllRoots) + .trustPassword(trustPassword) + .x509HostnameVerifier(x509HostnameVerifier) + .username(username) + .password(password) + .build(); + Map result = new HttpClientGetAction().execute( - "https://oauth-openshift.apps.oscbtp448t.swinfra.net/oauth/authorize", - "Anonymous", - "", - "", - "true", - "", - "", - "", - "", - "", - "", - trustAllRoots, - x509HostnameVerifier, - "", - "", - "", - "", - "", - "", - "", - "true", - "true", - "Authorization: Basic " + new String(encodedAuth), - "", - "", - "response_type=code&client_id=openshift-browser-client", - "true", - "false", - "", - "", - "", + input.getHost() +AUTHORIZE_TOKEN_URL, + ANONYMOUS, + EMPTY_STRING, + EMPTY_STRING, + TRUE, + input.getProxyHost(), + input.getProxyPort(), + input.getProxyUsername(), + input.getProxyPassword(), + input.getTlsVersion(), + input.getAllowedCyphers(), + input.getTrustAllRoots(), + input.getX509HostnameVerifier(), + input.getTrustKeystore(), + input.getTrustPassword(), + input.getKeystore(), + input.getKeystorePassword(), + input.getKeepAlive(), + input.getConnectionsMaxPerRoute(), + input.getConnectionsMaxTotal(), + TRUE, + TRUE, + AUTHORIZATION_BASIC + new String(encodedAuth), + EMPTY_STRING, + EMPTY_STRING, + QUERY_PARAM, + TRUE, + FALSE, + input.getConnectTimeout(), + EMPTY_STRING, + input.getExecutionTimeout(), sessionCookies, sessionConnectionPool); - processHttpResult(result); + + processAuthTokenResult(result,input,sessionCookies,sessionConnectionPool); + return result; } catch (Exception exception) { return OutputUtilities.getFailureResultsMap(exception); diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/entities/HttpInput.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/entities/HttpInput.java new file mode 100644 index 0000000000..68b002911d --- /dev/null +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/entities/HttpInput.java @@ -0,0 +1,347 @@ +package io.cloudslang.content.redhat.entities; + +import com.hp.oo.sdk.content.plugin.GlobalSessionObject; + +import static org.apache.commons.lang3.StringUtils.EMPTY; + +public class HttpInput { + private String host; + private String proxyHost; + private String proxyPort; + private String proxyUsername; + private String proxyPassword; + private String tlsVersion; + private String allowedCyphers; + private String trustAllRoots; + private String x509HostnameVerifier; + private String trustKeystore; + private String trustPassword; + private String keystore; + private String keystorePassword; + private String keepAlive; + private String connectionsMaxPerRoute; + private String connectionsMaxTotal; + private String username; + private String password; + private String connectTimeout; + private String executionTimeout; + + public HttpInput(String host, String proxyHost, String proxyPort, String proxyUsername, String proxyPassword, + String tlsVersion, String allowedCyphers, String trustAllRoots, String x509HostnameVerifier, + String trustKeystore, String trustPassword, String keystore, String keystorePassword, + String keepAlive, String connectionsMaxPerRoute, String connectionsMaxTotal,String username, + String password,String connectTimeout,String executionTimeout) { + this.host = host; + this.proxyHost = proxyHost; + this.proxyPort = proxyPort; + this.proxyUsername = proxyUsername; + this.proxyPassword = proxyPassword; + this.tlsVersion = tlsVersion; + this.allowedCyphers = allowedCyphers; + this.trustAllRoots = trustAllRoots; + this.x509HostnameVerifier = x509HostnameVerifier; + this.trustKeystore = trustKeystore; + this.trustPassword = trustPassword; + this.keystore = keystore; + this.keystorePassword = keystorePassword; + this.keepAlive = keepAlive; + this.connectionsMaxPerRoute = connectionsMaxPerRoute; + this.connectionsMaxTotal = connectionsMaxTotal; + this.username = username; + this.password = password; + this.connectTimeout = connectTimeout; + this.executionTimeout = executionTimeout; + } + + public String getExecutionTimeout() { + return executionTimeout; + } + + public void setExecutionTimeout(String executionTimeout) { + this.executionTimeout = executionTimeout; + } + + public String getConnectTimeout() { + return connectTimeout; + } + + public void setConnectTimeout(String connectTimeout) { + this.connectTimeout = connectTimeout; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getProxyUsername() { + return proxyUsername; + } + + public void setProxyUsername(String proxyUsername) { + this.proxyUsername = proxyUsername; + } + + public String getProxyPassword() { + return proxyPassword; + } + + public void setProxyPassword(String proxyPassword) { + this.proxyPassword = proxyPassword; + } + + public String getTlsVersion() { + return tlsVersion; + } + + public void setTlsVersion(String tlsVersion) { + this.tlsVersion = tlsVersion; + } + + public String getAllowedCyphers() { + return allowedCyphers; + } + + public void setAllowedCyphers(String allowedCyphers) { + this.allowedCyphers = allowedCyphers; + } + + public String getTrustAllRoots() { + return trustAllRoots; + } + + public void setTrustAllRoots(String trustAllRoots) { + this.trustAllRoots = trustAllRoots; + } + + public String getX509HostnameVerifier() { + return x509HostnameVerifier; + } + + public void setX509HostnameVerifier(String x509HostnameVerifier) { + this.x509HostnameVerifier = x509HostnameVerifier; + } + + public String getTrustKeystore() { + return trustKeystore; + } + + public void setTrustKeystore(String trustKeystore) { + this.trustKeystore = trustKeystore; + } + + public String getTrustPassword() { + return trustPassword; + } + + public void setTrustPassword(String trustPassword) { + this.trustPassword = trustPassword; + } + + public String getKeystore() { + return keystore; + } + + public void setKeystore(String keystore) { + this.keystore = keystore; + } + + public String getKeystorePassword() { + return keystorePassword; + } + + public void setKeystorePassword(String keystorePassword) { + this.keystorePassword = keystorePassword; + } + + public String getKeepAlive() { + return keepAlive; + } + + public void setKeepAlive(String keepAlive) { + this.keepAlive = keepAlive; + } + + public String getConnectionsMaxPerRoute() { + return connectionsMaxPerRoute; + } + + public void setConnectionsMaxPerRoute(String connectionsMaxPerRoute) { + this.connectionsMaxPerRoute = connectionsMaxPerRoute; + } + + public String getConnectionsMaxTotal() { + return connectionsMaxTotal; + } + + public void setConnectionsMaxTotal(String connectionsMaxTotal) { + this.connectionsMaxTotal = connectionsMaxTotal; + } + + public static GetHttpInputBuilder builder() { + return new GetHttpInputBuilder(); + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public String getProxyHost() { + return proxyHost; + } + + public void setProxyHost(String proxyHost) { + this.proxyHost = proxyHost; + } + + public String getProxyPort() { + return proxyPort; + } + + public void setProxyPort(String proxyPort) { + this.proxyPort = proxyPort; + } + + public static class GetHttpInputBuilder { + private String host = EMPTY; + private String connectTimeout = EMPTY; + private String proxyHost = EMPTY; + private String proxyPort = EMPTY; + private String proxyUsername = EMPTY; + private String proxyPassword = EMPTY; + private String tlsVersion = EMPTY; + private String allowedCyphers = EMPTY; + private String trustAllRoots = EMPTY; + private String x509HostnameVerifier = EMPTY; + private String trustKeystore = EMPTY; + private String trustPassword = EMPTY; + private String keystore = EMPTY; + private String keystorePassword = EMPTY; + private String keepAlive = EMPTY; + private String connectionsMaxPerRoute = EMPTY; + private String connectionsMaxTotal = EMPTY; + private String username = EMPTY; + private String password = EMPTY; + private String executionTimeout = EMPTY; + + public GetHttpInputBuilder() { + } + + public GetHttpInputBuilder host(final String host) { + this.host = host; + return this; + } + public GetHttpInputBuilder executionTimeout(final String executionTimeout) { + this.executionTimeout = executionTimeout; + return this; + } + public GetHttpInputBuilder connectTimeout(final String connectTimeout) { + this.connectTimeout = connectTimeout; + return this; + } + public GetHttpInputBuilder username(final String username) { + this.username = username; + return this; + } + public GetHttpInputBuilder password(final String password) { + this.password = password; + return this; + } + + public GetHttpInputBuilder trustKeystore(final String trustKeystore) { + this.trustKeystore = trustKeystore; + return this; + } + + public GetHttpInputBuilder keystorePassword(final String keystorePassword) { + this.keystorePassword = keystorePassword; + return this; + } + + public GetHttpInputBuilder keystore(final String keystore) { + this.keystore = keystore; + return this; + } + + public GetHttpInputBuilder trustPassword(final String trustPassword) { + this.trustPassword = trustPassword; + return this; + } + + public GetHttpInputBuilder proxyPassword(final String proxyPassword) { + this.proxyPassword = proxyPassword; + return this; + } + + public GetHttpInputBuilder tlsVersion(final String tlsVersion) { + this.tlsVersion = tlsVersion; + return this; + } + + public GetHttpInputBuilder proxyUsername(final String proxyUsername) { + this.proxyUsername = proxyUsername; + return this; + } + + public GetHttpInputBuilder allowedCyphers(final String allowedCyphers) { + this.allowedCyphers = allowedCyphers; + return this; + } + + public GetHttpInputBuilder trustAllRoots(final String trustAllRoots) { + this.trustAllRoots = trustAllRoots; + return this; + } + + public GetHttpInputBuilder x509HostnameVerifier(final String x509HostnameVerifier) { + this.x509HostnameVerifier = x509HostnameVerifier; + return this; + } + + public GetHttpInputBuilder keepAlive(final String keepAlive) { + this.keepAlive = keepAlive; + return this; + } + + public GetHttpInputBuilder connectionsMaxPerRoute(final String connectionsMaxPerRoute) { + this.connectionsMaxPerRoute = connectionsMaxPerRoute; + return this; + } + + public GetHttpInputBuilder connectionsMaxTotal(final String connectionsMaxTotal) { + this.connectionsMaxTotal = connectionsMaxTotal; + return this; + } + + public GetHttpInputBuilder proxyHost(final String proxyHost) { + this.proxyHost = proxyHost; + return this; + } + + public GetHttpInputBuilder proxyPort(final String proxyPort) { + this.proxyPort = proxyPort; + return this; + } + + public HttpInput build() { + return new HttpInput(host,proxyHost,proxyPort,proxyUsername,proxyPassword,tlsVersion,allowedCyphers, + trustAllRoots, x509HostnameVerifier,trustKeystore,trustPassword,keystore,keystorePassword,keepAlive, + connectionsMaxPerRoute,connectionsMaxTotal,username,password,connectTimeout,executionTimeout); + } + } +} diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java index 6e3965aee8..3ab668f895 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java @@ -1,8 +1,14 @@ package io.cloudslang.content.redhat.services; +import com.hp.oo.sdk.content.plugin.GlobalSessionObject; +import com.hp.oo.sdk.content.plugin.SerializableSessionObject; +import io.cloudslang.content.httpclient.actions.HttpClientPostAction; +import io.cloudslang.content.redhat.entities.HttpInput; import com.google.gson.JsonPrimitive; import com.jayway.jsonpath.JsonPath; import org.apache.commons.lang3.StringUtils; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; import java.util.List; import java.util.Map; @@ -12,11 +18,81 @@ import static io.cloudslang.content.constants.OutputNames.RETURN_CODE; import static io.cloudslang.content.constants.OutputNames.RETURN_RESULT; +import static io.cloudslang.content.redhat.utils.Constants.CommonConstants.*; +import static io.cloudslang.content.redhat.utils.Descriptions.GetTokenAction.SUCCESS_DESC; +import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.EXCEPTION; +import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.STATUS_CODE; import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.*; import static io.cloudslang.content.redhat.utils.Descriptions.GetDeploymentStatus.RETURN_RESULT_MESSAGE_DESC; public class OpenshiftService { + public static void processAuthTokenResult(Map httpResults, HttpInput input, SerializableSessionObject sessionCookies, GlobalSessionObject sessionConnectionPool) { + + try { + String getHtmlResponse = httpResults.get(RETURN_RESULT); + Document doc = Jsoup.parse(getHtmlResponse); + String code = doc.select(FORM_INPUT).get(0).attr(VALUE); + String csrf = doc.select(FORM_INPUT).get(1).attr(VALUE); + String statusCode = httpResults.get(STATUS_CODE); + + if (StringUtils.isEmpty(statusCode) || Integer.parseInt(statusCode) < 200 || Integer.parseInt(statusCode) >= 300) { + if (StringUtils.isEmpty(httpResults.get(EXCEPTION))) + httpResults.put(EXCEPTION, httpResults.get(RETURN_RESULT)); + httpResults.put(RETURN_CODE, "-1"); + } + Map test = new HttpClientPostAction().execute( + input.getHost() + DISPLAY_TOKEN_ENDPOINT, + BASIC, + input.getUsername(), + input.getPassword(), + EMPTY_STRING, + input.getProxyHost(), + input.getProxyPort(), + input.getProxyUsername(), + input.getPassword(), + input.getTlsVersion(), + input.getAllowedCyphers(), + TRUE, + input.getX509HostnameVerifier(), + input.getTrustKeystore(), + input.getTrustPassword(), + input.getKeystore(), + input.getKeystorePassword(), + input.getKeepAlive(), + input.getConnectionsMaxPerRoute(), + input.getConnectionsMaxTotal(), + EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, + EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, + EMPTY_STRING, + EMPTY_STRING, + CODE + EQUAL + code + CSRF + EQUAL + csrf, + TRUE, EMPTY_STRING, + EMPTY_STRING, + CONTENT_TYPE_FORM, + EMPTY_STRING, input.getConnectTimeout(), + EMPTY_STRING, input.getExecutionTimeout(), + sessionCookies, sessionConnectionPool); + + String postHtmlResponse = test.get(RETURN_RESULT); + Document second_response = Jsoup.parse(postHtmlResponse); + if (!StringUtils.isEmpty(postHtmlResponse)){ + httpResults.put(RETURN_RESULT, SUCCESS_DESC); + httpResults.put(AUTH_TOKEN, second_response.select(CODE).get(0).text()); + } + + if (StringUtils.isEmpty(statusCode) || Integer.parseInt(statusCode) < 200 || Integer.parseInt(statusCode) >= 300) { + if (StringUtils.isEmpty(httpResults.get(EXCEPTION))) + httpResults.put(EXCEPTION, httpResults.get(RETURN_RESULT)); + httpResults.put(RETURN_CODE, "-1"); + } + + + } catch (IndexOutOfBoundsException e) { + httpResults.put(EXCEPTION, httpResults.get(RETURN_RESULT)); + httpResults.put(RETURN_CODE, "-1"); + } + } public static void processHttpResult(Map httpResults) { String statusCode = httpResults.get(STATUS_CODE); @@ -95,4 +171,5 @@ public static void processHttpGetDeploymentStatusResult(Map http httpResults.put(RETURN_CODE, "-1"); } } + } diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java index 37e46d6262..57ce4071f6 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java @@ -64,14 +64,28 @@ public static final class CommonConstants { public static final String APPLICATION_JSON = "application/json"; public static final String CONTENT_TYPE = "Content-Type:"; public static final String TRUE = "true"; + public static final String FALSE = "FALSE"; public static final String EMPTY_STRING = ""; public static final String AUTHORIZATION_BEARER = "Authorization: Bearer "; + public static final String AUTHORIZATION_BASIC = "Authorization: Basic "; public static final String ANONYMOUS = "Anonymous"; //apis public static final String APPS_V1_NAMESPACES = "/apis/apps/v1/namespaces/"; public static final String DEPLOYMENTS = "/deployments/"; + //query for authorization + public static final String QUERY_PARAM = "response_type=code&client_id=openshift-browser-client"; + public static final String AUTHORIZE_TOKEN_URL = "/oauth/authorize"; + public static final String FORM_INPUT = "form > input"; + public static final String VALUE = "value"; + public static final String CODE = "code"; + public static final String CSRF = "&csrf"; + public static final String EQUAL = "="; + public static final String CONTENT_TYPE_FORM = "application/x-www-form-urlencoded"; + public static final String DISPLAY_TOKEN_ENDPOINT = "/oauth/token/display"; + public static final String COLON_PUNCTUATION = ":"; + } } diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java index a099cef394..95c4da2a5c 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java @@ -81,6 +81,7 @@ public static class Common { public static class GetTokenAction { public static final String GET_TOKEN_NAME = "Get Token"; + public static final String GET_TOKEN_NAME_DESC = "Get Authorization Token"; public static final String RETURN_RESULT_DESC = "The authorization token for Openshift."; public static final String AUTH_TOKEN_DESC = "Generated authentication token."; public static final String EXCEPTION_DESC = "An error message in case there was an error while generating the " + From 99bae020defcf1eef76ffcc0bedeb29ace5ab019 Mon Sep 17 00:00:00 2001 From: Cristian - Adrian Dora <73101731+SilentCheater@users.noreply.github.com> Date: Mon, 12 Dec 2022 14:59:19 +0200 Subject: [PATCH 09/16] CreateDeployment + added some inputs (#1112) * CreateDeployment + added some inputs * code alignment Co-authored-by: CDora --- .../redhat/actions/CreateDeployment.java | 138 ++++++++++++++++++ .../content/redhat/utils/Constants.java | 21 ++- .../content/redhat/utils/Descriptions.java | 12 ++ .../content/redhat/utils/Utils.java | 5 + 4 files changed, 173 insertions(+), 3 deletions(-) create mode 100644 cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/CreateDeployment.java create mode 100644 cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Utils.java diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/CreateDeployment.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/CreateDeployment.java new file mode 100644 index 0000000000..e0e64d71a9 --- /dev/null +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/CreateDeployment.java @@ -0,0 +1,138 @@ +/* + * (c) Copyright 2022 Micro Focus + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License v2.0 which accompany this distribution. + * + * The Apache License is available at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.cloudslang.content.redhat.actions; + + +import com.hp.oo.sdk.content.annotations.Action; +import com.hp.oo.sdk.content.annotations.Output; +import com.hp.oo.sdk.content.annotations.Param; +import com.hp.oo.sdk.content.annotations.Response; +import com.hp.oo.sdk.content.plugin.ActionMetadata.MatchType; +import com.hp.oo.sdk.content.plugin.ActionMetadata.ResponseType; +import com.hp.oo.sdk.content.plugin.GlobalSessionObject; +import com.hp.oo.sdk.content.plugin.SerializableSessionObject; +import io.cloudslang.content.constants.ResponseNames; +import io.cloudslang.content.constants.ReturnCodes; +import io.cloudslang.content.httpclient.actions.HttpClientPostAction; +import io.cloudslang.content.utils.OutputUtilities; + +import java.util.Map; + +import static io.cloudslang.content.constants.OutputNames.RETURN_CODE; +import static io.cloudslang.content.constants.OutputNames.RETURN_RESULT; +import static io.cloudslang.content.httpclient.utils.Descriptions.HTTPClient.SESSION_CONNECTION_POOL_DESC; +import static io.cloudslang.content.httpclient.utils.Descriptions.HTTPClient.SESSION_COOKIES_DESC; +import static io.cloudslang.content.redhat.services.OpenshiftService.processHttpResult; +import static io.cloudslang.content.redhat.utils.Constants.CommonConstants.*; +import static io.cloudslang.content.redhat.utils.Descriptions.Common.*; +import static io.cloudslang.content.redhat.utils.Descriptions.CreateDeploymentAction.*; +import static io.cloudslang.content.redhat.utils.Descriptions.CreateDeploymentAction.EXCEPTION_DESC; +import static io.cloudslang.content.redhat.utils.Descriptions.CreateDeploymentAction.RETURN_RESULT_DESC; +import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.EXCEPTION; +import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.STATUS_CODE; +import static org.apache.commons.lang3.StringUtils.EMPTY; + +public class CreateDeployment { + + @Action(name = CREATE_DEPLOYMENT, + description = CREATE_DEPLOYMENT_DESCRIPTION, + outputs = { + @Output(value = RETURN_RESULT, description = RETURN_RESULT_DESC), + @Output(value = RETURN_CODE, description = RETURN_CODE_DESC), + @Output(value = EXCEPTION, description = EXCEPTION_DESC), + @Output(value = STATUS_CODE, description = STATUS_CODE_DESC) + }, + responses = { + @Response(text = ResponseNames.SUCCESS, field = RETURN_CODE, value = ReturnCodes.SUCCESS, matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.RESOLVED), + @Response(text = ResponseNames.FAILURE, field = RETURN_CODE, value = ReturnCodes.FAILURE, matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.ERROR, isOnFail = true) + }) + public Map execute( + @Param(value = HOST, description = HOST_DESC, required = true) String hostName, + @Param(value = AUTH_TOKEN, description = AUTH_TOKEN_DESCRIPTION, required = true) String authToken, + @Param(value = DEFINITION, description = DEFINITION_DESCRIPTION, required = true) String body, + @Param(value = NAMESPACE, description = NAMESPACE_DESCRIPTION, required = true) String namespace, + + @Param(value = PROXY_HOST, description = PROXY_HOST_DESC) String proxyHost, + @Param(value = PROXY_PORT, description = PROXY_PORT_DESC) String proxyPort, + @Param(value = PROXY_USERNAME, description = PROXY_USERNAME_DESC) String proxyUsername, + @Param(value = PROXY_PASSWORD, encrypted = true, description = PROXY_PASSWORD_DESC) String proxyPassword, + @Param(value = TLS_VERSION, description = TLS_VERSION_DESC) String tlsVersion, + @Param(value = ALLOWED_CIPHERS, description = ALLOWED_CIPHERS_DESC) String allowedCiphers, + @Param(value = TRUST_ALL_ROOTS, description = TRUST_ALL_ROOTS_DESC) String trustAllRoots, + @Param(value = X509_HOSTNAME_VERIFIER, description = X509_HOSTNAME_VERIFIER_DESC) String x509HostnameVerifier, + @Param(value = TRUST_KEYSTORE, description = TRUST_KEYSTORE_DESC) String trustKeystore, + @Param(value = TRUST_PASSWORD, encrypted = true, description = TRUST_PASSWORD_DESC) String trustPassword, + @Param(value = KEYSTORE, description = KEYSTORE_DESC) String keystore, + @Param(value = KEYSTORE_PASSWORD, encrypted = true, description = KEYSTORE_PASSWORD_DESC) String keystorePassword, + @Param(value = CONNECT_TIMEOUT, description = CONNECT_TIMEOUT_DESC) String connectTimeout, + @Param(value = EXECUTION_TIMEOUT, description = EXECUTION_TIMEOUT_DESC) String executionTimeout, + @Param(value = KEEP_ALIVE, description = KEEP_ALIVE_DESC) String keepAlive, + @Param(value = CONNECTIONS_MAX_PER_ROUTE, description = CONNECTIONS_MAX_PER_ROUTE_DESC) String connectionsMaxPerRoute, + @Param(value = CONNECTIONS_MAX_TOTAL, description = CONNECTIONS_MAX_TOTAL_DESC) String connectionsMaxTotal, + @Param(value = SESSION_COOKIES, description = SESSION_COOKIES_DESC) SerializableSessionObject sessionCookies, + @Param(value = SESSION_CONNECTION_POOL, description = SESSION_CONNECTION_POOL_DESC) GlobalSessionObject sessionConnectionPool) { + + try { + + Map result = new HttpClientPostAction().execute( + hostName + APPS_V1_NAMESPACES + namespace + "/deployments", + ANONYMOUS, + EMPTY, + EMPTY, + TRUE, + proxyHost, + proxyPort, + proxyUsername, + proxyPassword, + tlsVersion, + allowedCiphers, + trustAllRoots, + x509HostnameVerifier, + trustKeystore, + trustPassword, + keystore, + keystorePassword, + keepAlive, + connectionsMaxPerRoute, + connectionsMaxTotal, + EMPTY, + EMPTY, + CONTENT_TYPE + APPLICATION_JSON + COMMA + AUTHORIZATION_BEARER + authToken, + EMPTY, + EMPTY, + EMPTY, + EMPTY, + EMPTY, + EMPTY, + EMPTY, + EMPTY, + body, + APPLICATION_JSON, + EMPTY, + connectTimeout, + EMPTY, + executionTimeout, + sessionCookies, + sessionConnectionPool + ); + + processHttpResult(result); + return result; + + } catch (Exception exception) { + return OutputUtilities.getFailureResultsMap(exception); + } + } +} diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java index 57ce4071f6..d867b779b1 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java @@ -11,7 +11,7 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. -*/ + */ /* * (c) Copyright 2022 Micro Focus * All rights reserved. This program and the accompanying materials @@ -70,7 +70,23 @@ public static final class CommonConstants { public static final String AUTHORIZATION_BASIC = "Authorization: Basic "; public static final String ANONYMOUS = "Anonymous"; - //apis + //Other + public static final String PROTOCOL_DELIMITER = "://"; + public static final String FORWARD_SLASH = "/"; + public static final String COMMA = ","; + public static final String EQUALS = "="; + public static final String SEMICOLON = ";"; + + // Create Deployment + public static final String AUTH_TOKEN = "authToken"; + public static final String DEFINITION = "definition"; + + + // endpoints + public static final String CREATE_DEPLOYMENT_ENDPOINT(String namespace) { + return "/apis/apps/v1/namespaces/" + namespace + "/deployments"; + } + public static final String APPS_V1_NAMESPACES = "/apis/apps/v1/namespaces/"; public static final String DEPLOYMENTS = "/deployments/"; @@ -85,7 +101,6 @@ public static final class CommonConstants { public static final String CONTENT_TYPE_FORM = "application/x-www-form-urlencoded"; public static final String DISPLAY_TOKEN_ENDPOINT = "/oauth/token/display"; public static final String COLON_PUNCTUATION = ":"; - } } diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java index 95c4da2a5c..e704bcd031 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java @@ -94,6 +94,18 @@ public static class GetTokenAction { } + public static class CreateDeploymentAction { + public static final String CREATE_DEPLOYMENT = "Create Deployment"; + public static final String CREATE_DEPLOYMENT_DESCRIPTION = "This operation creates a deployment with the inputs from the user."; + public static final String AUTH_TOKEN_DESCRIPTION = "Token used to authenticate to the openshift environment."; + public static final String NAMESPACE_DESCRIPTION = "The namespace in which the deployment will be created."; + public static final String DEFINITION_DESCRIPTION = "YAML or JSON definitions."; + public static final String RETURN_RESULT_DESC = "The authorization token for Openshift."; + public static final String EXCEPTION_DESC = "An error message in case there was an error"; + public static final String SUCCESS_DESC = "Deployment created successfully."; + public static final String FAILURE_DESC = "There was an error while trying to create the deployment."; + } + public static class DeleteDeployment { public static final String DELETE_DEPLOYMENT_NAME = "Delete Deployment"; diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Utils.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Utils.java new file mode 100644 index 0000000000..69c6b55402 --- /dev/null +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Utils.java @@ -0,0 +1,5 @@ +package io.cloudslang.content.redhat.utils; + +public class Utils { + +} From 2582c1d7289e8084676bff2639ecc66bf154974d Mon Sep 17 00:00:00 2001 From: Florin Anca Date: Tue, 13 Dec 2022 10:45:44 +0200 Subject: [PATCH 10/16] Get pod list (#1115) * Added the GetPodList operation and removed deprecated JsonParser utilisation * Merged latest changes into GetPodList and fixed some minor bugs * Made namespace required and fixed a description Co-authored-by: Florin --- cs-openshift/pom.xml | 13 +- .../actions/GetDeploymentStatusAction.java | 3 +- .../content/redhat/actions/GetPodList.java | 142 ++++++++++++++++++ .../redhat/services/OpenshiftService.java | 78 ++++++++-- .../content/redhat/utils/Descriptions.java | 29 +++- .../content/redhat/utils/Outputs.java | 5 + 6 files changed, 250 insertions(+), 20 deletions(-) create mode 100644 cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetPodList.java diff --git a/cs-openshift/pom.xml b/cs-openshift/pom.xml index 415106e8be..872bacecb1 100644 --- a/cs-openshift/pom.xml +++ b/cs-openshift/pom.xml @@ -76,18 +76,16 @@ - - com.hp.score.sdk score-content-sdk ${score-content-sdk.version} - - io.cloudslang.content - cs-commons - ${cs-commons.version} - + + + + + io.cloudslang.content cs-httpclient-commons @@ -96,7 +94,6 @@ - org.jsoup jsoup 1.15.3 diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetDeploymentStatusAction.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetDeploymentStatusAction.java index aca8b5801e..e036bcc099 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetDeploymentStatusAction.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetDeploymentStatusAction.java @@ -19,8 +19,9 @@ import java.util.Map; -import static io.cloudslang.content.redhat.utils.Descriptions.Common.STATUS_CODE_DESC; +import static io.cloudslang.content.redhat.utils.Constants.CommonConstants.AUTH_TOKEN; import static io.cloudslang.content.redhat.utils.Descriptions.GetDeploymentStatus.FAILURE_DESC; +import static io.cloudslang.content.redhat.utils.Descriptions.GetDeploymentStatus.STATUS_CODE_DESC; import static io.cloudslang.content.redhat.utils.Descriptions.GetDeploymentStatus.SUCCESS_DESC; import static io.cloudslang.content.redhat.utils.Descriptions.GetDeploymentStatus.RETURN_RESULT_DESC; import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.*; diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetPodList.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetPodList.java new file mode 100644 index 0000000000..9b32a660d7 --- /dev/null +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetPodList.java @@ -0,0 +1,142 @@ +package io.cloudslang.content.redhat.actions; + + +import com.hp.oo.sdk.content.annotations.Action; +import com.hp.oo.sdk.content.annotations.Output; +import com.hp.oo.sdk.content.annotations.Param; +import com.hp.oo.sdk.content.annotations.Response; +import com.hp.oo.sdk.content.plugin.ActionMetadata.MatchType; +import com.hp.oo.sdk.content.plugin.ActionMetadata.ResponseType; +import com.hp.oo.sdk.content.plugin.GlobalSessionObject; +import com.hp.oo.sdk.content.plugin.SerializableSessionObject; +import io.cloudslang.content.constants.OutputNames; +import io.cloudslang.content.constants.ResponseNames; +import io.cloudslang.content.constants.ReturnCodes; +import io.cloudslang.content.httpclient.actions.HttpClientGetAction; + +import java.util.HashMap; +import java.util.Map; + +import static io.cloudslang.content.constants.OutputNames.RETURN_CODE; +import static io.cloudslang.content.constants.OutputNames.RETURN_RESULT; +import static io.cloudslang.content.httpclient.utils.Descriptions.HTTPClient.SESSION_CONNECTION_POOL_DESC; +import static io.cloudslang.content.httpclient.utils.Descriptions.HTTPClient.SESSION_COOKIES_DESC; +import static io.cloudslang.content.redhat.services.OpenshiftService.*; +import static io.cloudslang.content.redhat.utils.Constants.CommonConstants.*; +import static io.cloudslang.content.redhat.utils.Constants.CommonConstants.AUTH_TOKEN; +import static io.cloudslang.content.redhat.utils.Descriptions.Common.*; +import static io.cloudslang.content.redhat.utils.Descriptions.GetPodList.*; +import static io.cloudslang.content.redhat.utils.Descriptions.GetPodList.STATUS_CODE_DESC; +import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.*; +import static org.apache.commons.lang3.StringUtils.EMPTY; + +public class GetPodList { + + + @Action(name = GET_POD_LIST, + description = GET_POD_LIST_DESC, + outputs = { + //Common outputs + @Output(value = RETURN_RESULT, description = RETURN_RESULT_DESC), + @Output(value = STATUS_CODE, description = STATUS_CODE_DESC), + @Output(value = RETURN_CODE, description = RETURN_CODE_DESC), + @Output(value = EXCEPTION, description = EXCEPTION_DESC), + //Specific outputs - general + @Output(value = DOCUMENT_OUTPUT, description = DOCUMENT_OUTPUT_DESC), + @Output(value = POD_LIST, description = POD_LIST_DESC), + @Output(value = POD_ARRAY, description = POD_ARRAY_DESC), + }, + responses = { + @Response(text = ResponseNames.SUCCESS, field = OutputNames.RETURN_CODE, value = ReturnCodes.SUCCESS, + matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.RESOLVED, description = SUCCESS_DESC), + @Response(text = ResponseNames.FAILURE, field = OutputNames.RETURN_CODE, value = ReturnCodes.FAILURE, + matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.ERROR, description = FAILURE_DESC) + }) + + public Map execute( + //Specific input + @Param(value = HOST, description = HOST_DESC, required = true) String host, + @Param(value = AUTH_TOKEN, description = AUTH_TOKEN_DESC, required = true) String authToken, + @Param(value = NAMESPACE, description = NAMESPACE_DESC, required = true) String namespace, + //Common Inputs + @Param(value = PROXY_HOST, description = PROXY_HOST_DESC) String proxyHost, + @Param(value = PROXY_PORT, description = PROXY_PORT_DESC) String proxyPort, + @Param(value = PROXY_USERNAME, description = PROXY_USERNAME_DESC) String proxyUsername, + @Param(value = PROXY_PASSWORD, encrypted = true, description = PROXY_PASSWORD_DESC) String proxyPassword, + @Param(value = TLS_VERSION, description = TLS_VERSION_DESC) String tlsVersion, + @Param(value = ALLOWED_CIPHERS, description = ALLOWED_CIPHERS_DESC) String allowedCiphers, + @Param(value = TRUST_ALL_ROOTS, description = TRUST_ALL_ROOTS_DESC) String trustAllRoots, + @Param(value = X509_HOSTNAME_VERIFIER, description = X509_HOSTNAME_VERIFIER_DESC) String x509HostnameVerifier, + @Param(value = TRUST_KEYSTORE, description = TRUST_KEYSTORE_DESC) String trustKeystore, + @Param(value = TRUST_PASSWORD, encrypted = true, description = TRUST_PASSWORD_DESC) String trustPassword, + @Param(value = KEYSTORE, description = KEYSTORE_DESC) String keystore, + @Param(value = KEYSTORE_PASSWORD, encrypted = true, description = KEYSTORE_PASSWORD_DESC) String keystorePassword, + @Param(value = CONNECT_TIMEOUT, description = CONNECT_TIMEOUT_DESC) String connectTimeout, + @Param(value = EXECUTION_TIMEOUT, description = EXECUTION_TIMEOUT_DESC) String executionTimeout, + @Param(value = KEEP_ALIVE, description = KEEP_ALIVE_DESC) String keepAlive, + @Param(value = CONNECTIONS_MAX_PER_ROUTE, description = CONNECTIONS_MAX_PER_ROUTE_DESC) String connectionsMaxPerRoute, + @Param(value = CONNECTIONS_MAX_TOTAL, description = CONNECTIONS_MAX_TOTAL_DESC) String connectionsMaxTotal, + @Param(value = SESSION_COOKIES, description = SESSION_COOKIES_DESC) SerializableSessionObject sessionCookies, + @Param(value = SESSION_CONNECTION_POOL, description = SESSION_CONNECTION_POOL_DESC) GlobalSessionObject sessionConnectionPool) { + + Map result = new HashMap<>(); + + try { + + result = new HttpClientGetAction().execute( + host + GET_POD_LIST_ENDPOINT_1 + namespace + GET_POD_LIST_ENDPOINT_2, + ANONYMOUS, + EMPTY, + EMPTY, + EMPTY, + proxyHost, + proxyPort, + proxyUsername, + proxyPassword, + tlsVersion, + allowedCiphers, + trustAllRoots, + x509HostnameVerifier, + trustKeystore, + trustPassword, + keystore, + keystorePassword, + keepAlive, + connectionsMaxPerRoute, + connectionsMaxTotal, + EMPTY, + EMPTY, + AUTHORIZATION_BEARER + authToken, + EMPTY, + EMPTY, + EMPTY, + EMPTY, + EMPTY, + connectTimeout, + EMPTY, + executionTimeout, + sessionCookies, + sessionConnectionPool + ); + + if (Integer.parseInt(result.get(RETURN_CODE)) != -1) { + if (Integer.parseInt(result.get(STATUS_CODE)) >= 200 && Integer.parseInt(result.get(STATUS_CODE)) < 300) + addPodListResults(result); + else { + setFailureCustomResults(result); + result.put(RETURN_CODE, NEGATIVE_RETURN_CODE); + result.put(EXCEPTION, result.get(RETURN_RESULT)); + result.put(RETURN_RESULT, FAILURE_RETURN_RESULT); + } + + } else + setFailureCustomResults(result); + return result; + + } catch (Exception exception) { + setFailureCommonResults(result, exception); + return result; + } + } + +} diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java index 3ab668f895..a52071da95 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java @@ -1,5 +1,6 @@ package io.cloudslang.content.redhat.services; +import com.google.gson.*; import com.hp.oo.sdk.content.plugin.GlobalSessionObject; import com.hp.oo.sdk.content.plugin.SerializableSessionObject; import io.cloudslang.content.httpclient.actions.HttpClientPostAction; @@ -9,21 +10,24 @@ import org.apache.commons.lang3.StringUtils; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; +import org.apache.commons.lang3.exception.ExceptionUtils; +import java.util.ArrayList; import java.util.List; import java.util.Map; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; - import static io.cloudslang.content.constants.OutputNames.RETURN_CODE; import static io.cloudslang.content.constants.OutputNames.RETURN_RESULT; import static io.cloudslang.content.redhat.utils.Constants.CommonConstants.*; +import static io.cloudslang.content.redhat.utils.Constants.CommonConstants.COMMA; import static io.cloudslang.content.redhat.utils.Descriptions.GetTokenAction.SUCCESS_DESC; +import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.AUTH_TOKEN; import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.EXCEPTION; import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.STATUS_CODE; +import static io.cloudslang.content.redhat.utils.Descriptions.GetPodList.*; import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.*; import static io.cloudslang.content.redhat.utils.Descriptions.GetDeploymentStatus.RETURN_RESULT_MESSAGE_DESC; +import static org.apache.commons.lang3.StringUtils.EMPTY; public class OpenshiftService { @@ -39,7 +43,7 @@ public static void processAuthTokenResult(Map httpResults, HttpI if (StringUtils.isEmpty(statusCode) || Integer.parseInt(statusCode) < 200 || Integer.parseInt(statusCode) >= 300) { if (StringUtils.isEmpty(httpResults.get(EXCEPTION))) httpResults.put(EXCEPTION, httpResults.get(RETURN_RESULT)); - httpResults.put(RETURN_CODE, "-1"); + httpResults.put(RETURN_CODE, NEGATIVE_RETURN_CODE); } Map test = new HttpClientPostAction().execute( input.getHost() + DISPLAY_TOKEN_ENDPOINT, @@ -84,13 +88,13 @@ public static void processAuthTokenResult(Map httpResults, HttpI if (StringUtils.isEmpty(statusCode) || Integer.parseInt(statusCode) < 200 || Integer.parseInt(statusCode) >= 300) { if (StringUtils.isEmpty(httpResults.get(EXCEPTION))) httpResults.put(EXCEPTION, httpResults.get(RETURN_RESULT)); - httpResults.put(RETURN_CODE, "-1"); + httpResults.put(RETURN_CODE, NEGATIVE_RETURN_CODE); } } catch (IndexOutOfBoundsException e) { httpResults.put(EXCEPTION, httpResults.get(RETURN_RESULT)); - httpResults.put(RETURN_CODE, "-1"); + httpResults.put(RETURN_CODE, NEGATIVE_RETURN_CODE); } } public static void processHttpResult(Map httpResults) { @@ -100,8 +104,63 @@ public static void processHttpResult(Map httpResults) { if (StringUtils.isEmpty(statusCode) || Integer.parseInt(statusCode) < 200 || Integer.parseInt(statusCode) >= 300) { if (StringUtils.isEmpty(httpResults.get(EXCEPTION))) httpResults.put(EXCEPTION, httpResults.get(RETURN_RESULT)); - httpResults.put(RETURN_CODE, "-1"); + httpResults.put(RETURN_CODE, NEGATIVE_RETURN_CODE); + } + } + + public static void addPodListResults(Map httpResults) { + try { + String returnResult = httpResults.get(RETURN_RESULT); + if (!(returnResult.isEmpty())) { + + //populate the document output + httpResults.put(DOCUMENT_OUTPUT, returnResult); + + //parse the API response, extract required information construct the podList and podArray outputs + JsonObject jsonResponse = JsonParser.parseString(httpResults.get(RETURN_RESULT)).getAsJsonObject(); + JsonArray podArray = jsonResponse.getAsJsonArray(PROPERTY_ITEMS); + StringBuilder podList = new StringBuilder(); + List podPairList = new ArrayList<>(); + for (JsonElement pod : podArray) { + JsonObject podObject = new JsonObject(); + podObject.add(PROPERTY_NAME, pod.getAsJsonObject().get(PROPERTY_METADATA).getAsJsonObject().get(PROPERTY_NAME)); + podObject.add(PROPERTY_UID, pod.getAsJsonObject().get(PROPERTY_METADATA).getAsJsonObject().get(PROPERTY_UID)); + podPairList.add(podObject); + podList.append(pod.getAsJsonObject().get(PROPERTY_METADATA).getAsJsonObject().get(PROPERTY_UID).getAsString()); + podList.append(COMMA); + } + + //remove the last comma from the podList value + podList.deleteCharAt(podList.length()-1); + + //populate the podList and podArray outputs + httpResults.put(POD_LIST, podList.toString()); + httpResults.put(POD_ARRAY, podPairList.toString()); + + //overwrite the returnResult output with a success message + httpResults.put(RETURN_RESULT, SUCCESSFUL_RETURN_RESULT); + + } + + } catch (Exception e) { + //in case an error arises during the parsing, populate the custom outputs with empty values + setFailureCustomResults(httpResults); + + throw new RuntimeException(e); } + + } + + public static void setFailureCustomResults(Map httpResults){ + httpResults.put(POD_LIST, EMPTY); + httpResults.put(POD_ARRAY, EMPTY); + httpResults.put(DOCUMENT_OUTPUT, EMPTY); + } + + public static void setFailureCommonResults(Map httpResults, Exception e){ + httpResults.put(RETURN_CODE, NEGATIVE_RETURN_CODE); + httpResults.put(RETURN_RESULT, e.getMessage()); + httpResults.put(EXCEPTION, ExceptionUtils.getStackTrace(e)); } public static void processHttpGetDeploymentStatusResult(Map httpResults) { @@ -112,7 +171,7 @@ public static void processHttpGetDeploymentStatusResult(Map http if (!(returnResult.isEmpty())) { httpResults.put(DOCUMENT_OUTPUT, returnResult); - JsonObject jsonResponse = ((new JsonParser()).parse(httpResults.get(RETURN_RESULT))).getAsJsonObject(); + JsonObject jsonResponse = JsonParser.parseString(httpResults.get(RETURN_RESULT)).getAsJsonObject(); //Kind output JsonPrimitive tmpResponse = (JsonPrimitive) jsonResponse.get(KIND_OUTPUT); @@ -168,8 +227,7 @@ public static void processHttpGetDeploymentStatusResult(Map http if (StringUtils.isEmpty(statusCode) || Integer.parseInt(statusCode) < 200 || Integer.parseInt(statusCode) >= 300) { if (StringUtils.isEmpty(httpResults.get(EXCEPTION))) httpResults.put(EXCEPTION, httpResults.get(RETURN_RESULT)); - httpResults.put(RETURN_CODE, "-1"); + httpResults.put(RETURN_CODE, NEGATIVE_RETURN_CODE); } } - } diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java index e704bcd031..0a645b7c7d 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java @@ -22,7 +22,7 @@ public static class Common { public static final String RETURN_CODE_DESC = "0 if success, -1 if failure."; public static final String STATUS_CODE_DESC = "The HTTP status code for Openshift API request."; - public static final String HOST_DESC = "HOST."; + public static final String HOST_DESC = "The OpenShift hostname."; public static final String AUTH_TOKEN_DESC = "Token used to authenticate to the Openshift environment."; public static final String PROXY_HOST_DESC = "The proxy server used to access the web site."; public static final String PROXY_PORT_DESC = "The proxy server port." + @@ -164,4 +164,31 @@ public static class GetDeploymentStatus { public static final String GET_DEPLOYMENT_STATUS_ENDPOINT_3 = "/status"; } + + public static class GetPodList { + public static final String GET_POD_LIST = "Get Pod List"; + public static final String GET_POD_LIST_DESC = "List objects of Pod type."; + public static final String NAMESPACE_DESC = "The object name and auth scope, such as for teams and projects."; + public static final String RETURN_RESULT_DESC = "A suggestive message both for the case of success and for the " + + "case of failure."; + public static final String STATUS_CODE_DESC = "The status code of the request."; + public static final String EXCEPTION_DESC = "An error message in case there was an error while retrieving the Pod list."; + public static final String SUCCESS_DESC = "The retrieval of the pod list was made successfully."; + public static final String FAILURE_DESC = "There was an error while trying to retrieve the pod list."; + public static final String POD_LIST_DESC = "The comma separated list of pod uids."; + public static final String POD_ARRAY_DESC = "The list containing pairs of pod name and uids."; + + public static final String DOCUMENT_OUTPUT_DESC = "All the information related to a the pod list in json format."; + + //API + public static final String GET_POD_LIST_ENDPOINT_1 = "/api/v1/namespaces/"; + public static final String GET_POD_LIST_ENDPOINT_2 = "/pods"; + public static final String NEGATIVE_RETURN_CODE = "-1"; + public static final String SUCCESSFUL_RETURN_RESULT = "The Pod list was returned successfully."; + public static final String FAILURE_RETURN_RESULT = "An issue was encountered during your query. Please check the exception output for more information."; + public static final String PROPERTY_NAME = "name"; + public static final String PROPERTY_UID = "uid"; + public static final String PROPERTY_ITEMS = "items"; + public static final String PROPERTY_METADATA = "metadata"; + } } diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Outputs.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Outputs.java index 6936b6468a..d697dffe02 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Outputs.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Outputs.java @@ -37,6 +37,11 @@ public static class OutputNames { public static final String UNAVAILABLE_REPLICAS_OUTPUT = "unavailableReplicas"; public static final String CONDITIONS_OUTPUT = "conditions"; + //Get Pod List + + public static final String POD_LIST = "podList"; + public static final String POD_ARRAY = "podArray"; + } } From f24189af218f2b784eb457fdb6d17d6df5e6d851 Mon Sep 17 00:00:00 2001 From: AlexandruPresecan Date: Tue, 13 Dec 2022 12:42:58 +0200 Subject: [PATCH 11/16] added encryption for passwords --- .../cloudslang/content/redhat/actions/DeleteDeployment.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/DeleteDeployment.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/DeleteDeployment.java index 9f640f5f79..1ff390ec0f 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/DeleteDeployment.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/DeleteDeployment.java @@ -64,15 +64,15 @@ public Map execute(@Param(value = HOST, required = true, descrip @Param(value = PROXY_HOST, description = PROXY_HOST_DESC) String proxyHost, @Param(value = PROXY_PORT, description = PROXY_PORT_DESC) String proxyPort, @Param(value = PROXY_USERNAME, description = PROXY_USERNAME_DESC) String proxyUsername, - @Param(value = PROXY_PASSWORD, description = PROXY_PASSWORD_DESC) String proxyPassword, + @Param(value = PROXY_PASSWORD, encrypted = true, description = PROXY_PASSWORD_DESC) String proxyPassword, @Param(value = TLS_VERSION, description = TLS_VERSION_DESC) String tlsVersion, @Param(value = ALLOWED_CIPHERS, description = ALLOWED_CIPHERS_DESC) String allowedCyphers, @Param(value = TRUST_ALL_ROOTS, description = TRUST_ALL_ROOTS_DESC) String trustAllRoots, @Param(value = X509_HOSTNAME_VERIFIER, description = X509_HOSTNAME_VERIFIER_DESC) String x509HostnameVerifier, @Param(value = TRUST_KEYSTORE, description = TRUST_KEYSTORE_DESC) String trustKeystore, - @Param(value = TRUST_PASSWORD, description = TRUST_PASSWORD_DESC) String trustPassword, + @Param(value = TRUST_PASSWORD, encrypted = true, description = TRUST_PASSWORD_DESC) String trustPassword, @Param(value = KEYSTORE, description = KEYSTORE_DESC) String keystore, - @Param(value = KEYSTORE_PASSWORD, description = KEYSTORE_PASSWORD_DESC) String keystorePassword, + @Param(value = KEYSTORE_PASSWORD, encrypted = true, description = KEYSTORE_PASSWORD_DESC) String keystorePassword, @Param(value = CONNECT_TIMEOUT, description = CONNECT_TIMEOUT_DESC) String connectTimeout, @Param(value = EXECUTION_TIMEOUT, description = EXECUTION_TIMEOUT_DESC) String executionTimeout, @Param(value = KEEP_ALIVE, description = KEEP_ALIVE_DESC) String keepAlive, From b302c5a7d27200e6a80fe3ae6e45e61f0a4bc4d9 Mon Sep 17 00:00:00 2001 From: mateii Date: Tue, 13 Dec 2022 15:06:11 +0200 Subject: [PATCH 12/16] changed params for get token --- .../content/redhat/actions/GetTokenAction.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetTokenAction.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetTokenAction.java index ea2615dae6..655e80dff8 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetTokenAction.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetTokenAction.java @@ -39,9 +39,8 @@ import static io.cloudslang.content.constants.ResponseNames.SUCCESS; import static io.cloudslang.content.httpclient.utils.Descriptions.HTTPClient.SESSION_CONNECTION_POOL_DESC; import static io.cloudslang.content.httpclient.utils.Descriptions.HTTPClient.SESSION_COOKIES_DESC; -import static io.cloudslang.content.redhat.entities.HttpInput.builder; + import static io.cloudslang.content.redhat.services.OpenshiftService.processAuthTokenResult; -import static io.cloudslang.content.redhat.services.OpenshiftService.processHttpResult; import static io.cloudslang.content.redhat.utils.Constants.CommonConstants.*; import static io.cloudslang.content.redhat.utils.Descriptions.Common.*; import static io.cloudslang.content.redhat.utils.Descriptions.GetTokenAction.AUTH_TOKEN_DESC; @@ -61,22 +60,22 @@ public class GetTokenAction { @Response(text = SUCCESS, field = RETURN_CODE, value = ReturnCodes.SUCCESS, matchType = COMPARE_EQUAL, responseType = RESOLVED, description = SUCCESS_DESC), @Response(text = FAILURE, field = RETURN_CODE, value = ReturnCodes.FAILURE, matchType = COMPARE_EQUAL, responseType = ERROR, description = FAILURE_DESC) }) - public Map execute(@Param(value = HOST, description = HOST_DESC) String host, - @Param(value = USERNAME, description = USERNAME_DESC) String username, - @Param(value = PASSWORD, encrypted = true, description = PASSWORD_DESC) String password, + public Map execute(@Param(value = HOST, required = true, description = HOST_DESC) String host, + @Param(value = USERNAME, required = true, description = USERNAME_DESC) String username, + @Param(value = PASSWORD, required = true, encrypted = true, description = PASSWORD_DESC) String password, @Param(value = PROXY_HOST, description = PROXY_HOST_DESC) String proxyHost, @Param(value = PROXY_PORT, description = PROXY_PORT_DESC) String proxyPort, @Param(value = PROXY_USERNAME, description = PROXY_USERNAME_DESC) String proxyUsername, - @Param(value = PROXY_PASSWORD, description = PROXY_PASSWORD_DESC) String proxyPassword, + @Param(value = PROXY_PASSWORD,encrypted = true, description = PROXY_PASSWORD_DESC) String proxyPassword, @Param(value = TLS_VERSION, description = TLS_VERSION_DESC) String tlsVersion, @Param(value = ALLOWED_CIPHERS, description = ALLOWED_CIPHERS_DESC) String allowedCyphers, @Param(value = TRUST_ALL_ROOTS, description = TRUST_ALL_ROOTS_DESC) String trustAllRoots, @Param(value = X509_HOSTNAME_VERIFIER, description = X509_HOSTNAME_VERIFIER_DESC) String x509HostnameVerifier, @Param(value = TRUST_KEYSTORE, description = TRUST_KEYSTORE_DESC) String trustKeystore, - @Param(value = TRUST_PASSWORD, description = TRUST_PASSWORD_DESC) String trustPassword, + @Param(value = TRUST_PASSWORD,encrypted = true, description = TRUST_PASSWORD_DESC) String trustPassword, @Param(value = KEYSTORE, description = KEYSTORE_DESC) String keystore, - @Param(value = KEYSTORE_PASSWORD, description = KEYSTORE_PASSWORD_DESC) String keystorePassword, + @Param(value = KEYSTORE_PASSWORD,encrypted = true, description = KEYSTORE_PASSWORD_DESC) String keystorePassword, @Param(value = CONNECT_TIMEOUT, description = CONNECT_TIMEOUT_DESC) String connectTimeout, @Param(value = EXECUTION_TIMEOUT, description = EXECUTION_TIMEOUT_DESC) String executionTimeout, @Param(value = KEEP_ALIVE, description = KEEP_ALIVE_DESC) String keepAlive, From 52c016ef80b33784aa60807d338e5764181f9c8b Mon Sep 17 00:00:00 2001 From: TodereanLenuta <77394653+TodereanLenuta@users.noreply.github.com> Date: Tue, 13 Dec 2022 16:12:02 +0200 Subject: [PATCH 13/16] Get Deployment Status - small refactoring. (#1117) --- ...usAction.java => GetDeploymentStatus.java} | 23 +++- .../redhat/services/OpenshiftService.java | 123 +++++++++--------- 2 files changed, 77 insertions(+), 69 deletions(-) rename cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/{GetDeploymentStatusAction.java => GetDeploymentStatus.java} (90%) diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetDeploymentStatusAction.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetDeploymentStatus.java similarity index 90% rename from cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetDeploymentStatusAction.java rename to cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetDeploymentStatus.java index e036bcc099..e23cc91eba 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetDeploymentStatusAction.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetDeploymentStatus.java @@ -14,7 +14,7 @@ import io.cloudslang.content.httpclient.actions.HttpClientGetAction; import io.cloudslang.content.utils.OutputUtilities; -import static io.cloudslang.content.redhat.services.OpenshiftService.processHttpGetDeploymentStatusResult; +import static io.cloudslang.content.redhat.services.OpenshiftService.*; import static io.cloudslang.content.redhat.utils.Constants.CommonConstants.*; import java.util.Map; @@ -24,6 +24,8 @@ import static io.cloudslang.content.redhat.utils.Descriptions.GetDeploymentStatus.STATUS_CODE_DESC; import static io.cloudslang.content.redhat.utils.Descriptions.GetDeploymentStatus.SUCCESS_DESC; import static io.cloudslang.content.redhat.utils.Descriptions.GetDeploymentStatus.RETURN_RESULT_DESC; +import static io.cloudslang.content.redhat.utils.Descriptions.GetPodList.FAILURE_RETURN_RESULT; +import static io.cloudslang.content.redhat.utils.Descriptions.GetPodList.NEGATIVE_RETURN_CODE; import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.*; import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.EXCEPTION; import static org.apache.commons.lang3.StringUtils.EMPTY; @@ -37,7 +39,7 @@ import static io.cloudslang.content.httpclient.utils.Inputs.HTTPInputs.SESSION_CONNECTION_POOL; import static io.cloudslang.content.httpclient.utils.Inputs.HTTPInputs.SESSION_COOKIES; -public class GetDeploymentStatusAction { +public class GetDeploymentStatus { @Action(name = GET_DEPLOYMENT_STATUS, description = GET_DEPLOYMENT_STATUS_DESC, @@ -54,7 +56,7 @@ public class GetDeploymentStatusAction { @Output(value = NAME_OUTPUT, description = NAME_OUTPUT_DESC), @Output(value = NAMESPACE_OUTPUT, description = NAMESPACE_OUTPUT_DESC), @Output(value = UID_OUTPUT, description = UID_OUTPUT_DESC), - //Specific outputs - general + @Output(value = OBSERVED_GENERATION_OUTPUT, description = OBSERVED_GENERATION_OUTPUT_DESC), @Output(value = REPLICAS_OUTPUT, description = REPLICAS_OUTPUT_DESC), @Output(value = UPDATED_REPLICAS_OUTPUT, description = UPDATED_REPLICAS_OUTPUT_DESC), @@ -73,8 +75,8 @@ public Map execute( @Param(value = HOST, description = HOST_DESC, required = true) String host, @Param(value = AUTH_TOKEN, description = AUTH_TOKEN_DESC, required = true) String authToken, //Specific inputs - @Param(value = NAME, description = NAME_DESC) String name, - @Param(value = NAMESPACE, description = NAMESPACE_DESC) String namespace, + @Param(value = NAME, description = NAME_DESC, required = true) String name, + @Param(value = NAMESPACE, description = NAMESPACE_DESC, required = true) String namespace, //Common Inputs @Param(value = PROXY_HOST, description = PROXY_HOST_DESC) String proxyHost, @Param(value = PROXY_PORT, description = PROXY_PORT_DESC) String proxyPort, @@ -136,7 +138,16 @@ public Map execute( sessionConnectionPool ); - processHttpGetDeploymentStatusResult(result); + if (Integer.parseInt(result.get(RETURN_CODE)) != -1) { + if (Integer.parseInt(result.get(STATUS_CODE)) >= 200 && Integer.parseInt(result.get(STATUS_CODE)) < 300) + processHttpGetDeploymentStatusResult(result); + else { + result.put(RETURN_CODE, NEGATIVE_RETURN_CODE); + result.put(EXCEPTION, result.get(RETURN_RESULT)); + result.put(RETURN_RESULT, FAILURE_RETURN_RESULT); + } + } + return result; } catch (Exception exception) { diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java index a52071da95..f01aa2e86e 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java @@ -80,7 +80,7 @@ public static void processAuthTokenResult(Map httpResults, HttpI String postHtmlResponse = test.get(RETURN_RESULT); Document second_response = Jsoup.parse(postHtmlResponse); - if (!StringUtils.isEmpty(postHtmlResponse)){ + if (!StringUtils.isEmpty(postHtmlResponse)) { httpResults.put(RETURN_RESULT, SUCCESS_DESC); httpResults.put(AUTH_TOKEN, second_response.select(CODE).get(0).text()); } @@ -97,6 +97,7 @@ public static void processAuthTokenResult(Map httpResults, HttpI httpResults.put(RETURN_CODE, NEGATIVE_RETURN_CODE); } } + public static void processHttpResult(Map httpResults) { String statusCode = httpResults.get(STATUS_CODE); @@ -108,7 +109,7 @@ public static void processHttpResult(Map httpResults) { } } - public static void addPodListResults(Map httpResults) { + public static void addPodListResults(Map httpResults) { try { String returnResult = httpResults.get(RETURN_RESULT); if (!(returnResult.isEmpty())) { @@ -131,7 +132,7 @@ public static void addPodListResults(Map httpResults) { } //remove the last comma from the podList value - podList.deleteCharAt(podList.length()-1); + podList.deleteCharAt(podList.length() - 1); //populate the podList and podArray outputs httpResults.put(POD_LIST, podList.toString()); @@ -140,7 +141,7 @@ public static void addPodListResults(Map httpResults) { //overwrite the returnResult output with a success message httpResults.put(RETURN_RESULT, SUCCESSFUL_RETURN_RESULT); - } + } } catch (Exception e) { //in case an error arises during the parsing, populate the custom outputs with empty values @@ -151,13 +152,13 @@ public static void addPodListResults(Map httpResults) { } - public static void setFailureCustomResults(Map httpResults){ + public static void setFailureCustomResults(Map httpResults) { httpResults.put(POD_LIST, EMPTY); httpResults.put(POD_ARRAY, EMPTY); httpResults.put(DOCUMENT_OUTPUT, EMPTY); } - public static void setFailureCommonResults(Map httpResults, Exception e){ + public static void setFailureCommonResults(Map httpResults, Exception e) { httpResults.put(RETURN_CODE, NEGATIVE_RETURN_CODE); httpResults.put(RETURN_RESULT, e.getMessage()); httpResults.put(EXCEPTION, ExceptionUtils.getStackTrace(e)); @@ -167,67 +168,63 @@ public static void processHttpGetDeploymentStatusResult(Map http //Process the return result output String returnResult = httpResults.get(RETURN_RESULT); + try { - if (!(returnResult.isEmpty())) { - httpResults.put(DOCUMENT_OUTPUT, returnResult); - - JsonObject jsonResponse = JsonParser.parseString(httpResults.get(RETURN_RESULT)).getAsJsonObject(); - - //Kind output - JsonPrimitive tmpResponse = (JsonPrimitive) jsonResponse.get(KIND_OUTPUT); - httpResults.put(KIND_OUTPUT, tmpResponse.toString()); - - //Name outputs - String namePath = "$.metadata.name"; - String namePathResponse = JsonPath.read(jsonResponse.toString(), namePath); - httpResults.put(NAME_OUTPUT, namePathResponse); - - //Namespace outputs - String namespacePath = "$.metadata.namespace"; - String namespacePathResponse = JsonPath.read(jsonResponse.toString(), namespacePath); - httpResults.put(NAMESPACE_OUTPUT, namespacePathResponse); - - //Uid outputs - String uidPath = "$.metadata.uid"; - String uidPathResponse = JsonPath.read(jsonResponse.toString(), uidPath); - httpResults.put(UID_OUTPUT, uidPathResponse); - - //ObservedGeneration output - String observedGenerationPath = "$.status.observedGeneration"; - Integer observedGenerationPathResponse = JsonPath.read(jsonResponse.toString(), observedGenerationPath); - httpResults.put(OBSERVED_GENERATION_OUTPUT, observedGenerationPathResponse.toString()); - - //Replicas output - String replicasPath = "$.status.replicas"; - Integer replicasPathResponse = JsonPath.read(jsonResponse.toString(), replicasPath); - httpResults.put(REPLICAS_OUTPUT, replicasPathResponse.toString()); - - //UpdatedReplicas output - String updatedReplicasPath = "$.status.updatedReplicas"; - Integer updatedReplicasPathResponse = JsonPath.read(jsonResponse.toString(), updatedReplicasPath); - httpResults.put(UPDATED_REPLICAS_OUTPUT, updatedReplicasPathResponse.toString()); - - //UnavailableReplicas output - String unavailableReplicasPath = "$.status.unavailableReplicas"; - Integer unavailableReplicasPathResponse = JsonPath.read(jsonResponse.toString(), unavailableReplicasPath); - httpResults.put(UNAVAILABLE_REPLICAS_OUTPUT, unavailableReplicasPathResponse.toString()); - - //Conditions output - String conditionsPath = "$.status.conditions"; - List conditionsPathResponse = JsonPath.read(jsonResponse.toString(), conditionsPath); - httpResults.put(CONDITIONS_OUTPUT, conditionsPathResponse.toString()); - - httpResults.put(RETURN_RESULT, RETURN_RESULT_MESSAGE_DESC); - } + if (!(returnResult.isEmpty())) { + httpResults.put(DOCUMENT_OUTPUT, returnResult); + JsonObject jsonResponse = JsonParser.parseString(httpResults.get(RETURN_RESULT)).getAsJsonObject(); - //Process HTTP status code - String statusCode = httpResults.get(STATUS_CODE); + //Kind output + JsonPrimitive tmpResponse = (JsonPrimitive) jsonResponse.get(KIND_OUTPUT); + httpResults.put(KIND_OUTPUT, tmpResponse.toString()); + + //Name outputs + String namePath = "$.metadata.name"; + String namePathResponse = JsonPath.read(jsonResponse.toString(), namePath); + httpResults.put(NAME_OUTPUT, namePathResponse); + + //Namespace outputs + String namespacePath = "$.metadata.namespace"; + String namespacePathResponse = JsonPath.read(jsonResponse.toString(), namespacePath); + httpResults.put(NAMESPACE_OUTPUT, namespacePathResponse); + + //Uid outputs + String uidPath = "$.metadata.uid"; + String uidPathResponse = JsonPath.read(jsonResponse.toString(), uidPath); + httpResults.put(UID_OUTPUT, uidPathResponse); + + //ObservedGeneration output + String observedGenerationPath = "$.status.observedGeneration"; + Integer observedGenerationPathResponse = JsonPath.read(jsonResponse.toString(), observedGenerationPath); + httpResults.put(OBSERVED_GENERATION_OUTPUT, observedGenerationPathResponse.toString()); + + //Replicas output + String replicasPath = "$.status.replicas"; + Integer replicasPathResponse = JsonPath.read(jsonResponse.toString(), replicasPath); + httpResults.put(REPLICAS_OUTPUT, replicasPathResponse.toString()); + + //UpdatedReplicas output + String updatedReplicasPath = "$.status.updatedReplicas"; + Integer updatedReplicasPathResponse = JsonPath.read(jsonResponse.toString(), updatedReplicasPath); + httpResults.put(UPDATED_REPLICAS_OUTPUT, updatedReplicasPathResponse.toString()); + + //UnavailableReplicas output + String unavailableReplicasPath = "$.status.unavailableReplicas"; + Integer unavailableReplicasPathResponse = JsonPath.read(jsonResponse.toString(), unavailableReplicasPath); + httpResults.put(UNAVAILABLE_REPLICAS_OUTPUT, unavailableReplicasPathResponse.toString()); + + //Conditions output + String conditionsPath = "$.status.conditions"; + List conditionsPathResponse = JsonPath.read(jsonResponse.toString(), conditionsPath); + httpResults.put(CONDITIONS_OUTPUT, conditionsPathResponse.toString()); + + httpResults.put(RETURN_RESULT, RETURN_RESULT_MESSAGE_DESC); + } - if (StringUtils.isEmpty(statusCode) || Integer.parseInt(statusCode) < 200 || Integer.parseInt(statusCode) >= 300) { - if (StringUtils.isEmpty(httpResults.get(EXCEPTION))) - httpResults.put(EXCEPTION, httpResults.get(RETURN_RESULT)); - httpResults.put(RETURN_CODE, NEGATIVE_RETURN_CODE); + } catch (Exception e) { + throw new RuntimeException(e); } } + } From 9d5c8168a3753192c630ac76a6ab717a97634a0f Mon Sep 17 00:00:00 2001 From: Florin Anca Date: Wed, 14 Dec 2022 11:08:16 +0200 Subject: [PATCH 14/16] Updated and grouped some constants and descriptions (#1120) Made the authToken input encrypted Added the CreatePod and DeletePod operations Co-authored-by: Florin --- .../redhat/actions/CreateDeployment.java | 6 +- .../content/redhat/actions/CreatePod.java | 121 ++++++++++++++++++ .../redhat/actions/DeleteDeployment.java | 4 +- .../content/redhat/actions/DeletePod.java | 109 ++++++++++++++++ .../redhat/actions/GetDeploymentStatus.java | 3 +- .../content/redhat/actions/GetPodList.java | 4 +- .../content/redhat/utils/Constants.java | 17 ++- .../content/redhat/utils/Descriptions.java | 49 ++++--- 8 files changed, 284 insertions(+), 29 deletions(-) create mode 100644 cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/CreatePod.java create mode 100644 cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/DeletePod.java diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/CreateDeployment.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/CreateDeployment.java index e0e64d71a9..f16ed5cfd4 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/CreateDeployment.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/CreateDeployment.java @@ -59,8 +59,8 @@ public class CreateDeployment { @Response(text = ResponseNames.FAILURE, field = RETURN_CODE, value = ReturnCodes.FAILURE, matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.ERROR, isOnFail = true) }) public Map execute( - @Param(value = HOST, description = HOST_DESC, required = true) String hostName, - @Param(value = AUTH_TOKEN, description = AUTH_TOKEN_DESCRIPTION, required = true) String authToken, + @Param(value = HOST, description = HOST_DESC, required = true) String host, + @Param(value = AUTH_TOKEN, description = AUTH_TOKEN_DESCRIPTION, required = true, encrypted = true) String authToken, @Param(value = DEFINITION, description = DEFINITION_DESCRIPTION, required = true) String body, @Param(value = NAMESPACE, description = NAMESPACE_DESCRIPTION, required = true) String namespace, @@ -87,7 +87,7 @@ public Map execute( try { Map result = new HttpClientPostAction().execute( - hostName + APPS_V1_NAMESPACES + namespace + "/deployments", + host + APPS_V1_NAMESPACES + namespace + DEPLOYMENTS, ANONYMOUS, EMPTY, EMPTY, diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/CreatePod.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/CreatePod.java new file mode 100644 index 0000000000..d514f9cc1d --- /dev/null +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/CreatePod.java @@ -0,0 +1,121 @@ +package io.cloudslang.content.redhat.actions; + +import com.hp.oo.sdk.content.annotations.Action; +import com.hp.oo.sdk.content.annotations.Output; +import com.hp.oo.sdk.content.annotations.Param; +import com.hp.oo.sdk.content.annotations.Response; +import com.hp.oo.sdk.content.plugin.ActionMetadata.MatchType; +import com.hp.oo.sdk.content.plugin.ActionMetadata.ResponseType; +import com.hp.oo.sdk.content.plugin.GlobalSessionObject; +import com.hp.oo.sdk.content.plugin.SerializableSessionObject; +import io.cloudslang.content.constants.ResponseNames; +import io.cloudslang.content.constants.ReturnCodes; +import io.cloudslang.content.httpclient.actions.HttpClientPostAction; +import io.cloudslang.content.utils.OutputUtilities; + +import java.util.Map; + +import static io.cloudslang.content.constants.OutputNames.RETURN_CODE; +import static io.cloudslang.content.constants.OutputNames.RETURN_RESULT; +import static io.cloudslang.content.httpclient.utils.Descriptions.HTTPClient.SESSION_CONNECTION_POOL_DESC; +import static io.cloudslang.content.httpclient.utils.Descriptions.HTTPClient.SESSION_COOKIES_DESC; +import static io.cloudslang.content.redhat.services.OpenshiftService.processHttpResult; +import static io.cloudslang.content.redhat.utils.Constants.CommonConstants.*; +import static io.cloudslang.content.redhat.utils.Descriptions.Common.*; +import static io.cloudslang.content.redhat.utils.Descriptions.CreatePod.*; +import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.EXCEPTION; +import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.STATUS_CODE; +import static org.apache.commons.lang3.StringUtils.EMPTY; + +public class CreatePod { + + @Action(name = CREATE_POD, + description = CREATE_POD_DESCRIPTION, + outputs = { + @Output(value = RETURN_RESULT, description = RETURN_RESULT_DESC), + @Output(value = RETURN_CODE, description = RETURN_CODE_DESC), + @Output(value = EXCEPTION, description = EXCEPTION_DESC), + @Output(value = STATUS_CODE, description = STATUS_CODE_DESC) + }, + responses = { + @Response(text = ResponseNames.SUCCESS, field = RETURN_CODE, value = ReturnCodes.SUCCESS, matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.RESOLVED, description = SUCCESS_DESC), + @Response(text = ResponseNames.FAILURE, field = RETURN_CODE, value = ReturnCodes.FAILURE, matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.ERROR, isOnFail = true, description = FAILURE_DESC) + }) + public Map execute( + @Param(value = HOST, description = HOST_DESC, required = true) String host, + @Param(value = AUTH_TOKEN, description = AUTH_TOKEN_DESCRIPTION, required = true, encrypted = true) String authToken, + @Param(value = SPECIFICATION, description = SPEC_DESCRIPTION, required = true) String body, + @Param(value = NAMESPACE, description = NAMESPACE_DESCRIPTION, required = true) String namespace, + + @Param(value = PROXY_HOST, description = PROXY_HOST_DESC) String proxyHost, + @Param(value = PROXY_PORT, description = PROXY_PORT_DESC) String proxyPort, + @Param(value = PROXY_USERNAME, description = PROXY_USERNAME_DESC) String proxyUsername, + @Param(value = PROXY_PASSWORD, encrypted = true, description = PROXY_PASSWORD_DESC) String proxyPassword, + @Param(value = TLS_VERSION, description = TLS_VERSION_DESC) String tlsVersion, + @Param(value = ALLOWED_CIPHERS, description = ALLOWED_CIPHERS_DESC) String allowedCiphers, + @Param(value = TRUST_ALL_ROOTS, description = TRUST_ALL_ROOTS_DESC) String trustAllRoots, + @Param(value = X509_HOSTNAME_VERIFIER, description = X509_HOSTNAME_VERIFIER_DESC) String x509HostnameVerifier, + @Param(value = TRUST_KEYSTORE, description = TRUST_KEYSTORE_DESC) String trustKeystore, + @Param(value = TRUST_PASSWORD, encrypted = true, description = TRUST_PASSWORD_DESC) String trustPassword, + @Param(value = KEYSTORE, description = KEYSTORE_DESC) String keystore, + @Param(value = KEYSTORE_PASSWORD, encrypted = true, description = KEYSTORE_PASSWORD_DESC) String keystorePassword, + @Param(value = CONNECT_TIMEOUT, description = CONNECT_TIMEOUT_DESC) String connectTimeout, + @Param(value = EXECUTION_TIMEOUT, description = EXECUTION_TIMEOUT_DESC) String executionTimeout, + @Param(value = KEEP_ALIVE, description = KEEP_ALIVE_DESC) String keepAlive, + @Param(value = CONNECTIONS_MAX_PER_ROUTE, description = CONNECTIONS_MAX_PER_ROUTE_DESC) String connectionsMaxPerRoute, + @Param(value = CONNECTIONS_MAX_TOTAL, description = CONNECTIONS_MAX_TOTAL_DESC) String connectionsMaxTotal, + @Param(value = SESSION_COOKIES, description = SESSION_COOKIES_DESC) SerializableSessionObject sessionCookies, + @Param(value = SESSION_CONNECTION_POOL, description = SESSION_CONNECTION_POOL_DESC) GlobalSessionObject sessionConnectionPool) { + + try { + + Map result = new HttpClientPostAction().execute( + host + POD_ENDPOINT_1 + namespace + POD_ENDPOINT_2, + ANONYMOUS, + EMPTY, + EMPTY, + TRUE, + proxyHost, + proxyPort, + proxyUsername, + proxyPassword, + tlsVersion, + allowedCiphers, + trustAllRoots, + x509HostnameVerifier, + trustKeystore, + trustPassword, + keystore, + keystorePassword, + keepAlive, + connectionsMaxPerRoute, + connectionsMaxTotal, + EMPTY, + EMPTY, + AUTHORIZATION_BEARER + authToken, + EMPTY, + EMPTY, + EMPTY, + EMPTY, + EMPTY, + EMPTY, + EMPTY, + EMPTY, + body, + APPLICATION_JSON, + EMPTY, + connectTimeout, + EMPTY, + executionTimeout, + sessionCookies, + sessionConnectionPool + ); + + processHttpResult(result); + return result; + + } catch (Exception exception) { + return OutputUtilities.getFailureResultsMap(exception); + } + } +} diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/DeleteDeployment.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/DeleteDeployment.java index 1ff390ec0f..05cd3c743a 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/DeleteDeployment.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/DeleteDeployment.java @@ -57,7 +57,7 @@ public class DeleteDeployment { @Response(text = FAILURE, field = RETURN_CODE, value = ReturnCodes.FAILURE, matchType = COMPARE_EQUAL, responseType = ERROR, description = FAILURE_DESC) }) public Map execute(@Param(value = HOST, required = true, description = HOST_DESC) String host, - @Param(value = AUTH_TOKEN, required = true, description = AUTH_TOKEN_DESC) String authToken, + @Param(value = AUTH_TOKEN, required = true, description = AUTH_TOKEN_DESC, encrypted = true) String authToken, @Param(value = NAMESPACE, required = true, description = NAMESPACE_DESC) String namespace, @Param(value = DEPLOYMENT, required = true, description = DEPLOYMENT_DESC) String deployment, @@ -83,7 +83,7 @@ public Map execute(@Param(value = HOST, required = true, descrip try { Map result = new HttpClientDeleteAction().execute( - host + APPS_V1_NAMESPACES + namespace + DEPLOYMENTS + deployment, + host + APPS_V1_NAMESPACES + namespace + DEPLOYMENTS + FORWARD_SLASH + deployment, ANONYMOUS, EMPTY_STRING, EMPTY_STRING, diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/DeletePod.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/DeletePod.java new file mode 100644 index 0000000000..11d62cb79f --- /dev/null +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/DeletePod.java @@ -0,0 +1,109 @@ +package io.cloudslang.content.redhat.actions; + +import com.hp.oo.sdk.content.annotations.Action; +import com.hp.oo.sdk.content.annotations.Output; +import com.hp.oo.sdk.content.annotations.Param; +import com.hp.oo.sdk.content.annotations.Response; +import com.hp.oo.sdk.content.plugin.GlobalSessionObject; +import com.hp.oo.sdk.content.plugin.SerializableSessionObject; +import io.cloudslang.content.constants.ReturnCodes; +import io.cloudslang.content.httpclient.actions.HttpClientDeleteAction; +import io.cloudslang.content.utils.OutputUtilities; + +import java.util.Map; + +import static com.hp.oo.sdk.content.plugin.ActionMetadata.MatchType.COMPARE_EQUAL; +import static com.hp.oo.sdk.content.plugin.ActionMetadata.ResponseType.ERROR; +import static com.hp.oo.sdk.content.plugin.ActionMetadata.ResponseType.RESOLVED; +import static io.cloudslang.content.constants.OutputNames.*; +import static io.cloudslang.content.constants.OutputNames.RETURN_CODE; +import static io.cloudslang.content.constants.ResponseNames.FAILURE; +import static io.cloudslang.content.constants.ResponseNames.SUCCESS; +import static io.cloudslang.content.httpclient.utils.Descriptions.HTTPClient.SESSION_CONNECTION_POOL_DESC; +import static io.cloudslang.content.httpclient.utils.Descriptions.HTTPClient.SESSION_COOKIES_DESC; +import static io.cloudslang.content.redhat.services.OpenshiftService.processHttpResult; +import static io.cloudslang.content.redhat.utils.Constants.CommonConstants.*; +import static io.cloudslang.content.redhat.utils.Descriptions.Common.*; +import static io.cloudslang.content.redhat.utils.Descriptions.DeletePod.*; +import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.AUTH_TOKEN; +import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.STATUS_CODE; + +public class DeletePod { + + @Action(name = DELETE_POD, + description = DELETE_POD_DESCRIPTION, + outputs = { + @Output(value = RETURN_RESULT, description = RETURN_RESULT_DESC), + @Output(value = RETURN_CODE, description = RETURN_CODE_DESC), + @Output(value = EXCEPTION, description = EXCEPTION_DESC), + @Output(value = STATUS_CODE, description = STATUS_CODE_DESC) + }, + responses = { + @Response(text = SUCCESS, field = RETURN_CODE, value = ReturnCodes.SUCCESS, matchType = COMPARE_EQUAL, responseType = RESOLVED, description = SUCCESS_DESC), + @Response(text = FAILURE, field = RETURN_CODE, value = ReturnCodes.FAILURE, matchType = COMPARE_EQUAL, responseType = ERROR, description = FAILURE_DESC) + }) + public Map execute(@Param(value = HOST, required = true, description = HOST_DESC) String host, + @Param(value = AUTH_TOKEN, required = true, description = AUTH_TOKEN_DESCRIPTION, encrypted = true) String authToken, + @Param(value = NAMESPACE, required = true, description = NAMESPACE_DESCRIPTION) String namespace, + @Param(value = POD_NAME, required = true, description = POD_NAME_DESCRIPTION) String podName, + + @Param(value = PROXY_HOST, description = PROXY_HOST_DESC) String proxyHost, + @Param(value = PROXY_PORT, description = PROXY_PORT_DESC) String proxyPort, + @Param(value = PROXY_USERNAME, description = PROXY_USERNAME_DESC) String proxyUsername, + @Param(value = PROXY_PASSWORD, encrypted = true, description = PROXY_PASSWORD_DESC) String proxyPassword, + @Param(value = TLS_VERSION, description = TLS_VERSION_DESC) String tlsVersion, + @Param(value = ALLOWED_CIPHERS, description = ALLOWED_CIPHERS_DESC) String allowedCyphers, + @Param(value = TRUST_ALL_ROOTS, description = TRUST_ALL_ROOTS_DESC) String trustAllRoots, + @Param(value = X509_HOSTNAME_VERIFIER, description = X509_HOSTNAME_VERIFIER_DESC) String x509HostnameVerifier, + @Param(value = TRUST_KEYSTORE, description = TRUST_KEYSTORE_DESC) String trustKeystore, + @Param(value = TRUST_PASSWORD, encrypted = true, description = TRUST_PASSWORD_DESC) String trustPassword, + @Param(value = KEYSTORE, description = KEYSTORE_DESC) String keystore, + @Param(value = KEYSTORE_PASSWORD, encrypted = true, description = KEYSTORE_PASSWORD_DESC) String keystorePassword, + @Param(value = CONNECT_TIMEOUT, description = CONNECT_TIMEOUT_DESC) String connectTimeout, + @Param(value = EXECUTION_TIMEOUT, description = EXECUTION_TIMEOUT_DESC) String executionTimeout, + @Param(value = KEEP_ALIVE, description = KEEP_ALIVE_DESC) String keepAlive, + @Param(value = CONNECTIONS_MAX_PER_ROUTE, description = CONNECTIONS_MAX_PER_ROUTE_DESC) String connectionsMaxPerRoute, + @Param(value = CONNECTIONS_MAX_TOTAL, description = CONNECTIONS_MAX_TOTAL_DESC) String connectionsMaxTotal, + @Param(value = SESSION_COOKIES, description = SESSION_COOKIES_DESC) SerializableSessionObject sessionCookies, + @Param(value = SESSION_CONNECTION_POOL, description = SESSION_CONNECTION_POOL_DESC) GlobalSessionObject sessionConnectionPool) { + try { + + Map result = new HttpClientDeleteAction().execute( + host + POD_ENDPOINT_1 + namespace + POD_ENDPOINT_2 + FORWARD_SLASH + podName, + ANONYMOUS, + EMPTY_STRING, + EMPTY_STRING, + TRUE, + proxyHost, + proxyPort, + proxyUsername, + proxyPassword, + tlsVersion, + allowedCyphers, + trustAllRoots, + x509HostnameVerifier, + trustKeystore, + trustPassword, + keystore, + keystorePassword, + keepAlive, + connectionsMaxPerRoute, + connectionsMaxTotal, + TRUE, + AUTHORIZATION_BEARER + authToken, + EMPTY_STRING, + EMPTY_STRING, + connectTimeout, + EMPTY_STRING, + executionTimeout, + sessionCookies, + sessionConnectionPool); + + processHttpResult(result); + return result; + } catch (Exception exception) { + return OutputUtilities.getFailureResultsMap(exception); + } + } + +} diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetDeploymentStatus.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetDeploymentStatus.java index e23cc91eba..79210c361c 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetDeploymentStatus.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetDeploymentStatus.java @@ -25,7 +25,6 @@ import static io.cloudslang.content.redhat.utils.Descriptions.GetDeploymentStatus.SUCCESS_DESC; import static io.cloudslang.content.redhat.utils.Descriptions.GetDeploymentStatus.RETURN_RESULT_DESC; import static io.cloudslang.content.redhat.utils.Descriptions.GetPodList.FAILURE_RETURN_RESULT; -import static io.cloudslang.content.redhat.utils.Descriptions.GetPodList.NEGATIVE_RETURN_CODE; import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.*; import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.EXCEPTION; import static org.apache.commons.lang3.StringUtils.EMPTY; @@ -73,7 +72,7 @@ public class GetDeploymentStatus { public Map execute( //Common Inputs @Param(value = HOST, description = HOST_DESC, required = true) String host, - @Param(value = AUTH_TOKEN, description = AUTH_TOKEN_DESC, required = true) String authToken, + @Param(value = AUTH_TOKEN, description = AUTH_TOKEN_DESC, required = true, encrypted = true) String authToken, //Specific inputs @Param(value = NAME, description = NAME_DESC, required = true) String name, @Param(value = NAMESPACE, description = NAMESPACE_DESC, required = true) String namespace, diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetPodList.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetPodList.java index 9b32a660d7..a34c34e4c3 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetPodList.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetPodList.java @@ -56,7 +56,7 @@ public class GetPodList { public Map execute( //Specific input @Param(value = HOST, description = HOST_DESC, required = true) String host, - @Param(value = AUTH_TOKEN, description = AUTH_TOKEN_DESC, required = true) String authToken, + @Param(value = AUTH_TOKEN, description = AUTH_TOKEN_DESC, required = true, encrypted = true) String authToken, @Param(value = NAMESPACE, description = NAMESPACE_DESC, required = true) String namespace, //Common Inputs @Param(value = PROXY_HOST, description = PROXY_HOST_DESC) String proxyHost, @@ -84,7 +84,7 @@ public Map execute( try { result = new HttpClientGetAction().execute( - host + GET_POD_LIST_ENDPOINT_1 + namespace + GET_POD_LIST_ENDPOINT_2, + host + POD_ENDPOINT_1 + namespace + POD_ENDPOINT_2, ANONYMOUS, EMPTY, EMPTY, diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java index d867b779b1..9de06be090 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java @@ -88,7 +88,7 @@ public static final String CREATE_DEPLOYMENT_ENDPOINT(String namespace) { } public static final String APPS_V1_NAMESPACES = "/apis/apps/v1/namespaces/"; - public static final String DEPLOYMENTS = "/deployments/"; + public static final String DEPLOYMENTS = "/deployments"; //query for authorization public static final String QUERY_PARAM = "response_type=code&client_id=openshift-browser-client"; @@ -101,6 +101,21 @@ public static final String CREATE_DEPLOYMENT_ENDPOINT(String namespace) { public static final String CONTENT_TYPE_FORM = "application/x-www-form-urlencoded"; public static final String DISPLAY_TOKEN_ENDPOINT = "/oauth/token/display"; public static final String COLON_PUNCTUATION = ":"; + + public static final String GET_DEPLOYMENT_STATUS_ENDPOINT_1 = "/apis/apps/v1/namespaces/"; + public static final String GET_DEPLOYMENT_STATUS_ENDPOINT_2 = "/deployments/"; + public static final String GET_DEPLOYMENT_STATUS_ENDPOINT_3 = "/status"; + + public static final String POD_ENDPOINT_1 = "/api/v1/namespaces/"; + public static final String POD_ENDPOINT_2 = "/pods"; + public static final String NEGATIVE_RETURN_CODE = "-1"; + + public static final String PROPERTY_NAME = "name"; + public static final String PROPERTY_UID = "uid"; + public static final String PROPERTY_ITEMS = "items"; + public static final String PROPERTY_METADATA = "metadata"; + public static final String SPECIFICATION = "specification"; + public static final String POD_NAME = "podName"; } } diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java index 0a645b7c7d..4739df5250 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java @@ -22,7 +22,8 @@ public static class Common { public static final String RETURN_CODE_DESC = "0 if success, -1 if failure."; public static final String STATUS_CODE_DESC = "The HTTP status code for Openshift API request."; - public static final String HOST_DESC = "The OpenShift hostname."; + public static final String HOST_DESC = "The url of the service to which API calls are made.\n" + + "Example: https://api.domain:6443"; public static final String AUTH_TOKEN_DESC = "Token used to authenticate to the Openshift environment."; public static final String PROXY_HOST_DESC = "The proxy server used to access the web site."; public static final String PROXY_PORT_DESC = "The proxy server port." + @@ -100,7 +101,7 @@ public static class CreateDeploymentAction { public static final String AUTH_TOKEN_DESCRIPTION = "Token used to authenticate to the openshift environment."; public static final String NAMESPACE_DESCRIPTION = "The namespace in which the deployment will be created."; public static final String DEFINITION_DESCRIPTION = "YAML or JSON definitions."; - public static final String RETURN_RESULT_DESC = "The authorization token for Openshift."; + public static final String RETURN_RESULT_DESC = "The created deployment in case of success or a comprehensive message in case of failure."; public static final String EXCEPTION_DESC = "An error message in case there was an error"; public static final String SUCCESS_DESC = "Deployment created successfully."; public static final String FAILURE_DESC = "There was an error while trying to create the deployment."; @@ -158,11 +159,6 @@ public static class GetDeploymentStatus { public static final String DOCUMENT_OUTPUT_DESC = "All the information related to a specific deployment in the " + "json format."; - //API - public static final String GET_DEPLOYMENT_STATUS_ENDPOINT_1 = "/apis/apps/v1/namespaces/"; - public static final String GET_DEPLOYMENT_STATUS_ENDPOINT_2 = "/deployments/"; - public static final String GET_DEPLOYMENT_STATUS_ENDPOINT_3 = "/status"; - } public static class GetPodList { @@ -176,19 +172,34 @@ public static class GetPodList { public static final String SUCCESS_DESC = "The retrieval of the pod list was made successfully."; public static final String FAILURE_DESC = "There was an error while trying to retrieve the pod list."; public static final String POD_LIST_DESC = "The comma separated list of pod uids."; - public static final String POD_ARRAY_DESC = "The list containing pairs of pod name and uids."; - - public static final String DOCUMENT_OUTPUT_DESC = "All the information related to a the pod list in json format."; - - //API - public static final String GET_POD_LIST_ENDPOINT_1 = "/api/v1/namespaces/"; - public static final String GET_POD_LIST_ENDPOINT_2 = "/pods"; - public static final String NEGATIVE_RETURN_CODE = "-1"; + public static final String POD_ARRAY_DESC = "The list containing pairs of pod name and uids in JSON format."; + public static final String DOCUMENT_OUTPUT_DESC = "All the information related to the pod list in json format."; public static final String SUCCESSFUL_RETURN_RESULT = "The Pod list was returned successfully."; public static final String FAILURE_RETURN_RESULT = "An issue was encountered during your query. Please check the exception output for more information."; - public static final String PROPERTY_NAME = "name"; - public static final String PROPERTY_UID = "uid"; - public static final String PROPERTY_ITEMS = "items"; - public static final String PROPERTY_METADATA = "metadata"; + + } + + public static class CreatePod { + public static final String CREATE_POD = "Create Pod"; + public static final String CREATE_POD_DESCRIPTION = "This operation creates a new pod with the inputs from the user."; + public static final String AUTH_TOKEN_DESCRIPTION = "Token used to authenticate to the openshift environment."; + public static final String NAMESPACE_DESCRIPTION = "The namespace in which the pod will be created."; + public static final String SPEC_DESCRIPTION = "The pod specification in JSON format."; + public static final String RETURN_RESULT_DESC = "The created pod in case of success or a comprehensive message in case of failure."; + public static final String EXCEPTION_DESC = "An error message in case there was an error while creating the pod."; + public static final String SUCCESS_DESC = "The pod was created successfully."; + public static final String FAILURE_DESC = "There was an error while creating the pod."; + } + + public static class DeletePod { + public static final String DELETE_POD = "Delete Pod"; + public static final String DELETE_POD_DESCRIPTION = "This operation deletes a pod from a namespace."; + public static final String AUTH_TOKEN_DESCRIPTION = "Token used to authenticate to the openshift environment."; + public static final String NAMESPACE_DESCRIPTION = "The namespace from which to delete the pod."; + public static final String POD_NAME_DESCRIPTION = "Name of the pod to delete."; + public static final String RETURN_RESULT_DESC = "A suggestive message in case of success or failure."; + public static final String EXCEPTION_DESC = "An error message in case there was an error while deleting the pod."; + public static final String SUCCESS_DESC = "The pod was deleted successfully."; + public static final String FAILURE_DESC = "There was an error while deleting the pod."; } } From 716a805c9369b914d72a320fd346ac0d55bc4d14 Mon Sep 17 00:00:00 2001 From: Florin Anca Date: Wed, 14 Dec 2022 11:28:13 +0200 Subject: [PATCH 15/16] Get route list (#1121) * Updated and grouped some constants and descriptions Made the authToken input encrypted Added the CreatePod and DeletePod operations * Added the GetRouteList operation refactored code to be easier to reuse added extra error handling validation Co-authored-by: Florin --- .../content/redhat/actions/GetPodList.java | 4 +- .../content/redhat/actions/GetRouteList.java | 140 ++++++++++++++++++ .../redhat/services/OpenshiftService.java | 88 ++++++++--- .../content/redhat/utils/Constants.java | 3 + .../content/redhat/utils/Descriptions.java | 19 ++- .../content/redhat/utils/Outputs.java | 5 + 6 files changed, 232 insertions(+), 27 deletions(-) create mode 100644 cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetRouteList.java diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetPodList.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetPodList.java index a34c34e4c3..71c86e43b1 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetPodList.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetPodList.java @@ -123,14 +123,14 @@ public Map execute( if (Integer.parseInt(result.get(STATUS_CODE)) >= 200 && Integer.parseInt(result.get(STATUS_CODE)) < 300) addPodListResults(result); else { - setFailureCustomResults(result); + setFailureCustomResults(result, POD_LIST, POD_ARRAY, DOCUMENT_OUTPUT); result.put(RETURN_CODE, NEGATIVE_RETURN_CODE); result.put(EXCEPTION, result.get(RETURN_RESULT)); result.put(RETURN_RESULT, FAILURE_RETURN_RESULT); } } else - setFailureCustomResults(result); + setFailureCustomResults(result, POD_LIST, POD_ARRAY, DOCUMENT_OUTPUT); return result; } catch (Exception exception) { diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetRouteList.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetRouteList.java new file mode 100644 index 0000000000..39b6b1c2ac --- /dev/null +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetRouteList.java @@ -0,0 +1,140 @@ +package io.cloudslang.content.redhat.actions; + +import com.hp.oo.sdk.content.annotations.Action; +import com.hp.oo.sdk.content.annotations.Output; +import com.hp.oo.sdk.content.annotations.Param; +import com.hp.oo.sdk.content.annotations.Response; +import com.hp.oo.sdk.content.plugin.ActionMetadata.MatchType; +import com.hp.oo.sdk.content.plugin.ActionMetadata.ResponseType; +import com.hp.oo.sdk.content.plugin.GlobalSessionObject; +import com.hp.oo.sdk.content.plugin.SerializableSessionObject; +import io.cloudslang.content.constants.OutputNames; +import io.cloudslang.content.constants.ResponseNames; +import io.cloudslang.content.constants.ReturnCodes; +import io.cloudslang.content.httpclient.actions.HttpClientGetAction; + +import java.util.HashMap; +import java.util.Map; + +import static io.cloudslang.content.constants.OutputNames.RETURN_CODE; +import static io.cloudslang.content.constants.OutputNames.RETURN_RESULT; +import static io.cloudslang.content.httpclient.utils.Descriptions.HTTPClient.SESSION_CONNECTION_POOL_DESC; +import static io.cloudslang.content.httpclient.utils.Descriptions.HTTPClient.SESSION_COOKIES_DESC; +import static io.cloudslang.content.redhat.services.OpenshiftService.*; +import static io.cloudslang.content.redhat.utils.Constants.CommonConstants.*; +import static io.cloudslang.content.redhat.utils.Constants.CommonConstants.AUTH_TOKEN; +import static io.cloudslang.content.redhat.utils.Descriptions.Common.*; +import static io.cloudslang.content.redhat.utils.Descriptions.Common.CONNECTIONS_MAX_TOTAL_DESC; +import static io.cloudslang.content.redhat.utils.Descriptions.GetRouteList.*; +import static io.cloudslang.content.redhat.utils.Descriptions.GetPodList.FAILURE_RETURN_RESULT; +import static io.cloudslang.content.redhat.utils.Descriptions.GetRouteList.STATUS_CODE_DESC; +import static io.cloudslang.content.redhat.utils.Outputs.OutputNames.*; +import static org.apache.commons.lang3.StringUtils.EMPTY; + +public class GetRouteList { + + @Action(name = GET_ROUTE_LIST, + description = GET_ROUTE_LIST_DESC, + outputs = { + @Output(value = RETURN_RESULT, description = RETURN_RESULT_DESC), + @Output(value = STATUS_CODE, description = STATUS_CODE_DESC), + @Output(value = RETURN_CODE, description = RETURN_CODE_DESC), + @Output(value = EXCEPTION, description = EXCEPTION_DESC), + @Output(value = DOCUMENT_OUTPUT, description = DOCUMENT_OUTPUT_DESC), + @Output(value = ROUTE_LIST, description = ROUTE_LIST_DESC), + @Output(value = ROUTE_ARRAY, description = ROUTE_ARRAY_DESC), + }, + responses = { + @Response(text = ResponseNames.SUCCESS, field = OutputNames.RETURN_CODE, value = ReturnCodes.SUCCESS, + matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.RESOLVED, description = SUCCESS_DESC), + @Response(text = ResponseNames.FAILURE, field = OutputNames.RETURN_CODE, value = ReturnCodes.FAILURE, + matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.ERROR, description = FAILURE_DESC) + }) + + public Map execute( + //Specific input + @Param(value = HOST, description = HOST_DESC, required = true) String host, + @Param(value = AUTH_TOKEN, description = AUTH_TOKEN_DESC, required = true, encrypted = true) String authToken, + @Param(value = NAMESPACE, description = NAMESPACE_DESC, required = true) String namespace, + //Common Inputs + @Param(value = PROXY_HOST, description = PROXY_HOST_DESC) String proxyHost, + @Param(value = PROXY_PORT, description = PROXY_PORT_DESC) String proxyPort, + @Param(value = PROXY_USERNAME, description = PROXY_USERNAME_DESC) String proxyUsername, + @Param(value = PROXY_PASSWORD, encrypted = true, description = PROXY_PASSWORD_DESC) String proxyPassword, + @Param(value = TLS_VERSION, description = TLS_VERSION_DESC) String tlsVersion, + @Param(value = ALLOWED_CIPHERS, description = ALLOWED_CIPHERS_DESC) String allowedCiphers, + @Param(value = TRUST_ALL_ROOTS, description = TRUST_ALL_ROOTS_DESC) String trustAllRoots, + @Param(value = X509_HOSTNAME_VERIFIER, description = X509_HOSTNAME_VERIFIER_DESC) String x509HostnameVerifier, + @Param(value = TRUST_KEYSTORE, description = TRUST_KEYSTORE_DESC) String trustKeystore, + @Param(value = TRUST_PASSWORD, encrypted = true, description = TRUST_PASSWORD_DESC) String trustPassword, + @Param(value = KEYSTORE, description = KEYSTORE_DESC) String keystore, + @Param(value = KEYSTORE_PASSWORD, encrypted = true, description = KEYSTORE_PASSWORD_DESC) String keystorePassword, + @Param(value = CONNECT_TIMEOUT, description = CONNECT_TIMEOUT_DESC) String connectTimeout, + @Param(value = EXECUTION_TIMEOUT, description = EXECUTION_TIMEOUT_DESC) String executionTimeout, + @Param(value = KEEP_ALIVE, description = KEEP_ALIVE_DESC) String keepAlive, + @Param(value = CONNECTIONS_MAX_PER_ROUTE, description = CONNECTIONS_MAX_PER_ROUTE_DESC) String connectionsMaxPerRoute, + @Param(value = CONNECTIONS_MAX_TOTAL, description = CONNECTIONS_MAX_TOTAL_DESC) String connectionsMaxTotal, + @Param(value = SESSION_COOKIES, description = SESSION_COOKIES_DESC) SerializableSessionObject sessionCookies, + @Param(value = SESSION_CONNECTION_POOL, description = SESSION_CONNECTION_POOL_DESC) GlobalSessionObject sessionConnectionPool) { + + Map result = new HashMap<>(); + + try { + + result = new HttpClientGetAction().execute( + host + ROUTE_ENDPOINT_1 + namespace + ROUTE_ENDPOINT_2, + ANONYMOUS, + EMPTY, + EMPTY, + EMPTY, + proxyHost, + proxyPort, + proxyUsername, + proxyPassword, + tlsVersion, + allowedCiphers, + trustAllRoots, + x509HostnameVerifier, + trustKeystore, + trustPassword, + keystore, + keystorePassword, + keepAlive, + connectionsMaxPerRoute, + connectionsMaxTotal, + EMPTY, + EMPTY, + AUTHORIZATION_BEARER + authToken, + EMPTY, + EMPTY, + EMPTY, + EMPTY, + EMPTY, + connectTimeout, + EMPTY, + executionTimeout, + sessionCookies, + sessionConnectionPool + ); + + if (Integer.parseInt(result.get(RETURN_CODE)) != -1) { + if (Integer.parseInt(result.get(STATUS_CODE)) >= 200 && Integer.parseInt(result.get(STATUS_CODE)) < 300) + addRouteListResults(result); + else { + setFailureCustomResults(result, DOCUMENT_OUTPUT, ROUTE_LIST, ROUTE_ARRAY); + result.put(RETURN_CODE, NEGATIVE_RETURN_CODE); + result.put(EXCEPTION, result.get(RETURN_RESULT)); + result.put(RETURN_RESULT, FAILURE_RETURN_RESULT); + } + + } else + setFailureCustomResults(result, DOCUMENT_OUTPUT, ROUTE_LIST, ROUTE_ARRAY); + return result; + + } catch (Exception exception) { + setFailureCommonResults(result, exception); + return result; + } + } + +} diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java index f01aa2e86e..49eb991f24 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java @@ -7,6 +7,7 @@ import io.cloudslang.content.redhat.entities.HttpInput; import com.google.gson.JsonPrimitive; import com.jayway.jsonpath.JsonPath; +import io.cloudslang.content.redhat.utils.Descriptions; import org.apache.commons.lang3.StringUtils; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; @@ -111,28 +112,12 @@ public static void processHttpResult(Map httpResults) { public static void addPodListResults(Map httpResults) { try { - String returnResult = httpResults.get(RETURN_RESULT); - if (!(returnResult.isEmpty())) { - - //populate the document output - httpResults.put(DOCUMENT_OUTPUT, returnResult); + if (!(httpResults.get(RETURN_RESULT).isEmpty())) { - //parse the API response, extract required information construct the podList and podArray outputs - JsonObject jsonResponse = JsonParser.parseString(httpResults.get(RETURN_RESULT)).getAsJsonObject(); - JsonArray podArray = jsonResponse.getAsJsonArray(PROPERTY_ITEMS); StringBuilder podList = new StringBuilder(); List podPairList = new ArrayList<>(); - for (JsonElement pod : podArray) { - JsonObject podObject = new JsonObject(); - podObject.add(PROPERTY_NAME, pod.getAsJsonObject().get(PROPERTY_METADATA).getAsJsonObject().get(PROPERTY_NAME)); - podObject.add(PROPERTY_UID, pod.getAsJsonObject().get(PROPERTY_METADATA).getAsJsonObject().get(PROPERTY_UID)); - podPairList.add(podObject); - podList.append(pod.getAsJsonObject().get(PROPERTY_METADATA).getAsJsonObject().get(PROPERTY_UID).getAsString()); - podList.append(COMMA); - } - - //remove the last comma from the podList value - podList.deleteCharAt(podList.length() - 1); + + extractValue(httpResults, podList, podPairList); //populate the podList and podArray outputs httpResults.put(POD_LIST, podList.toString()); @@ -145,17 +130,72 @@ public static void addPodListResults(Map httpResults) { } catch (Exception e) { //in case an error arises during the parsing, populate the custom outputs with empty values - setFailureCustomResults(httpResults); + setFailureCustomResults(httpResults, POD_LIST, POD_ARRAY, DOCUMENT_OUTPUT); + + throw new RuntimeException(e); + } + + } + + public static void addRouteListResults(Map httpResults) { + try { + if (!(httpResults.get(RETURN_RESULT).isEmpty())) { + + StringBuilder routeList = new StringBuilder(); + List routePairList = new ArrayList<>(); + + extractValue(httpResults, routeList, routePairList); + + //populate the routeList and routeArray outputs + httpResults.put(ROUTE_LIST, routeList.toString()); + httpResults.put(ROUTE_ARRAY, routePairList.toString()); + + //overwrite the returnResult output with a success message + httpResults.put(RETURN_RESULT, Descriptions.GetRouteList.SUCCESSFUL_RETURN_RESULT); + + } + + } catch (Exception e) { + //in case an error arises during the parsing, populate the custom outputs with empty values + setFailureCustomResults(httpResults, DOCUMENT_OUTPUT, ROUTE_LIST, ROUTE_ARRAY); throw new RuntimeException(e); } } - public static void setFailureCustomResults(Map httpResults) { - httpResults.put(POD_LIST, EMPTY); - httpResults.put(POD_ARRAY, EMPTY); - httpResults.put(DOCUMENT_OUTPUT, EMPTY); + private static void extractValue(Map httpResults, StringBuilder elementList, List elementPairList) { + + //populate the document output + httpResults.put(DOCUMENT_OUTPUT, httpResults.get(RETURN_RESULT)); + + //parse the API response, extract required information construct the elementList and elementArray outputs + JsonObject jsonResponse = JsonParser.parseString(httpResults.get(RETURN_RESULT)).getAsJsonObject(); + JsonArray elementArray = jsonResponse.getAsJsonArray(PROPERTY_ITEMS); + + for (JsonElement element : elementArray) { + JsonObject elementObject = new JsonObject(); + elementObject.add(PROPERTY_NAME, element.getAsJsonObject().get(PROPERTY_METADATA).getAsJsonObject().get(PROPERTY_NAME)); + elementObject.add(PROPERTY_UID, element.getAsJsonObject().get(PROPERTY_METADATA).getAsJsonObject().get(PROPERTY_UID)); + elementPairList.add(elementObject); + elementList.append(element.getAsJsonObject().get(PROPERTY_METADATA).getAsJsonObject().get(PROPERTY_UID).getAsString()); + elementList.append(COMMA); + } + + //remove the last comma from the podList value + + try { + elementList.deleteCharAt(elementList.length() - 1); + } catch (IndexOutOfBoundsException e) { + //do nothing with the exception as this was added for the case where the http request is successful but no item is returned + //in that case this would throw an IndexOutOfBoundsException + } + } + + public static void setFailureCustomResults(Map httpResults, String... inputs) { + + for (String input : inputs) + httpResults.put(input, EMPTY); } public static void setFailureCommonResults(Map httpResults, Exception e) { diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java index 9de06be090..92a3c15872 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Constants.java @@ -116,6 +116,9 @@ public static final String CREATE_DEPLOYMENT_ENDPOINT(String namespace) { public static final String PROPERTY_METADATA = "metadata"; public static final String SPECIFICATION = "specification"; public static final String POD_NAME = "podName"; + + public static final String ROUTE_ENDPOINT_1 = "/apis/route.openshift.io/v1/namespaces/"; + public static final String ROUTE_ENDPOINT_2 = "/routes"; } } diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java index 4739df5250..f6aef7f107 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java @@ -174,7 +174,7 @@ public static class GetPodList { public static final String POD_LIST_DESC = "The comma separated list of pod uids."; public static final String POD_ARRAY_DESC = "The list containing pairs of pod name and uids in JSON format."; public static final String DOCUMENT_OUTPUT_DESC = "All the information related to the pod list in json format."; - public static final String SUCCESSFUL_RETURN_RESULT = "The Pod list was returned successfully."; + public static final String SUCCESSFUL_RETURN_RESULT = "The pod list was returned successfully."; public static final String FAILURE_RETURN_RESULT = "An issue was encountered during your query. Please check the exception output for more information."; } @@ -202,4 +202,21 @@ public static class DeletePod { public static final String SUCCESS_DESC = "The pod was deleted successfully."; public static final String FAILURE_DESC = "There was an error while deleting the pod."; } + + public static class GetRouteList { + public static final String GET_ROUTE_LIST = "Get Route List"; + public static final String GET_ROUTE_LIST_DESC = "This operation returns a list of routes present in a given namespace."; + public static final String NAMESPACE_DESC = "The object name and auth scope, such as for teams and projects."; + public static final String RETURN_RESULT_DESC = "A suggestive message both for the case of success and for the " + + "case of failure."; + public static final String STATUS_CODE_DESC = "The status code of the request."; + public static final String EXCEPTION_DESC = "An error message in case there was an error while retrieving the route list."; + public static final String SUCCESS_DESC = "The retrieval of the route list was made successfully."; + public static final String FAILURE_DESC = "There was an error while trying to retrieve the route list."; + public static final String ROUTE_LIST_DESC = "The comma separated list of route uids."; + public static final String ROUTE_ARRAY_DESC = "The list containing pairs of route name and uids in JSON format."; + public static final String DOCUMENT_OUTPUT_DESC = "All the information related to the route list in json format."; + public static final String SUCCESSFUL_RETURN_RESULT = "The route list was returned successfully."; + + } } diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Outputs.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Outputs.java index d697dffe02..f8412bcca8 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Outputs.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Outputs.java @@ -42,6 +42,11 @@ public static class OutputNames { public static final String POD_LIST = "podList"; public static final String POD_ARRAY = "podArray"; + //Get Route List + + public static final String ROUTE_LIST = "routeList"; + public static final String ROUTE_ARRAY = "routeArray"; + } } From e5a2018ddcd5d50d24ca12ff55483971f9247355 Mon Sep 17 00:00:00 2001 From: TodereanLenuta Date: Wed, 14 Dec 2022 13:18:07 +0200 Subject: [PATCH 16/16] Refactoring - Get Status Deployment --- .../content/redhat/actions/GetDeploymentStatus.java | 4 ++-- .../cloudslang/content/redhat/services/OpenshiftService.java | 4 ++-- .../java/io/cloudslang/content/redhat/utils/Descriptions.java | 4 ++-- .../main/java/io/cloudslang/content/redhat/utils/Outputs.java | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetDeploymentStatus.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetDeploymentStatus.java index 79210c361c..c592125532 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetDeploymentStatus.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/actions/GetDeploymentStatus.java @@ -52,8 +52,8 @@ public class GetDeploymentStatus { @Output(value = DOCUMENT_OUTPUT, description = DOCUMENT_OUTPUT_DESC), @Output(value = KIND_OUTPUT, description = KIND_OUTPUT_DESC), - @Output(value = NAME_OUTPUT, description = NAME_OUTPUT_DESC), - @Output(value = NAMESPACE_OUTPUT, description = NAMESPACE_OUTPUT_DESC), + @Output(value = DEPLOYMENT_NAME_OUTPUT, description = DEPLOYMENT_NAME_OUTPUT_DESC), + @Output(value = DEPLOYMENT_NAMESPACE_OUTPUT, description = DEPLOYMENT_NAMESPACE_OUTPUT_DESC), @Output(value = UID_OUTPUT, description = UID_OUTPUT_DESC), @Output(value = OBSERVED_GENERATION_OUTPUT, description = OBSERVED_GENERATION_OUTPUT_DESC), diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java index 49eb991f24..7b4e8ede2e 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/services/OpenshiftService.java @@ -222,12 +222,12 @@ public static void processHttpGetDeploymentStatusResult(Map http //Name outputs String namePath = "$.metadata.name"; String namePathResponse = JsonPath.read(jsonResponse.toString(), namePath); - httpResults.put(NAME_OUTPUT, namePathResponse); + httpResults.put(DEPLOYMENT_NAME_OUTPUT, namePathResponse); //Namespace outputs String namespacePath = "$.metadata.namespace"; String namespacePathResponse = JsonPath.read(jsonResponse.toString(), namespacePath); - httpResults.put(NAMESPACE_OUTPUT, namespacePathResponse); + httpResults.put(DEPLOYMENT_NAMESPACE_OUTPUT, namespacePathResponse); //Uid outputs String uidPath = "$.metadata.uid"; diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java index f6aef7f107..b765138012 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Descriptions.java @@ -146,8 +146,8 @@ public static class GetDeploymentStatus { //Specific outputs //Get Deployment Status - public static final String NAME_OUTPUT_DESC = "The deployment name."; - public static final String NAMESPACE_OUTPUT_DESC = "The deployment namespace."; + public static final String DEPLOYMENT_NAME_OUTPUT_DESC = "The deployment name."; + public static final String DEPLOYMENT_NAMESPACE_OUTPUT_DESC = "The deployment namespace."; public static final String UID_OUTPUT_DESC = "The deployment uid."; public static final String KIND_OUTPUT_DESC = "The deployment kind."; public static final String OBSERVED_GENERATION_OUTPUT_DESC = "The observedGeneration status property of the deployment."; diff --git a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Outputs.java b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Outputs.java index f8412bcca8..ca9198fcbb 100644 --- a/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Outputs.java +++ b/cs-openshift/src/main/java/io/cloudslang/content/redhat/utils/Outputs.java @@ -26,9 +26,9 @@ public static class OutputNames { public static final String AUTH_TOKEN = "authToken"; //Get Deployment Status - public static final String NAME_OUTPUT = "name"; + public static final String DEPLOYMENT_NAME_OUTPUT = "deploymentName"; public static final String DOCUMENT_OUTPUT = "document"; - public static final String NAMESPACE_OUTPUT = "namespace"; + public static final String DEPLOYMENT_NAMESPACE_OUTPUT = "deploymentNamespace"; public static final String UID_OUTPUT = "uid"; public static final String KIND_OUTPUT = "kind"; public static final String OBSERVED_GENERATION_OUTPUT = "observedGeneration";