Skip to content

Commit f29cf08

Browse files
committed
support backup days in cloudServerCreate
1 parent 299e5d4 commit f29cf08

File tree

4 files changed

+68
-49
lines changed

4 files changed

+68
-49
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ Available Commands:
2626
cloud Interact with LiquidWeb's Cloud platform.
2727
help Help about any command
2828
network network actions
29+
plan Process YAML plan file
2930
version show build information
3031
3132
Flags:
32-
--config string config file (default is $HOME/.liquidweb-cli.yaml)
33-
-h, --help help for lw
33+
--config string config file (default is $HOME/.liquidweb-cli.yaml)
34+
-h, --help help for lw
35+
--use-context string forces current context, without persisting the context change
3436
3537
Use "lw [command] --help" for more information about a command.
3638
```
@@ -83,7 +85,6 @@ cloud:
8385
ips: 1
8486
public-ssh-key: "your public ssh key here
8587
config-id: 88
86-
backup-plan: "None"
8788
bandwidth: "SS.5000"
8889
```
8990

cmd/cloudServerCreate.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ For a list of backups, see 'cloud backups list'
6464
params.Ips, _ = cmd.Flags().GetInt("ips")
6565
pubSshKey, _ := cmd.Flags().GetString("public-ssh-key")
6666
params.ConfigId, _ = cmd.Flags().GetInt("config-id")
67-
params.BackupPlan, _ = cmd.Flags().GetString("backup-plan")
68-
params.BackupPlanQuota, _ = cmd.Flags().GetInt("backup-plan-quota")
67+
params.BackupDays, _ = cmd.Flags().GetInt("backup-days")
68+
params.BackupQuota, _ = cmd.Flags().GetInt("backup-quota")
6969
params.Bandwidth, _ = cmd.Flags().GetString("bandwidth")
7070
params.Zone, _ = cmd.Flags().GetInt("zone")
7171
params.WinAv, _ = cmd.Flags().GetString("winav")
@@ -110,14 +110,14 @@ func init() {
110110
cloudServerCreateCmd.Flags().String("public-ssh-key", sshPubKeyFile,
111111
"path to file containing the public ssh key you wish to be on the new Cloud Server")
112112
cloudServerCreateCmd.Flags().Int("config-id", 0, "config-id to use")
113-
cloudServerCreateCmd.Flags().String("backup-plan", "None", "Cloud Server backup plan to use")
114-
cloudServerCreateCmd.Flags().Int("backup-plan-quota", 300, "Quota amount. Should only be used with '--backup-plan Quota'")
113+
cloudServerCreateCmd.Flags().Int("backup-days", -1, "Enable daily backup plan. This is the amount of days to keep a backup")
114+
cloudServerCreateCmd.Flags().Int("backup-quota", -1, "Enable quota backup plan. This is the total amount of GB to keep.")
115115
cloudServerCreateCmd.Flags().String("bandwidth", "SS.10000", "bandwidth package to use")
116116
cloudServerCreateCmd.Flags().Int("zone", 0, "zone (id) to create new Cloud Server in (see 'cloud server options --zones')")
117117
cloudServerCreateCmd.Flags().String("password", "", "root or administrator password to set")
118118

119-
cloudServerCreateCmd.Flags().Int("backup-id", -1, "id of backup to create from (see 'cloud backup list')")
120-
cloudServerCreateCmd.Flags().Int("image-id", -1, "id of image to create from (see 'cloud image list')")
119+
cloudServerCreateCmd.Flags().Int("backup-id", -1, "id of cloud backup to create from (see 'cloud backup list')")
120+
cloudServerCreateCmd.Flags().Int("image-id", -1, "id of cloud image to create from (see 'cloud image list')")
121121

122122
cloudServerCreateCmd.Flags().StringSliceVar(&cloudServerCreateCmdPoolIpsFlag, "pool-ips", []string{},
123123
"ips from your IP Pool separated by ',' to assign to the new Cloud Server")

instance/cloudServerCreate.go

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,41 +27,42 @@ import (
2727
)
2828

2929
type CloudServerCreateParams struct {
30-
Template string `yaml:"template"`
31-
Type string `yaml:"type"`
32-
Hostname string `yaml:"hostname"`
33-
Ips int `yaml:"ips"`
34-
PoolIps []string `yaml:"pool-ips"`
35-
PublicSshKey string `yaml:"public-ssh-key"`
36-
ConfigId int `yaml:"config-id"`
37-
BackupPlan string `yaml:"backup-plan"`
38-
BackupPlanQuota int `yaml:"backup-plan-quota"`
39-
Bandwidth string `yaml:"bandwidth"`
40-
Zone int `yaml:"zone"`
41-
WinAv string `yaml:"winav"` // windows
42-
MsSql string `yaml:"ms-sql"` // windows
43-
PrivateParent string `yaml:"private-parent"`
44-
Password string `yaml:"password"`
45-
Memory int `yaml:"memory"` // required only if private parent
46-
Diskspace int `yaml:"diskspace"` // required only if private parent
47-
Vcpu int `yaml:"vcpu"` // required only if private parent
48-
BackupId int `yaml:"backup-id"` //create from backup
49-
ImageId int `yaml:"image-id"` // create from image
30+
Template string `yaml:"template"`
31+
Type string `yaml:"type"`
32+
Hostname string `yaml:"hostname"`
33+
Ips int `yaml:"ips"`
34+
PoolIps []string `yaml:"pool-ips"`
35+
PublicSshKey string `yaml:"public-ssh-key"`
36+
ConfigId int `yaml:"config-id"`
37+
BackupDays int `yaml:"backup-days"` // daily backup plan; how many days to keep a backup
38+
BackupQuota int `yaml:"backup-quota"` // backup quota plan; how many gb of backups to keep
39+
Bandwidth string `yaml:"bandwidth"`
40+
Zone int `yaml:"zone"`
41+
WinAv string `yaml:"winav"` // windows
42+
MsSql string `yaml:"ms-sql"` // windows
43+
PrivateParent string `yaml:"private-parent"`
44+
Password string `yaml:"password"`
45+
Memory int `yaml:"memory"` // required only if private parent
46+
Diskspace int `yaml:"diskspace"` // required only if private parent
47+
Vcpu int `yaml:"vcpu"` // required only if private parent
48+
BackupId int `yaml:"backup-id"` //create from backup
49+
ImageId int `yaml:"image-id"` // create from image
5050
}
5151

5252
func (s *CloudServerCreateParams) UnmarshalYAML(unmarshal func(interface{}) error) error {
5353
// define defaults
5454
type rawType CloudServerCreateParams
5555
raw := rawType{
56-
BackupId: -1,
57-
ImageId: -1,
58-
Vcpu: -1,
59-
Memory: -1,
60-
Diskspace: -1,
61-
Bandwidth: "SS.5000",
62-
BackupPlan: "None",
63-
Ips: 1,
64-
Type: "SS.VPS",
56+
BackupId: -1,
57+
BackupDays: -1,
58+
BackupQuota: -1,
59+
ImageId: -1,
60+
Vcpu: -1,
61+
Memory: -1,
62+
Diskspace: -1,
63+
Bandwidth: "SS.5000",
64+
Ips: 1,
65+
Type: "SS.VPS",
6566
} // Put your defaults here
6667
if err := unmarshal(&raw); err != nil {
6768
return err
@@ -126,13 +127,16 @@ func (ci *Client) CloudServerCreate(params *CloudServerCreateParams) (string, er
126127
return "", fmt.Errorf("at least one of the following flags must be set --template --image-id --backup-id")
127128
}
128129

130+
if params.BackupDays != -1 && params.BackupQuota != -1 {
131+
return "", fmt.Errorf("flags --backup-days and --backup-quota conflict")
132+
}
133+
129134
validateFields := map[interface{}]interface{}{
130-
params.Zone: map[string]string{"type": "PositiveInt", "optional": "true"},
131-
params.Hostname: "NonEmptyString",
132-
params.Type: "NonEmptyString",
133-
params.Ips: "PositiveInt",
134-
params.Password: "NonEmptyString",
135-
params.BackupPlan: "NonEmptyString",
135+
params.Zone: map[string]string{"type": "PositiveInt", "optional": "true"},
136+
params.Hostname: "NonEmptyString",
137+
params.Type: "NonEmptyString",
138+
params.Ips: "PositiveInt",
139+
params.Password: "NonEmptyString",
136140
}
137141
if params.BackupId != -1 {
138142
validateFields[params.BackupId] = "PositiveInt"
@@ -152,6 +156,13 @@ func (ci *Client) CloudServerCreate(params *CloudServerCreateParams) (string, er
152156
return "", err
153157
}
154158

159+
cloudBackupPlan := "None"
160+
if params.BackupDays != -1 {
161+
cloudBackupPlan = "Daily"
162+
} else if params.BackupQuota != -1 {
163+
cloudBackupPlan = "Quota"
164+
}
165+
155166
// buildout args for bleed/server/create
156167
createArgs := map[string]interface{}{
157168
"domain": params.Hostname,
@@ -166,7 +177,7 @@ func (ci *Client) CloudServerCreate(params *CloudServerCreateParams) (string, er
166177
"value": params.Ips,
167178
"count": 0,
168179
},
169-
"LiquidWebBackupPlan": params.BackupPlan,
180+
"LiquidWebBackupPlan": cloudBackupPlan,
170181
},
171182
}
172183

@@ -258,14 +269,22 @@ func (ci *Client) CloudServerCreate(params *CloudServerCreateParams) (string, er
258269
createArgs["memory"] = params.Memory
259270
}
260271

261-
if params.BackupPlan == "Quota" {
262-
createArgs["features"].(map[string]interface{})["BackupQuota"] = params.BackupPlanQuota
272+
if cloudBackupPlan == "Quota" {
273+
createArgs["features"].(map[string]interface{})["BackupQuota"] = params.BackupQuota
274+
} else if cloudBackupPlan == "Daily" {
275+
createArgs["features"].(map[string]interface{})["BackupDay"] = map[string]int{
276+
"value": 1,
277+
"num_units": params.BackupDays,
278+
}
263279
}
264280

265281
if params.PublicSshKey != "" {
266282
createArgs["public_ssh_key"] = params.PublicSshKey
267283
}
268284

285+
//pretty, _ := ci.JsonEncodeAndPrettyPrint(createArgs)
286+
//fmt.Println(pretty)
287+
269288
result, err := ci.LwCliApiClient.Call("bleed/server/create", createArgs)
270289
if err != nil {
271290
return "", err

plan.yaml.example

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ cloud:
1010
ips: 1
1111
public-ssh-key: ""
1212
config-id: 88
13-
backup-plan: "daily"
14-
backup-plan-quota: 5
13+
backup-days: 5
1514
bandwidth: "SS.5000"
1615
backup-id: -1
1716
image-id: -1

0 commit comments

Comments
 (0)