Skip to content

Commit

Permalink
feat: param and pipeline configuration classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Pirovano committed Aug 14, 2024
1 parent fd832f5 commit 1092925
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.opendatamesh.platform.up.executor.gitlabci.clients;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.opendatamesh.platform.up.executor.gitlabci.config.ParamConfiguration;
import org.opendatamesh.platform.up.executor.gitlabci.resources.ExecutorApiStandardErrors;
import org.opendatamesh.platform.up.executor.gitlabci.resources.client.params.ParamResource;
import org.opendatamesh.platform.up.executor.gitlabci.resources.exceptions.GitlabClientException;
Expand All @@ -27,24 +28,21 @@
*/
@Service
public class ParamsServiceClient {
private static final Logger logger = LoggerFactory.getLogger(GitlabPipelineService.class);
private static final Logger logger = LoggerFactory.getLogger(ParamsServiceClient.class);
protected String serverAddress;
protected ObjectMapper mapper;
public TestRestTemplate rest = new TestRestTemplate();
/**
* The client UUID to authenticate with the service, in order to retrieve the secret value decrypted.
*/
private final String clientUUID;
/**
* The client prefix, used to set the prefix to the display name of the parameter.
*/
@Value("${odm.productplane.params-service.client-prefix}")
private String clientPrefix;
private final ParamConfiguration paramConfiguration;

public ParamsServiceClient(@Value("${odm.productplane.params-service.address}") String serverAddress, @Value("${odm.productplane.params-service.client-uuid}") String clientUUID) {
this.serverAddress = serverAddress;
public ParamsServiceClient(ParamConfiguration paramConfiguration) {
this.serverAddress = paramConfiguration.getServerAddress();
this.paramConfiguration = paramConfiguration;
this.mapper = ObjectMapperFactory.JSON_MAPPER;
this.clientUUID = clientUUID;
this.clientUUID = paramConfiguration.getClientUUID();
}

/**
Expand Down Expand Up @@ -134,7 +132,7 @@ public ResponseEntity<ParamResource> getParamByName(String paramName) {
public void deleteParam(String paramName) {
HttpEntity<ParamResource> entity = new HttpEntity<>(null, getHttpHeaders());
ResponseEntity<ParamResource> param = getParamByName(paramName);
if (param.getStatusCode().is2xxSuccessful() && Objects.requireNonNull(param.getBody()).getDisplayName().startsWith(clientPrefix)) {
if (param.getStatusCode().is2xxSuccessful() && Objects.requireNonNull(param.getBody()).getDisplayName().startsWith(paramConfiguration.getClientPrefix())) {
try {
rest.exchange(
apiUrl(ParamsServerApiRoutes.DELETE_PARAM),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.opendatamesh.platform.up.executor.gitlabci.config;

import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
@Getter
public class ParamConfiguration {
/**
* The client prefix, used to set the prefix to the display name of the parameter.
*/
@Value("${odm.productplane.params-service.client-prefix}")
private String clientPrefix;
@Value("${odm.productplane.params-service.address}")
private String serverAddress;
@Value("${odm.productplane.params-service.client-uuid}")
private String clientUUID;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.opendatamesh.platform.up.executor.gitlabci.config;

import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
@Getter
public class PipelineConfiguration {
@Value("${odm.executors.gitlab.pipelines-config.polling.retries}")
private Integer pollingNumRetries;
@Value("${odm.executors.gitlab.pipelines-config.polling.interval}")
private Integer pollingIntervalSeconds;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ public interface ODMApiStandardErrors {

static ODMApiStandardErrors getNotFoundError(String className) {
return null;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import lombok.RequiredArgsConstructor;
import org.opendatamesh.platform.up.executor.gitlabci.clients.ParamsServiceClient;
import org.opendatamesh.platform.up.executor.gitlabci.config.ParamConfiguration;
import org.opendatamesh.platform.up.executor.gitlabci.resources.ExecutorApiStandardErrors;
import org.opendatamesh.platform.up.executor.gitlabci.resources.client.gitlab.GitlabConfigResource;
import org.opendatamesh.platform.up.executor.gitlabci.resources.client.params.ParamResource;
import org.opendatamesh.platform.up.executor.gitlabci.resources.exceptions.ConflictException;
import org.opendatamesh.platform.up.executor.gitlabci.resources.exceptions.UnprocessableEntityException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
Expand All @@ -23,8 +23,7 @@
public class GitlabConfigService {
private final ParamsServiceClient paramsServiceClient;

@Value("${odm.productplane.params-service.client-prefix}")
private String clientPrefix;
private final ParamConfiguration paramConfiguration;

/**
* Add configurations to the parameter service.
Expand All @@ -42,7 +41,7 @@ public ParamResource addConfig(GitlabConfigResource configResource) {
ParamResource paramResource = new ParamResource();
paramResource.setParamName(configResource.getInstanceUrl());
paramResource.setParamValue(configResource.getInstanceToken());
paramResource.setDisplayName(clientPrefix + configResource.getInstanceUrl());
paramResource.setDisplayName(paramConfiguration.getClientPrefix() + configResource.getInstanceUrl());
paramResource.setSecret(true);

ResponseEntity<ParamResource> createdParam = paramsServiceClient.createParam(paramResource);
Expand All @@ -69,7 +68,7 @@ public List<ParamResource> getAllGitlabInstances() {

List<ParamResource> result = new ArrayList<>();
for (ParamResource param : params) {
if (param.getDisplayName().startsWith(clientPrefix)) {
if (param.getDisplayName().startsWith(paramConfiguration.getClientPrefix())) {
result.add(param);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.RequiredArgsConstructor;
import org.opendatamesh.platform.up.executor.gitlabci.clients.GitlabClient;
import org.opendatamesh.platform.up.executor.gitlabci.clients.ParamsServiceClient;
import org.opendatamesh.platform.up.executor.gitlabci.config.PipelineConfiguration;
import org.opendatamesh.platform.up.executor.gitlabci.dao.PipelineRun;
import org.opendatamesh.platform.up.executor.gitlabci.dao.PipelineRunRepository;
import org.opendatamesh.platform.up.executor.gitlabci.mappers.GitlabPipelineMapper;
Expand All @@ -19,7 +20,6 @@
import org.opendatamesh.platform.up.executor.gitlabci.resources.exceptions.UnprocessableEntityException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
Expand All @@ -38,11 +38,8 @@
public class GitlabPipelineService {
private final GitlabPipelineMapper pipelineMapper;
private final PipelineRunRepository pipelineRunRepository;
@Value("${odm.executors.gitlab.pipelines-config.polling.retries}")
private Integer pollingNumRetries;
@Value("${odm.executors.gitlab.pipelines-config.polling.interval}")
private Integer pollingIntervalSeconds;
private final ParamsServiceClient paramsServiceClient;
private final PipelineConfiguration pipelineConfiguration;

private static final Logger logger = LoggerFactory.getLogger(GitlabPipelineService.class);

Expand Down Expand Up @@ -201,13 +198,13 @@ public CompletableFuture<TaskStatus> getPipelineStatus(Long taskId) {
Objects.requireNonNull(optGitlabInstance.getBody()).getParamValue()
);

while (counter < pollingNumRetries) {
while (counter < pipelineConfiguration.getPollingNumRetries()) {
gitlabResponse = gitlabClient.readTask(pipelineRun.getProject(), pipelineRun.getRunId());
if (gitlabResponse.getStatusCode().is2xxSuccessful() && Objects.requireNonNull(gitlabResponse.getBody()).getStatus().equals(GitlabRunState.success.toString())) {
break;
}
try {
Thread.sleep((long) pollingIntervalSeconds);
Thread.sleep((long) pipelineConfiguration.getPollingIntervalSeconds());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
logger.warn("Polling thread error: " + e.getMessage());
Expand Down

0 comments on commit 1092925

Please sign in to comment.