Skip to content

Commit 161fcc2

Browse files
committed
feat: vps command implementation
1 parent c26bb56 commit 161fcc2

File tree

19 files changed

+455
-275
lines changed

19 files changed

+455
-275
lines changed

client/generate.go

Lines changed: 0 additions & 3 deletions
This file was deleted.

cmd/vps/virtual_machines/get_attached_keys.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
var GetAttachedKeysCmd = &cobra.Command{
14-
Use: "get-attached-keys",
14+
Use: "get-attached-keys <virtual machine ID>",
1515
Short: "Get attached public keys",
1616
Long: `This endpoint retrieves a list of public keys attached to a specified virtual machine.`,
1717
Args: cobra.MatchAll(cobra.ExactArgs(1)),

cmd/vps/virtual_machines/item.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package virtual_machines
2+
3+
import (
4+
"context"
5+
"github.com/hostinger/api-cli/api"
6+
"github.com/hostinger/api-cli/output"
7+
"github.com/hostinger/api-cli/utils"
8+
"github.com/spf13/cobra"
9+
"log"
10+
)
11+
12+
var GetCmd = &cobra.Command{
13+
Use: "get <virtual machine ID>",
14+
Short: "Get virtual machine",
15+
Long: `This endpoint retrieves detailed information about a specified virtual machine.`,
16+
Args: cobra.MatchAll(cobra.ExactArgs(1)),
17+
Run: func(cmd *cobra.Command, args []string) {
18+
r, err := api.Request().VPSGetVirtualMachineV1WithResponse(context.TODO(), utils.StringToInt(args[0]))
19+
if err != nil {
20+
log.Fatal(err)
21+
}
22+
23+
output.Format(cmd, r.Body, r.StatusCode())
24+
},
25+
}

cmd/vps/virtual_machines/list.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,8 @@ import (
1010

1111
var ListCmd = &cobra.Command{
1212
Use: "list",
13-
Short: "A brief description of your command",
14-
Long: `A longer description that spans multiple lines and likely contains examples
15-
and usage of using your command. For example:
16-
17-
Cobra is a CLI library for Go that empowers applications.
18-
This application is a tool to generate the needed files
19-
to quickly create a Cobra application.`,
13+
Short: "Get virtual machine list",
14+
Long: `This endpoint retrieves a list of all available virtual machines.`,
2015
Run: func(cmd *cobra.Command, args []string) {
2116
r, err := api.Request().VPSGetVirtualMachineListV1WithResponse(context.TODO())
2217
if err != nil {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package virtual_machines
2+
3+
import (
4+
"context"
5+
"github.com/hostinger/api-cli/api"
6+
"github.com/hostinger/api-cli/client"
7+
"github.com/hostinger/api-cli/output"
8+
"github.com/hostinger/api-cli/utils"
9+
"github.com/spf13/cobra"
10+
"log"
11+
)
12+
13+
var GetMetricsCmd = &cobra.Command{
14+
Use: "metrics <virtual machine ID>",
15+
Short: "Get metrics",
16+
Long: `This endpoint retrieves the historical metrics for a specified virtual machine.`,
17+
Args: cobra.MatchAll(cobra.ExactArgs(1)),
18+
Run: func(cmd *cobra.Command, args []string) {
19+
r, err := api.Request().VPSGetMetricsV1WithResponse(context.TODO(), utils.StringToInt(args[0]), metricsRequestParameters(cmd))
20+
if err != nil {
21+
log.Fatal(err)
22+
}
23+
24+
output.Format(cmd, r.Body, r.StatusCode())
25+
},
26+
}
27+
28+
func init() {
29+
GetMetricsCmd.Flags().StringP("date-from", "f", "", "Date from")
30+
GetMetricsCmd.Flags().StringP("date-to", "t", "", "Date to")
31+
32+
GetMetricsCmd.MarkFlagRequired("date-from")
33+
GetMetricsCmd.MarkFlagRequired("date-to")
34+
}
35+
36+
func metricsRequestParameters(cmd *cobra.Command) *client.VPSGetMetricsV1Params {
37+
dateFrom, _ := cmd.Flags().GetString("date-from")
38+
dateTo, _ := cmd.Flags().GetString("date-to")
39+
40+
return &client.VPSGetMetricsV1Params{
41+
DateFrom: utils.StringToTime(dateFrom),
42+
DateTo: utils.StringToTime(dateTo),
43+
}
44+
}

cmd/vps/virtual_machines/recreate.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ import (
1313
var RecreateCmd = &cobra.Command{
1414
Use: "recreate <virtual machine ID>",
1515
Short: "Recreate virtual machine",
16-
Args: cobra.MatchAll(cobra.ExactArgs(1)),
16+
Long: `This endpoint will recreate a virtual machine from scratch. The recreation process involves reinstalling the
17+
operating system and resetting the virtual machine to its initial state. Snapshots, if there are any, will be deleted.`,
18+
Args: cobra.MatchAll(cobra.ExactArgs(1)),
1719
Run: func(cmd *cobra.Command, args []string) {
18-
r, err := api.Request().VPSRecreateVirtualMachineV1WithResponse(context.TODO(), utils.StringToInt(args[0]), createRequestFromFlags(cmd))
20+
r, err := api.Request().VPSRecreateVirtualMachineV1WithResponse(context.TODO(), utils.StringToInt(args[0]), recreateRequestFromFlags(cmd))
1921
if err != nil {
2022
log.Fatal(err)
2123
}
@@ -32,7 +34,7 @@ func init() {
3234
RecreateCmd.MarkFlagRequired("template_id")
3335
}
3436

35-
func createRequestFromFlags(cmd *cobra.Command) client.VPSV1VirtualMachineRecreateRequest {
37+
func recreateRequestFromFlags(cmd *cobra.Command) client.VPSV1VirtualMachineRecreateRequest {
3638
templateId, _ := cmd.Flags().GetInt("template_id")
3739
password, _ := cmd.Flags().GetString("password")
3840
postInstallScriptId, _ := cmd.Flags().GetInt("post_install_script_id")

cmd/vps/virtual_machines/set_hostname.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import (
1313
var SetHostnameCmd = &cobra.Command{
1414
Use: "set-hostname <virtual machine ID>",
1515
Short: "Set hostname",
16-
Long: `
17-
This endpoint sets the hostname for a specified virtual machine. Changing hostname does not update PTR record automatically.
16+
Long: `This endpoint sets the hostname for a specified virtual machine. Changing hostname does not update PTR record automatically.
1817
If you want your virtual machine to be reachable by a hostname, you need to point your domain A/AAAA records
1918
to virtual machine IP as well.`,
2019
Args: cobra.MatchAll(cobra.ExactArgs(1)),
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package virtual_machines
2+
3+
import (
4+
"context"
5+
"github.com/hostinger/api-cli/api"
6+
"github.com/hostinger/api-cli/client"
7+
"github.com/hostinger/api-cli/output"
8+
"github.com/hostinger/api-cli/utils"
9+
"github.com/spf13/cobra"
10+
"log"
11+
)
12+
13+
var SetNameserversCmd = &cobra.Command{
14+
Use: "set-nameservers <virtual machine ID>",
15+
Short: "Set nameservers",
16+
Long: `This endpoint sets the nameservers for a specified virtual machine.
17+
Be aware, that improper nameserver configuration can lead to the virtual machine being unable to resolve domain names.`,
18+
Args: cobra.MatchAll(cobra.ExactArgs(1)),
19+
Run: func(cmd *cobra.Command, args []string) {
20+
r, err := api.Request().VPSSetNameserversV1WithResponse(context.TODO(), utils.StringToInt(args[0]), setNameserversRequest(cmd))
21+
22+
if err != nil {
23+
log.Fatal(err)
24+
}
25+
26+
output.Format(cmd, r.Body, r.StatusCode())
27+
},
28+
}
29+
30+
func init() {
31+
SetHostnameCmd.Flags().StringP("ns1", "", "", "Name server 1")
32+
SetHostnameCmd.Flags().StringP("ns2", "", "", "Name server 2")
33+
34+
SetHostnameCmd.MarkFlagRequired("ns1")
35+
}
36+
37+
func setNameserversRequest(cmd *cobra.Command) client.VPSSetNameserversV1JSONRequestBody {
38+
ns1, _ := cmd.Flags().GetString("ns1")
39+
ns2, _ := cmd.Flags().GetString("ns2")
40+
41+
return client.VPSSetNameserversV1JSONRequestBody{
42+
Ns1: ns1,
43+
Ns2: utils.StringPtrOrNil(ns2),
44+
}
45+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package virtual_machines
2+
3+
import (
4+
"context"
5+
"github.com/hostinger/api-cli/api"
6+
"github.com/hostinger/api-cli/client"
7+
"github.com/hostinger/api-cli/output"
8+
"github.com/hostinger/api-cli/utils"
9+
"github.com/spf13/cobra"
10+
"log"
11+
)
12+
13+
var SetPanelPasswordCmd = &cobra.Command{
14+
Use: "set-panel-password <virtual machine ID>",
15+
Short: "Set panel password",
16+
Long: `This endpoint sets the panel password for a specified virtual machine.
17+
If virtual machine does not use panel OS, the request will still be processed without any effect.`,
18+
Args: cobra.MatchAll(cobra.ExactArgs(1)),
19+
Run: func(cmd *cobra.Command, args []string) {
20+
r, err := api.Request().VPSSetPanelPasswordV1WithResponse(context.TODO(), utils.StringToInt(args[0]), setPanelPasswordRequest(cmd))
21+
22+
if err != nil {
23+
log.Fatal(err)
24+
}
25+
26+
output.Format(cmd, r.Body, r.StatusCode())
27+
},
28+
}
29+
30+
func init() {
31+
SetHostnameCmd.Flags().StringP("password", "", "", "Password")
32+
33+
SetHostnameCmd.MarkFlagRequired("password")
34+
}
35+
36+
func setPanelPasswordRequest(cmd *cobra.Command) client.VPSSetPanelPasswordV1JSONRequestBody {
37+
password, _ := cmd.Flags().GetString("password")
38+
39+
return client.VPSSetPanelPasswordV1JSONRequestBody{
40+
Password: password,
41+
}
42+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package virtual_machines
2+
3+
import (
4+
"context"
5+
"github.com/hostinger/api-cli/api"
6+
"github.com/hostinger/api-cli/client"
7+
"github.com/hostinger/api-cli/output"
8+
"github.com/hostinger/api-cli/utils"
9+
"github.com/spf13/cobra"
10+
"log"
11+
)
12+
13+
var SetRootPasswordCmd = &cobra.Command{
14+
Use: "set-root-password <virtual machine ID>",
15+
Short: "Set root password",
16+
Long: `This endpoint sets the root password for a specified virtual machine.`,
17+
Args: cobra.MatchAll(cobra.ExactArgs(1)),
18+
Run: func(cmd *cobra.Command, args []string) {
19+
r, err := api.Request().VPSSetRootPasswordV1WithResponse(context.TODO(), utils.StringToInt(args[0]), setRootPasswordRequest(cmd))
20+
21+
if err != nil {
22+
log.Fatal(err)
23+
}
24+
25+
output.Format(cmd, r.Body, r.StatusCode())
26+
},
27+
}
28+
29+
func init() {
30+
SetRootPasswordCmd.Flags().StringP("password", "", "", "Password")
31+
32+
SetRootPasswordCmd.MarkFlagRequired("password")
33+
}
34+
35+
func setRootPasswordRequest(cmd *cobra.Command) client.VPSSetRootPasswordV1JSONRequestBody {
36+
password, _ := cmd.Flags().GetString("password")
37+
38+
return client.VPSSetRootPasswordV1JSONRequestBody{
39+
Password: password,
40+
}
41+
}

0 commit comments

Comments
 (0)