Skip to content

Commit 375f60d

Browse files
committed
make 'cloud network private details' support showing all private info
.. this makes it nicer
1 parent 09d2d77 commit 375f60d

File tree

2 files changed

+53
-22
lines changed

2 files changed

+53
-22
lines changed

cmd/cloudNetworkPrivateDetails.go

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ import (
2020

2121
"github.com/spf13/cobra"
2222

23+
"github.com/liquidweb/liquidweb-cli/instance"
2324
"github.com/liquidweb/liquidweb-cli/types/api"
2425
"github.com/liquidweb/liquidweb-cli/validate"
2526
)
2627

2728
var cloudNetworkPrivateDetailsCmd = &cobra.Command{
2829
Use: "details",
29-
Short: "Get Private Network details for a Cloud Server",
30-
Long: `Get Private Network details for a Cloud Server
30+
Short: "Get Private Network details for a single or all Cloud Server(s)",
31+
Long: `Get Private Network details for a single or all Cloud Server(s)
3132
3233
Private networking provides the option for several Cloud Servers to contact each other
3334
via a network interface that is:
@@ -40,36 +41,52 @@ and cost-savings.
4041
`,
4142
Run: func(cmd *cobra.Command, args []string) {
4243
uniqIdFlag, _ := cmd.Flags().GetString("uniq-id")
44+
allFlag, _ := cmd.Flags().GetBool("all")
4345

44-
validateFields := map[interface{}]interface{}{
45-
uniqIdFlag: "UniqId",
46-
}
47-
if err := validate.Validate(validateFields); err != nil {
48-
lwCliInst.Die(err)
46+
var uniqIds []string
47+
48+
if uniqIdFlag == "" || allFlag {
49+
methodArgs := instance.AllPaginatedResultsArgs{
50+
Method: "bleed/storm/server/list",
51+
ResultsPerPage: 100,
52+
}
53+
results, err := lwCliInst.AllPaginatedResults(&methodArgs)
54+
if err != nil {
55+
lwCliInst.Die(err)
56+
}
57+
for _, item := range results.Items {
58+
var cs apiTypes.CloudServerDetails
59+
if err := instance.CastFieldTypes(item, &cs); err != nil {
60+
lwCliInst.Die(err)
61+
}
62+
uniqIds = append(uniqIds, cs.UniqId)
63+
}
64+
} else {
65+
uniqIds = append(uniqIds, uniqIdFlag)
4966
}
5067

51-
apiArgs := map[string]interface{}{"uniq_id": uniqIdFlag}
68+
for _, uniqId := range uniqIds {
69+
validateFields := map[interface{}]interface{}{
70+
uniqId: "UniqId",
71+
}
72+
if err := validate.Validate(validateFields); err != nil {
73+
lwCliInst.Die(err)
74+
}
5275

53-
var details apiTypes.CloudNetworkPrivateGetIpResponse
54-
err := lwCliInst.CallLwApiInto("bleed/network/private/getip", apiArgs, &details)
55-
if err != nil {
56-
lwCliInst.Die(err)
57-
}
76+
apiArgs := map[string]interface{}{"uniq_id": uniqId}
5877

59-
if details.Ip == "" {
60-
fmt.Printf("Cloud Server is not attached to a Private Network\n")
61-
} else {
62-
fmt.Printf("Cloud Server is attached to a Private Network\n")
63-
fmt.Printf("\tIP: %s\n", details.Ip)
64-
fmt.Printf("\tLegacy: %t\n", details.Legacy)
78+
var details apiTypes.CloudNetworkPrivateGetIpResponse
79+
if err := lwCliInst.CallLwApiInto("bleed/network/private/getip", apiArgs, &details); err != nil {
80+
lwCliInst.Die(err)
81+
}
82+
83+
fmt.Print(details)
6584
}
6685
},
6786
}
6887

6988
func init() {
7089
cloudNetworkPrivateCmd.AddCommand(cloudNetworkPrivateDetailsCmd)
7190
cloudNetworkPrivateDetailsCmd.Flags().String("uniq-id", "", "uniq-id of the Cloud Server")
72-
if err := cloudNetworkPrivateDetailsCmd.MarkFlagRequired("uniq-id"); err != nil {
73-
lwCliInst.Die(err)
74-
}
91+
cloudNetworkPrivateDetailsCmd.Flags().Bool("all", false, "get details for all Cloud Servers")
7592
}

types/api/cloud.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,20 @@ type CloudNetworkPrivateGetIpResponse struct {
429429
Ip string `json:"ip" mapstructure:"ip"`
430430
}
431431

432+
func (self CloudNetworkPrivateGetIpResponse) String() string {
433+
var slice []string
434+
435+
if self.Ip == "" {
436+
slice = append(slice, fmt.Sprintf("Cloud Server [%s] is not attached to a Private Network\n", self.UniqId))
437+
} else {
438+
slice = append(slice, fmt.Sprintf("Cloud Server [%s] is attached to a Private Network\n", self.UniqId))
439+
slice = append(slice, fmt.Sprintf("\tIP: %s\n", self.Ip))
440+
slice = append(slice, fmt.Sprintf("\tLegacy: %t\n", self.Legacy))
441+
}
442+
443+
return strings.Join(slice[:], "")
444+
}
445+
432446
type CloudNetworkPrivateIsAttachedResponse struct {
433447
IsAttached bool `json:"is_attached" mapstructure:"is_attached"`
434448
}

0 commit comments

Comments
 (0)