This repository has been archived by the owner on Sep 15, 2021. It is now read-only.
forked from scaleway/scaleway-cli
/
x_patch.go
129 lines (115 loc) · 3.41 KB
/
x_patch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (C) 2015 Scaleway. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE.md file.
package cli
import (
"fmt"
"strings"
log "github.com/Sirupsen/logrus"
api "github.com/scaleway/scaleway-cli/pkg/api"
)
var cmdPatch = &Command{
Exec: runPatch,
UsageLine: "_patch [OPTIONS] IDENTIFIER FIELD=VALUE",
Description: "",
Hidden: true,
Help: "PATCH an object on the API",
Examples: `
$ scw _patch myserver state_detail=booted
$ scw _patch server:myserver state_detail=booted
`,
}
func init() {
cmdPatch.Flag.BoolVar(&patchHelp, []string{"h", "-help"}, false, "Print usage")
}
// Flags
var patchHelp bool // -h, --help flag
func runPatch(cmd *Command, args []string) error {
if patchHelp {
return cmd.PrintUsage()
}
if len(args) != 2 {
return cmd.PrintShortUsage()
}
// Parsing FIELD=VALUE
updateParts := strings.SplitN(args[1], "=", 2)
if len(updateParts) != 2 {
return cmd.PrintShortUsage()
}
fieldName := updateParts[0]
newValue := args[1][len(updateParts[0])+1:]
changes := 0
ident, err := api.GetIdentifier(cmd.API, args[0])
if err != nil {
return err
}
switch ident.Type {
case api.IdentifierServer:
currentServer, err := cmd.API.GetServer(ident.Identifier)
if err != nil {
return fmt.Errorf("Cannot get server %s: %v", ident.Identifier, err)
}
var payload api.ScalewayServerPatchDefinition
switch fieldName {
case "state_detail":
log.Debugf("%s=%s => %s=%s", fieldName, currentServer.StateDetail, fieldName, newValue)
if currentServer.StateDetail != newValue {
changes++
payload.StateDetail = &newValue
}
case "name":
log.Warnf("To rename a server, Use 'scw rename'")
log.Debugf("%s=%s => %s=%s", fieldName, currentServer.StateDetail, fieldName, newValue)
if currentServer.Name != newValue {
changes++
payload.Name = &newValue
}
case "bootscript":
log.Debugf("%s=%s => %s=%s", fieldName, currentServer.Bootscript.Identifier, fieldName, newValue)
if currentServer.Bootscript.Identifier != newValue {
changes++
payload.Bootscript = &newValue
}
case "security_group":
log.Debugf("%s=%s => %s=%s", fieldName, currentServer.SecurityGroup.Identifier, fieldName, newValue)
if currentServer.SecurityGroup.Identifier != newValue {
changes++
payload.SecurityGroup.Identifier = newValue
}
case "tags":
newTags := strings.Split(newValue, " ")
log.Debugf("%s=%s => %s=%s", fieldName, currentServer.Tags, fieldName, newTags)
// fixme test equality with reflect.DeepEqual ?
changes++
payload.Tags = &newTags
case "ipv6":
log.Debugf("%s=%s => %s=%s", fieldName, currentServer.Tags, fieldName, newValue)
switch strings.ToLower(newValue) {
case "true":
t := true
payload.EnableIPV6 = &t
changes++
case "false":
f := false
payload.EnableIPV6 = &f
changes++
}
default:
return fmt.Errorf("'_patch server %s=' not implemented", fieldName)
}
// FIXME: volumes, tags, dynamic_ip_required
if changes > 0 {
log.Debugf("updating server: %d change(s)", changes)
err = cmd.API.PatchServer(ident.Identifier, payload)
} else {
log.Debugf("no changes, not updating server")
}
if err != nil {
return fmt.Errorf("Cannot update server: %v", err)
}
default:
return fmt.Errorf("_patch not implemented for this kind of object")
}
fmt.Println(ident.Identifier)
return nil
}