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