2016-04-02 14:40:07 +02:00
package projects
2016-04-07 14:49:34 +02:00
import (
2016-04-13 18:09:44 +02:00
"database/sql"
2016-05-24 11:55:48 +02:00
database "github.com/ansible-semaphore/semaphore/db"
2016-04-07 14:49:34 +02:00
"github.com/ansible-semaphore/semaphore/models"
2016-04-13 18:09:44 +02:00
"github.com/ansible-semaphore/semaphore/util"
2016-04-07 14:49:34 +02:00
"github.com/gin-gonic/gin"
"github.com/masterminds/squirrel"
)
2016-04-02 14:40:07 +02:00
2016-04-04 15:44:34 +02:00
func EnvironmentMiddleware ( c * gin . Context ) {
2016-04-13 18:09:44 +02:00
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 ( )
2016-04-02 14:40:07 +02:00
}
2016-04-04 15:44:34 +02:00
func GetEnvironment ( c * gin . Context ) {
2016-04-07 14:49:34 +02:00
project := c . MustGet ( "project" ) . ( models . Project )
var env [ ] models . Environment
q := squirrel . Select ( "*" ) .
From ( "project__environment" ) .
Where ( "project_id=?" , project . ID )
query , args , _ := q . ToSql ( )
if _ , err := database . Mysql . Select ( & env , query , args ... ) ; err != nil {
panic ( err )
}
c . JSON ( 200 , env )
2016-04-02 14:40:07 +02:00
}
2016-04-13 18:09:44 +02:00
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 )
}
2016-04-04 15:44:34 +02:00
func AddEnvironment ( c * gin . Context ) {
2016-04-13 18:09:44 +02:00
project := c . MustGet ( "project" ) . ( models . Project )
var env models . Environment
if err := c . Bind ( & env ) ; err != nil {
return
}
2016-04-17 02:20:23 +02:00
res , err := database . Mysql . Exec ( "insert into project__environment set project_id=?, name=?, json=?, password=?" , project . ID , env . Name , env . JSON , env . Password )
if err != nil {
panic ( err )
}
insertID , _ := res . LastInsertId ( )
insertIDInt := int ( insertID )
objType := "environment"
desc := "Environment " + env . Name + " created"
if err := ( models . Event {
ProjectID : & project . ID ,
ObjectType : & objType ,
ObjectID : & insertIDInt ,
Description : & desc ,
} . Insert ( ) ) ; err != nil {
2016-04-13 18:09:44 +02:00
panic ( err )
}
c . AbortWithStatus ( 204 )
2016-04-04 15:44:34 +02:00
}
func RemoveEnvironment ( c * gin . Context ) {
2016-04-13 18:09:44 +02:00
env := c . MustGet ( "environment" ) . ( models . Environment )
if _ , err := database . Mysql . Exec ( "delete from project__environment where id=?" , env . ID ) ; err != nil {
panic ( err )
}
2016-04-17 02:20:23 +02:00
desc := "Environment " + env . Name + " deleted"
if err := ( models . Event {
ProjectID : & env . ProjectID ,
Description : & desc ,
} . Insert ( ) ) ; err != nil {
panic ( err )
}
2016-04-13 18:09:44 +02:00
c . AbortWithStatus ( 204 )
2016-04-02 14:40:07 +02:00
}