Skip to content

Commit

Permalink
✨ : settings- add external url mgmt
Browse files Browse the repository at this point in the history
resolves #6
  • Loading branch information
juwit committed Jun 4, 2019
1 parent 277046c commit 57c40d8
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 15 deletions.
26 changes: 26 additions & 0 deletions src/main/java/io/codeka/gaia/bo/Settings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.codeka.gaia.bo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
* Gaia settings
*/
@ConfigurationProperties(prefix = "gaia")
@Component
public class Settings {

/**
* Gaia's external url (used to allow runners to speak to Gaia)
*/
private String externalUrl;

public String getExternalUrl() {
return externalUrl;
}

public void setExternalUrl(String externalUrl) {
this.externalUrl = externalUrl;
}
}
38 changes: 38 additions & 0 deletions src/main/java/io/codeka/gaia/controller/SettingsController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.codeka.gaia.controller;

import io.codeka.gaia.bo.Settings;
import io.codeka.gaia.bo.TerraformModule;
import io.codeka.gaia.repository.TerraformModuleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
* The controller for the settings mgmt
*/
@Controller
public class SettingsController {

private Settings settings;

@Autowired
public SettingsController(Settings settings) {
this.settings = settings;
}

@GetMapping("/settings")
public String settings(Model model){
model.addAttribute("settings", settings);

return "settings";
}

@PutMapping("/settings")
public String saveModule(@RequestBody Settings settings){
// update global settings bean
this.settings.setExternalUrl( settings.getExternalUrl() );
return "settings";
}

}
9 changes: 6 additions & 3 deletions src/main/java/io/codeka/gaia/controller/StackController.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package io.codeka.gaia.controller;

import io.codeka.gaia.bo.Job;
import io.codeka.gaia.bo.Settings;
import io.codeka.gaia.repository.StackRepository;
import io.codeka.gaia.repository.TerraformModuleRepository;
import io.codeka.gaia.runner.StackRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
Expand All @@ -25,11 +24,14 @@ public class StackController {

private TerraformModuleRepository terraformModuleRepository;

private Settings settings;

@Autowired
public StackController(StackRepository stackRepository, StackRunner stackRunner, TerraformModuleRepository terraformModuleRepository) {
public StackController(StackRepository stackRepository, StackRunner stackRunner, TerraformModuleRepository terraformModuleRepository, Settings settings) {
this.stackRepository = stackRepository;
this.stackRunner = stackRunner;
this.terraformModuleRepository = terraformModuleRepository;
this.settings = settings;
}

@GetMapping("/stacks")
Expand All @@ -45,6 +47,7 @@ public String editStack(@PathVariable String stackId, Model model){
// TODO throw an exception (404) if not
if(stackRepository.existsById(stackId)){
model.addAttribute("stackId", stackId);
model.addAttribute("settings", settings);
}
return "stack";
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/io/codeka/gaia/runner/StackRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.LogStream;
import com.spotify.docker.client.exceptions.DockerException;
import com.spotify.docker.client.messages.ContainerConfig;
import io.codeka.gaia.bo.Job;
import io.codeka.gaia.bo.Settings;
import io.codeka.gaia.bo.Stack;
import io.codeka.gaia.bo.TerraformModule;
import org.apache.commons.io.IOUtils;
Expand All @@ -31,12 +31,15 @@ public class StackRunner {

private ContainerConfig containerConfig;

private Settings settings;

private Map<String, Job> jobs = new HashMap<>();

@Autowired
public StackRunner(DockerClient dockerClient, ContainerConfig containerConfig) {
public StackRunner(DockerClient dockerClient, ContainerConfig containerConfig, Settings settings) {
this.dockerClient = dockerClient;
this.containerConfig = containerConfig;
this.settings = settings;
}

@Async
Expand Down Expand Up @@ -97,7 +100,7 @@ public void run(Job job, TerraformModule module, Stack stack) {

var backendGenCommand = String.format("echo \"terraform {\n" +
" backend \\\"http\\\" {\n" +
"\t\taddress=\\\"http://172.17.0.1:8080/api/state/%s\\\"\n" +
"\t\taddress=\\\""+settings.getExternalUrl()+"/api/state/%s\\\"\n" +
"\t}\n" +
"}\n\" > backend.tf", stack.getId());

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/layout/sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ <h4>Menu</h4>
<li class="active"><a href="/" ><i class="fas fa-tachometer-alt yellow_color"></i> <span>Dashboard</span></a></li>
<li><a href="/modules"><i class="fa fa-object-group blue1_color"></i> <span>Modules</span></a></li>
<li><a href="/stacks"><i class="fas fa-layer-group blue2_color"></i> <span>Stacks</span></a></li>
<!-- <li><a href="/settings"><i class="fa fa-cog yellow_color"></i> <span>Settings</span></a></li>-->
<li><a href="/settings"><i class="fa fa-cog yellow_color"></i> <span>Settings</span></a></li>
</ul>
</div>
</nav>
Expand Down
105 changes: 105 additions & 0 deletions src/main/resources/templates/settings.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="layout/header :: header(~{::title})">

<title>Gaia - Edit module</title>

</head>
<body class="dashboard dashboard_2">
<div class="full_container">
<div class="inner_container">

<div th:replace="~{layout/sidebar}"></div>

<!-- right content -->
<div id="content">

<div th:replace="~{layout/topbar}"></div>

<div class="midde_cont">
<div class="container-fluid">
<div class="row column_title">
<div class="col-md-12">
<div class="page_title">
<h2> Gaia's Settings </h2>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="white_shd full">

<div id="app"></div>

</div>
</div>
</div>
</div>
</div>

</div>
</div>
</div>
<!-- jQuery & bootstrap-->
<script src="webjars/jquery/3.0.0/jquery.min.js"></script>
<script src="webjars/popper.js/1.14.3/popper.min.js"></script>
<script src="webjars/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</body>
</html>

<template id="template">
<div>
<div class="full graph_head">
<div class="heading1 margin_0">
<h2>Settings</h2>
</div>
</div>

<div class="padding_infor_info">
<div class="form-group">
<label for="externalUrl">External url</label>
<input type="text" class="form-control" id="externalUrl" v-model="externalUrl">
<small>This is gaia's external URL. This is needed to allow terraform backend support.</small>
</div>

<button class="btn btn-primary" onclick="saveSettings()"><i class="far fa-save"></i> Save</button>
</div>
</div>
</template>

<script src="/webjars/jquery/3.0.0/jquery.min.js"></script>
<script src="/webjars/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="/webjars/bootstrap/4.3.1/js/bootstrap.min.js"></script>

<script src="/webjars/vue/2.5.16/vue.js"></script>

<script th:inline="javascript" type="application/ecmascript">
const settings = [[${settings}]];

new Vue({
el: "#app",
data : settings,
template: "#template"
});

function saveSettings(){
$.ajax({
url: `/settings`,
data: JSON.stringify(settings),
contentType: "application/json",
method: "PUT"
})
}
</script>

<script type="application/ecmascript">
$(document).ready(function () {
/*-- sidebar js --*/
$('#sidebarCollapse').on('click', function () {
$('#sidebar').toggleClass('active');
});
});
</script>

</body>
</html>
20 changes: 12 additions & 8 deletions src/main/resources/templates/stack.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@
<div>
<div class="full graph_head">
<div class="heading1 margin_0">
<h2>Stack {{name}}</h2>
<h2>Stack {{stack.name}}</h2>
</div>
</div>

<div class="padding_infor_info">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" v-model="name">
<input type="text" class="form-control" id="name" v-model="stack.name">
</div>
</div>
</div>
Expand Down Expand Up @@ -81,7 +81,7 @@ <h2>Backend configuration</h2>
<div class="tab-pane fade active show" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">
<pre><code class="language-hcl">terraform {
backend "http" {
address = "http://gaia:8080/api/state/{{id}}"
address = "{{settings.externalUrl}}/api/state/{{stack.id}}"
}
}</code></pre>
</div>
Expand All @@ -90,7 +90,7 @@ <h2>Backend configuration</h2>
"terraform" : {
"backend" : {
"http": {
"address": "http://gaia:8080/api/state/{{id}}"
"address": "{{settings.externalUrl}}/api/state/{{stack.id}}"
}
}
}
Expand All @@ -112,7 +112,7 @@ <h2>Provider configuration</h2>
</div>
</div>
<div class="full">
<div id="provider-editor" style="height: 200px;">{{ providerSpec }}</div>
<div id="provider-editor" style="height: 200px;">{{ stack.providerSpec }}</div>
</div>
</div>
</div>
Expand All @@ -130,10 +130,10 @@ <h2>Module variables values</h2>
</div>

<div class="padding_infor_info">
<div class="form-row align-items-end" v-for="(modVar,modVarIdx) in module.variables">
<div class="form-row align-items-end" v-for="(modVar,modVarIdx) in stack.module.variables">
<div class="form-group col-md-4" >
<label for="var-name">{{modVar.name}}: </label>
<input type="text" class="form-control" id="var-name" v-model="variableValues[modVar.name]">
<input type="text" class="form-control" id="var-name" v-model="stack.variableValues[modVar.name]">
<small id="emailHelp" class="form-text text-muted">{{modVar.description}}</small>
</div>
</div>
Expand All @@ -156,6 +156,7 @@ <h2>Module variables values</h2>

<script th:inline="javascript" type="application/ecmascript">
const stackId = [[${stackId}]];
const settings = [[${settings}]];

let stackData;
let providerEditor;
Expand All @@ -180,7 +181,10 @@ <h2>Module variables values</h2>
function initFront(stack){
new Vue({
el: "#app",
data: stack,
data: {
stack,
settings
},
template: "#template",
});

Expand Down
27 changes: 27 additions & 0 deletions src/test/java/io/codeka/gaia/bo/SettingsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.codeka.gaia.bo;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
@TestPropertySource(properties = "gaia.externalUrl=http://172.17.0.1:8080")
class SettingsTest {

@Autowired
private Settings settings;

@Test
void settingsShouldNotBeNull(){
assertNotNull(settings);
}

@Test
void externalUrl_shouldBeConfigurableViaProperty(){
assertEquals("http://172.17.0.1:8080", settings.getExternalUrl());
}

}

0 comments on commit 57c40d8

Please sign in to comment.