From b039c89e14d66d87e42cd9b22ffef98ec7929751 Mon Sep 17 00:00:00 2001 From: Julien WITTOUCK Date: Fri, 13 Aug 2021 15:49:05 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20:=20add=20a=20refresh=20module=20de?= =?UTF-8?q?finition=20button?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit add a button to refresh module definition (variables and outputs) to allow users to inherit latests modules updates --- src/main/client/app/pages/modules/module.vue | 32 ++++++++++- src/main/client/app/shared/api/modules-api.js | 2 + .../controller/ModuleRestController.java | 57 ++++++++++++++----- .../controller/ModuleRestControllerTest.java | 26 +++++++++ 4 files changed, 103 insertions(+), 14 deletions(-) diff --git a/src/main/client/app/pages/modules/module.vue b/src/main/client/app/pages/modules/module.vue index e794453cf..5a3e946b7 100644 --- a/src/main/client/app/pages/modules/module.vue +++ b/src/main/client/app/pages/modules/module.vue @@ -95,6 +95,8 @@ +
+

Authorized Teams

@@ -113,6 +115,8 @@ +
+

Variables

+ + Refresh module definition + + +
+ diff --git a/src/main/client/app/shared/api/modules-api.js b/src/main/client/app/shared/api/modules-api.js index 95376dd13..c24632c5f 100644 --- a/src/main/client/app/shared/api/modules-api.js +++ b/src/main/client/app/shared/api/modules-api.js @@ -6,6 +6,8 @@ export const getModule = async (moduleId) => axios.get(`/api/modules/${moduleId} export const getModuleReadme = async (moduleId) => axios.get(`/api/modules/${moduleId}/readme`); +export const refreshModule = async (moduleId) => axios.post(`/api/modules/${moduleId}/refresh`); + export const updateModule = async (module) => axios.put(`/api/modules/${module.id}`, module); export const createModule = async (module) => axios.post('/api/modules', module); diff --git a/src/main/java/io/gaia_app/modules/controller/ModuleRestController.java b/src/main/java/io/gaia_app/modules/controller/ModuleRestController.java index 3e17d85cb..122ad97d7 100644 --- a/src/main/java/io/gaia_app/modules/controller/ModuleRestController.java +++ b/src/main/java/io/gaia_app/modules/controller/ModuleRestController.java @@ -3,6 +3,7 @@ import io.gaia_app.modules.bo.TerraformModule; import io.gaia_app.modules.repository.TerraformModuleGitRepository; import io.gaia_app.modules.repository.TerraformModuleRepository; +import io.gaia_app.registries.service.RegistryService; import io.gaia_app.teams.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -21,34 +22,37 @@ */ @RestController @RequestMapping("/api/modules") -@Secured({"ROLE_USER","ROLE_ADMIN"}) +@Secured({"ROLE_USER", "ROLE_ADMIN"}) public class ModuleRestController { private TerraformModuleRepository moduleRepository; private TerraformModuleGitRepository moduleGitRepository; + private RegistryService registryService; + @Autowired - public ModuleRestController(TerraformModuleRepository moduleRepository, TerraformModuleGitRepository moduleGitRepository) { + public ModuleRestController(TerraformModuleRepository moduleRepository, TerraformModuleGitRepository moduleGitRepository, RegistryService registryService) { this.moduleRepository = moduleRepository; this.moduleGitRepository = moduleGitRepository; + this.registryService = registryService; } @GetMapping - public List findAllModules(User user){ - if(user.isAdmin()){ + public List findAllModules(User user) { + if (user.isAdmin()) { return moduleRepository.findAll(); } - if(user.getTeam() != null){ + if (user.getTeam() != null) { return moduleRepository.findAllByModuleMetadataCreatedByOrAuthorizedTeamsContaining(user, user.getTeam()); } return moduleRepository.findAllByModuleMetadataCreatedBy(user); } @GetMapping("/{id}") - public TerraformModule findModule(@PathVariable String id, User user){ + public TerraformModule findModule(@PathVariable String id, User user) { var module = moduleRepository.findById(id).orElseThrow(ModuleNotFoundException::new); - if(!module.isAuthorizedFor(user)){ + if (!module.isAuthorizedFor(user)) { throw new ModuleForbiddenException(); } return module; @@ -56,16 +60,16 @@ public TerraformModule findModule(@PathVariable String id, User user){ @PostMapping @ResponseStatus(HttpStatus.CREATED) - public TerraformModule createModule(@RequestBody TerraformModule module, User user){ + public TerraformModule createModule(@RequestBody TerraformModule module, User user) { module.setId(UUID.randomUUID().toString()); module.getModuleMetadata().setCreatedBy(user); return moduleRepository.save(module); } @PutMapping("/{id}") - public TerraformModule saveModule(@PathVariable String id, @RequestBody @Valid TerraformModule module, User user){ + public TerraformModule saveModule(@PathVariable String id, @RequestBody @Valid TerraformModule module, User user) { var existingModule = moduleRepository.findById(id).orElseThrow(ModuleNotFoundException::new); - if(!existingModule.isAuthorizedFor(user)){ + if (!existingModule.isAuthorizedFor(user)) { throw new ModuleForbiddenException(); } @@ -78,16 +82,43 @@ public TerraformModule saveModule(@PathVariable String id, @RequestBody @Valid T @GetMapping(value = "/{id}/readme", produces = MediaType.TEXT_PLAIN_VALUE) public String readme(@PathVariable String id) { var module = moduleRepository.findById(id).orElseThrow(); - return moduleGitRepository.getReadme(module).orElseThrow( () -> new ResponseStatusException(HttpStatus.NOT_FOUND) ); + return moduleGitRepository.getReadme(module).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND)); + } + + /** + * Refresh the module definition (variables/outputs) from the repository (git) + * + * @param id id of the module to refresh + * @param user loggued user + * @return the refreshed module + */ + @PostMapping(value = "/{id}/refresh") + public TerraformModule refreshModule(@PathVariable String id, User user) { + var existingModule = moduleRepository.findById(id).orElseThrow(ModuleNotFoundException::new); + if (!existingModule.isAuthorizedFor(user)) { + throw new ModuleForbiddenException(); + } + + var projectId = existingModule.getRegistryDetails().getProjectId(); + var registryType = existingModule.getRegistryDetails().getRegistryType(); + + // refresh variables & outputs + existingModule.setVariables(registryService.importVariables(projectId, registryType, user)); + existingModule.setOutputs(registryService.importOutputs(projectId, registryType, user)); + + existingModule.getModuleMetadata().setUpdatedBy(user); + existingModule.getModuleMetadata().setUpdatedAt(LocalDateTime.now()); + + return moduleRepository.save(existingModule); } } @ResponseStatus(HttpStatus.NOT_FOUND) -class ModuleNotFoundException extends RuntimeException{ +class ModuleNotFoundException extends RuntimeException { } @ResponseStatus(HttpStatus.FORBIDDEN) -class ModuleForbiddenException extends RuntimeException{ +class ModuleForbiddenException extends RuntimeException { } diff --git a/src/test/java/io/gaia_app/modules/controller/ModuleRestControllerTest.java b/src/test/java/io/gaia_app/modules/controller/ModuleRestControllerTest.java index dfd71f6ca..1fa178bac 100644 --- a/src/test/java/io/gaia_app/modules/controller/ModuleRestControllerTest.java +++ b/src/test/java/io/gaia_app/modules/controller/ModuleRestControllerTest.java @@ -4,6 +4,9 @@ import io.gaia_app.modules.bo.TerraformModule; import io.gaia_app.modules.repository.TerraformModuleGitRepository; import io.gaia_app.modules.repository.TerraformModuleRepository; +import io.gaia_app.registries.RegistryDetails; +import io.gaia_app.registries.RegistryType; +import io.gaia_app.registries.service.RegistryService; import io.gaia_app.teams.Team; import io.gaia_app.teams.User; import io.gaia_app.modules.bo.TerraformModule; @@ -14,6 +17,7 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import org.testcontainers.shaded.org.bouncycastle.math.raw.Mod; import java.time.LocalDateTime; import java.util.List; @@ -36,6 +40,9 @@ class ModuleRestControllerTest { @Mock private TerraformModuleGitRepository moduleGitRepository; + @Mock + private RegistryService registryService; + private User admin; private User bob; @@ -247,4 +254,23 @@ void readme_shouldThrowExceptionIfModuleNotFound() { verifyNoInteractions(moduleGitRepository); } + @Test + void refreshModule_shouldRefreshModuleVariablesAndOutputs(){ + // given + var module = new TerraformModule(); + module.getModuleMetadata().setCreatedBy(bob); + module.setRegistryDetails(new RegistryDetails(RegistryType.GITHUB, "github/id")); + when(moduleRepository.findById("12")).thenReturn(Optional.of(module)); + + // when + moduleRestController.refreshModule("12", bob); + + // then + verify(registryService).importVariables("github/id", RegistryType.GITHUB, bob); + verify(registryService).importOutputs("github/id", RegistryType.GITHUB, bob); + + assertThat(module.getModuleMetadata().getUpdatedAt()).isEqualToIgnoringMinutes(LocalDateTime.now()); + assertThat(module.getModuleMetadata().getUpdatedBy()).isEqualTo(bob); + } + }