79803b98b551ad1d924594263038bc901f684233 — Louis Solofrizzo 3 days ago b1757dc master
api, sdk, csc: Add set-role route and commands for admins

I've also added new commands for destroy, launch, and restart instances.

Signed-off-by: Louis Solofrizzo <lsolofrizzo@online.net>
14 files changed, 221 insertions(+), 11 deletions(-)

M api/admin.go
M api/instance.go
M api/quotas.go
M csc/cmd/admin/user/get.go
A csc/cmd/admin/user/set-role.go
A csc/cmd/instance/destroy.go
A csc/cmd/instance/launch.go
A csc/cmd/instance/restart.go
A csc/cmd/instance/set.go
R csc/cmd/instance/{resize.go => set/size.go}
R csc/cmd/instance/{set-type.go => set/type.go}
M csc/main.go
M sdk/request.go
M sdk/user.go
M api/admin.go => api/admin.go +26 -0
@@ 400,9 400,35 @@ DB.Model(&instanceType).Update("enabled", enable.Enable)
  }
  
+ func admin_user_role(ctx iris.Context) {
+ 	var role cisco.UserInfoRole
+ 	var user DbUser
+ 
+ 	name := ctx.Params().Get("username")
+ 	ctx.ReadJSON(&role)
+ 
+ 	if role.Role == "" {
+ 		APIErrorField(ctx, "role")
+ 		return
+ 	}
+ 
+ 	if !CheckQuota(ctx, role.Role) {
+ 		return
+ 	}
+ 
+ 	DB.First(&user, "username = ?", name)
+ 	if user.ID == 0 {
+ 		APIError(ctx, 404, "Could not find the user")
+ 		return
+ 	}
+ 
+ 	DB.Model(&user).Update("role", role.Role)
+ }
+ 
  func InitAdminRoutes(app *iris.Application) {
  	app.Get("/admin/user/list", AdminAuth, admin_user_list)
  	app.Get("/admin/user/{username:string}", AdminAuth, admin_user_info)
+ 	app.Put("/admin/user/role/{username:string}", AdminAuth, admin_user_role)
  
  	app.Get("/admin/quota/list", AdminAuth, admin_quota_list)
  	app.Get("/admin/quota/{name:string}", AdminAuth, admin_quota_get)

M api/instance.go => api/instance.go +5 -0
@@ 495,6 495,11 @@ }
  	}
  
+ 	if instanceType.Name[0] != entry.Type[0] {
+ 		APIError(ctx, 400, "The instance type "+entry.Type+" cannot be transformed to "+instanceType.Name)
+ 		return
+ 	}
+ 
  	inst, _, err := Context.LXD.GetContainer("LF" + entry.Uuid)
  	if err != nil {
  		APIError(ctx, 503, "Error: "+err.Error())

M api/quotas.go => api/quotas.go +12 -0
@@ 105,3 105,15 @@   	return true
  }
+ 
+ func CheckQuota(ctx iris.Context, name string) bool {
+ 	var quotas DbQuotas
+ 
+ 	DB.First(&quotas, "role = ?", name)
+ 	if quotas.ID == 0 {
+ 		APIError(ctx, 404, "Could not find quotas for this name")
+ 		return false
+ 	}
+ 
+ 	return true
+ }

M csc/cmd/admin/user/get.go => csc/cmd/admin/user/get.go +0 -1
@@ 75,7 75,6 @@ fmt.Printf("        Name         : %s\n", instance.Name)
  			fmt.Printf("        Size         : %dGB\n", instance.Size)
  			fmt.Printf("        OS           : %s\n", instance.OS)
- 			fmt.Printf("        Architecture : %s\n", instance.Architecture)
  			fmt.Printf("        Type         : %s\n", instance.Type)
  			fmt.Printf("        Created      : %s\n", instance.Created)
  		}

A csc/cmd/admin/user/set-role.go => csc/cmd/admin/user/set-role.go +26 -0
@@ 0,0 1,26 @@
+ package adminuser
+ 
+ import (
+ 	"cisco/csc/cmd/admin"
+ 	"cisco/sdk"
+ 	"fmt"
+ 	"github.com/spf13/cobra"
+ )
+ 
+ var AdminUserSetRoleRootCmd = &cobra.Command{
+ 	Use:   "set-role",
+ 	Short: "Set role for an user",
+ 	Args:  cobra.ExactArgs(2),
+ 	Run: func(cmd *cobra.Command, args []string) {
+ 		err := cisco.SetRoleUser(args[0], args[1])
+ 
+ 		if err != nil {
+ 			fmt.Printf("Error: %s\n", err.Error())
+ 		}
+ 
+ 	},
+ }
+ 
+ func init() {
+ 	admin.AdminUserRootCmd.AddCommand(AdminUserSetRoleRootCmd)
+ }

A csc/cmd/instance/destroy.go => csc/cmd/instance/destroy.go +32 -0
@@ 0,0 1,32 @@
+ package instance
+ 
+ import (
+ 	"cisco/csc/cmd"
+ 	"cisco/sdk"
+ 	"fmt"
+ 	"github.com/spf13/cobra"
+ )
+ 
+ var instanceDestroy = &cobra.Command{
+ 	Use:   "destroy",
+ 	Short: "Destroy an instance",
+ 	Args:  cobra.ExactArgs(1),
+ 	Run: func(cmd *cobra.Command, args []string) {
+ 		name := args[0]
+ 
+ 		err := cisco.StopInstance(name)
+ 		if err != nil {
+ 			fmt.Printf("Error: %s\n", err.Error())
+ 		}
+ 
+ 		err = cisco.DeleteInstance(name)
+ 
+ 		if err != nil {
+ 			fmt.Printf("Error: %s\n", err.Error())
+ 		}
+ 	},
+ }
+ 
+ func init() {
+ 	cmd.InstanceRootCmd.AddCommand(instanceDestroy)
+ }

A csc/cmd/instance/launch.go => csc/cmd/instance/launch.go +42 -0
@@ 0,0 1,42 @@
+ package instance
+ 
+ import (
+ 	"cisco/csc/cmd"
+ 	"cisco/sdk"
+ 	"fmt"
+ 	"github.com/spf13/cobra"
+ )
+ 
+ var instanceLaunch = &cobra.Command{
+ 	Use:   "launch",
+ 	Short: "Create a launch instance",
+ 	Args:  cobra.ExactArgs(3),
+ 	Run: func(cmd *cobra.Command, args []string) {
+ 		name := args[0]
+ 		os := args[1]
+ 		model := args[2]
+ 		size, _ := cmd.Flags().GetUint("size")
+ 
+ 		err := cisco.NewInstance(cisco.InstanceNew{
+ 			Name: name,
+ 			OS:   os,
+ 			Type: model,
+ 			Size: size,
+ 		})
+ 
+ 		if err != nil {
+ 			fmt.Printf("Error: %s\n", err.Error())
+ 		}
+ 
+ 		err = cisco.StartInstance(name)
+ 		if err != nil {
+ 			fmt.Printf("Error: %s\n", err.Error())
+ 		}
+ 
+ 	},
+ }
+ 
+ func init() {
+ 	instanceLaunch.Flags().Uint("size", 0, "Instance disk size (GB)")
+ 	cmd.InstanceRootCmd.AddCommand(instanceLaunch)
+ }

A csc/cmd/instance/restart.go => csc/cmd/instance/restart.go +33 -0
@@ 0,0 1,33 @@
+ package instance
+ 
+ import (
+ 	"cisco/csc/cmd"
+ 	"cisco/sdk"
+ 	"fmt"
+ 	"github.com/spf13/cobra"
+ )
+ 
+ var instanceRestart = &cobra.Command{
+ 	Use:   "restart",
+ 	Short: "Restart an instance",
+ 	Args:  cobra.ExactArgs(1),
+ 	Run: func(cmd *cobra.Command, args []string) {
+ 		name := args[0]
+ 
+ 		err := cisco.StopInstance(name)
+ 
+ 		if err != nil {
+ 			fmt.Printf("Error: %s\n", err.Error())
+ 		}
+ 
+ 		err = cisco.StartInstance(name)
+ 
+ 		if err != nil {
+ 			fmt.Printf("Error: %s\n", err.Error())
+ 		}
+ 	},
+ }
+ 
+ func init() {
+ 	cmd.InstanceRootCmd.AddCommand(instanceRestart)
+ }

A csc/cmd/instance/set.go => csc/cmd/instance/set.go +15 -0
@@ 0,0 1,15 @@
+ package instance
+ 
+ import (
+ 	"cisco/csc/cmd"
+ 	"github.com/spf13/cobra"
+ )
+ 
+ var InstanceSetRootCmd = &cobra.Command{
+ 	Use:   "set",
+ 	Short: "Utilities for instance configuration",
+ }
+ 
+ func init() {
+ 	cmd.InstanceRootCmd.AddCommand(InstanceSetRootCmd)
+ }

R csc/cmd/instance/resize.go => csc/cmd/instance/set/size.go +4 -4
@@ 1,7 1,7 @@-package instance
+ package instanceset
  
  import (
- 	"cisco/csc/cmd"
+ 	"cisco/csc/cmd/instance"
  	"cisco/sdk"
  	"fmt"
  	"github.com/spf13/cobra"


@@ 9,7 9,7 @@ )
  
  var instanceResize = &cobra.Command{
- 	Use:   "resize",
+ 	Use:   "size",
  	Short: "Resize an instance",
  	Args:  cobra.ExactArgs(2),
  	Run: func(cmd *cobra.Command, args []string) {


@@ 33,5 33,5 @@ }
  
  func init() {
- 	cmd.InstanceRootCmd.AddCommand(instanceResize)
+ 	instance.InstanceSetRootCmd.AddCommand(instanceResize)
  }

R csc/cmd/instance/set-type.go => csc/cmd/instance/set/type.go +4 -6
@@ 1,14 1,14 @@-package instance
+ package instanceset
  
  import (
- 	"cisco/csc/cmd"
+ 	"cisco/csc/cmd/instance"
  	"cisco/sdk"
  	"fmt"
  	"github.com/spf13/cobra"
  )
  
  var instanceSetType = &cobra.Command{
- 	Use:   "set-type",
+ 	Use:   "type",
  	Short: "Changes the type of an instance",
  	Args:  cobra.ExactArgs(2),
  	Run: func(cmd *cobra.Command, args []string) {


@@ 18,11 18,9 @@ fmt.Printf("Error: %s\n", err.Error())
  			return
  		}
- 
- 		//fmt.Printf("Done! You need to restart the instance in order to use the new disk space.\n")
  	},
  }
  
  func init() {
- 	cmd.InstanceRootCmd.AddCommand(instanceSetType)
+ 	instance.InstanceSetRootCmd.AddCommand(instanceSetType)
  }

M csc/main.go => csc/main.go +1 -0
@@ 10,6 10,7 @@ _ "cisco/csc/cmd/admin/user"
  	_ "cisco/csc/cmd/image"
  	_ "cisco/csc/cmd/instance"
+ 	_ "cisco/csc/cmd/instance/set"
  	_ "cisco/csc/cmd/invite"
  	_ "cisco/csc/cmd/ssh"
  	_ "cisco/csc/cmd/token"

M sdk/request.go => sdk/request.go +6 -0
@@ 62,6 62,12 @@ Protected: true,
  }
  
+ var AdminUserSetRoleRoute Route = Route{
+ 	Path:      "/admin/user/role",
+ 	Method:    "PUT",
+ 	Protected: true,
+ }
+ 
  var AdminQuotaGetRoute Route = Route{
  	Path:      "/admin/quota",
  	Method:    "GET",

M sdk/user.go => sdk/user.go +15 -0
@@ 47,3 47,18 @@ err = json.Unmarshal(data.([]byte), &result)
  	return result, err
  }
+ 
+ type UserInfoRole struct {
+ 	Role string `json:"role"`
+ }
+ 
+ func SetRoleUser(user string, role string) error {
+ 	_, err := SendRequest(AdminUserSetRoleRoute, Request{
+ 		Arg: user,
+ 		Data: &UserInfoRole{
+ 			Role: role,
+ 		},
+ 	})
+ 
+ 	return err
+ }