Skip to content

Commit

Permalink
Merge pull request #36 from liquidweb/cloud-backup-days-fixes
Browse files Browse the repository at this point in the history
Cloud backup days fixes
  • Loading branch information
sgsullivan authored Mar 5, 2020
2 parents 500a1e0 + aaeb464 commit bb65079
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 71 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ Available Commands:
cloud Interact with LiquidWeb's Cloud platform.
help Help about any command
network network actions
plan Process YAML plan file
version show build information
Flags:
--config string config file (default is $HOME/.liquidweb-cli.yaml)
-h, --help help for lw
--config string config file (default is $HOME/.liquidweb-cli.yaml)
-h, --help help for lw
--use-context string forces current context, without persisting the context change
Use "lw [command] --help" for more information about a command.
```
Expand Down Expand Up @@ -83,7 +85,6 @@ cloud:
ips: 1
public-ssh-key: "your public ssh key here
config-id: 88
backup-plan: "None"
bandwidth: "SS.5000"
```

Expand Down
12 changes: 6 additions & 6 deletions cmd/cloudServerCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ For a list of backups, see 'cloud backups list'
params.Ips, _ = cmd.Flags().GetInt("ips")
pubSshKey, _ := cmd.Flags().GetString("public-ssh-key")
params.ConfigId, _ = cmd.Flags().GetInt("config-id")
params.BackupPlan, _ = cmd.Flags().GetString("backup-plan")
params.BackupPlanQuota, _ = cmd.Flags().GetInt("backup-plan-quota")
params.BackupDays, _ = cmd.Flags().GetInt("backup-days")
params.BackupQuota, _ = cmd.Flags().GetInt("backup-quota")
params.Bandwidth, _ = cmd.Flags().GetString("bandwidth")
params.Zone, _ = cmd.Flags().GetInt("zone")
params.WinAv, _ = cmd.Flags().GetString("winav")
Expand Down Expand Up @@ -110,14 +110,14 @@ func init() {
cloudServerCreateCmd.Flags().String("public-ssh-key", sshPubKeyFile,
"path to file containing the public ssh key you wish to be on the new Cloud Server")
cloudServerCreateCmd.Flags().Int("config-id", 0, "config-id to use")
cloudServerCreateCmd.Flags().String("backup-plan", "None", "Cloud Server backup plan to use")
cloudServerCreateCmd.Flags().Int("backup-plan-quota", 300, "Quota amount. Should only be used with '--backup-plan Quota'")
cloudServerCreateCmd.Flags().Int("backup-days", -1, "Enable daily backup plan. This is the amount of days to keep a backup")
cloudServerCreateCmd.Flags().Int("backup-quota", -1, "Enable quota backup plan. This is the total amount of GB to keep.")
cloudServerCreateCmd.Flags().String("bandwidth", "SS.10000", "bandwidth package to use")
cloudServerCreateCmd.Flags().Int("zone", 0, "zone (id) to create new Cloud Server in (see 'cloud server options --zones')")
cloudServerCreateCmd.Flags().String("password", "", "root or administrator password to set")

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

cloudServerCreateCmd.Flags().StringSliceVar(&cloudServerCreateCmdPoolIpsFlag, "pool-ips", []string{},
"ips from your IP Pool separated by ',' to assign to the new Cloud Server")
Expand Down
49 changes: 27 additions & 22 deletions cmd/cloudServerUpdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,19 @@ Update details about your server, including the backup and bandwidth plans, and
on the server. It merely updates what our records show.
bandwidth_plan is the bandwidth plan you wish to use. A quota of 0 indicates that you want
as-you-go, usage-based bandwidth charges.`,
as-you-go, usage-based bandwidth charges.
A quota backup plan allows you to save daily backups up to your set quota.
A daily backup plan will save backups of a server up to your set days.
Either backup plan has a maximum retention of 90 days.
`,
Run: func(cmd *cobra.Command, args []string) {
uniqIdFlag, _ := cmd.Flags().GetString("uniq-id")
hostnameFlag, _ := cmd.Flags().GetString("hostname")
backupPlanFlag, _ := cmd.Flags().GetString("backup-plan")
disableBackupsFlag, _ := cmd.Flags().GetBool("disable-backups")
bandwidthQuotaFlag, _ := cmd.Flags().GetInt64("bandwidth-quota")
backupDaysFlag, _ := cmd.Flags().GetInt64("backup-days")
backupQuotaFlag, _ := cmd.Flags().GetInt64("backup-quota")

validateFields := map[interface{}]interface{}{
Expand All @@ -50,19 +56,19 @@ as-you-go, usage-based bandwidth charges.`,
lwCliInst.Die(err)
}

if backupPlanFlag == "Quota" {
if backupQuotaFlag == -1 {
lwCliInst.Die(fmt.Errorf("cannot enable Quota backups without --backup-quota"))
}
if backupDaysFlag != -1 && backupQuotaFlag != -1 {
lwCliInst.Die(fmt.Errorf("--backup-days and --backup-quota are conflicting flags"))
}

if backupPlanFlag == "" && !disableBackupsFlag && hostnameFlag == "" &&
bandwidthQuotaFlag == -1 {
if backupDaysFlag == -1 && backupQuotaFlag == -1 && !disableBackupsFlag &&
hostnameFlag == "" && bandwidthQuotaFlag == -1 {
lwCliInst.Die(fmt.Errorf(
"must pass one of: enable-backups disable-backups hostname bandwidth-quota"))
"must pass a valid flag; check 'help cloud server update' for usage"))
}
if backupPlanFlag != "" && disableBackupsFlag {
lwCliInst.Die(fmt.Errorf("cant both enable and disable backups"))
if disableBackupsFlag {
if backupDaysFlag != -1 || backupQuotaFlag != -1 {
lwCliInst.Die(fmt.Errorf("cant both enable and disable backups"))
}
}

apiArgs := map[string]interface{}{
Expand All @@ -76,14 +82,13 @@ as-you-go, usage-based bandwidth charges.`,
apiArgs["bandwidth_quota"] = bandwidthQuotaFlag
}

if backupPlanFlag != "" {
apiArgs["backup_plan"] = backupPlanFlag
if backupPlanFlag == "Quota" {
apiArgs["backup_quota"] = backupQuotaFlag
}
}

if disableBackupsFlag {
if backupDaysFlag != -1 {
apiArgs["backup_plan"] = "daily"
apiArgs["backup_quota"] = backupDaysFlag
} else if backupQuotaFlag != -1 {
apiArgs["backup_plan"] = "quota"
apiArgs["backup_quota"] = backupQuotaFlag
} else if disableBackupsFlag {
apiArgs["backup_plan"] = "None"
}

Expand All @@ -100,9 +105,9 @@ func init() {
cloudServerCmd.AddCommand(cloudServerUpdateCmd)

cloudServerUpdateCmd.Flags().String("uniq-id", "", "uniq-id of the Cloud Server")
cloudServerUpdateCmd.Flags().String("hostname", "", "hostname to set")
cloudServerUpdateCmd.Flags().String("backup-plan", "", "Name of the backup plan")
cloudServerUpdateCmd.Flags().Int64("backup-quota", -1, "Quota to set for Quota type backup-plan")
cloudServerUpdateCmd.Flags().String("hostname", "", "hostname to set (will only update record at LiquidWeb)")
cloudServerUpdateCmd.Flags().Int64("backup-days", -1, "Enable daily backup plan. This is the amount of days to keep a backup")
cloudServerUpdateCmd.Flags().Int64("backup-quota", -1, "Enable quota backup plan. This is the total amount of GB to keep.")
cloudServerUpdateCmd.Flags().Bool("disable-backups", false, "disable backups")
cloudServerUpdateCmd.Flags().Int64("bandwidth-quota", -1,
"bandwidth quota (0 indicates as-you-go, usage-based bandwidth charges)")
Expand Down
92 changes: 54 additions & 38 deletions instance/cloudServerCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,42 @@ import (
)

type CloudServerCreateParams struct {
Template string `yaml:"template"`
Type string `yaml:"type"`
Hostname string `yaml:"hostname"`
Ips int `yaml:"ips"`
PoolIps []string `yaml:"pool-ips"`
PublicSshKey string `yaml:"public-ssh-key"`
ConfigId int `yaml:"config-id"`
BackupPlan string `yaml:"backup-plan"`
BackupPlanQuota int `yaml:"backup-plan-quota"`
Bandwidth string `yaml:"bandwidth"`
Zone int `yaml:"zone"`
WinAv string `yaml:"winav"` // windows
MsSql string `yaml:"ms-sql"` // windows
PrivateParent string `yaml:"private-parent"`
Password string `yaml:"password"`
Memory int `yaml:"memory"` // required only if private parent
Diskspace int `yaml:"diskspace"` // required only if private parent
Vcpu int `yaml:"vcpu"` // required only if private parent
BackupId int `yaml:"backup-id"` //create from backup
ImageId int `yaml:"image-id"` // create from image
Template string `yaml:"template"`
Type string `yaml:"type"`
Hostname string `yaml:"hostname"`
Ips int `yaml:"ips"`
PoolIps []string `yaml:"pool-ips"`
PublicSshKey string `yaml:"public-ssh-key"`
ConfigId int `yaml:"config-id"`
BackupDays int `yaml:"backup-days"` // daily backup plan; how many days to keep a backup
BackupQuota int `yaml:"backup-quota"` // backup quota plan; how many gb of backups to keep
Bandwidth string `yaml:"bandwidth"`
Zone int `yaml:"zone"`
WinAv string `yaml:"winav"` // windows
MsSql string `yaml:"ms-sql"` // windows
PrivateParent string `yaml:"private-parent"`
Password string `yaml:"password"`
Memory int `yaml:"memory"` // required only if private parent
Diskspace int `yaml:"diskspace"` // required only if private parent
Vcpu int `yaml:"vcpu"` // required only if private parent
BackupId int `yaml:"backup-id"` //create from backup
ImageId int `yaml:"image-id"` // create from image
}

func (s *CloudServerCreateParams) UnmarshalYAML(unmarshal func(interface{}) error) error {
// define defaults
type rawType CloudServerCreateParams
raw := rawType{
BackupId: -1,
ImageId: -1,
Vcpu: -1,
Memory: -1,
Diskspace: -1,
Bandwidth: "SS.5000",
BackupPlan: "None",
Ips: 1,
Type: "SS.VPS",
BackupId: -1,
BackupDays: -1,
BackupQuota: -1,
ImageId: -1,
Vcpu: -1,
Memory: -1,
Diskspace: -1,
Bandwidth: "SS.5000",
Ips: 1,
Type: "SS.VPS",
} // Put your defaults here
if err := unmarshal(&raw); err != nil {
return err
Expand Down Expand Up @@ -126,13 +127,16 @@ func (ci *Client) CloudServerCreate(params *CloudServerCreateParams) (string, er
return "", fmt.Errorf("at least one of the following flags must be set --template --image-id --backup-id")
}

if params.BackupDays != -1 && params.BackupQuota != -1 {
return "", fmt.Errorf("flags --backup-days and --backup-quota conflict")
}

validateFields := map[interface{}]interface{}{
params.Zone: map[string]string{"type": "PositiveInt", "optional": "true"},
params.Hostname: "NonEmptyString",
params.Type: "NonEmptyString",
params.Ips: "PositiveInt",
params.Password: "NonEmptyString",
params.BackupPlan: "NonEmptyString",
params.Zone: map[string]string{"type": "PositiveInt", "optional": "true"},
params.Hostname: "NonEmptyString",
params.Type: "NonEmptyString",
params.Ips: "PositiveInt",
params.Password: "NonEmptyString",
}
if params.BackupId != -1 {
validateFields[params.BackupId] = "PositiveInt"
Expand All @@ -152,6 +156,13 @@ func (ci *Client) CloudServerCreate(params *CloudServerCreateParams) (string, er
return "", err
}

cloudBackupPlan := "None"
if params.BackupDays != -1 {
cloudBackupPlan = "Daily"
} else if params.BackupQuota != -1 {
cloudBackupPlan = "Quota"
}

// buildout args for bleed/server/create
createArgs := map[string]interface{}{
"domain": params.Hostname,
Expand All @@ -166,7 +177,7 @@ func (ci *Client) CloudServerCreate(params *CloudServerCreateParams) (string, er
"value": params.Ips,
"count": 0,
},
"LiquidWebBackupPlan": params.BackupPlan,
"LiquidWebBackupPlan": cloudBackupPlan,
},
}

Expand Down Expand Up @@ -258,8 +269,13 @@ func (ci *Client) CloudServerCreate(params *CloudServerCreateParams) (string, er
createArgs["memory"] = params.Memory
}

if params.BackupPlan == "Quota" {
createArgs["features"].(map[string]interface{})["BackupQuota"] = params.BackupPlanQuota
if cloudBackupPlan == "Quota" {
createArgs["features"].(map[string]interface{})["BackupQuota"] = params.BackupQuota
} else if cloudBackupPlan == "Daily" {
createArgs["features"].(map[string]interface{})["BackupDay"] = map[string]int{
"value": 1,
"num_units": params.BackupDays,
}
}

if params.PublicSshKey != "" {
Expand Down
3 changes: 1 addition & 2 deletions plan.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ cloud:
ips: 1
public-ssh-key: ""
config-id: 88
backup-plan: "daily"
backup-plan-quota: 5
backup-days: 5
bandwidth: "SS.5000"
backup-id: -1
image-id: -1
Expand Down

0 comments on commit bb65079

Please sign in to comment.