Skip to content

Commit

Permalink
add server modify command
Browse files Browse the repository at this point in the history
  • Loading branch information
caguiclajmg committed Jul 30, 2022
1 parent 73b66e5 commit 3b66f51
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
41 changes: 41 additions & 0 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ type DeployServerRequest struct {
Name string `mapstructure:"name"`
}

type ModifyServerRequest struct {
ServerId string `mapstructure:"server_id"`
InstanceType string `mapstructure:"instance_type"`
GPUModel string `mapstructure:"gpu_model,omitempty"`
GPUCount int `mapstructure:"gpu_count,omitempty"`
CPUModel string `mapstructure:"cpu_model,omitempty"`
VCPUs int `mapstructure:"vcpus"`
RAM int `mapstructure:"ram"`
Storage int `mapstructure:"storage"`
}

type DeployServerResponse struct {
Response
Server struct {
Expand All @@ -76,6 +87,10 @@ type ListCpuStockResponse struct {
} `json:"stock"`
}

type ModifyServerResponse struct {
Response
}

type Client struct {
BaseUrl string
ApiKey string
Expand Down Expand Up @@ -352,3 +367,29 @@ func (client *Client) ListCpuStock() (*ListCpuStockResponse, error) {

return &res, nil
}

func (client *Client) ModifyServer(req ModifyServerRequest) (*ModifyServerResponse, error) {
var rawBody map[string]interface{}
err := mapstructure.Decode(req, &rawBody)
if err != nil {
return nil, err
}

body := map[string]string{}
for key, elem := range rawBody {
str := fmt.Sprintf("%v", elem)
body[key] = str
}

raw, err := client.post("modify/single/custom", body, true)
if err != nil {
return nil, err
}

var res ModifyServerResponse
if err := json.Unmarshal(*raw, &res); err != nil {
return nil, err
}

return &res, nil
}
87 changes: 87 additions & 0 deletions commands/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ var (
Args: cobra.ExactArgs(1),
RunE: restartServer,
}
modifyCmd = &cobra.Command{
Use: "modify [flags] server_id",
Short: "Modify a server",
Args: cobra.ExactArgs(1),
RunE: modifyServer,
PostRun: logAction("success"),
}
)

func init() {
Expand Down Expand Up @@ -98,6 +105,15 @@ func init() {

serversCmd.AddCommand(restartCmd)

serversCmd.AddCommand(modifyCmd)
modifyCmd.Flags().String("gpuModel", "Quadro_4000", "The GPU model that you would like to provision")
modifyCmd.Flags().String("instanceType", "gpu", "Either \"gpu\" or \"cpu\"")
modifyCmd.Flags().Int("gpuCount", 1, "The number of GPUs of the model you specified earlier")
modifyCmd.Flags().String("cpuModel", "Intel_Xeon_v4", "The CPU model that you would like to provision")
modifyCmd.Flags().Int("vcpus", 2, "Number of vCPUs that you would like")
modifyCmd.Flags().Int("storage", 20, "Number of GB of networked storage")
modifyCmd.Flags().Int("ram", 4, "Number of GB of RAM to be deployed.")

rootCmd.AddCommand(serversCmd)
}

Expand Down Expand Up @@ -338,3 +354,74 @@ func restartServer(cmd *cobra.Command, args []string) error {

return nil
}

func modifyServer(cmd *cobra.Command, args []string) error {
flags := cmd.Flags()

serverId := args[0]

instanceType, err := flags.GetString("instanceType")
if err != nil {
return err
}

gpuModel, err := flags.GetString("gpuModel")
if err != nil {
return err
}

gpuCount, err := flags.GetInt("gpuCount")
if err != nil {
return err
}

cpuModel, err := flags.GetString("cpuModel")
if err != nil {
return err
}

vcpus, err := flags.GetInt("vcpus")
if err != nil {
return err
}

ram, err := flags.GetInt("ram")
if err != nil {
return err
}

storage, err := flags.GetInt("storage")
if err != nil {
return err
}

req := api.ModifyServerRequest{
ServerId: serverId,
InstanceType: instanceType,
VCPUs: vcpus,
RAM: ram,
Storage: storage,
}

switch instanceType {
case "cpu":
req.CPUModel = cpuModel
case "gpu":
req.GPUModel = gpuModel
req.GPUCount = gpuCount
default:
return errors.New("unknown instance type")
}

res, err := client.ModifyServer(req)

if err != nil {
return err
}

if !res.Success {
return errors.New(res.Error)
}

return nil
}

0 comments on commit 3b66f51

Please sign in to comment.