Skip to content
Snippets Groups Projects
Commit 4f6d7828 authored by Lukasz Lopatowski's avatar Lukasz Lopatowski
Browse files

Merge branch '205-don-t-hardcode-http-as-the-protocol-when-using-gitlab' into 'develop'

Using new property for GitLab instance api url

See merge request !11
parents ec091fd4 987b25bb
No related branches found
No related tags found
1 merge request!11Using new property for GitLab instance api url
Pipeline #87572 passed
......@@ -141,8 +141,7 @@ janitor.port=${JANITOR_PORT}
# -------------------- #
# GitLab configuration #
# -------------------- #
gitlab.address=${GITLAB_ADDRESS}
gitlab.port=${GITLAB_PORT}
gitlab.apiUrl=${GITLAB_API_URL}
gitlab.token=${GITLAB_TOKEN}
# ------------------------ #
......
......@@ -118,8 +118,7 @@ janitor.port=5000
# -------------------- #
# GitLab configuration #
# -------------------- #
gitlab.address=nmaas-gitlab-unicorn
gitlab.port=8080
gitlab.apiUrl=http://nmaas-gitlab-unicorn:8080
gitlab.token=test_gitlab_token
# ------------------------ #
......
......@@ -22,21 +22,16 @@ import static com.google.common.base.Preconditions.checkArgument;
@Log4j2
public class GitLabManager {
@Value("${gitlab.address}")
private String gitLabAddress;
private static final String GITLAB_API_NAMESPACE = "/api/v4";
@Value("${gitlab.port}")
private Integer gitLabPort;
@Value("${gitlab.apiUrl}")
private String gitLabApiUrl;
@Value("${gitlab.token}")
private String gitLabToken;
public String getGitlabServer() {
return this.gitLabAddress;
}
public int getGitlabPort() {
return this.gitLabPort;
public String getGitLabApiUrl() {
return this.gitLabApiUrl;
}
public GroupApi groups() {
......@@ -59,19 +54,20 @@ public class GitLabManager {
return new GitLabApi(GitLabApi.ApiVersion.V4, getApiUrl(), this.gitLabToken);
}
private String getApiUrl(){
return String.format("http://%s:%d", this.gitLabAddress, this.gitLabPort);
String getApiUrl() {
return gitLabApiUrl.endsWith(GITLAB_API_NAMESPACE)
? gitLabApiUrl.substring(0, gitLabApiUrl.length() - GITLAB_API_NAMESPACE.length())
: gitLabApiUrl;
}
public void validateGitLabInstance() {
checkArgument(this.gitLabAddress != null && !this.gitLabAddress.isEmpty(), "GitLab address is null or empty");
checkArgument(this.gitLabPort != null, "GitLab port is null");
checkArgument(this.gitLabApiUrl != null && !this.gitLabApiUrl.isEmpty(), "GitLab api URL is null or empty");
checkArgument(this.gitLabToken != null && !this.gitLabToken.isEmpty(), "GitLab token is null or empty");
try {
api().getVersion();
log.trace("GitLab instance is running");
} catch (GitLabApiException e) {
throw new GitLabInvalidConfigurationException("GitLab instance is not running -> " + e.getMessage());
throw new GitLabInvalidConfigurationException("GitLab instance doesn't respond -> " + e.getMessage());
}
}
......
......@@ -201,8 +201,8 @@ public class GitLabConfigHandler implements GitConfigHandler {
String getHttpUrlToRepo(Integer gitLabProjectId) throws GitLabApiException {
String[] urlFromGitlabApiParts = gitLabManager.projects().getProject(gitLabProjectId).getHttpUrlToRepo().split("//");
String[] urlParts = urlFromGitlabApiParts[1].split("/");
urlParts[0] = gitLabManager.getGitlabServer() + ":" + gitLabManager.getGitlabPort();
return urlFromGitlabApiParts[0] + "//" + String.join("/", urlParts);
urlParts[0] = gitLabManager.getGitLabApiUrl();
return String.join("/", urlParts);
}
@Override
......@@ -261,8 +261,8 @@ public class GitLabConfigHandler implements GitConfigHandler {
*
* @param deploymentId unique identifier of service deployment
* @param configIds list of identifiers of configuration files that should be loaded from database and uploaded to the git repository
* @throws InvalidDeploymentIdException if a service for given deployment identifier could not be found in database
* @throws ConfigFileNotFoundException if any of the configuration files for which an identifier is given could not be found in database
* @throws InvalidDeploymentIdException if a service for given deployment identifier could not be found in the database
* @throws ConfigFileNotFoundException if any of the configuration files for which an identifier is given could not be found in the database
* @throws FileTransferException if any error occurs during communication with the git repository API
*/
@Override
......
......@@ -10,12 +10,23 @@ import net.geant.nmaas.notifications.MailAttributes;
import net.geant.nmaas.notifications.NotificationEvent;
import net.geant.nmaas.notifications.templates.MailType;
import net.geant.nmaas.orchestration.AppLifecycleManager;
import net.geant.nmaas.portal.api.domain.*;
import net.geant.nmaas.portal.api.domain.AppInstanceState;
import net.geant.nmaas.portal.api.domain.AppRateView;
import net.geant.nmaas.portal.api.domain.ApplicationBaseView;
import net.geant.nmaas.portal.api.domain.ApplicationStateChangeRequest;
import net.geant.nmaas.portal.api.domain.ApplicationView;
import net.geant.nmaas.portal.api.domain.Id;
import net.geant.nmaas.portal.api.domain.UserView;
import net.geant.nmaas.portal.api.exception.MarketException;
import net.geant.nmaas.portal.api.exception.MissingElementException;
import net.geant.nmaas.portal.api.exception.ProcessingException;
import net.geant.nmaas.portal.exceptions.ObjectAlreadyExistsException;
import net.geant.nmaas.portal.persistent.entity.*;
import net.geant.nmaas.portal.persistent.entity.Application;
import net.geant.nmaas.portal.persistent.entity.ApplicationBase;
import net.geant.nmaas.portal.persistent.entity.ApplicationState;
import net.geant.nmaas.portal.persistent.entity.ApplicationVersion;
import net.geant.nmaas.portal.persistent.entity.Role;
import net.geant.nmaas.portal.persistent.entity.User;
import net.geant.nmaas.portal.persistent.repositories.RatingRepository;
import net.geant.nmaas.portal.service.ApplicationInstanceService;
import net.geant.nmaas.portal.service.impl.ApplicationServiceImpl;
......@@ -141,8 +152,7 @@ public class ApplicationController extends AppBaseController {
@PreAuthorize("hasRole('ROLE_SYSTEM_ADMIN') || hasRole('ROLE_TOOL_MANAGER')")
@Transactional
public void updateApplicationBaseOwner(@PathVariable Long id, @PathVariable String owner, Principal principal) {
// only system admin and owner can update application base
log.info("Upate owner for application {} to {}", id, owner);
log.info("Updating owner of application {} to {}", id, owner);
this.applicationBaseOwnerCheck(id, principal);
appBaseService.updateOwner(id, owner);
}
......@@ -154,10 +164,11 @@ public class ApplicationController extends AppBaseController {
ApplicationBase base = appBaseService.getBaseApp(id);
// only system admin and owner can update application base
this.applicationBaseOwnerCheck(base.getName(), principal);
ApplicationState state = ApplicationState.DELETED;
for (ApplicationVersion appVersion : base.getVersions()) {
Application app = getApp(appVersion.getAppVersionId());
if(app.getState() != ApplicationState.DELETED) throw new ProcessingException("Can not delete base, version " + app.getVersion() +" is not deleted");
if (app.getState() != ApplicationState.DELETED) {
throw new ProcessingException("Can't delete " + base.getName() + " application base since version " + app.getVersion() + " is not deleted");
}
}
appBaseService.deleteAppBase(base);
}
......@@ -207,14 +218,15 @@ public class ApplicationController extends AppBaseController {
@Transactional
public ApplicationDTOVersionList getApplicationDTOWithAllVersions(@PathVariable Long id) {
ApplicationBase base = appBaseService.getBaseApp(id);
List<Application> versionList = this.applicationService.findAll().stream().filter(app -> app.getName().equalsIgnoreCase(base.getName())).collect(Collectors.toList());
List<Application> versionList = this.applicationService.findAll().stream()
.filter(app -> app.getName().equalsIgnoreCase(base.getName()))
.collect(Collectors.toList());
return new ApplicationDTOVersionList(
modelMapper.map(base, ApplicationBaseView.class),
versionList.stream().map(app->modelMapper.map(app, ApplicationView.class)).collect(Collectors.toList())
);
}
@GetMapping(value = "/versions/{id}")
@Transactional
public Set<ApplicationVersion> getApplicationVersion(@PathVariable Long id) {
......@@ -338,20 +350,14 @@ public class ApplicationController extends AppBaseController {
public void changeApplicationState(@PathVariable long id, @RequestBody ApplicationStateChangeRequest stateChangeRequest, Principal principal) {
Application app = getApp(id);
if (stateChangeRequest.getState().equals(ApplicationState.DELETED)) {
applicationInstanceService.findAllByApplication(app).forEach(ai -> {
AppInstanceStatus instanceState = appInstanceController.getState(ai.getId(), principal);
int numberOfRunningInstances = 0;
if(!(instanceState.getState().equals(AppInstanceState.DONE)
|| instanceState.getState().equals(AppInstanceState.FAILURE)
|| instanceState.getState().equals(AppInstanceState.REMOVED) )) {
numberOfRunningInstances = +1;
}
long numberOfRunningInstances = applicationInstanceService.findAllByApplication(app).stream()
.map(ai -> appInstanceController.getState(ai.getId(), principal))
.filter(s -> !List.of(AppInstanceState.DONE, AppInstanceState.FAILURE, AppInstanceState.REMOVED).contains(s.getState()))
.count();
if (numberOfRunningInstances > 0) {
throw new ProcessingException("Can not set state to Disabled. There is still " + numberOfRunningInstances + " running instances of this version.");
}
}
);
}
applicationService.changeApplicationState(app, stateChangeRequest.getState());
appBaseService.updateApplicationVersionState(app.getName(), app.getVersion(), stateChangeRequest.getState());
this.sendMails(app, stateChangeRequest);
......
......@@ -138,7 +138,7 @@ helm.repositoryName=nmaas-test
helm.repositoryUrl=https://nmaas-test.helm.repository
helm.chartsDirectory=/home/nmaas/charts
helm.enableTls=false
# possible values for Helm version are v2 and v3 (if none is provided v3 is used by default)
# possible values for the Helm version are v2 and v3 (if none is provided v3 is used by default)
helm.version=v2
# --------------------- #
......@@ -150,8 +150,7 @@ janitor.port=5000
# -------------------- #
# GitLab configuration #
# -------------------- #
gitlab.address=nmaas-gitlab-unicorn
gitlab.port=8080
gitlab.apiUrl=http://nmaas-gitlab-unicorn:8080
gitlab.token=test_gitlab_token
# ------------------------ #
......
......@@ -3,31 +3,37 @@ package net.geant.nmaas.externalservices.gitlab;
import net.geant.nmaas.externalservices.gitlab.exceptions.GitLabInvalidConfigurationException;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class GitLabManagerTest {
private GitLabManager manager;
private final GitLabManager manager = new GitLabManager();
@Test
void shouldValidateGitLabInstance() {
manager = new GitLabManager();
Exception thrown;
thrown = assertThrows(IllegalArgumentException.class, () -> manager.validateGitLabInstance());
assertTrue(thrown.getMessage().contains("GitLab address is null or empty"));
thrown = assertThrows(IllegalArgumentException.class, manager::validateGitLabInstance);
assertTrue(thrown.getMessage().contains("GitLab api URL is null or empty"));
manager.setGitLabAddress("localhost");
thrown = assertThrows(IllegalArgumentException.class, () -> manager.validateGitLabInstance());
assertTrue(thrown.getMessage().contains("GitLab port is null"));
manager.setGitLabPort(8080);
thrown = assertThrows(IllegalArgumentException.class, () -> manager.validateGitLabInstance());
assertTrue(thrown.getMessage().contains("GitLab token is null or empty"));
manager.setGitLabApiUrl("http://localhost:8080");
thrown = assertThrows(IllegalArgumentException.class, manager::validateGitLabInstance);
assertThat(thrown.getMessage()).contains("GitLab token is null or empty");
manager.setGitLabToken("token");
assertThrows(GitLabInvalidConfigurationException.class, () -> manager.validateGitLabInstance());
assertThrows(GitLabInvalidConfigurationException.class, manager::validateGitLabInstance);
}
@Test
void shouldPrepareApiBaseUrl() {
manager.setGitLabApiUrl("http://localhost:8080");
assertEquals("http://localhost:8080", manager.getApiUrl());
manager.setGitLabApiUrl("http://localhost:8080/api/v4");
assertEquals("http://localhost:8080", manager.getApiUrl());
}
}
......@@ -47,8 +47,7 @@ public class GitLabConfigHandlerTest {
Project project = mock(Project.class);
when(projectApi.getProject(anyInt())).thenReturn(project);
when(project.getHttpUrlToRepo()).thenReturn("http://example.gitlab.com/group/project.git");
when(gitLabManager.getGitlabServer()).thenReturn("test-server");
when(gitLabManager.getGitlabPort()).thenReturn(80);
when(gitLabManager.getGitLabApiUrl()).thenReturn("http://test-server:80");
when(gitLabManager.projects()).thenReturn(projectApi);
String result = handler.getHttpUrlToRepo(1);
......
......@@ -131,8 +131,7 @@ janitor.port=5000
# -------------------- #
# GitLab configuration #
# -------------------- #
gitlab.address=nmaas-gitlab-unicorn
gitlab.port=8080
gitlab.apiUrl=http://nmaas-gitlab-unicorn:8080
gitlab.token=test_gitlab_token
# ------------------------ #
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment