|
| 1 | +/* |
| 2 | +Copyright © LiquidWeb |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package cmd |
| 17 | + |
| 18 | +import ( |
| 19 | + "errors" |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/spf13/cast" |
| 23 | + "github.com/spf13/cobra" |
| 24 | + |
| 25 | + "github.com/liquidweb/liquidweb-cli/types/api" |
| 26 | + "github.com/liquidweb/liquidweb-cli/utils" |
| 27 | + "github.com/liquidweb/liquidweb-cli/validate" |
| 28 | +) |
| 29 | + |
| 30 | +var cloudServerResizeExpectationCmd = &cobra.Command{ |
| 31 | + Use: "resize-expectation", |
| 32 | + Short: "Determine if a Cloud Server can be resized without downtime", |
| 33 | + Long: `This command can be used to determine if a Cloud Server can be resized to the requested |
| 34 | +config-id without downtime. |
| 35 | +
|
| 36 | +Depending on inventory and desired config-id (configuration) the resize could either |
| 37 | +require a reboot to complete, or be performed entirely live. The intention of this |
| 38 | +command is to provide the user with a sane expectation ahead of making the resize |
| 39 | +request. |
| 40 | +
|
| 41 | +If there is no inventory available, an exception will be raised. |
| 42 | +
|
| 43 | +Its important to note, this command will *not* make any changes to your Cloud Server. |
| 44 | +This command is purely for information gathering. |
| 45 | +`, |
| 46 | + Run: func(cmd *cobra.Command, args []string) { |
| 47 | + uniqIdFlag, _ := cmd.Flags().GetString("uniq-id") |
| 48 | + privateParentFlag, _ := cmd.Flags().GetString("private-parent") |
| 49 | + diskFlag, _ := cmd.Flags().GetInt64("disk") |
| 50 | + memoryFlag, _ := cmd.Flags().GetInt64("memory") |
| 51 | + vcpuFlag, _ := cmd.Flags().GetInt64("vcpu") |
| 52 | + configIdFlag, _ := cmd.Flags().GetInt64("config-id") |
| 53 | + |
| 54 | + validateFields := map[interface{}]interface{}{ |
| 55 | + uniqIdFlag: "UniqId", |
| 56 | + } |
| 57 | + |
| 58 | + if err := validate.Validate(validateFields); err != nil { |
| 59 | + lwCliInst.Die(err) |
| 60 | + } |
| 61 | + |
| 62 | + if privateParentFlag != "" && configIdFlag != -1 { |
| 63 | + lwCliInst.Die(errors.New("cant pass both --config-id and --private-parent flags")) |
| 64 | + } |
| 65 | + if privateParentFlag == "" && configIdFlag == -1 { |
| 66 | + lwCliInst.Die(errors.New("must pass --config-id or --private-parent")) |
| 67 | + } |
| 68 | + |
| 69 | + apiArgs := map[string]interface{}{ |
| 70 | + "uniq_id": uniqIdFlag, |
| 71 | + "config_id": configIdFlag, |
| 72 | + } |
| 73 | + |
| 74 | + // if private parent, add args |
| 75 | + if privateParentFlag != "" { |
| 76 | + if memoryFlag <= 0 && diskFlag <= 0 && vcpuFlag <= 0 { |
| 77 | + lwCliInst.Die(errors.New("when --private-parent , at least one of --memory --disk --vcpu are required")) |
| 78 | + } |
| 79 | + |
| 80 | + privateParentUniqId, err := lwCliInst.DerivePrivateParentUniqId(privateParentFlag) |
| 81 | + if err != nil { |
| 82 | + lwCliInst.Die(err) |
| 83 | + } |
| 84 | + |
| 85 | + var cloudServerDetails apiTypes.CloudServerDetails |
| 86 | + if err = lwCliInst.CallLwApiInto( |
| 87 | + "bleed/storm/server/details", |
| 88 | + map[string]interface{}{ |
| 89 | + "uniq_id": uniqIdFlag, |
| 90 | + }, &cloudServerDetails); err != nil { |
| 91 | + lwCliInst.Die(err) |
| 92 | + } |
| 93 | + |
| 94 | + apiArgs["config_id"] = 0 |
| 95 | + apiArgs["private_parent"] = privateParentUniqId |
| 96 | + apiArgs["disk"] = cloudServerDetails.DiskSpace |
| 97 | + apiArgs["memory"] = cloudServerDetails.Memory |
| 98 | + apiArgs["vcpu"] = cloudServerDetails.Vcpu |
| 99 | + |
| 100 | + if diskFlag > 0 { |
| 101 | + apiArgs["disk"] = diskFlag |
| 102 | + } |
| 103 | + if vcpuFlag > 0 { |
| 104 | + apiArgs["vcpu"] = vcpuFlag |
| 105 | + } |
| 106 | + if memoryFlag > 0 { |
| 107 | + apiArgs["memory"] = memoryFlag |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + expectationInter, err := lwCliInst.LwCliApiClient.Call("bleed/storm/server/resizePlan", apiArgs) |
| 112 | + if err != nil { |
| 113 | + lwCliInst.Die(fmt.Errorf("ERROR: %s", err)) |
| 114 | + } |
| 115 | + expectation, ok := expectationInter.(map[string]interface{}) |
| 116 | + if !ok { |
| 117 | + lwCliInst.Die(errors.New("returned an unexpected structure")) |
| 118 | + } |
| 119 | + |
| 120 | + memoryDifference := cast.ToInt(expectation["memoryDifference"]) |
| 121 | + diskDifference := cast.ToInt(expectation["diskDifference"]) |
| 122 | + vcpuDifference := cast.ToInt(expectation["vcpuDifference"]) |
| 123 | + |
| 124 | + utils.PrintGreen("Configuration is available\n\n") |
| 125 | + |
| 126 | + fmt.Print("Resource Changes: Disk [") |
| 127 | + if diskDifference == 0 { |
| 128 | + fmt.Printf("%d] ", diskDifference) |
| 129 | + } else if diskDifference >= 0 { |
| 130 | + utils.PrintGreen("%d] ", diskDifference) |
| 131 | + } else { |
| 132 | + utils.PrintRed("%d] ", diskDifference) |
| 133 | + } |
| 134 | + |
| 135 | + fmt.Print("Memory [") |
| 136 | + if memoryDifference == 0 { |
| 137 | + fmt.Printf("%d] ", memoryDifference) |
| 138 | + } else if memoryDifference >= 0 { |
| 139 | + utils.PrintGreen("%d] ", memoryDifference) |
| 140 | + } else { |
| 141 | + utils.PrintRed("%d] ", memoryDifference) |
| 142 | + } |
| 143 | + |
| 144 | + fmt.Print("Vcpu [") |
| 145 | + if vcpuDifference == 0 { |
| 146 | + fmt.Printf("%d]\n", vcpuDifference) |
| 147 | + } else if vcpuDifference >= 0 { |
| 148 | + utils.PrintGreen("%d]\n", vcpuDifference) |
| 149 | + } else { |
| 150 | + utils.PrintRed("%d]\n", vcpuDifference) |
| 151 | + } |
| 152 | + }, |
| 153 | +} |
| 154 | + |
| 155 | +func init() { |
| 156 | + cloudServerCmd.AddCommand(cloudServerResizeExpectationCmd) |
| 157 | + |
| 158 | + cloudServerResizeExpectationCmd.Flags().String("uniq-id", "", "uniq-id of Cloud Server") |
| 159 | + |
| 160 | + cloudServerResizeExpectationCmd.Flags().String("private-parent", "", |
| 161 | + "name or uniq-id of the Private Parent (see: 'cloud private-parent list')") |
| 162 | + cloudServerResizeExpectationCmd.Flags().Int64("disk", -1, "diskspace for the Cloud Server (when private-parent)") |
| 163 | + cloudServerResizeExpectationCmd.Flags().Int64("memory", -1, "memory for the Cloud Server (when private-parent)") |
| 164 | + cloudServerResizeExpectationCmd.Flags().Int64("vcpu", -1, "vcpus for the Cloud Server (when private-parent)") |
| 165 | + |
| 166 | + cloudServerResizeExpectationCmd.Flags().Int64("config-id", -1, |
| 167 | + "config-id to check availability for (when !private-parent) (see: 'cloud server options --configs')") |
| 168 | + |
| 169 | + if err := cloudServerResizeExpectationCmd.MarkFlagRequired("uniq-id"); err != nil { |
| 170 | + lwCliInst.Die(err) |
| 171 | + } |
| 172 | +} |
0 commit comments