Skip to content

Commit

Permalink
Merge pull request #142 from CodeKaio/kotlin
Browse files Browse the repository at this point in the history
🔀 : kotlin integration
  • Loading branch information
cdubuisson committed Sep 24, 2019
2 parents a8ce6e2 + ba0feac commit ed1d70c
Show file tree
Hide file tree
Showing 38 changed files with 206 additions and 226 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
# fetching master refs when building other branches helps sonar computing PRs
- git fetch origin +refs/heads/master:refs/remotes/origin/master
# the following command line builds the project, runs the tests with coverage and then execute the SonarCloud analysis
- mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent verify sonar:sonar
- mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent verify org.jacoco:jacoco-maven-plugin:report sonar:sonar
- stage: deploy
name: "Deploy to hub.docker.com"
script:
Expand Down
40 changes: 39 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>io.codeka</groupId>
<artifactId>gaia</artifactId>
<version>1.2.0</version>
<version>1.3.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>gaia</name>
Expand Down Expand Up @@ -42,9 +42,23 @@
<properties>
<java.version>11</java.version>
<junit-jupiter.version>5.5.0</junit-jupiter.version>
<kotlin.version>1.3.50</kotlin.version>
</properties>

<dependencies>

<!-- kotlin standard lib -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
Expand Down Expand Up @@ -198,7 +212,31 @@
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<!-- kotlin compilation -->
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import io.codeka.gaia.modules.repository.TerraformModuleRepository;
import io.codeka.gaia.stacks.bo.StackState;
import io.codeka.gaia.stacks.repository.StackRepository;
import io.codeka.gaia.teams.bo.Team;
import io.codeka.gaia.teams.bo.User;
import io.codeka.gaia.teams.Team;
import io.codeka.gaia.teams.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/codeka/gaia/modules/bo/TerraformModule.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.codeka.gaia.modules.bo;

import io.codeka.gaia.teams.bo.Team;
import io.codeka.gaia.teams.bo.User;
import io.codeka.gaia.teams.Team;
import io.codeka.gaia.teams.User;
import org.springframework.data.mongodb.core.mapping.DBRef;

import javax.validation.Valid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,5 @@ public String getValidationRegex() {
public void setValidationRegex(String validationRegex) {
this.validationRegex = validationRegex;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.codeka.gaia.modules.bo.TerraformModule;
import io.codeka.gaia.modules.repository.TerraformModuleRepository;
import io.codeka.gaia.teams.bo.User;
import io.codeka.gaia.teams.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.codeka.gaia.modules.bo.TerraformModule;
import io.codeka.gaia.modules.repository.TerraformModuleGitRepository;
import io.codeka.gaia.modules.repository.TerraformModuleRepository;
import io.codeka.gaia.teams.bo.User;
import io.codeka.gaia.teams.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.annotation.Secured;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.codeka.gaia.modules.repository;

import io.codeka.gaia.modules.bo.TerraformModule;
import io.codeka.gaia.teams.bo.Team;
import io.codeka.gaia.teams.Team;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/codeka/gaia/stacks/bo/Job.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.codeka.gaia.stacks.bo;

import io.codeka.gaia.teams.bo.User;
import io.codeka.gaia.teams.User;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.util.CollectionUtils;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/codeka/gaia/stacks/bo/Stack.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.codeka.gaia.stacks.bo;

import io.codeka.gaia.teams.bo.Team;
import io.codeka.gaia.teams.bo.User;
import io.codeka.gaia.teams.Team;
import io.codeka.gaia.teams.User;
import org.springframework.data.mongodb.core.mapping.DBRef;

import javax.validation.constraints.NotBlank;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import io.codeka.gaia.stacks.bo.JobType;
import io.codeka.gaia.stacks.repository.JobRepository;
import io.codeka.gaia.stacks.repository.StackRepository;
import io.codeka.gaia.teams.bo.User;
import io.codeka.gaia.teams.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import io.codeka.gaia.stacks.bo.Stack;
import io.codeka.gaia.stacks.repository.StackRepository;
import io.codeka.gaia.stacks.service.StackCostCalculator;
import io.codeka.gaia.teams.bo.Team;
import io.codeka.gaia.teams.bo.User;
import io.codeka.gaia.teams.Team;
import io.codeka.gaia.teams.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.codeka.gaia.stacks.bo.Stack;
import io.codeka.gaia.stacks.bo.StackState;
import io.codeka.gaia.teams.bo.Team;
import io.codeka.gaia.teams.Team;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.codeka.gaia.stacks.service

import io.codeka.gaia.modules.repository.TerraformModuleRepository
import io.codeka.gaia.stacks.bo.MandatoryStackVariablesValidation
import io.codeka.gaia.stacks.bo.Stack
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import javax.validation.ConstraintValidator
import javax.validation.ConstraintValidatorContext

@Service
class MandatoryStackVariablesValidator(@Autowired val moduleRepository: TerraformModuleRepository)
: ConstraintValidator<MandatoryStackVariablesValidation, Stack> {

override fun isValid(stack: Stack, constraintValidatorContext: ConstraintValidatorContext?): Boolean {
// we do not validate anything if the stacks has no module reference (it will send a BadRequest anyway)
if (stack.moduleId.isNullOrBlank()) {
return true
}
// getting the module, returning true if module cannot be found !
// this should (let's hope so) never happen ! another validator should handle this case
val module = this.moduleRepository.findByIdOrNull(stack.moduleId) ?: return true

return module.variables.filter { it.isMandatory }
.filter { it.defaultValue.isNullOrBlank() }
.all { ! stack.variableValues[it.name].isNullOrBlank() }
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.codeka.gaia.stacks.service

import io.codeka.gaia.modules.bo.TerraformVariable
import io.codeka.gaia.modules.repository.TerraformModuleRepository
import io.codeka.gaia.stacks.bo.RegexStackVariablesValidation
import io.codeka.gaia.stacks.bo.Stack
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import java.util.regex.Pattern
import javax.validation.ConstraintValidator
import javax.validation.ConstraintValidatorContext

@Service
class RegexStackVariablesValidator(@Autowired val moduleRepository: TerraformModuleRepository)
: ConstraintValidator<RegexStackVariablesValidation, Stack> {

override fun isValid(stack: Stack, constraintValidatorContext: ConstraintValidatorContext?): Boolean {
// we do not validate anything if the stacks has no module reference (it will send a BadRequest anyway)
if( stack.moduleId.isNullOrBlank()) {
return true
}
// getting the module, returning true if module cannot be found !
// this should (let's hope so) never happen ! another validator should handle this case
val module = this.moduleRepository.findByIdOrNull(stack.moduleId) ?: return true

return module.variables
// not checking non regex values
.filter { ! it.validationRegex.isNullOrBlank() }
// not checking blank values (doing it first because Pair want non-null values)
.filter { ! stack.variableValues[it.name].isNullOrBlank() }
// validate the pattern (using non null assert as variable value is not more null here)
.all { it.validateWithPattern(stack.variableValues[it.name]!!) }
}
}

private fun TerraformVariable.validateWithPattern(value: String): Boolean {
val pattern = Pattern.compile(this.validationRegex)
return pattern.matcher(value).matches()
}
20 changes: 20 additions & 0 deletions src/main/java/io/codeka/gaia/teams/bo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.codeka.gaia.teams

import org.springframework.data.annotation.Id
import org.springframework.data.mongodb.core.mapping.DBRef

/**
* A Gaia Team, which groups users
*/
data class Team(val id: String?)

/**
* a Gaia user, which has granted access to modules
*/
data class User(
@Id val username: String,
@DBRef val team: Team?) {

val isAdmin: Boolean
get() = "admin" == this.username
}
21 changes: 0 additions & 21 deletions src/main/java/io/codeka/gaia/teams/bo/Team.java

This file was deleted.

Loading

0 comments on commit ed1d70c

Please sign in to comment.