198e705940eb0d4a6f4febb196f4c6878aea4658 — Louis Solofrizzo a month ago 87336d8
api: Add new route to set instance name

Also fixed some typos in the config

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

M api/config.go
M api/instance.go
M api/mail.go
A csc/cmd/instance/set/name.go
M sdk/instance.go
M sdk/request.go
M api/config.go => api/config.go +8 -8
@@ 20,17 20,17 @@ } `yaml:"http"`
  
  	Mail struct {
- 		Host     string `yaml:"smtp_host"`
- 		User     string `yaml:"smtp_user"`
- 		Password string `yaml:"smtp_password"`
+ 		Host     string `yaml:"host"`
+ 		User     string `yaml:"user"`
+ 		Password string `yaml:"password"`
  	} `yaml:"mail"`
  
  	LXD struct {
- 		Cert        string `yaml:"lxd_cert"`
- 		Key         string `yaml:"lxd_key"`
- 		CA          string `yaml:"lxd_ca"`
- 		Url         string `yaml:"lxd_url"`
- 		StoragePool string `yaml:"lxd_storage_pool"`
+ 		Cert        string `yaml:"cert"`
+ 		Key         string `yaml:"key"`
+ 		CA          string `yaml:"ca"`
+ 		Url         string `yaml:"url"`
+ 		StoragePool string `yaml:"storage_pool"`
  	} `yaml:"lxd"`
  }
  

M api/instance.go => api/instance.go +26 -0
@@ 547,7 547,32 @@ }
  
  	DB.Model(&entry).Update("type", instanceType.Name)
+ }
+ 
+ func instance_set_name(ctx iris.Context) {
+ 	var entry DbInstance
+ 	var name cisco.InstanceNameUpdate
+ 
+ 	instance := ctx.Params().Get("name")
+ 	user := ctx.Values().Get("User")
+ 
+ 	ctx.ReadJSON(&name)
+ 
+ 	if name.Name == "" {
+ 		APIErrorField(ctx, "name")
+ 		return
+ 	}
+ 
+ 	DB.First(&entry, "name = ? AND user = ?", instance, user)
+ 	if entry.ID == 0 {
+ 		DB.First(&entry, "uuid = ? AND user = ?", instance, user)
+ 		if entry.ID == 0 {
+ 			APIError(ctx, 404, "Can't find the specified instance")
+ 			return
+ 		}
+ 	}
  
+ 	DB.Model(&entry).Update("name", name.Name)
  }
  
  func InitInstanceRoutes(app *iris.Application) {


@@ 561,4 586,5 @@ app.Get("/instance/types", APIAuth, instance_types)
  	app.Put("/instance/size/{name:string}", APIAuth, instance_size)
  	app.Put("/instance/type/{name:string}", APIAuth, instance_type)
+ 	app.Put("/instance/name/{name:string}", APIAuth, instance_set_name)
  }

M api/mail.go => api/mail.go +3 -3
@@ 8,16 8,16 @@ )
  
  func SendMail(to string, subject string, msg string) error {
- 	auth := sasl.NewPlainClient("", Config.SMTP.User, Config.SMTP.Password)
+ 	auth := sasl.NewPlainClient("", Config.Mail.User, Config.Mail.Password)
  
  	body := strings.NewReader(
  		"To: " + to + "\r\n" +
- 			"From: " + Config.SMTP.User + "\r\n" +
+ 			"From: " + Config.Mail.User + "\r\n" +
  			"Subject: " + subject + "\r\n" +
  			"\r\n" +
  			msg + "\r\n" +
  			"---\r\n" +
  			"I'm just a dumb script, please don't respond!\r\n")
  
- 	return smtp.SendMail(Config.SMTP.Host, auth, Config.SMTP.User, []string{to}, body)
+ 	return smtp.SendMail(Config.Mail.Host, auth, Config.Mail.User, []string{to}, body)
  }

A csc/cmd/instance/set/name.go => csc/cmd/instance/set/name.go +26 -0
@@ 0,0 1,26 @@
+ package instanceset
+ 
+ import (
+ 	"cisco/csc/cmd/instance"
+ 	"cisco/sdk"
+ 	"fmt"
+ 	"github.com/spf13/cobra"
+ )
+ 
+ var instanceSetName = &cobra.Command{
+ 	Use:   "name",
+ 	Short: "Changes the name of an instance",
+ 	Args:  cobra.ExactArgs(2),
+ 	Run: func(cmd *cobra.Command, args []string) {
+ 		err := cisco.SetNameInstance(args[0], args[1])
+ 
+ 		if err != nil {
+ 			fmt.Printf("Error: %s\n", err.Error())
+ 			return
+ 		}
+ 	},
+ }
+ 
+ func init() {
+ 	instance.InstanceSetRootCmd.AddCommand(instanceSetName)
+ }

M sdk/instance.go => sdk/instance.go +15 -0
@@ 240,3 240,18 @@   	return err
  }
+ 
+ type InstanceNameUpdate struct {
+ 	Name string `json:"name"`
+ }
+ 
+ func SetNameInstance(name string, newName string) error {
+ 	_, err := SendRequest(InstanceSetNameRoute, Request{
+ 		Arg: name,
+ 		Data: &InstanceNameUpdate{
+ 			Name: newName,
+ 		},
+ 	})
+ 
+ 	return err
+ }

M sdk/request.go => sdk/request.go +6 -0
@@ 182,6 182,12 @@ Protected: true,
  }
  
+ var InstanceSetNameRoute Route = Route{
+ 	Path:      "/instance/name",
+ 	Method:    "PUT",
+ 	Protected: true,
+ }
+ 
  var InstanceTypesRoute Route = Route{
  	Path:      "/instance/types",
  	Method:    "GET",