2016-04-02 14:40:07 +02:00
package projects
2016-04-04 15:44:34 +02:00
import (
2016-04-13 18:09:44 +02:00
"database/sql"
2016-04-04 15:44:34 +02:00
"github.com/ansible-semaphore/semaphore/database"
"github.com/ansible-semaphore/semaphore/models"
2016-04-13 18:09:44 +02:00
"github.com/ansible-semaphore/semaphore/util"
2016-04-04 15:44: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 InventoryMiddleware ( c * gin . Context ) {
2016-04-13 18:09:44 +02:00
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 ( )
2016-04-02 14:40:07 +02:00
}
2016-04-04 15:44:34 +02:00
func GetInventory ( c * gin . Context ) {
project := c . MustGet ( "project" ) . ( models . Project )
var inv [ ] models . Inventory
query , args , _ := squirrel . Select ( "*" ) .
From ( "project__inventory" ) .
Where ( "project_id=?" , project . ID ) .
ToSql ( )
if _ , err := database . Mysql . Select ( & inv , query , args ... ) ; err != nil {
panic ( err )
}
c . JSON ( 200 , inv )
}
func AddInventory ( c * gin . Context ) {
project := c . MustGet ( "project" ) . ( models . Project )
2016-04-16 21:42:57 +02:00
var inventory struct {
Name string ` json:"name" binding:"required" `
KeyID * int ` json:"key_id" `
SshKeyID int ` json:"ssh_key_id" `
Type string ` json:"type" `
Inventory string ` json:"inventory" `
}
2016-04-04 15:44:34 +02:00
if err := c . Bind ( & inventory ) ; err != nil {
return
}
switch inventory . Type {
case "static" , "aws" , "do" , "gcloud" :
break
default :
c . AbortWithStatus ( 400 )
return
}
2016-04-16 21:42:57 +02:00
if _ , err := database . Mysql . Exec ( "insert into project__inventory set project_id=?, name=?, type=?, key_id=?, ssh_key_id=?, inventory=?" , project . ID , inventory . Name , inventory . Type , inventory . KeyID , inventory . SshKeyID , inventory . Inventory ) ; err != nil {
2016-04-04 15:44:34 +02:00
panic ( err )
}
c . AbortWithStatus ( 204 )
}
func UpdateInventory ( c * gin . Context ) {
2016-04-16 21:42:57 +02:00
oldInventory := c . MustGet ( "inventory" ) . ( models . Inventory )
var inventory struct {
Name string ` json:"name" binding:"required" `
KeyID * int ` json:"key_id" `
SshKeyID int ` json:"ssh_key_id" `
Type string ` json:"type" `
Inventory string ` json:"inventory" `
}
if err := c . Bind ( & inventory ) ; err != nil {
return
}
switch inventory . Type {
case "static" , "aws" , "do" , "gcloud" :
break
default :
c . AbortWithStatus ( 400 )
return
}
if _ , err := database . Mysql . Exec ( "update project__inventory set name=?, type=?, key_id=?, ssh_key_id=?, inventory=? where id=?" , inventory . Name , inventory . Type , inventory . KeyID , inventory . SshKeyID , inventory . Inventory , oldInventory . ID ) ; err != nil {
panic ( err )
}
c . AbortWithStatus ( 204 )
2016-04-02 14:40:07 +02:00
}
2016-04-04 15:44:34 +02:00
func RemoveInventory ( c * gin . Context ) {
2016-04-13 18:09:44 +02:00
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 )
2016-04-02 14:40:07 +02:00
}