diff --git a/src/main/java/io/gaia_app/modules/bo/TerraformModule.java b/src/main/java/io/gaia_app/modules/bo/TerraformModule.java index 189fab2ee..3a5a44746 100644 --- a/src/main/java/io/gaia_app/modules/bo/TerraformModule.java +++ b/src/main/java/io/gaia_app/modules/bo/TerraformModule.java @@ -25,6 +25,9 @@ public class TerraformModule { @Valid private List variables = new ArrayList<>(); + @Valid + private List outputs = new ArrayList<>(); + @NotBlank private String name; @@ -102,6 +105,14 @@ public void setVariables(List variables) { this.variables = variables; } + public List getOutputs() { + return outputs; + } + + public void setOutputs(List outputs) { + this.outputs = outputs; + } + public TerraformImage getTerraformImage() { return terraformImage; } diff --git a/src/main/java/io/gaia_app/registries/service/RegistryService.kt b/src/main/java/io/gaia_app/registries/service/RegistryService.kt index 6e8faaede..f8145cd02 100644 --- a/src/main/java/io/gaia_app/registries/service/RegistryService.kt +++ b/src/main/java/io/gaia_app/registries/service/RegistryService.kt @@ -1,9 +1,7 @@ package io.gaia_app.registries.service import io.gaia_app.hcl.HclParser -import io.gaia_app.modules.bo.ModuleMetadata -import io.gaia_app.modules.bo.TerraformImage -import io.gaia_app.modules.bo.TerraformModule +import io.gaia_app.modules.bo.* import io.gaia_app.modules.repository.TerraformModuleRepository import io.gaia_app.registries.RegistryApi import io.gaia_app.registries.RegistryDetails @@ -15,6 +13,16 @@ import java.util.* interface RegistryService { fun importRepository(projectId: String, registryType: RegistryType, user: User): TerraformModule + + /** + * import variables definition from a repository, by reading the variables.tf file and parsing it + */ + fun importVariables(projectId: String, registryType: RegistryType, user: User): List + + /** + * import outputs definition from a repository, by reading the outputs.tf file and parsing it + */ + fun importOutputs(projectId: String, registryType: RegistryType, user: User): List } @Service @@ -39,8 +47,10 @@ class RegistryServiceImpl( module.moduleMetadata = ModuleMetadata(createdBy = user) // get variables - val variablesFile = registryApis[registryType]?.getFileContent(user, projectId, "variables.tf")!! - module.variables = hclParser.parseVariables(variablesFile) + module.variables = this.importVariables(projectId, registryType, user) + + // get outputs + module.outputs = this.importOutputs(projectId, registryType, user) // find main provider val mainFile = registryApis[registryType]?.getFileContent(user, projectId, "main.tf")!! @@ -50,4 +60,13 @@ class RegistryServiceImpl( return moduleRepository.save(module) } + override fun importVariables(projectId: String, registryType: RegistryType, user: User): List { + val variablesFile = registryApis[registryType]?.getFileContent(user, projectId, "variables.tf")!! + return hclParser.parseVariables(variablesFile) + } + + override fun importOutputs(projectId: String, registryType: RegistryType, user: User): List { + val outputsFile = registryApis[registryType]?.getFileContent(user, projectId, "outputs.tf")!! + return hclParser.parseOutputs(outputsFile) + } } diff --git a/src/test/java/io/gaia_app/registries/controller/GithubRegistryControllerIT.kt b/src/test/java/io/gaia_app/registries/controller/GithubRegistryControllerIT.kt index e356a85b8..36e018b6b 100644 --- a/src/test/java/io/gaia_app/registries/controller/GithubRegistryControllerIT.kt +++ b/src/test/java/io/gaia_app/registries/controller/GithubRegistryControllerIT.kt @@ -1,6 +1,7 @@ package io.gaia_app.registries.controller import com.fasterxml.jackson.databind.ObjectMapper +import io.gaia_app.modules.bo.Output import io.gaia_app.modules.bo.Variable import io.gaia_app.modules.repository.TerraformModuleRepository import io.gaia_app.registries.RegistryDetails @@ -100,6 +101,10 @@ class GithubRegistryControllerIT: SharedMongoContainerTest() { .andExpect(MockRestRequestMatchers.header("Authorization", "Bearer Tok'ra")) .andRespond(MockRestResponseCreators.withSuccess(ClassPathResource("/rest/github/selmak-terraform-docker-mongo-content-variables.json"), MediaType.APPLICATION_JSON)) + server.expect(requestTo("https://api.github.com/repos/selmak/terraform-docker-mongo/contents/outputs.tf")) + .andExpect(MockRestRequestMatchers.header("Authorization", "Bearer Tok'ra")) + .andRespond(MockRestResponseCreators.withSuccess(ClassPathResource("/rest/github/selmak-terraform-docker-mongo-content-outputs.json"), MediaType.APPLICATION_JSON)) + server.expect(requestTo("https://api.github.com/repos/selmak/terraform-docker-mongo/contents/main.tf")) .andExpect(MockRestRequestMatchers.header("Authorization", "Bearer Tok'ra")) .andRespond(MockRestResponseCreators.withSuccess(ClassPathResource("/rest/github/selmak-terraform-docker-mongo-content-main.json"), MediaType.APPLICATION_JSON)) @@ -128,6 +133,10 @@ class GithubRegistryControllerIT: SharedMongoContainerTest() { Variable("mongo_exposed_port", "string", "exposed port of the mongo container", "27017") ) + assertThat(importedModule.outputs).containsExactly( + Output("docker_container_name", "docker_container.mongo.name", "name of the docker container", "false") + ) + assertThat(terraformModuleRepository.findById(importedModule.id)) .isNotEmpty .hasValueSatisfying { it.name == "selmak/terraform-docker-mongo" } diff --git a/src/test/java/io/gaia_app/registries/controller/GitlabRegistryControllerIT.kt b/src/test/java/io/gaia_app/registries/controller/GitlabRegistryControllerIT.kt index ac750c7ef..ae55db881 100644 --- a/src/test/java/io/gaia_app/registries/controller/GitlabRegistryControllerIT.kt +++ b/src/test/java/io/gaia_app/registries/controller/GitlabRegistryControllerIT.kt @@ -1,6 +1,7 @@ package io.gaia_app.registries.controller import com.fasterxml.jackson.databind.ObjectMapper +import io.gaia_app.modules.bo.Output import io.gaia_app.modules.bo.Variable import io.gaia_app.modules.repository.TerraformModuleRepository import io.gaia_app.registries.RegistryDetails @@ -96,6 +97,10 @@ class GitlabRegistryControllerIT: SharedMongoContainerTest() { .andExpect(MockRestRequestMatchers.header("Authorization", "Bearer Tok'ra")) .andRespond(MockRestResponseCreators.withSuccess(ClassPathResource("/rest/gitlab/selmak-terraform-docker-mongo-content-variables.json"), MediaType.APPLICATION_JSON)) + server.expect(requestTo("https://gitlab.com/api/v4/projects/16181047/repository/files/outputs.tf?ref=master")) + .andExpect(MockRestRequestMatchers.header("Authorization", "Bearer Tok'ra")) + .andRespond(MockRestResponseCreators.withSuccess(ClassPathResource("/rest/gitlab/selmak-terraform-docker-mongo-content-outputs.json"), MediaType.APPLICATION_JSON)) + server.expect(requestTo("https://gitlab.com/api/v4/projects/16181047/repository/files/main.tf?ref=master")) .andExpect(MockRestRequestMatchers.header("Authorization", "Bearer Tok'ra")) .andRespond(MockRestResponseCreators.withSuccess(ClassPathResource("/rest/gitlab/selmak-terraform-docker-mongo-content-main.json"), MediaType.APPLICATION_JSON)) @@ -124,6 +129,10 @@ class GitlabRegistryControllerIT: SharedMongoContainerTest() { Variable("mongo_exposed_port", "string", "exposed port of the mongo container", "27017") ) + assertThat(importedModule.outputs).containsExactly( + Output("docker_container_name", "docker_container.mongo.name", "name of the docker container", "false") + ) + assertThat(terraformModuleRepository.findById(importedModule.id)) .isNotEmpty .hasValueSatisfying { it.name == "selmak/terraform-docker-mongo" } diff --git a/src/test/java/io/gaia_app/registries/service/RegistryServiceImplTest.kt b/src/test/java/io/gaia_app/registries/service/RegistryServiceImplTest.kt index 1e1e41855..2a76c5510 100644 --- a/src/test/java/io/gaia_app/registries/service/RegistryServiceImplTest.kt +++ b/src/test/java/io/gaia_app/registries/service/RegistryServiceImplTest.kt @@ -1,6 +1,7 @@ package io.gaia_app.registries.service import io.gaia_app.hcl.HclParser +import io.gaia_app.modules.bo.Output import io.gaia_app.modules.bo.TerraformModule import io.gaia_app.modules.bo.Variable import io.gaia_app.modules.repository.TerraformModuleRepository @@ -60,6 +61,10 @@ class RegistryServiceImplTest { whenever(gitlabRegistryApi.getFileContent(user, "15689", "variables.tf")).thenReturn(variablesFileContent) whenever(hclParser.parseVariables(variablesFileContent)).thenReturn(listOf(Variable("dummy"))) + val outputsFileContent = "outputs file content" + whenever(gitlabRegistryApi.getFileContent(user, "15689", "outputs.tf")).thenReturn(outputsFileContent) + whenever(hclParser.parseOutputs(outputsFileContent)).thenReturn(listOf(Output("dummyOutput"))) + whenever(gitlabRegistryApi.getFileContent(user, "15689", "main.tf")).thenReturn(variablesFileContent) whenever(hclParser.parseProvider(variablesFileContent)).thenReturn("docker") @@ -101,6 +106,10 @@ class RegistryServiceImplTest { whenever(githubRegistryApi.getFileContent(user, "juwit/terraform-docker-mongo", "variables.tf")).thenReturn(variablesFileContent) whenever(hclParser.parseVariables(variablesFileContent)).thenReturn(listOf(Variable("dummy"))) + val outputsFileContent = "outputs file content" + whenever(githubRegistryApi.getFileContent(user, "juwit/terraform-docker-mongo", "outputs.tf")).thenReturn(outputsFileContent) + whenever(hclParser.parseOutputs(outputsFileContent)).thenReturn(listOf(Output("dummyOutput"))) + whenever(githubRegistryApi.getFileContent(user, "juwit/terraform-docker-mongo", "main.tf")).thenReturn(variablesFileContent) whenever(hclParser.parseProvider(variablesFileContent)).thenReturn("docker") @@ -127,4 +136,41 @@ class RegistryServiceImplTest { assertThat(module.variables).containsExactly(Variable("dummy")) } + + @Test + fun `importVariables() should call the registry and return variables definitions`() { + // returns saved module as first arg + val user = User("juwit", null) + + val variablesFileContent = "mock file content" + whenever(githubRegistryApi.getFileContent(user, "juwit/terraform-docker-mongo", "variables.tf")).thenReturn(variablesFileContent) + whenever(hclParser.parseVariables(variablesFileContent)).thenReturn(listOf(Variable("dummy"))) + + val variables = registryService.importVariables("juwit/terraform-docker-mongo", RegistryType.GITHUB, user) + + verify(githubRegistryApi).getFileContent(user, "juwit/terraform-docker-mongo", "variables.tf") + + verifyNoMoreInteractions(githubRegistryApi) + + assertThat(variables).containsExactly(Variable("dummy")) + } + + @Test + fun `importOutputs() should call the registry and return outputs definitions`() { + // returns saved module as first arg + val user = User("juwit", null) + + val outputsFileContent = "mock file content" + whenever(githubRegistryApi.getFileContent(user, "juwit/terraform-docker-mongo", "outputs.tf")).thenReturn(outputsFileContent) + whenever(hclParser.parseOutputs(outputsFileContent)).thenReturn(listOf(Output("dummy"))) + + val outputs = registryService.importOutputs("juwit/terraform-docker-mongo", RegistryType.GITHUB, user) + + verify(githubRegistryApi).getFileContent(user, "juwit/terraform-docker-mongo", "outputs.tf") + + verifyNoMoreInteractions(githubRegistryApi) + + assertThat(outputs).containsExactly(Output("dummy")) + } + } diff --git a/src/test/resources/rest/github/selmak-terraform-docker-mongo-content-outputs.json b/src/test/resources/rest/github/selmak-terraform-docker-mongo-content-outputs.json new file mode 100644 index 000000000..1b8fbf152 --- /dev/null +++ b/src/test/resources/rest/github/selmak-terraform-docker-mongo-content-outputs.json @@ -0,0 +1,18 @@ +{ + "name": "outputs.tf", + "path": "outputs.tf", + "sha": "fb9ddf63eb2cccd4dd5e4b352fdae5c59e5d67c2", + "size": 169, + "url": "https://api.github.com/repos/juwit/terraform-docker-mongo/contents/outputs.tf?ref=master", + "html_url": "https://github.com/juwit/terraform-docker-mongo/blob/master/outputs.tf", + "git_url": "https://api.github.com/repos/juwit/terraform-docker-mongo/git/blobs/fb9ddf63eb2cccd4dd5e4b352fdae5c59e5d67c2", + "download_url": "https://raw.githubusercontent.com/juwit/terraform-docker-mongo/master/outputs.tf", + "type": "file", + "content": "IyBvdXRwdXQgdGhlIGNvbnRhaW5lciBuYW1lIGFzIGFuIGV4YW1wbGUKCm91\ndHB1dCAiZG9ja2VyX2NvbnRhaW5lcl9uYW1lIiB7CiAgZGVzY3JpcHRpb24g\nPSAibmFtZSBvZiB0aGUgZG9ja2VyIGNvbnRhaW5lciIKICB2YWx1ZSAgICAg\nICA9IGRvY2tlcl9jb250YWluZXIubW9uZ28ubmFtZQp9Cg==\n", + "encoding": "base64", + "_links": { + "self": "https://api.github.com/repos/juwit/terraform-docker-mongo/contents/outputs.tf?ref=master", + "git": "https://api.github.com/repos/juwit/terraform-docker-mongo/git/blobs/fb9ddf63eb2cccd4dd5e4b352fdae5c59e5d67c2", + "html": "https://github.com/juwit/terraform-docker-mongo/blob/master/outputs.tf" + } +} diff --git a/src/test/resources/rest/github/selmak-terraform-docker-mongo-content-variables.json b/src/test/resources/rest/github/selmak-terraform-docker-mongo-content-variables.json index edfe5d702..15b0ebd01 100644 --- a/src/test/resources/rest/github/selmak-terraform-docker-mongo-content-variables.json +++ b/src/test/resources/rest/github/selmak-terraform-docker-mongo-content-variables.json @@ -15,4 +15,4 @@ "git": "https://api.github.com/repos/juwit/terraform-docker-mongo/git/blobs/533f859b7c038f7d3003ca3f693abc34370bcf67", "html": "https://github.com/juwit/terraform-docker-mongo/blob/master/variables.tf" } -} \ No newline at end of file +} diff --git a/src/test/resources/rest/gitlab/selmak-terraform-docker-mongo-content-outputs.json b/src/test/resources/rest/gitlab/selmak-terraform-docker-mongo-content-outputs.json new file mode 100644 index 000000000..77cd57299 --- /dev/null +++ b/src/test/resources/rest/gitlab/selmak-terraform-docker-mongo-content-outputs.json @@ -0,0 +1,12 @@ +{ + "file_name": "outputs.tf", + "file_path": "outputs.tf", + "size": 169, + "encoding": "base64", + "content_sha256": "2427e98c3dfdc3778b1174d21a302151fb4cd6634300a0fd759c6952b0682028", + "ref": "master", + "blob_id": "fb9ddf63eb2cccd4dd5e4b352fdae5c59e5d67c2", + "commit_id": "5ed5569ba3ead1966c0b39e04f3e6de676bb5564", + "last_commit_id": "5ed5569ba3ead1966c0b39e04f3e6de676bb5564", + "content": "IyBvdXRwdXQgdGhlIGNvbnRhaW5lciBuYW1lIGFzIGFuIGV4YW1wbGUKCm91dHB1dCAiZG9ja2VyX2NvbnRhaW5lcl9uYW1lIiB7CiAgZGVzY3JpcHRpb24gPSAibmFtZSBvZiB0aGUgZG9ja2VyIGNvbnRhaW5lciIKICB2YWx1ZSAgICAgICA9IGRvY2tlcl9jb250YWluZXIubW9uZ28ubmFtZQp9Cg==" +}