Skip to content

Commit

Permalink
FlexibleEngine plugin - implement, start, stop and restart services
Browse files Browse the repository at this point in the history
  • Loading branch information
WangLiNaruto committed Dec 12, 2023
1 parent 9c90d85 commit 84e34d1
Show file tree
Hide file tree
Showing 19 changed files with 620 additions and 126 deletions.
5 changes: 5 additions & 0 deletions plugins/flexibleengine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@
<artifactId>huaweicloud-sdk-ces</artifactId>
<version>${huaweicloud.sdk.version}</version>
</dependency>
<dependency>
<groupId>com.huaweicloud.sdk</groupId>
<artifactId>huaweicloud-sdk-ecs</artifactId>
<version>${huaweicloud.sdk.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
*/

package org.eclipse.xpanse.plugins.flexibleengine.monitor.models;
package org.eclipse.xpanse.plugins.flexibleengine;

import com.cloud.apigateway.sdk.utils.Client;
import java.util.List;
Expand All @@ -14,35 +14,34 @@
import org.eclipse.xpanse.modules.models.credential.CredentialVariable;
import org.eclipse.xpanse.modules.models.credential.CredentialVariables;
import org.eclipse.xpanse.modules.models.credential.enums.CredentialType;
import org.eclipse.xpanse.plugins.flexibleengine.monitor.constant.FlexibleEngineMonitorConstants;
import org.eclipse.xpanse.plugins.flexibleengine.models.constant.FlexibleEngineConstants;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

/**
* FlexibleEngine Monitor Client.
*/
@Component
public class FlexibleEngineMonitorClient {
public class FlexibleEngineClient {

private FlexibleEngineMonitorClientCredential getCredentialForClient(
private FlexibleEngineClientCredential getCredentialForClient(
AbstractCredentialInfo credential) {
String accessKey = null;
String securityKey = null;
if (CredentialType.VARIABLES.toValue().equals(credential.getType().toValue())) {
List<CredentialVariable> variables = ((CredentialVariables) credential).getVariables();
for (CredentialVariable credentialVariable : variables) {
if (FlexibleEngineMonitorConstants.OS_ACCESS_KEY.equals(
if (FlexibleEngineConstants.OS_ACCESS_KEY.equals(
credentialVariable.getName())) {
accessKey = credentialVariable.getValue();
}
if (FlexibleEngineMonitorConstants.OS_SECRET_KEY.equals(
if (FlexibleEngineConstants.OS_SECRET_KEY.equals(
credentialVariable.getName())) {
securityKey = credentialVariable.getValue();
}
}
}
return new FlexibleEngineMonitorClientCredential(accessKey, securityKey);

return new FlexibleEngineClientCredential(accessKey, securityKey);
}

/**
Expand All @@ -55,7 +54,7 @@ private FlexibleEngineMonitorClientCredential getCredentialForClient(
public HttpRequestBase buildGetRequest(AbstractCredentialInfo credential, String url)
throws Exception {

FlexibleEngineMonitorClientCredential clientCredential = getCredentialForClient(credential);
FlexibleEngineClientCredential clientCredential = getCredentialForClient(credential);
return Client.get(clientCredential.accessKey(), clientCredential.secretKey(), url,
Map.of("Content-Type", MediaType.APPLICATION_JSON_VALUE));
}
Expand All @@ -68,11 +67,9 @@ public HttpRequestBase buildGetRequest(AbstractCredentialInfo credential, String
* @return Returns HttpRequestBase
*/
public HttpRequestBase buildPostRequest(AbstractCredentialInfo credential, String url,
String postBody) throws Exception {
FlexibleEngineMonitorClientCredential clientCredential = getCredentialForClient(credential);
String postBody) throws Exception {
FlexibleEngineClientCredential clientCredential = getCredentialForClient(credential);
return Client.post(clientCredential.accessKey(), clientCredential.secretKey(), url,
Map.of("Content-Type", MediaType.APPLICATION_JSON_VALUE), postBody);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*
*/

package org.eclipse.xpanse.plugins.flexibleengine;

/**
* Credential for FlexibleEngine Client.
*/
public record FlexibleEngineClientCredential(String accessKey,
String secretKey) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*
*/

package org.eclipse.xpanse.plugins.flexibleengine;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.xpanse.plugins.flexibleengine.models.constant.FlexibleEngineConstants;
import org.springframework.stereotype.Component;

/**
* FlexibleEngine Resource Converter.
*/
@Slf4j
@Component
public class FlexibleEngineConverter {

/**
* Get url to query project info.
*
* @param region The region of resource.
* @return Returns query url.
*/
public StringBuilder buildProjectQueryUrl(String region) {
return new StringBuilder(FlexibleEngineConstants.PROTOCOL_HTTPS)
.append(FlexibleEngineConstants.IAM_ENDPOINT_PREFIX)
.append(region)
.append(FlexibleEngineConstants.ENDPOINT_SUFFIX).append("/")
.append(FlexibleEngineConstants.IAM_API_VERSION).append("/")
.append(FlexibleEngineConstants.PROJECTS_PATH).append("?")
.append("name=").append(region);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import org.eclipse.xpanse.modules.orchestrator.manage.ServiceManagerRequest;
import org.eclipse.xpanse.modules.orchestrator.monitor.ResourceMetricsRequest;
import org.eclipse.xpanse.modules.orchestrator.monitor.ServiceMetricsRequest;
import org.eclipse.xpanse.plugins.flexibleengine.monitor.constant.FlexibleEngineMonitorConstants;
import org.eclipse.xpanse.plugins.flexibleengine.manage.util.FlexibleEngineVmStateManagerService;
import org.eclipse.xpanse.plugins.flexibleengine.models.constant.FlexibleEngineConstants;
import org.eclipse.xpanse.plugins.flexibleengine.monitor.utils.FlexibleEngineMetricsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -36,16 +37,22 @@ public class FlexibleEngineOrchestratorPlugin implements OrchestratorPlugin {

private final FlexibleEngineTerraformResourceHandler flexibleEngineTerraformResourceHandler;
private final FlexibleEngineMetricsService flexibleEngineMetricsService;
private final FlexibleEngineVmStateManagerService flexibleEngineVmStateManagerService;

@Value("${terraform.provider.flexibleengine.version}")
private String terraformFlexibleEngineVersion;

/**
* Building OrchestratorPlugin for FlexibleEngine.
*/
@Autowired
public FlexibleEngineOrchestratorPlugin(
FlexibleEngineMetricsService flexibleEngineMetricsService,
FlexibleEngineTerraformResourceHandler flexibleEngineTerraformResourceHandler) {
FlexibleEngineTerraformResourceHandler flexibleEngineTerraformResourceHandler,
FlexibleEngineVmStateManagerService flexibleEngineVmStateManagerService) {
this.flexibleEngineMetricsService = flexibleEngineMetricsService;
this.flexibleEngineTerraformResourceHandler = flexibleEngineTerraformResourceHandler;
this.flexibleEngineVmStateManagerService = flexibleEngineVmStateManagerService;
}

@Override
Expand Down Expand Up @@ -77,10 +84,10 @@ public List<AbstractCredentialInfo> getCredentialDefinitions() {
getCsp(), CredentialType.VARIABLES, "AK_SK", "The access key and security key.",
null, credentialVariables);
credentialVariables.add(
new CredentialVariable(FlexibleEngineMonitorConstants.OS_ACCESS_KEY,
new CredentialVariable(FlexibleEngineConstants.OS_ACCESS_KEY,
"The access key.", true));
credentialVariables.add(
new CredentialVariable(FlexibleEngineMonitorConstants.OS_SECRET_KEY,
new CredentialVariable(FlexibleEngineConstants.OS_SECRET_KEY,
"The security key.", true));
List<AbstractCredentialInfo> credentialInfos = new ArrayList<>();
credentialInfos.add(accessKey);
Expand Down Expand Up @@ -116,6 +123,21 @@ public List<Metric> getMetricsForService(ServiceMetricsRequest serviceMetricRequ
return flexibleEngineMetricsService.getMetricsForService(serviceMetricRequest);
}

@Override
public boolean startService(ServiceManagerRequest serviceManagerRequest) {
return flexibleEngineVmStateManagerService.startService(serviceManagerRequest);
}

@Override
public boolean stopService(ServiceManagerRequest serviceManagerRequest) {
return flexibleEngineVmStateManagerService.stopService(serviceManagerRequest);
}

@Override
public boolean restartService(ServiceManagerRequest serviceManagerRequest) {
return flexibleEngineVmStateManagerService.restartService(serviceManagerRequest);
}

@Override
public String getProvider(String region) {
return String.format("""
Expand All @@ -133,19 +155,4 @@ public String getProvider(String region) {
}
""", terraformFlexibleEngineVersion, region);
}

@Override
public boolean startService(ServiceManagerRequest serviceManagerRequest) {
return true;
}

@Override
public boolean stopService(ServiceManagerRequest serviceManagerRequest) {
return true;
}

@Override
public boolean restartService(ServiceManagerRequest serviceManagerRequest) {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
*/

package org.eclipse.xpanse.plugins.flexibleengine.monitor.utils;
package org.eclipse.xpanse.plugins.flexibleengine;

import jakarta.annotation.Resource;
import java.util.Collections;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
*
*/

package org.eclipse.xpanse.plugins.flexibleengine.monitor.utils;
package org.eclipse.xpanse.plugins.flexibleengine;

import com.huaweicloud.sdk.ces.v1.model.BatchListMetricDataResponse;
import com.huaweicloud.sdk.ces.v1.model.ListMetricsResponse;
import com.huaweicloud.sdk.ces.v1.model.ShowMetricDataResponse;
import com.huaweicloud.sdk.core.internal.model.KeystoneListProjectsResponse;
import com.huaweicloud.sdk.ecs.v2.model.BatchRebootServersResponse;
import com.huaweicloud.sdk.ecs.v2.model.BatchStartServersResponse;
import com.huaweicloud.sdk.ecs.v2.model.BatchStopServersResponse;
import com.huaweicloud.sdk.ecs.v2.model.ShowJobResponse;
import jakarta.annotation.Resource;
import java.util.Arrays;
import java.util.Map;
Expand Down Expand Up @@ -128,7 +132,7 @@ public ListMetricsResponse queryMetricItemList(HttpRequestBase httpRequestBase)
listeners = "restTemplateRetryListener",
backoff = @Backoff(delay = DELAY_MILLISECONDS, multiplier = DELAY_MULTIPLIER))
public BatchListMetricDataResponse batchQueryMetricData(HttpRequestBase httpRequestBase,
String requestBody) {
String requestBody) {
HttpEntity<String> httpEntity = new HttpEntity<>(requestBody,
getHttpHeaders(httpRequestBase));
ResponseEntity<BatchListMetricDataResponse> responseEntity =
Expand All @@ -140,6 +144,77 @@ public BatchListMetricDataResponse batchQueryMetricData(HttpRequestBase httpRequ
throw new RestClientException("batch query metric data failed.");
}

/**
* Start service.
*
* @param httpRequestBase httpRequestBase
* @param requestBody requestBody
* @return BatchStartServersResponse
*/
@Retryable(retryFor = RestClientException.class, maxAttempts = RETRY_TIMES,
listeners = "restTemplateRetryListener",
backoff = @Backoff(delay = DELAY_MILLISECONDS, multiplier = DELAY_MULTIPLIER))
public ResponseEntity<BatchStartServersResponse> startService(HttpRequestBase httpRequestBase,
String requestBody) {
HttpEntity<String> httpEntity = new HttpEntity<>(requestBody,
getHttpHeaders(httpRequestBase));
return restTemplate.exchange(httpRequestBase.getURI(), HttpMethod.POST, httpEntity,
BatchStartServersResponse.class);
}

/**
* Stop service.
*
* @param httpRequestBase httpRequestBase
* @param requestBody requestBody
* @return BatchStopServersResponse
*/
@Retryable(retryFor = RestClientException.class, maxAttempts = RETRY_TIMES,
listeners = "restTemplateRetryListener",
backoff = @Backoff(delay = DELAY_MILLISECONDS, multiplier = DELAY_MULTIPLIER))
public ResponseEntity<BatchStopServersResponse> stopService(HttpRequestBase httpRequestBase,
String requestBody) {
HttpEntity<String> httpEntity = new HttpEntity<>(requestBody,
getHttpHeaders(httpRequestBase));
return restTemplate.exchange(httpRequestBase.getURI(), HttpMethod.POST, httpEntity,
BatchStopServersResponse.class);
}

/**
* Restart service.
*
* @param httpRequestBase httpRequestBase
* @param requestBody requestBody
* @return BatchRebootServersResponse
*/
@Retryable(retryFor = RestClientException.class, maxAttempts = RETRY_TIMES,
listeners = "restTemplateRetryListener",
backoff = @Backoff(delay = DELAY_MILLISECONDS, multiplier = DELAY_MULTIPLIER))
public ResponseEntity<BatchRebootServersResponse> restartService(
HttpRequestBase httpRequestBase,
String requestBody) {
HttpEntity<String> httpEntity = new HttpEntity<>(requestBody,
getHttpHeaders(httpRequestBase));
return restTemplate.exchange(httpRequestBase.getURI(), HttpMethod.POST, httpEntity,
BatchRebootServersResponse.class);
}

/**
* Query JOB execution status.
*
* @param httpRequestBase httpRequestBase
* @return ShowJobResponse
*/
@Retryable(retryFor = RestClientException.class, maxAttempts = RETRY_TIMES,
listeners = "restTemplateRetryListener",
backoff = @Backoff(delay = DELAY_MILLISECONDS, multiplier = DELAY_MULTIPLIER))
public ResponseEntity<ShowJobResponse> checkEcsExecResultByJobId(
HttpRequestBase httpRequestBase) {
HttpEntity<String> httpEntity =
new HttpEntity<>("parameters", getHttpHeaders(httpRequestBase));
return restTemplate.exchange(httpRequestBase.getURI(), HttpMethod.GET, httpEntity,
ShowJobResponse.class);
}

private HttpHeaders getHttpHeaders(HttpRequestBase httpRequestBase) {
HttpHeaders headers = new HttpHeaders();
Expand Down Expand Up @@ -172,7 +247,7 @@ public Object recoverGetRequest(RestClientException e, HttpRequestBase httpReque
*/
@Recover
public Object recoverPostRequest(RestClientException e, HttpRequestBase httpRequestBase,
String requestBody) {
String requestBody) {
log.error("RestTemplate post request[Url:{},Body:{}] still failed after retried {} times.",
httpRequestBase.getURI(), RETRY_TIMES, requestBody, e);
return null;
Expand All @@ -186,22 +261,22 @@ public RetryListener retryListener() {
return new RetryListener() {
@Override
public <T, E extends Throwable> boolean open(RetryContext context,
RetryCallback<T, E> callback) {
RetryCallback<T, E> callback) {
log.error("Retry open context:{}", context);
return true;
}

@Override
public <T, E extends Throwable> void close(RetryContext context,
RetryCallback<T, E> callback,
Throwable throwable) {
RetryCallback<T, E> callback,
Throwable throwable) {
log.error("Retry close context:{}", context);
}

@Override
public <T, E extends Throwable> void onError(RetryContext context,
RetryCallback<T, E> callback,
Throwable throwable) {
RetryCallback<T, E> callback,
Throwable throwable) {
log.error("Retry onError context:{}, error:{}.", context, throwable.getMessage());
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*
*/

package org.eclipse.xpanse.plugins.flexibleengine.manage.constant;

import org.eclipse.xpanse.plugins.flexibleengine.models.constant.FlexibleEngineConstants;

/**
* FlexibleEngine Manage Constants Class.
*/
public class FlexibleEngineManageConstants extends FlexibleEngineConstants {

public static final String ECS_ENDPOINT_PREFIX = "ecs.";

public static final String ECS_API_VERSION = "v1";
public static final String CLOUD_SERVERS = "cloudservers";
public static final String ACTION = "action";
public static final String JOBS = "jobs";
}

0 comments on commit 84e34d1

Please sign in to comment.