Skip to content

Commit

Permalink
✨ : add archived state support
Browse files Browse the repository at this point in the history
  • Loading branch information
juwit committed Jan 11, 2021
1 parent 9ce7862 commit 274e037
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 75 deletions.
4 changes: 4 additions & 0 deletions src/main/java/io/gaia_app/stacks/bo/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,8 @@ public String tfvars() {
});
return variablesBuilder.toString();
}

public boolean isArchived() {
return this.state == StackState.ARCHIVED;
}
}
7 changes: 6 additions & 1 deletion src/main/java/io/gaia_app/stacks/bo/StackState.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public enum StackState {
/**
* When the stack has been stopped
*/
STOPPED
STOPPED,

/**
* When the stack is archived, and cannot be run anymore
*/
ARCHIVED

}
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,15 @@ public Map<String, String> launchJob(@PathVariable String id, @PathVariable JobT
// get the stack
var stack = this.stackRepository.findById(id).orElseThrow(StackNotFoundException::new);

// do not launch jobs of archived stacks
if (stack.isArchived()) {
throw new StackArchivedException();
}

// create a new job
var job = new Job(jobType, id, user);
job.setTerraformImage(stack.getModule().getTerraformImage());
if(stack.getCredentialsId() != null){
if (stack.getCredentialsId() != null) {
this.credentialsRepository.findById(stack.getCredentialsId())
.ifPresent(job::setCredentials);
}
Expand All @@ -105,3 +110,7 @@ public Map<String, String> launchJob(@PathVariable String id, @PathVariable JobT
@ResponseStatus(HttpStatus.NOT_FOUND)
class StackNotFoundException extends RuntimeException {
}

@ResponseStatus(HttpStatus.FORBIDDEN)
class StackArchivedException extends RuntimeException {
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ void listStacks_shouldReturnLimitedStacks_forStandardUsers() throws Exception {
void listStacks_shouldReturnAllStacks_forAdmin() throws Exception {
mockMvc.perform(get("/api/stacks"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(4)))
.andExpect(jsonPath("$.[*].name", contains("mongo-instance-1", "mongo-instance-2", "mongo-instance-limited", "local-mongo")))
.andExpect(jsonPath("$", hasSize(5)))
.andExpect(jsonPath("$.[*].name", contains("mongo-instance-1", "mongo-instance-2", "mongo-instance-limited", "local-mongo", "archived-stack")))
.andExpect(jsonPath("$.[*].ownerTeam.id", contains("Ze Team", "Ze Team", "Not Ze Team")));
}

Expand Down Expand Up @@ -163,4 +163,11 @@ void saveStack_shouldValidateStackVariablesRegex() throws Exception {
.andExpect(jsonPath("$.message", is("variables should match the regex")));
}

@Test
void launchJob_shouldThrowABadRequest_forArchivedStacks() throws Exception {
mockMvc.perform(post("/api/stacks/archived-stack/RUN")
.with(csrf()))
.andExpect(status().isForbidden());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import io.gaia_app.modules.bo.TerraformImage;
import io.gaia_app.modules.bo.TerraformModule;
import io.gaia_app.modules.repository.TerraformModuleRepository;
import io.gaia_app.stacks.bo.Job;
import io.gaia_app.stacks.bo.JobType;
import io.gaia_app.stacks.bo.Stack;
import io.gaia_app.stacks.bo.*;
import io.gaia_app.stacks.repository.JobRepository;
import io.gaia_app.stacks.repository.StackRepository;
import io.gaia_app.stacks.service.StackCostCalculator;
Expand Down Expand Up @@ -241,4 +239,18 @@ void launchJob_shouldInjectCredentialsIntoJob() {
assertEquals(job.getCredentials(), awsCredentials);
}

@Test
void launchJob_shouldThrowAnException_forArchivedStacks(){
// given
var stack = new Stack();
stack.setState(StackState.ARCHIVED);

var user = new User("test_user", null);

// when
when(stackRepository.findById(anyString())).thenReturn(Optional.of(stack));

assertThrows(StackArchivedException.class, () -> stackRestController.launchJob("test_stack", JobType.RUN, user));
}

}
143 changes: 75 additions & 68 deletions src/test/resources/db/30_stack.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,82 @@
gaia = db.getSiblingDB('gaia');
gaia.stack.drop();
gaia.stack.insert([
{
"_id": "5a215b6b-fe53-4afa-85f0-a10175a7f264",
"module": {"$ref": "terraformModule", "$id": "e01f9925-a559-45a2-8a55-f93dc434c676"},
"name": "mongo-instance-1",
"description": "first instance of mongo module",
"variableValues": {
"mongo_container_name": "test",
"mongo_exposed_port": "27117"
},
"providerSpec": "provider \"docker\" {\n host = \"unix:///var/run/docker.sock\"\n}",
"state": "NEW",
"ownerTeam": {"$ref": "team", "$id": "Ze Team"},
"estimatedRunningCost": null,
"createdBy": {"$ref": "user", "$id": "admin"},
"createdAt": new Date(),
"updatedBy": null,
"updatedAt": null
{
'_id': '5a215b6b-fe53-4afa-85f0-a10175a7f264',
'module': { '$ref': 'terraformModule', '$id': 'e01f9925-a559-45a2-8a55-f93dc434c676' },
'name': 'mongo-instance-1',
'description': 'first instance of mongo module',
'variableValues': {
'mongo_container_name': 'test',
'mongo_exposed_port': '27117',
},
{
"_id": "143773fa-4c2e-4baf-a7fb-79d23e01c5ca",
"module": {"$ref": "terraformModule", "$id": "e01f9925-a559-45a2-8a55-f93dc434c676"},
"name": "mongo-instance-2",
"description": "second instance of mongo module",
"variablesValues": {},
"providerSpec": null,
"state": "NEW",
"ownerTeam": {"$ref": "team", "$id": "Ze Team"},
"estimatedRunningCost": null,
"createdBy": {"$ref": "user", "$id": "admin"},
"createdAt": new Date(),
"updatedBy": null,
"updatedAt": null
'providerSpec': 'provider "docker" {\n host = "unix:///var/run/docker.sock"\n}',
'state': 'NEW',
'ownerTeam': { '$ref': 'team', '$id': 'Ze Team' },
'estimatedRunningCost': null,
'createdBy': { '$ref': 'user', '$id': 'admin' },
'createdAt': new Date(),
'updatedBy': null,
'updatedAt': null,
},
{
'_id': '143773fa-4c2e-4baf-a7fb-79d23e01c5ca',
'module': { '$ref': 'terraformModule', '$id': 'e01f9925-a559-45a2-8a55-f93dc434c676' },
'name': 'mongo-instance-2',
'description': 'second instance of mongo module',
'variablesValues': {},
'providerSpec': null,
'state': 'NEW',
'ownerTeam': { '$ref': 'team', '$id': 'Ze Team' },
'estimatedRunningCost': null,
'createdBy': { '$ref': 'user', '$id': 'admin' },
'createdAt': new Date(),
'updatedBy': null,
'updatedAt': null,
},
{
'_id': '845543d0-20a5-466c-8978-33c9a4661606',
'module': { '$ref': 'terraformModule', '$id': 'e01f9925-a559-45a2-8a55-f93dc434c676' },
'name': 'mongo-instance-limited',
'description': 'instance of mongo module for team Not Ze Team',
'variablesValues': {},
'providerSpec': null,
'state': 'NEW',
'ownerTeam': { '$ref': 'team', '$id': 'Not Ze Team' },
'estimatedRunningCost': null,
'createdBy': { '$ref': 'user', '$id': 'Mary J' },
'createdAt': new Date(),
'updatedBy': null,
'updatedAt': null,
},
{
'_id': 'de28a01f-257a-448d-8e1b-00e4e3a41db2',
'module': { '$ref': 'terraformModule', '$id': 'e01f9925-a559-45a2-8a55-f93dc434c676' },
'variableValues': {
'mongo_container_name': 'local-mongo',
'mongo_exposed_port': '28017',
},
{
"_id": "845543d0-20a5-466c-8978-33c9a4661606",
"module": {"$ref": "terraformModule", "$id": "e01f9925-a559-45a2-8a55-f93dc434c676"},
"name": "mongo-instance-limited",
"description": "instance of mongo module for team Not Ze Team",
"variablesValues": {},
"providerSpec": null,
"state": "NEW",
"ownerTeam": {"$ref": "team", "$id": "Not Ze Team"},
"estimatedRunningCost": null,
"createdBy": {"$ref": "user", "$id": "Mary J"},
"createdAt": new Date(),
"updatedBy": null,
"updatedAt": null
'name': 'local-mongo',
'description': 'local docker mongo container',
'state': 'RUNNING',
'estimatedRunningCost': '0',
'createdBy': {
'$ref': 'user',
'$id': 'admin',
},
{
"_id" : "de28a01f-257a-448d-8e1b-00e4e3a41db2",
"module": {"$ref": "terraformModule", "$id": "e01f9925-a559-45a2-8a55-f93dc434c676"},
"variableValues" : {
"mongo_container_name" : "local-mongo",
"mongo_exposed_port" : "28017"
},
"name" : "local-mongo",
"description" : "local docker mongo container",
"state" : "RUNNING",
"estimatedRunningCost" : "0",
"createdBy" : {
"$ref" : "user",
"$id" : "admin"
},
"createdAt" : ISODate("2020-02-03T17:57:07.341Z"),
"updatedBy" : {
"$ref" : "user",
"$id" : "admin"
},
"updatedAt" : ISODate("2020-02-05T07:01:29.935Z"),
"_class" : "io.gaia_app.stacks.bo.Stack"
}
'createdAt': ISODate('2020-02-03T17:57:07.341Z'),
'updatedBy': {
'$ref': 'user',
'$id': 'admin',
},
'updatedAt': ISODate('2020-02-05T07:01:29.935Z'),
'_class': 'io.gaia_app.stacks.bo.Stack',
},
{
'_id': 'archived-stack',
'name': 'archived-stack',
'description': 'archived stack for testing purpose',
'state': 'ARCHIVED',
'_class': 'io.gaia_app.stacks.bo.Stack',
},
]);

0 comments on commit 274e037

Please sign in to comment.