Skip to content

Commit 25eaa68

Browse files
committed
add dedicated list,details
1 parent 146483b commit 25eaa68

File tree

5 files changed

+235
-1
lines changed

5 files changed

+235
-1
lines changed

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ install: security
44
scripts/build/install
55

66
security:
7-
#go get github.com/securego/gosec/cmd/gosec
87
@gosec ./...
98

109
clean:

cmd/dedicated.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
"os"
20+
21+
"github.com/spf13/cobra"
22+
)
23+
24+
var dedicatedCmd = &cobra.Command{
25+
Use: "dedicated",
26+
Short: "Traditional dedicated servers.",
27+
Long: `Command line interface to traditional dedicated servers.
28+
29+
For a full list of capabilities, please refer to the "Available Commands" section.`,
30+
Run: func(cmd *cobra.Command, args []string) {
31+
if err := cmd.Help(); err != nil {
32+
lwCliInst.Die(err)
33+
}
34+
os.Exit(1)
35+
},
36+
}
37+
38+
func init() {
39+
rootCmd.AddCommand(dedicatedCmd)
40+
}

cmd/dedicatedDetails.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
"fmt"
20+
21+
"github.com/spf13/cobra"
22+
23+
//"github.com/liquidweb/liquidweb-cli/instance"
24+
"github.com/liquidweb/liquidweb-cli/types/api"
25+
"github.com/liquidweb/liquidweb-cli/validate"
26+
)
27+
28+
var dedicatedDetailsCmd = &cobra.Command{
29+
Use: "details",
30+
Short: "Get details of a dedicated server",
31+
Long: `Get details of a dedicated server`,
32+
Run: func(cmd *cobra.Command, args []string) {
33+
uniqIdFlag, _ := cmd.Flags().GetString("uniq-id")
34+
jsonFlag, _ := cmd.Flags().GetBool("json")
35+
36+
validateFields := map[interface{}]interface{}{
37+
uniqIdFlag: "UniqId",
38+
}
39+
if err := validate.Validate(validateFields); err != nil {
40+
lwCliInst.Die(err)
41+
}
42+
43+
var details apiTypes.Subaccnt
44+
apiArgs := map[string]interface{}{
45+
"uniq_id": uniqIdFlag,
46+
"alsowith": []string{"categories"},
47+
}
48+
49+
if err := lwCliInst.CallLwApiInto("bleed/asset/details", apiArgs, &details); err != nil {
50+
lwCliInst.Die(err)
51+
}
52+
53+
var found bool
54+
for _, category := range details.Categories {
55+
if category == "StrictDedicated" {
56+
found = true
57+
break
58+
}
59+
}
60+
61+
if !found {
62+
lwCliInst.Die(fmt.Errorf("UniqId [%s] is not a dedicated server", uniqIdFlag))
63+
}
64+
65+
if jsonFlag {
66+
pretty, err := lwCliInst.JsonEncodeAndPrettyPrint(details)
67+
if err != nil {
68+
panic(err)
69+
}
70+
fmt.Print(pretty)
71+
} else {
72+
fmt.Print(details)
73+
}
74+
},
75+
}
76+
77+
func init() {
78+
dedicatedCmd.AddCommand(dedicatedDetailsCmd)
79+
80+
dedicatedDetailsCmd.Flags().Bool("json", false, "output in json format")
81+
dedicatedDetailsCmd.Flags().String("uniq-id", "", "uniq-id of the dedicated server")
82+
83+
if err := dedicatedDetailsCmd.MarkFlagRequired("uniq-id"); err != nil {
84+
lwCliInst.Die(err)
85+
}
86+
}

cmd/dedicatedList.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
"fmt"
20+
21+
"github.com/spf13/cobra"
22+
23+
"github.com/liquidweb/liquidweb-cli/instance"
24+
"github.com/liquidweb/liquidweb-cli/types/api"
25+
)
26+
27+
var dedicatedListCmd = &cobra.Command{
28+
Use: "list",
29+
Short: "List Dedicated Servers on your account",
30+
Long: `List Dedicated Servers on your account`,
31+
Run: func(cmd *cobra.Command, args []string) {
32+
jsonFlag, _ := cmd.Flags().GetBool("json")
33+
34+
methodArgs := instance.AllPaginatedResultsArgs{
35+
Method: "bleed/asset/list",
36+
ResultsPerPage: 100,
37+
MethodArgs: map[string]interface{}{
38+
"category": []string{"StrictDedicated"},
39+
},
40+
}
41+
results, err := lwCliInst.AllPaginatedResults(&methodArgs)
42+
if err != nil {
43+
lwCliInst.Die(err)
44+
}
45+
46+
if jsonFlag {
47+
pretty, err := lwCliInst.JsonEncodeAndPrettyPrint(results)
48+
if err != nil {
49+
lwCliInst.Die(err)
50+
}
51+
fmt.Printf(pretty)
52+
} else {
53+
serverCnt := 1
54+
for _, item := range results.Items {
55+
56+
var details apiTypes.Subaccnt
57+
if err := instance.CastFieldTypes(item, &details); err != nil {
58+
lwCliInst.Die(err)
59+
}
60+
61+
fmt.Printf("%d.) ", serverCnt)
62+
fmt.Print(details)
63+
serverCnt++
64+
}
65+
}
66+
},
67+
}
68+
69+
func init() {
70+
dedicatedCmd.AddCommand(dedicatedListCmd)
71+
72+
dedicatedListCmd.Flags().Bool("json", false, "output in json format")
73+
}

types/api/subaccnt.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package apiTypes
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
type Subaccnt struct {
9+
Active bool `json:"active" mapstructure:"active"`
10+
Domain string `json:"domain" mapstructure:"domain"`
11+
Ip string `json:"ip" mapstructure:"ip"`
12+
ProjectId int64 `json:"project_id" mapstructure:"project_id"`
13+
ProjectName string `json:"project_name" mapstructure:"project_name"`
14+
RegionId int `json:"region_id" mapstructure:"region_id"`
15+
Status string `json:"status" mapstructure:"status"`
16+
Type string `json:"type" mapstructure:"type"`
17+
UniqId string `json:"uniq_id" mapstructure:"uniq_id"`
18+
Username string `json:"username" mapstructure:"username"`
19+
Categories []string `json:"categories" mapstructure:"categories"`
20+
}
21+
22+
func (x Subaccnt) String() string {
23+
var slice []string
24+
25+
slice = append(slice, fmt.Sprintf("Domain: %s UniqId: %s\n", x.Domain, x.UniqId))
26+
27+
slice = append(slice, fmt.Sprintf("\tIp: %s\n", x.Ip))
28+
if x.ProjectName != "" && x.ProjectId != 0 {
29+
slice = append(slice, fmt.Sprintf("\tProjectName: %s (id %d)\n", x.ProjectName, x.ProjectId))
30+
}
31+
slice = append(slice, fmt.Sprintf("\tRegionId: %d\n", x.RegionId))
32+
slice = append(slice, fmt.Sprintf("\tStatus: %s\n", x.Status))
33+
slice = append(slice, fmt.Sprintf("\tType: %s\n", x.Type))
34+
35+
return strings.Join(slice[:], "")
36+
}

0 commit comments

Comments
 (0)