Skip to content

Commit 2878c28

Browse files
authored
Merge branch 'master' into schedule-snapshots
2 parents 184a045 + acc29ff commit 2878c28

27 files changed

+1011
-80
lines changed

cmd/instance/instance.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ func init() {
4141
InstanceCmd.AddCommand(instanceVncCmd)
4242
InstanceCmd.AddCommand(instanceRecoveryCmd)
4343
InstanceCmd.AddCommand(instanceRecoveryStatusCmd)
44+
InstanceCmd.AddCommand(snapshotCmd)
45+
InstanceCmd.AddCommand(instanceAllowedIPsUpdateCmd)
46+
InstanceCmd.AddCommand(instanceBandwidthUpdateCmd)
47+
4448

4549
instanceUpdateCmd.Flags().StringVarP(&notes, "notes", "n", "", "notes stored against the instance")
4650
instanceUpdateCmd.Flags().StringVarP(&reverseDNS, "reverse-dns", "r", "", "the reverse DNS entry for the instance")
@@ -62,8 +66,14 @@ func init() {
6266
instanceCreateCmd.Flags().BoolVar(&skipShebangCheck, "skip-shebang-check", false, "skip the shebang line check when passing a user init script")
6367
instanceCreateCmd.Flags().StringSliceVarP(&volumes, "volumes", "v", []string{}, "List of volumes to attach at boot")
6468
instanceCreateCmd.Flags().StringVarP(&volumetype, "volume-type", "", "", "Specify the volume type for the instance")
69+
instanceCreateCmd.Flags().StringArrayVar(&allowedIPs, "allowed-ips", []string{}, "A comma separated list of IP addresses that the instance is allowed to use")
70+
instanceCreateCmd.Flags().IntVar(&networkBandwidthLimit, "network-bandwidth-limit", 0, "The network bandwidth limit for the instance in Mbps (0 for unlimited)")
6571

6672
instanceVncCmd.Flags().StringVarP(&duration, "duration", "d", "", "Duration for VNC access (e.g. 30m, 1h, 24h)")
6773

6874
instanceStopCmd.Flags().BoolVarP(&waitStop, "wait", "w", false, "wait until the instance's is stoped")
75+
76+
instanceAllowedIPsUpdateCmd.Flags().StringSliceVarP(&allowedIPsUpdate, "ips", "", []string{}, "Comma-separated list of IP addresses to allow (e.g., --ips 1.2.3.4,5.6.7.8). To clear all IPs, provide an empty string.")
77+
78+
instanceBandwidthUpdateCmd.Flags().IntVarP(&bandwidthLimitUpdate, "limit", "l", 0, "Network bandwidth limit in Mbps (e.g., 1000). Use 0 for unlimited")
6979
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package instance
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
// "strings" // Uncomment if needed for string manipulations not covered by flags
8+
9+
"github.com/civo/cli/common"
10+
"github.com/civo/cli/config"
11+
"github.com/civo/cli/utility"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
var allowedIPsUpdate []string
16+
17+
var instanceAllowedIPsUpdateCmd = &cobra.Command{
18+
Use: "allowed-ips-update <INSTANCE_ID_OR_NAME>",
19+
Aliases: []string{"update-allowed-ips"},
20+
Short: "Update the allowed IP addresses for an instance",
21+
Long: `Update the list of IP addresses that an instance is allowed to use for network traffic (IP/MAC spoofing protection).
22+
Note: This replaces the existing list of allowed IPs. To clear all allowed IPs, pass an empty list with --ips "".`,
23+
Example: "civo instance allowed-ips-update my-instance --ips 192.168.0.10,10.0.0.5",
24+
Args: cobra.ExactArgs(1),
25+
Run: func(cmd *cobra.Command, args []string) {
26+
utility.EnsureCurrentRegion()
27+
28+
client, err := config.CivoAPIClient()
29+
if common.RegionSet != "" {
30+
client.Region = common.RegionSet
31+
}
32+
if err != nil {
33+
utility.Error("Creating the connection to Civo's API failed with %s", err)
34+
os.Exit(1)
35+
}
36+
37+
instance, err := client.FindInstance(args[0])
38+
if err != nil {
39+
utility.Error("Finding instance %s", err)
40+
os.Exit(1)
41+
}
42+
43+
// 'allowedIPsUpdate' is already a []string from the StringSliceVarP flag.
44+
// If --ips is not provided, allowedIPsUpdate will be an empty slice by default.
45+
// If --ips is provided as an empty string (e.g., --ips \"\"), it should also result in an empty slice,
46+
// which is useful for clearing the allowed IPs if the API supports it.
47+
48+
resp, err := client.UpdateInstanceAllowedIPs(instance.ID, allowedIPsUpdate)
49+
if err != nil {
50+
utility.Error("Updating allowed IPs for instance %s: %s", instance.ID, err)
51+
os.Exit(1)
52+
}
53+
54+
ow := utility.NewOutputWriter()
55+
ow.StartLine()
56+
ow.AppendDataWithLabel("id", instance.ID, "ID")
57+
ow.AppendDataWithLabel("hostname", instance.Hostname, "Hostname")
58+
ow.AppendDataWithLabel("result", string(resp.Result), "Result")
59+
60+
if common.OutputFormat == "human" {
61+
if resp.Result == "success" { // Assuming SimpleResponse has a "Result" field that indicates success
62+
fmt.Printf("Allowed IPs for instance %s (%s) updated successfully.\n", utility.Green(instance.Hostname), instance.ID)
63+
if len(allowedIPsUpdate) > 0 {
64+
fmt.Printf("New allowed IPs: %v\n", allowedIPsUpdate)
65+
} else {
66+
fmt.Println("All allowed IPs have been cleared.")
67+
}
68+
} else {
69+
fmt.Printf("Failed to update allowed IPs for instance %s (%s). Result: %s\n", utility.Red(instance.Hostname), instance.ID, resp.Result)
70+
}
71+
} else {
72+
ow.WriteSingleObjectJSON(common.PrettySet)
73+
}
74+
},
75+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package instance
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strconv"
7+
8+
"github.com/civo/cli/common"
9+
"github.com/civo/cli/config"
10+
"github.com/civo/cli/utility"
11+
"github.com/spf13/cobra"
12+
)
13+
14+
var bandwidthLimitUpdate int
15+
16+
var instanceBandwidthUpdateCmd = &cobra.Command{
17+
Use: "bandwidth-update <INSTANCE_ID_OR_NAME>",
18+
Aliases: []string{"update-bandwidth"},
19+
Short: "Update the network bandwidth limit for an instance",
20+
Long: `Update the network bandwidth limit for a specified instance.
21+
The limit is specified in Mbps. Use 0 for unlimited bandwidth (if supported by the API).`,
22+
Example: "civo instance bandwidth-update my-instance --limit 1000",
23+
Args: cobra.ExactArgs(1),
24+
Run: func(cmd *cobra.Command, args []string) {
25+
utility.EnsureCurrentRegion()
26+
27+
client, err := config.CivoAPIClient()
28+
if common.RegionSet != "" {
29+
client.Region = common.RegionSet
30+
}
31+
if err != nil {
32+
utility.Error("Creating the connection to Civo's API failed with %s", err)
33+
os.Exit(1)
34+
}
35+
36+
instance, err := client.FindInstance(args[0])
37+
if err != nil {
38+
utility.Error("Finding instance %s: %s", args[0], err)
39+
os.Exit(1)
40+
}
41+
42+
resp, err := client.UpdateInstanceBandwidth(instance.ID, bandwidthLimitUpdate)
43+
if err != nil {
44+
utility.Error("Updating bandwidth limit for instance %s: %s", instance.ID, err)
45+
os.Exit(1)
46+
}
47+
48+
ow := utility.NewOutputWriter()
49+
ow.StartLine()
50+
ow.AppendDataWithLabel("id", instance.ID, "ID")
51+
ow.AppendDataWithLabel("hostname", instance.Hostname, "Hostname")
52+
ow.AppendDataWithLabel("result", string(resp.Result), "Result")
53+
54+
newLimitStr := strconv.Itoa(bandwidthLimitUpdate) + " Mbps"
55+
if bandwidthLimitUpdate == 0 {
56+
newLimitStr = "Unlimited"
57+
}
58+
ow.AppendDataWithLabel("new_bandwidth_limit", newLimitStr, "New Bandwidth Limit")
59+
60+
if common.OutputFormat == "human" {
61+
if string(resp.Result) == "success" {
62+
fmt.Printf("Network bandwidth limit for instance %s (%s) updated successfully to %s.\n",
63+
utility.Green(instance.Hostname), instance.ID, newLimitStr)
64+
} else {
65+
fmt.Printf("Failed to update network bandwidth limit for instance %s (%s). Result: %s\n",
66+
utility.Red(instance.Hostname), instance.ID, string(resp.Result))
67+
}
68+
} else {
69+
ow.WriteSingleObjectJSON(common.PrettySet)
70+
}
71+
},
72+
}

cmd/instance/instance_create.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ var hostnameCreate, size, diskimage, publicip, initialuser, sshkey, tags, networ
2222
var script string
2323
var skipShebangCheck bool
2424
var volumes []string
25+
var allowedIPs []string
26+
var networkBandwidthLimit int
2527

2628
var instanceCreateCmd = &cobra.Command{
2729
Use: "create",
@@ -145,6 +147,14 @@ If you wish to use a custom format, the available fields are:
145147
config.PublicIPRequired = publicip
146148
}
147149

150+
if len(allowedIPs) > 0 {
151+
config.AllowedIPs = allowedIPs
152+
}
153+
154+
if networkBandwidthLimit > 0 {
155+
config.NetworkBandwidthLimit = networkBandwidthLimit
156+
}
157+
148158
// Set reserved ip if provided
149159
if reservedIPv4 != "" {
150160
config.ReservedIPv4 = reservedIPv4
@@ -320,7 +330,7 @@ If you wish to use a custom format, the available fields are:
320330
}
321331
}
322332

323-
if common.OutputFormat == "human" {
333+
if common.OutputFormat == common.OutputFormatHuman {
324334
if executionTime != "" {
325335
fmt.Printf("The instance %s %s has been created in %s\n", utility.Green(instance.Hostname), publicIP, executionTime)
326336
} else {

cmd/instance/instance_firewall.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ If you wish to use a custom format, the available fields are:
7878
os.Exit(1)
7979
}
8080

81-
if common.OutputFormat == "human" {
82-
fmt.Printf("Set the firewall for the instance %s (%s) to %s (%s)\n", utility.Green(instance.Hostname), instance.ID, utility.Green(firewall.Name), firewall.ID)
81+
if common.OutputFormat == common.OutputFormatHuman {
82+
fmt.Printf("The instance %s (%s) has been assigned to firewall %s (%s)\n", utility.Green(instance.Hostname), instance.ID, utility.Green(firewall.Name), firewall.ID)
8383
} else {
8484
ow := utility.NewOutputWriter()
8585
ow.StartLine()

cmd/instance/instance_move_ip.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ If you wish to use a custom format, the available fields are:
7070
os.Exit(1)
7171
}
7272

73-
if common.OutputFormat == "human" {
73+
if common.OutputFormat == common.OutputFormatHuman {
7474
fmt.Printf("Moved the IP %s to the instance %s (%s)\n", utility.Green(args[1]), utility.Green(instance.Hostname), instance.ID)
7575
} else {
7676
ow := utility.NewOutputWriter()

cmd/instance/instance_password.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ If you wish to use a custom format, the available fields are:
4141
os.Exit(1)
4242
}
4343

44-
if common.OutputFormat == "human" {
44+
if common.OutputFormat == common.OutputFormatHuman {
4545
fmt.Printf("The instance %s (%s) has the password %s (and user %s)\n", utility.Green(instance.Hostname), instance.ID, utility.Green(instance.InitialPassword), utility.Green(instance.InitialUser))
4646
} else {
4747
ow := utility.NewOutputWriter()

cmd/instance/instance_reboot.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ If you wish to use a custom format, the available fields are:
4545
os.Exit(1)
4646
}
4747

48-
if common.OutputFormat == "human" {
48+
if common.OutputFormat == common.OutputFormatHuman {
4949
fmt.Printf("The instance %s (%s) is being rebooted\n", utility.Green(instance.Hostname), instance.ID)
5050
} else {
5151
ow := utility.NewOutputWriter()

cmd/instance/instance_show.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ If you wish to use a custom format, the available fields are:
4343
* public_ip
4444
* notes
4545
* script
46-
* reverse_dns`,
46+
* reverse_dns
47+
* allowed_ips
48+
* network_bandwidth_limit`,
4749
Run: func(cmd *cobra.Command, args []string) {
4850
utility.EnsureCurrentRegion()
4951

@@ -97,6 +99,20 @@ If you wish to use a custom format, the available fields are:
9799
ow.AppendDataWithLabel("attached_volumes", strings.Join(volumeIDs, ", "), "Attached Volume IDs")
98100
}
99101

102+
// Add AllowedIPs
103+
allowedIPsStr := "None"
104+
if len(instance.AllowedIPs) > 0 {
105+
allowedIPsStr = strings.Join(instance.AllowedIPs, ", ")
106+
}
107+
ow.AppendDataWithLabel("allowed_ips", allowedIPsStr, "Allowed IPs")
108+
109+
// Add NetworkBandwidthLimit
110+
bandwidthLimitStr := "Unlimited"
111+
if instance.NetworkBandwidthLimit > 0 {
112+
bandwidthLimitStr = strconv.Itoa(instance.NetworkBandwidthLimit) + " Mbps"
113+
}
114+
ow.AppendDataWithLabel("network_bandwidth_limit", bandwidthLimitStr, "Network Bandwidth Limit")
115+
100116
if common.OutputFormat == "json" || common.OutputFormat == "custom" {
101117
ow.AppendDataWithLabel("public_ip", instance.PublicIP, "Public IP")
102118
ow.AppendDataWithLabel("notes", instance.Notes, "notes")

0 commit comments

Comments
 (0)