Skip to content

Commit

Permalink
Merge pull request #52 from liquidweb/cloud-public-ip-list-clarity
Browse files Browse the repository at this point in the history
#39/#40 and ease `zone` param restriction if private-parent was passed
  • Loading branch information
sgsullivan authored Oct 14, 2020
2 parents 20e7095 + 047dafd commit fba5b62
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 53 deletions.
20 changes: 18 additions & 2 deletions cmd/cloudNetworkPublicList.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,30 @@ var cloudNetworkPublicListCmd = &cobra.Command{
lwCliInst.Die(err)
}

fmt.Printf("IP Assignments for %s:\n", uniqIdFlag)
fmt.Printf("IP Assignments for %s:\n\n", uniqIdFlag)

for _, item := range results.Items {
var private apiTypes.CloudNetworkPrivateGetIpResponse
privateArgs := map[string]interface{}{"uniq_id": uniqIdFlag}
if err := lwCliInst.CallLwApiInto("bleed/network/private/getip", privateArgs, &private); err != nil {
lwCliInst.Die(err)
}

for c, item := range results.Items {
var details apiTypes.NetworkAssignmentListEntry
if err := instance.CastFieldTypes(item, &details); err != nil {
lwCliInst.Die(err)
}

// first ip is always primary
if c == 0 {
fmt.Println("Primary IP:")
} else {
if details.Ip == private.Ip {
fmt.Println("Private Network IP:")
} else {
fmt.Println("Secondary IP:")
}
}
fmt.Print(details)
}
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/cloudPrivateParentDelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ as well as how many resources each Cloud Server gets.`,

// if passed a private-parent flag, derive its uniq_id
var privateParentUniqId string
privateParentUniqId, err := lwCliInst.DerivePrivateParentUniqId(nameFlag)
privateParentUniqId, _, err := lwCliInst.DerivePrivateParentUniqId(nameFlag)
if err != nil {
lwCliInst.Die(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cloudPrivateParentDetails.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ as well as how many resources each Cloud Server gets.`,

// if passed a private-parent flag, derive its uniq_id
var privateParentUniqId string
privateParentUniqId, err := lwCliInst.DerivePrivateParentUniqId(nameFlag)
privateParentUniqId, _, err := lwCliInst.DerivePrivateParentUniqId(nameFlag)
if err != nil {
lwCliInst.Die(err)
}
Expand Down
10 changes: 8 additions & 2 deletions cmd/cloudServerClone.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,17 @@ Server is not on a Private Parent.`,

var privateParentUniqId string
if privateParentFlag != "" {
var err error
privateParentUniqId, err = lwCliInst.DerivePrivateParentUniqId(privateParentFlag)
var (
err error
zone int64
)
privateParentUniqId, zone, err = lwCliInst.DerivePrivateParentUniqId(privateParentFlag)
if err != nil {
lwCliInst.Die(err)
}
if zoneFlag == -1 {
zoneFlag = zone
}
}

// buildout api bleed/server/clone parameters
Expand Down
8 changes: 2 additions & 6 deletions cmd/cloudServerCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ For a list of backups, see 'cloud backups list'
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.Zone, _ = cmd.Flags().GetInt64("zone")
params.WinAv, _ = cmd.Flags().GetString("winav")
params.MsSql, _ = cmd.Flags().GetString("ms-sql")
params.PrivateParent, _ = cmd.Flags().GetString("private-parent")
Expand Down Expand Up @@ -114,7 +114,7 @@ func init() {
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().Int64("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 cloud backup to create from (see 'cloud backup list')")
Expand All @@ -133,8 +133,4 @@ func init() {
// windows specific
cloudServerCreateCmd.Flags().String("winav", "", "Use only with Windows Servers. Typically (None or NOD32) for value when set")
cloudServerCreateCmd.Flags().String("ms-sql", "", "Microsoft SQL Server")

if err := cloudServerCreateCmd.MarkFlagRequired("zone"); err != nil {
lwCliInst.Die(err)
}
}
12 changes: 8 additions & 4 deletions instance/cloudServerCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type CloudServerCreateParams struct {
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"`
Zone int64 `yaml:"zone"`
WinAv string `yaml:"winav"` // windows
MsSql string `yaml:"ms-sql"` // windows
PrivateParent string `yaml:"private-parent"`
Expand Down Expand Up @@ -74,14 +74,18 @@ func (s *CloudServerCreateParams) UnmarshalYAML(unmarshal func(interface{}) erro
func (ci *Client) CloudServerCreate(params *CloudServerCreateParams) (string, error) {
var err error

// if passed a private-parent flag, derive its uniq_id
// if passed a private-parent flag, derive its uniq_id and zone
if params.PrivateParent != "" {
params.PrivateParent, err = ci.DerivePrivateParentUniqId(params.PrivateParent)
params.PrivateParent, params.Zone, err = ci.DerivePrivateParentUniqId(params.PrivateParent)
if err != nil {
return "", err
}
}

if params.Zone <= 0 {
return "", fmt.Errorf("--zone must be given")
}

// default password
if params.Password == "" {
params.Password = utils.RandomString(25)
Expand Down Expand Up @@ -132,7 +136,7 @@ func (ci *Client) CloudServerCreate(params *CloudServerCreateParams) (string, er
}

validateFields := map[interface{}]interface{}{
params.Zone: map[string]string{"type": "PositiveInt", "optional": "true"},
params.Zone: "PositiveInt64",
params.Hostname: "NonEmptyString",
params.Type: "NonEmptyString",
params.Ips: "PositiveInt",
Expand Down
2 changes: 1 addition & 1 deletion instance/cloudServerResize.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (self *Client) CloudServerResize(params *CloudServerResizeParams) (result s
}

var privateParentUniqId string
privateParentUniqId, err = self.DerivePrivateParentUniqId(params.PrivateParent)
privateParentUniqId, _, err = self.DerivePrivateParentUniqId(params.PrivateParent)
if err != nil {
return
}
Expand Down
74 changes: 38 additions & 36 deletions instance/private_parents.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"strings"

"github.com/liquidweb/liquidweb-cli/types/api"
"github.com/liquidweb/liquidweb-cli/validate"
)

// it helped package scope and getting access to the global client variable to
Expand All @@ -28,19 +29,18 @@ import (
// or maybe the "instance" package name just makes it feel wrong...
// feedback is welcome

func (ci *Client) DerivePrivateParentUniqId(name string) (string, error) {
var (
privateParentUniqId string
privateParentDetails apiTypes.CloudPrivateParentDetails
privateParentDetailsErr error
)

func (ci *Client) DerivePrivateParentUniqId(name string) (uniqId string, zone int64, privateParentDetailsErr error) {
// if name looks like a uniq_id, try it as a uniq_id first.
if len(name) == 6 && strings.ToUpper(name) == name {
validateFields := map[interface{}]interface{}{
strings.ToUpper(name): "UniqId",
}
if err := validate.Validate(validateFields); err == nil {
var privateParentDetails apiTypes.CloudPrivateParentDetails
if err := ci.CallLwApiInto("bleed/storm/private/parent/details",
map[string]interface{}{"uniq_id": name},
&privateParentDetails); err == nil {
privateParentUniqId = name
uniqId = name
zone = privateParentDetails.Zone.Id
} else {
privateParentDetailsErr = fmt.Errorf(
"failed fetching parent details treating given --private-parent arg as a uniq-id [%s]: %s",
Expand All @@ -49,44 +49,46 @@ func (ci *Client) DerivePrivateParentUniqId(name string) (string, error) {
}

// if we havent found the pp details yet, try assuming name is the name of the pp
if privateParentUniqId == "" {
if uniqId == "" {
methodArgs := AllPaginatedResultsArgs{
Method: "bleed/storm/private/parent/list",
ResultsPerPage: 100,
}
results, err := ci.AllPaginatedResults(&methodArgs)
if err != nil {
ci.Die(err)
}

for _, item := range results.Items {
var privateParentDetails apiTypes.CloudPrivateParentDetails
if err := CastFieldTypes(item, &privateParentDetails); err != nil {
ci.Die(err)
}
if err == nil {
for _, item := range results.Items {
var privateParentDetails apiTypes.CloudPrivateParentDetails
if err := CastFieldTypes(item, &privateParentDetails); err != nil {
privateParentDetailsErr = fmt.Errorf("%s %w", privateParentDetailsErr, err)
break
}

if privateParentDetails.Domain == name {
// found it get details
err := ci.CallLwApiInto("bleed/storm/private/parent/details",
map[string]interface{}{
"uniq_id": privateParentDetails.UniqId,
},
&privateParentDetails)
if err != nil {
privateParentDetailsErr = fmt.Errorf(
"failed fetching private parent details for discovered uniq-id [%s] error: %s %w",
privateParentDetails.UniqId, err, privateParentDetailsErr)
return "", privateParentDetailsErr
if privateParentDetails.Domain == name {
// found it get details
err := ci.CallLwApiInto("bleed/storm/private/parent/details",
map[string]interface{}{
"uniq_id": privateParentDetails.UniqId,
},
&privateParentDetails)
if err != nil {
privateParentDetailsErr = fmt.Errorf(
"failed fetching private parent details for discovered uniq-id [%s] error: %w %s",
privateParentDetails.UniqId, err, privateParentDetailsErr)
break
}
uniqId = privateParentDetails.UniqId
zone = privateParentDetails.Zone.Id
break // found the uniq_id so break
}
privateParentUniqId = privateParentDetails.UniqId
break // found the uniq_id so break
}
} else {
privateParentDetailsErr = fmt.Errorf("%s %w", privateParentDetailsErr, err)
}
}

if privateParentUniqId == "" {
return "", fmt.Errorf("failed deriving uniq-id of private parent from [%s]: %s", name, privateParentDetailsErr)
if uniqId == "" || zone == 0 {
privateParentDetailsErr = fmt.Errorf("failed deriving uniq-id and/or zone of private parent from [%s]: %w", name, privateParentDetailsErr)
}

return privateParentUniqId, nil
return
}

0 comments on commit fba5b62

Please sign in to comment.