Skip to content

Commit

Permalink
✨ : parse output definition when importing a module
Browse files Browse the repository at this point in the history
this will help in rendering outputs in the stack page, as we'll be able
to show description and special behaviour for sensitive outputs.
  • Loading branch information
juwit committed Aug 13, 2021
1 parent 76589c6 commit 4d477de
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 6 deletions.
11 changes: 11 additions & 0 deletions src/main/java/io/gaia_app/modules/bo/TerraformModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class TerraformModule {
@Valid
private List<Variable> variables = new ArrayList<>();

@Valid
private List<Output> outputs = new ArrayList<>();

@NotBlank
private String name;

Expand Down Expand Up @@ -102,6 +105,14 @@ public void setVariables(List<Variable> variables) {
this.variables = variables;
}

public List<Output> getOutputs() {
return outputs;
}

public void setOutputs(List<Output> outputs) {
this.outputs = outputs;
}

public TerraformImage getTerraformImage() {
return terraformImage;
}
Expand Down
29 changes: 24 additions & 5 deletions src/main/java/io/gaia_app/registries/service/RegistryService.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<Variable>

/**
* import outputs definition from a repository, by reading the outputs.tf file and parsing it
*/
fun importOutputs(projectId: String, registryType: RegistryType, user: User): List<Output>
}

@Service
Expand All @@ -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")!!
Expand All @@ -50,4 +60,13 @@ class RegistryServiceImpl(
return moduleRepository.save(module)
}

override fun importVariables(projectId: String, registryType: RegistryType, user: User): List<Variable> {
val variablesFile = registryApis[registryType]?.getFileContent(user, projectId, "variables.tf")!!
return hclParser.parseVariables(variablesFile)
}

override fun importOutputs(projectId: String, registryType: RegistryType, user: User): List<Output> {
val outputsFile = registryApis[registryType]?.getFileContent(user, projectId, "outputs.tf")!!
return hclParser.parseOutputs(outputsFile)
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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" }
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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" }
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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")

Expand All @@ -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"))
}

}
Original file line number Diff line number Diff line change
@@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Original file line number Diff line number Diff line change
@@ -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=="
}

0 comments on commit 4d477de

Please sign in to comment.