Skip to content

Commit

Permalink
♻️ : create a service for module creation
Browse files Browse the repository at this point in the history
resolves #206
  • Loading branch information
juwit committed Jan 25, 2020
1 parent d1384c7 commit dce1a8c
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 148 deletions.
10 changes: 5 additions & 5 deletions src/main/java/io/codeka/gaia/registries/AbstractRegistryApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import org.springframework.http.HttpStatus
import org.springframework.web.client.RestTemplate
import java.util.*

abstract class AbstractRegistryApi<K>(val restTemplate: RestTemplate,
private val registryType: RegistryType,
private val registryFileClass: Class<K>,
private val registryListFileClass: Class<Array<K>>): RegistryApi<K> {
abstract class AbstractRegistryApi<K: SourceRepository>(val restTemplate: RestTemplate,
private val registryType: RegistryType,
private val registryFileClass: Class<K>,
private val registryListFileClass: Class<Array<K>>): RegistryApi<K> {

private fun <T> callWithAuth(url: String, token: String, responseType: Class<T>): T {
val headers = HttpHeaders()
Expand All @@ -25,7 +25,7 @@ abstract class AbstractRegistryApi<K>(val restTemplate: RestTemplate,
requestEntity,
responseType)
if(response.statusCode == HttpStatus.OK) {
return response.body
return response.body!!
}
else {
TODO("error code mgmt")
Expand Down
21 changes: 0 additions & 21 deletions src/main/java/io/codeka/gaia/registries/config/RegistryConfig.java

This file was deleted.

32 changes: 32 additions & 0 deletions src/main/java/io/codeka/gaia/registries/config/RegistryConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.codeka.gaia.registries.config

import io.codeka.gaia.registries.RegistryApi
import io.codeka.gaia.registries.RegistryOAuth2Provider
import io.codeka.gaia.registries.RegistryType
import io.codeka.gaia.registries.SourceRepository
import io.codeka.gaia.registries.github.GitHubOAuth2Provider
import io.codeka.gaia.registries.github.GithubRegistryApi
import io.codeka.gaia.registries.gitlab.GitLabOAuth2Provider
import io.codeka.gaia.registries.gitlab.GitlabRegistryApi
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

/**
* Config for registry management
*/
@Configuration
open class RegistryConfig {

@Bean
open fun registryOAuth2Providers(): List<RegistryOAuth2Provider> {
return listOf(GitHubOAuth2Provider(), GitLabOAuth2Provider())
}

@Bean
open fun registryApis(
githubRegistryApi: GithubRegistryApi,
gitlabRegistryApi: GitlabRegistryApi): Map<RegistryType, RegistryApi<out SourceRepository>> {
return mapOf(Pair(RegistryType.GITHUB, githubRegistryApi),
Pair(RegistryType.GITLAB, gitlabRegistryApi))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import io.codeka.gaia.modules.repository.TerraformModuleRepository
import io.codeka.gaia.registries.RegistryApi
import io.codeka.gaia.registries.RegistryDetails
import io.codeka.gaia.registries.RegistryType
import io.codeka.gaia.registries.github.GithubRegistryApi
import io.codeka.gaia.registries.github.GithubRepository
import io.codeka.gaia.registries.service.RegistryService
import io.codeka.gaia.teams.User
import org.springframework.http.HttpStatus
import org.springframework.security.access.annotation.Secured
Expand All @@ -20,9 +22,7 @@ import java.util.*
@Secured
class GithubRegistryController(
private val githubRegistryApi: RegistryApi<GithubRepository>,
private val hclParser: HclParser,
private val cliRepository: TerraformCLIRepository,
private val moduleRepository: TerraformModuleRepository) {
private val registryService: RegistryService) {

@GetMapping("/repositories")
fun getRepositories(user: User): List<GithubRepository> {
Expand All @@ -32,24 +32,6 @@ class GithubRegistryController(
@GetMapping("/repositories/{owner}/{repo}/import")
@ResponseStatus(HttpStatus.CREATED)
fun importRepository(@PathVariable owner: String, @PathVariable repo: String, user: User): TerraformModule {
val module = TerraformModule()
module.id = UUID.randomUUID().toString()

val githubRepository = githubRegistryApi.getRepository(user, "$owner/$repo")

module.gitRepositoryUrl = githubRepository.htmlUrl
module.gitBranch = "master"
module.name = githubRepository.fullName
module.cliVersion = cliRepository.listCLIVersion().first()

module.registryDetails = RegistryDetails(RegistryType.GITHUB, githubRepository.fullName)
module.moduleMetadata = ModuleMetadata(createdBy = user)

// get variables
val variablesFile = githubRegistryApi.getFileContent(user, "$owner/$repo", "variables.tf")
module.variables = hclParser.parseVariables(variablesFile)

// saving module !
return moduleRepository.save(module)
return registryService.importRepository("$owner/$repo", RegistryType.GITHUB, user)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.codeka.gaia.registries.RegistryApi
import io.codeka.gaia.registries.RegistryDetails
import io.codeka.gaia.registries.RegistryType
import io.codeka.gaia.registries.gitlab.GitlabRepository
import io.codeka.gaia.registries.service.RegistryService
import io.codeka.gaia.teams.User
import org.springframework.http.HttpStatus
import org.springframework.security.access.annotation.Secured
Expand All @@ -19,9 +20,7 @@ import java.util.*
@Secured
class GitlabRegistryController(
private val gitlabRegistryApi: RegistryApi<GitlabRepository>,
private val hclParser: HclParser,
private val cliRepository: TerraformCLIRepository,
private val moduleRepository: TerraformModuleRepository) {
private val registryService: RegistryService) {

@GetMapping("/repositories")
fun getRepositories(user: User): List<GitlabRepository> {
Expand All @@ -31,24 +30,6 @@ class GitlabRegistryController(
@GetMapping("/repositories/{projectId}/import")
@ResponseStatus(HttpStatus.CREATED)
fun importRepository(@PathVariable projectId: String, user: User): TerraformModule {
val module = TerraformModule()
module.id = UUID.randomUUID().toString()

val gitlabRepository = gitlabRegistryApi.getRepository(user, projectId)

module.gitRepositoryUrl = gitlabRepository.htmlUrl
module.gitBranch = "master"
module.name = gitlabRepository.fullName
module.cliVersion = cliRepository.listCLIVersion().first()

module.registryDetails = RegistryDetails(RegistryType.GITLAB, gitlabRepository.id)
module.moduleMetadata.createdBy = user

// get variables
val variablesFile = gitlabRegistryApi.getFileContent(user, projectId, "variables.tf")
module.variables = hclParser.parseVariables(variablesFile)

// saving module !
return moduleRepository.save(module)
return registryService.importRepository(projectId, RegistryType.GITLAB, user)
}
}
50 changes: 50 additions & 0 deletions src/main/java/io/codeka/gaia/registries/service/RegistryService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.codeka.gaia.registries.service

import io.codeka.gaia.hcl.HclParser
import io.codeka.gaia.modules.bo.ModuleMetadata
import io.codeka.gaia.modules.bo.TerraformModule
import io.codeka.gaia.modules.repository.TerraformCLIRepository
import io.codeka.gaia.modules.repository.TerraformModuleRepository
import io.codeka.gaia.registries.RegistryApi
import io.codeka.gaia.registries.RegistryDetails
import io.codeka.gaia.registries.RegistryType
import io.codeka.gaia.registries.SourceRepository
import io.codeka.gaia.teams.User
import org.springframework.stereotype.Service
import java.util.*

interface RegistryService {
fun importRepository(projectId: String, registryType: RegistryType, user: User): TerraformModule
}

@Service
class RegistryServiceImpl(
private val cliRepository: TerraformCLIRepository,
private val hclParser: HclParser,
private val moduleRepository: TerraformModuleRepository,
private val registryApis: Map<RegistryType, RegistryApi<out SourceRepository>>
) : RegistryService {

override fun importRepository(projectId: String, registryType: RegistryType, user: User) : TerraformModule {
val module = TerraformModule()
module.id = UUID.randomUUID().toString()

val codeRepository = registryApis[registryType]?.getRepository(user, projectId)!!

module.gitRepositoryUrl = codeRepository.htmlUrl
module.gitBranch = "master"
module.name = codeRepository.fullName
module.cliVersion = cliRepository.listCLIVersion().first()

module.registryDetails = RegistryDetails(registryType, codeRepository.id)
module.moduleMetadata = ModuleMetadata(createdBy = user)

// get variables
val variablesFile = registryApis[registryType]?.getFileContent(user, projectId, "variables.tf")!!
module.variables = hclParser.parseVariables(variablesFile)

// saving module !
return moduleRepository.save(module)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import io.codeka.gaia.registries.RegistryRawContent;
import io.codeka.gaia.registries.github.GitHubOAuth2Provider;
import io.codeka.gaia.registries.github.GitHubRawContent;
import io.codeka.gaia.registries.github.GithubRegistryApi;
import io.codeka.gaia.registries.gitlab.GitLabOAuth2Provider;
import io.codeka.gaia.registries.gitlab.GitLabRawContent;
import io.codeka.gaia.registries.gitlab.GitlabRegistryApi;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -15,7 +17,7 @@

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(classes = {RegistryConfig.class, BeansConfig.class, GitHubRawContent.class, GitLabRawContent.class})
@SpringBootTest(classes = {RegistryConfig.class, BeansConfig.class, GitHubRawContent.class, GitLabRawContent.class, GithubRegistryApi.class, GitlabRegistryApi.class})
class RegistryConfigIT {
@Test
void registryRawContents_shouldBeInstantiated(@Autowired(required = false) List<RegistryRawContent> registryRawContents) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.codeka.gaia.modules.repository.TerraformModuleRepository
import io.codeka.gaia.registries.RegistryApi
import io.codeka.gaia.registries.RegistryType
import io.codeka.gaia.registries.github.GithubRepository
import io.codeka.gaia.registries.service.RegistryService
import io.codeka.gaia.teams.User
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
Expand All @@ -29,13 +30,7 @@ class GithubRegistryControllerTest{
lateinit var githubRegistryApi: RegistryApi<GithubRepository>

@Mock
lateinit var hclParser: HclParser

@Mock
lateinit var terraformCLIRepository: TerraformCLIRepository

@Mock
lateinit var moduleRepository: TerraformModuleRepository
lateinit var registryService: RegistryService

@InjectMocks
lateinit var githubRegistryController: GithubRegistryController
Expand All @@ -52,41 +47,16 @@ class GithubRegistryControllerTest{

@Test
fun `importRepository() should call the github registry and create a module`() {
// returns saved module as first arg
whenever(moduleRepository.save(any(TerraformModule::class.java))).then { it.arguments[0] }

whenever(terraformCLIRepository.listCLIVersion()).thenReturn(listOf("1.12.8", "1.12.7"))

// given
val user = User("juwit", null)

val githubRepository = GithubRepository(fullName="juwit/terraform-docker-mongo", htmlUrl = "https://github.com/juwit/terraform-docker-mongo")
whenever(githubRegistryApi.getRepository(user, "juwit/terraform-docker-mongo")).thenReturn(githubRepository)

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 module = githubRegistryController.importRepository("juwit", "terraform-docker-mongo", user)

verify(githubRegistryApi).getRepository(user, "juwit/terraform-docker-mongo")
verify(githubRegistryApi).getFileContent(user, "juwit/terraform-docker-mongo", "variables.tf")

verifyNoMoreInteractions(githubRegistryApi)

assertThat(module.id).isNotBlank()

assertThat(module.name).isEqualTo("juwit/terraform-docker-mongo")
assertThat(module.gitRepositoryUrl).isEqualTo("https://github.com/juwit/terraform-docker-mongo")
assertThat(module.gitBranch).isEqualTo("master")

assertThat(module.registryDetails.registryType).isEqualTo(RegistryType.GITHUB)
assertThat(module.registryDetails.projectId).isEqualTo("juwit/terraform-docker-mongo")

assertThat(module.cliVersion).isEqualTo("1.12.8")
assertThat(module.moduleMetadata.createdBy).isEqualTo(user)
// when
githubRegistryController.importRepository("juwit", "terraform-docker-mongo", user)

assertThat(module.variables).containsExactly(Variable("dummy"))
// then
verify(registryService).importRepository("juwit/terraform-docker-mongo", RegistryType.GITHUB, user)

verifyNoMoreInteractions(registryService)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.codeka.gaia.registries.RegistryApi
import io.codeka.gaia.registries.RegistryType
import io.codeka.gaia.registries.github.GithubRepository
import io.codeka.gaia.registries.gitlab.GitlabRepository
import io.codeka.gaia.registries.service.RegistryService
import io.codeka.gaia.teams.User
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
Expand All @@ -27,13 +28,7 @@ class GitlabRegistryControllerTest{
lateinit var gitlabRegistryApi: RegistryApi<GitlabRepository>

@Mock
lateinit var hclParser: HclParser

@Mock
lateinit var terraformCLIRepository: TerraformCLIRepository

@Mock
lateinit var moduleRepository: TerraformModuleRepository
lateinit var registryService: RegistryService

@InjectMocks
lateinit var gitlabRegistryController: GitlabRegistryController
Expand All @@ -49,41 +44,16 @@ class GitlabRegistryControllerTest{
}

@Test
fun `importRepository() should call the gitlab registry and create a module`() {
// returns saved module as first arg
whenever(moduleRepository.save(any(TerraformModule::class.java))).then { it.arguments[0] }

whenever(terraformCLIRepository.listCLIVersion()).thenReturn(listOf("1.12.8", "1.12.7"))

fun `importRepository() should call the registryService`() {
// given
val user = User("juwit", null)

val gitlabRepository = GitlabRepository("15689", "juwit/terraform-docker-mongo", "https://gitlab.com/juwit/terraform-docker-mongo")
whenever(gitlabRegistryApi.getRepository(user, "15689")).thenReturn(gitlabRepository)

val variablesFileContent = "mock file content"
whenever(gitlabRegistryApi.getFileContent(user, "15689", "variables.tf")).thenReturn(variablesFileContent)
whenever(hclParser.parseVariables(variablesFileContent)).thenReturn(listOf(Variable("dummy")))

val module = gitlabRegistryController.importRepository("15689", user)

verify(gitlabRegistryApi).getRepository(user, "15689")
verify(gitlabRegistryApi).getFileContent(user, "15689", "variables.tf")

verifyNoMoreInteractions(gitlabRegistryApi)

assertThat(module.id).isNotBlank()

assertThat(module.name).isEqualTo("juwit/terraform-docker-mongo")
assertThat(module.gitRepositoryUrl).isEqualTo("https://gitlab.com/juwit/terraform-docker-mongo")
assertThat(module.gitBranch).isEqualTo("master")

assertThat(module.registryDetails.registryType).isEqualTo(RegistryType.GITLAB)
assertThat(module.registryDetails.projectId).isEqualTo("15689")

assertThat(module.cliVersion).isEqualTo("1.12.8")
assertThat(module.moduleMetadata.createdBy).isEqualTo(user)
// when
gitlabRegistryController.importRepository("15689", user)

assertThat(module.variables).containsExactly(Variable("dummy"))
// then
verify(registryService).importRepository("15689", RegistryType.GITLAB, user)

verifyNoMoreInteractions(registryService)
}
}

0 comments on commit dce1a8c

Please sign in to comment.