Environment API/UI

- Implement Environment/Inventory API
- Edit Environment
- UI to add/edit environment
This commit is contained in:
Matej Kramny 2016-04-13 17:09:44 +01:00
parent 33ce6d7327
commit cd5a978bb9
8 changed files with 156 additions and 7 deletions

View File

@ -6,6 +6,10 @@
label.control-label.col-sm-4 Name
.col-sm-6
input.form-control(type="text" ng-model="env.name")
.form-group
.col-sm-12
textarea.form-control(ng-model="env.json" rows="20" ng-init="env.json = '{}'")
p.help-block Must be valid JSON
.modal-footer
button.btn.btn-default.pull-left(ng-click="$dismiss()") Dismiss
button.btn.btn-success(ng-click="$close(env)") Create
button.btn.btn-success(ng-click="$close(env)") Create

View File

@ -0,0 +1,10 @@
.modal-header
h3.modal-title Edit Environment
.modal-body
p.help-block Must be valid JSON
textarea.form-control(ng-model="env" rows="20")
.modal-footer
button.btn.btn-default.pull-left(ng-click="$dismiss()") Cancel
button.btn.btn-success(ng-click="$close(env)") Save Changes

View File

@ -1,4 +1,5 @@
h3 Environment
button.btn.btn-default.btn-xs.pull-right(ng-click="add()") Create Environment
table.table
thead: tr

View File

@ -1,5 +1,5 @@
define(function () {
app.registerController('ProjectEnvironmentCtrl', ['$scope', '$http', '$uibModal', 'Project', function ($scope, $http, $modal, Project) {
app.registerController('ProjectEnvironmentCtrl', ['$scope', '$http', '$uibModal', 'Project', '$rootScope', function ($scope, $http, $modal, Project, $rootScope) {
$scope.reload = function () {
$http.get(Project.getURL() + '/environment').success(function (environment) {
$scope.environment = environment;
@ -14,6 +14,37 @@ define(function () {
});
}
$scope.add = function () {
$modal.open({
templateUrl: '/tpl/projects/environment/add.html'
}).result.then(function (env) {
$http.post(Project.getURL() + '/environment', env)
.success(function () {
$scope.reload();
}).error(function (_, status) {
swal('Erorr', 'Environment not added: ' + status, 'error');
});
});
}
$scope.editEnvironment = function (env) {
var scope = $rootScope.$new();
scope.env = env.json;
$modal.open({
templateUrl: '/tpl/projects/environment/environment.html',
scope: scope
}).result.then(function (v) {
env.json = v;
$http.put(Project.getURL() + '/environment/' + env.id, env)
.success(function () {
$scope.reload();
}).error(function (_, status) {
swal('Erorr', 'Environment not updated: ' + status, 'error');
});
});
}
$scope.reload();
}]);
});

View File

@ -1,14 +1,40 @@
package projects
import (
"database/sql"
"github.com/ansible-semaphore/semaphore/database"
"github.com/ansible-semaphore/semaphore/models"
"github.com/ansible-semaphore/semaphore/util"
"github.com/gin-gonic/gin"
"github.com/masterminds/squirrel"
)
func EnvironmentMiddleware(c *gin.Context) {
c.AbortWithStatus(501)
project := c.MustGet("project").(models.Project)
envID, err := util.GetIntParam("environment_id", c)
if err != nil {
return
}
query, args, _ := squirrel.Select("*").
From("project__environment").
Where("project_id=?", project.ID).
Where("id=?", envID).
ToSql()
var env models.Environment
if err := database.Mysql.SelectOne(&env, query, args...); err != nil {
if err == sql.ErrNoRows {
c.AbortWithStatus(404)
return
}
panic(err)
}
c.Set("environment", env)
c.Next()
}
func GetEnvironment(c *gin.Context) {
@ -28,10 +54,41 @@ func GetEnvironment(c *gin.Context) {
c.JSON(200, env)
}
func UpdateEnvironment(c *gin.Context) {
oldEnv := c.MustGet("environment").(models.Environment)
var env models.Environment
if err := c.Bind(&env); err != nil {
return
}
if _, err := database.Mysql.Exec("update project__environment set name=?, json=? where id=?", env.Name, env.JSON, oldEnv.ID); err != nil {
panic(err)
}
c.AbortWithStatus(204)
}
func AddEnvironment(c *gin.Context) {
c.AbortWithStatus(501)
project := c.MustGet("project").(models.Project)
var env models.Environment
if err := c.Bind(&env); err != nil {
return
}
if _, err := database.Mysql.Exec("insert into project__environment set project_id=?, name=?, json=?, password=?", project.ID, env.Name, env.JSON, env.Password); err != nil {
panic(err)
}
c.AbortWithStatus(204)
}
func RemoveEnvironment(c *gin.Context) {
c.AbortWithStatus(501)
env := c.MustGet("environment").(models.Environment)
if _, err := database.Mysql.Exec("delete from project__environment where id=?", env.ID); err != nil {
panic(err)
}
c.AbortWithStatus(204)
}

View File

@ -1,14 +1,40 @@
package projects
import (
"database/sql"
"github.com/ansible-semaphore/semaphore/database"
"github.com/ansible-semaphore/semaphore/models"
"github.com/ansible-semaphore/semaphore/util"
"github.com/gin-gonic/gin"
"github.com/masterminds/squirrel"
)
func InventoryMiddleware(c *gin.Context) {
c.AbortWithStatus(501)
project := c.MustGet("project").(models.Project)
inventoryID, err := util.GetIntParam("inventory_id", c)
if err != nil {
return
}
query, args, _ := squirrel.Select("*").
From("project__inventory").
Where("project_id=?", project.ID).
Where("id=?", inventoryID).
ToSql()
var inventory models.Inventory
if err := database.Mysql.SelectOne(&inventory, query, args...); err != nil {
if err == sql.ErrNoRows {
c.AbortWithStatus(404)
return
}
panic(err)
}
c.Set("inventory", inventory)
c.Next()
}
func GetInventory(c *gin.Context) {
@ -55,5 +81,11 @@ func UpdateInventory(c *gin.Context) {
}
func RemoveInventory(c *gin.Context) {
c.AbortWithStatus(501)
inventory := c.MustGet("inventory").(models.Inventory)
if _, err := database.Mysql.Exec("delete from project__inventory where id=?", inventory.ID); err != nil {
panic(err)
}
c.AbortWithStatus(204)
}

View File

@ -76,6 +76,7 @@ func Route(r *gin.Engine) {
api.GET("/environment", projects.GetEnvironment)
api.POST("/environment", projects.AddEnvironment)
api.PUT("/environment/:environment_id", projects.EnvironmentMiddleware, projects.UpdateEnvironment)
api.DELETE("/environment/:environment_id", projects.EnvironmentMiddleware, projects.RemoveEnvironment)
api.GET("/templates", projects.GetTemplates)

View File

@ -617,6 +617,19 @@ paths:
parameters:
- $ref: "#/parameters/project_id"
- $ref: "#/parameters/environment_id"
put:
tags:
- project
summary: Update environment
parameters:
- name: environment
in: body
required: true
schema:
$ref: "#/definitions/Environment"
responses:
204:
description: Environment Updated
delete:
tags:
- project