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