Skip to content

Commit

Permalink
Merge pull request #56 from liquidweb/add-rm-pub-ips-update
Browse files Browse the repository at this point in the history
add `cloud network {public,private}` to plans
  • Loading branch information
sgsullivan authored Nov 17, 2020
2 parents c3a6cf3 + 3663bcb commit 09d2d77
Show file tree
Hide file tree
Showing 18 changed files with 585 additions and 165 deletions.
34 changes: 9 additions & 25 deletions cmd/cloudNetworkPrivateAttach.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import (

"github.com/spf13/cobra"

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

var cloudNetworkPrivateAttachCmdUniqIdFlag []string

var cloudNetworkPrivateAttachCmd = &cobra.Command{
Use: "attach",
Short: "Attach a Cloud Server to a Private Network",
Expand All @@ -39,40 +40,23 @@ Applications that communicate internally will frequently use this for both secur
and cost-savings.
`,
Run: func(cmd *cobra.Command, args []string) {
uniqIdFlag, _ := cmd.Flags().GetString("uniq-id")

validateFields := map[interface{}]interface{}{
uniqIdFlag: "UniqId",
}
if err := validate.Validate(validateFields); err != nil {
lwCliInst.Die(err)
}
params := &instance.CloudNetworkPrivateAttachParams{}

apiArgs := map[string]interface{}{"uniq_id": uniqIdFlag}

var attachedDetails apiTypes.CloudNetworkPrivateIsAttachedResponse
err := lwCliInst.CallLwApiInto("bleed/network/private/isattached", apiArgs, &attachedDetails)
if err != nil {
lwCliInst.Die(err)
}
if attachedDetails.IsAttached {
lwCliInst.Die(fmt.Errorf("Cloud Server is already attached to the Private Network"))
}
params.UniqId = cloudNetworkPrivateAttachCmdUniqIdFlag

var details apiTypes.CloudNetworkPrivateAttachResponse
err = lwCliInst.CallLwApiInto("bleed/network/private/attach", apiArgs, &details)
status, err := lwCliInst.CloudNetworkPrivateAttach(params)
if err != nil {
lwCliInst.Die(err)
}

fmt.Printf("Attaching %s to private network\n", details.Attached)
fmt.Printf("\n\nYou can check progress with 'cloud server status --uniq-id %s'\n", uniqIdFlag)
fmt.Print(status)
},
}

func init() {
cloudNetworkPrivateCmd.AddCommand(cloudNetworkPrivateAttachCmd)
cloudNetworkPrivateAttachCmd.Flags().String("uniq-id", "", "uniq-id of the Cloud Server")
cloudNetworkPrivateAttachCmd.Flags().StringSliceVar(&cloudNetworkPrivateAttachCmdUniqIdFlag, "uniq-id",
[]string{}, "uniq-ids separated by ',' of Cloud Servers to attach to private networking")
if err := cloudNetworkPrivateAttachCmd.MarkFlagRequired("uniq-id"); err != nil {
lwCliInst.Die(err)
}
Expand Down
34 changes: 9 additions & 25 deletions cmd/cloudNetworkPrivateDetach.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import (

"github.com/spf13/cobra"

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

var cloudNetworkPrivateDetachCmdUniqIdFlag []string

var cloudNetworkPrivateDetachCmd = &cobra.Command{
Use: "detach",
Short: "Detach a Cloud Server from a Private Network",
Expand All @@ -39,40 +40,23 @@ Applications that communicate internally will frequently use this for both secur
and cost-savings.
`,
Run: func(cmd *cobra.Command, args []string) {
uniqIdFlag, _ := cmd.Flags().GetString("uniq-id")

validateFields := map[interface{}]interface{}{
uniqIdFlag: "UniqId",
}
if err := validate.Validate(validateFields); err != nil {
lwCliInst.Die(err)
}
params := &instance.CloudNetworkPrivateDetachParams{}

apiArgs := map[string]interface{}{"uniq_id": uniqIdFlag}

var attachedDetails apiTypes.CloudNetworkPrivateIsAttachedResponse
err := lwCliInst.CallLwApiInto("bleed/network/private/isattached", apiArgs, &attachedDetails)
if err != nil {
lwCliInst.Die(err)
}
if !attachedDetails.IsAttached {
lwCliInst.Die(fmt.Errorf("Cloud Server is already detached to the Private Network"))
}
params.UniqId = cloudNetworkPrivateDetachCmdUniqIdFlag

var details apiTypes.CloudNetworkPrivateDetachResponse
err = lwCliInst.CallLwApiInto("bleed/network/private/detach", apiArgs, &details)
status, err := lwCliInst.CloudNetworkPrivateDetach(params)
if err != nil {
lwCliInst.Die(err)
}

fmt.Printf("Detaching %s from private network\n", details.Detached)
fmt.Printf("\n\nYou can check progress with 'cloud server status --uniq-id %s'\n\n", uniqIdFlag)
fmt.Print(status)
},
}

func init() {
cloudNetworkPrivateCmd.AddCommand(cloudNetworkPrivateDetachCmd)
cloudNetworkPrivateDetachCmd.Flags().String("uniq-id", "", "uniq-id of the Cloud Server")
cloudNetworkPrivateDetachCmd.Flags().StringSliceVar(&cloudNetworkPrivateDetachCmdUniqIdFlag, "uniq-id",
[]string{}, "uniq-ids separated by ',' of Cloud Servers to detach from private networking")
if err := cloudNetworkPrivateDetachCmd.MarkFlagRequired("uniq-id"); err != nil {
lwCliInst.Die(err)
}
Expand Down
70 changes: 17 additions & 53 deletions cmd/cloudNetworkPublicAdd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import (

"github.com/spf13/cobra"

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

var cloudNetworkPublicAddCmdPoolIpsFlag []string
Expand All @@ -31,71 +30,36 @@ var cloudNetworkPublicAddCmd = &cobra.Command{
Short: "Add Public IP(s) to a Cloud Server",
Long: `Add Public IP(s) to a Cloud Server.
Add a number of IPs to an existing Cloud Server. If the reboot flag is passed, the
server will be stopped, have the new IP addresses configured, and then started.
Add a number of IPs to an existing Cloud Server. If the configure-ips flag is
passed in, the IP addresses will be automatically configured within the guest
operating system.
When the reboot flag is not passed, the IP will be assigned to the server, but it
will be up to the administrator to configure the IP address(es) within the server.
`,
If the configure-ips flag is not passed, the IP addresses will be assigned, and
routing will be allowed. However the IP(s) will not be automatically configured
in the guest operating system. In this scenario, it will be up to the system
administrator to add the IP(s) to the network configuration.`,
Run: func(cmd *cobra.Command, args []string) {
uniqIdFlag, _ := cmd.Flags().GetString("uniq-id")
rebootFlag, _ := cmd.Flags().GetBool("reboot")
newIpsFlag, _ := cmd.Flags().GetInt64("new-ips")
params := &instance.CloudNetworkPublicAddParams{}

validateFields := map[interface{}]interface{}{
uniqIdFlag: "UniqId",
}
if err := validate.Validate(validateFields); err != nil {
lwCliInst.Die(err)
}
params.UniqId, _ = cmd.Flags().GetString("uniq-id")
params.ConfigureIps, _ = cmd.Flags().GetBool("configure-ips")
params.NewIps, _ = cmd.Flags().GetInt64("new-ips")
params.PoolIps = cloudNetworkPublicAddCmdPoolIpsFlag

if newIpsFlag == 0 && len(cloudNetworkPublicAddCmdPoolIpsFlag) == 0 {
lwCliInst.Die(fmt.Errorf("at least one of --new-ips --pool-ips must be given"))
}

apiArgs := map[string]interface{}{
"reboot": rebootFlag,
"uniq_id": uniqIdFlag,
}
if newIpsFlag != 0 {
apiArgs["ip_count"] = newIpsFlag
validateFields := map[interface{}]interface{}{newIpsFlag: "PositiveInt64"}
if err := validate.Validate(validateFields); err != nil {
lwCliInst.Die(err)
}
}
if len(cloudNetworkPublicAddCmdPoolIpsFlag) != 0 {
apiArgs["pool_ips"] = cloudNetworkPublicAddCmdPoolIpsFlag
validateFields := map[interface{}]interface{}{}
for _, ip := range cloudNetworkPublicAddCmdPoolIpsFlag {
validateFields[ip] = "IP"
}
if err := validate.Validate(validateFields); err != nil {
lwCliInst.Die(err)
}
}

var details apiTypes.NetworkIpAdd
err := lwCliInst.CallLwApiInto("bleed/network/ip/add", apiArgs, &details)
status, err := lwCliInst.CloudNetworkPublicAdd(params)
if err != nil {
lwCliInst.Die(err)
}

fmt.Printf("Adding [%s] to Cloud Server\n", details.Adding)

if rebootFlag {
fmt.Println("Server will be rebooted and IP(s) automatically configured.")
} else {
fmt.Println("Server will not be rebooted. IP's will need to be manually configured.")
}
fmt.Print(status)
},
}

func init() {
cloudNetworkPublicCmd.AddCommand(cloudNetworkPublicAddCmd)
cloudNetworkPublicAddCmd.Flags().String("uniq-id", "", "uniq-id of the Cloud Server")
cloudNetworkPublicAddCmd.Flags().Bool("reboot", false,
"wheter or not to automatically configure the new IP address(es) in the server (requires reboot)")
cloudNetworkPublicAddCmd.Flags().Bool("configure-ips", false,
"wheter or not to automatically configure the new IP address(es) in the server")
cloudNetworkPublicAddCmd.Flags().Int64("new-ips", 0, "amount of new ips to (randomly) grab")
cloudNetworkPublicAddCmd.Flags().StringSliceVar(&cloudNetworkPublicAddCmdPoolIpsFlag, "pool-ips", []string{},
"ips from your IP Pool separated by ',' to assign to the Cloud Server")
Expand Down
56 changes: 17 additions & 39 deletions cmd/cloudNetworkPublicRemove.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import (

"github.com/spf13/cobra"

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

var cloudNetworkPublicRemoveCmdIpsFlag []string
Expand All @@ -31,57 +30,36 @@ var cloudNetworkPublicRemoveCmd = &cobra.Command{
Short: "Remove Public IP(s) from a Cloud Server",
Long: `Remove Public IP(s) from a Cloud Server.
Remove specific Public IP(s) from a Cloud Server. If the reboot flag is passed in, the machine
will be stopped, have the old IP addresses removed, and then started.
Remove specific Public IP(s) from a Cloud Server. If the configure-ips flag is passed in,
the IP addresses given will also be automatically removed from the guest operating system.
If the reboot flag is not passed, the IP will be unassigned, and you will no longer be able
to route the IP. However the machine will not be shutdown to remove it from its network
configuration. It will be up to the administrator to remove the IP from the servers network
configuration.
If the configure-ips flag is not passed, the IP will be unassigned, and you will no longer
be able to route the IP. However the IP(s) will still be set in the guest operating system.
In this scenario, it will be up to the system administrator to remove the IP(s) from the
network configuration.
Note that you cannot remove the Cloud Servers primary ip with this command.`,
Run: func(cmd *cobra.Command, args []string) {
uniqIdFlag, _ := cmd.Flags().GetString("uniq-id")
rebootFlag, _ := cmd.Flags().GetBool("reboot")
params := &instance.CloudNetworkPublicRemoveParams{}

validateFields := map[interface{}]interface{}{
uniqIdFlag: "UniqId",
}
if err := validate.Validate(validateFields); err != nil {
lwCliInst.Die(err)
}
params.UniqId, _ = cmd.Flags().GetString("uniq-id")
params.ConfigureIps, _ = cmd.Flags().GetBool("configure-ips")
params.Ips = cloudNetworkPublicRemoveCmdIpsFlag

apiArgs := map[string]interface{}{
"reboot": rebootFlag,
"uniq_id": uniqIdFlag,
status, err := lwCliInst.CloudNetworkPublicRemove(params)
if err != nil {
lwCliInst.Die(err)
}

for _, ip := range cloudNetworkPublicRemoveCmdIpsFlag {
validateFields := map[interface{}]interface{}{
ip: "IP",
}
if err := validate.Validate(validateFields); err != nil {
fmt.Printf("%s ... skipping\n", err)
continue
}

var details apiTypes.NetworkIpRemove
apiArgs["ip"] = ip
err := lwCliInst.CallLwApiInto("bleed/network/ip/remove", apiArgs, &details)
if err != nil {
lwCliInst.Die(err)
}

fmt.Printf("Removing [%s] from Cloud Server\n", details.Removing)
}
fmt.Print(status)
},
}

func init() {
cloudNetworkPublicCmd.AddCommand(cloudNetworkPublicRemoveCmd)
cloudNetworkPublicRemoveCmd.Flags().String("uniq-id", "", "uniq-id of the Cloud Server")
cloudNetworkPublicRemoveCmd.Flags().Bool("reboot", false,
"whether or not to automatically remove the IP address(es) in the server config (requires reboot)")
cloudNetworkPublicRemoveCmd.Flags().Bool("configure-ips", false,
"whether or not to automatically remove the IP address(es) in the server config")
cloudNetworkPublicRemoveCmd.Flags().StringSliceVar(&cloudNetworkPublicRemoveCmdIpsFlag, "ips", []string{},
"ips separated by ',' to remove from the Cloud Server")

Expand Down
2 changes: 1 addition & 1 deletion cmd/cloudServerBlockStorageOptimized.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ purposely runs very tight. To work around this we can reduce the RAM allocated
to the Cloud Server to give more to the hypervisor. We call this Cloud Block
Storage Optimized.
Enabling or disabling Cloud Block Storage will cause your Cloud Server to reboot.
Enabling or disabling Cloud Block Storage Optimized will cause your Cloud Server to reboot.
For a full list of capabilities, please refer to the "Available Commands" section.`,

Expand Down
2 changes: 1 addition & 1 deletion cmd/cloudServerBlockStorageOptimizedDisable.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ purposely runs very tight. To work around this we can reduce the RAM allocated
to the Cloud Server to give more to the hypervisor. We call this Cloud Block
Storage Optimized.
Disabling Cloud Block Storage will cause your Cloud Server to reboot.`,
Disabling Cloud Block Storage Optimized will cause your Cloud Server to reboot.`,

Run: func(cmd *cobra.Command, args []string) {
uniqIdFlag, _ := cmd.Flags().GetString("uniq-id")
Expand Down
2 changes: 1 addition & 1 deletion cmd/cloudServerBlockStorageOptimizedEnable.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ purposely runs very tight. To work around this we can reduce the RAM allocated
to the Cloud Server to give more to the hypervisor. We call this Cloud Block
Storage Optimized.
Enabling Cloud Block Storage will cause your Cloud Server to reboot.`,
Enabling Cloud Block Storage Optimized will cause your Cloud Server to reboot.`,

Run: func(cmd *cobra.Command, args []string) {
uniqIdFlag, _ := cmd.Flags().GetString("uniq-id")
Expand Down
8 changes: 8 additions & 0 deletions examples/plans/cloud.network.private.attach.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
cloud:
network:
private:
attach:
- uniq-id:
- "{{- .Var.uniq_id -}}"
- "{{- .Var.uniq_id2 -}}"
8 changes: 8 additions & 0 deletions examples/plans/cloud.network.private.detach.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
cloud:
network:
private:
detach:
- uniq-id:
- "{{- .Var.uniq_id -}}"
- "{{- .Var.uniq_id2 -}}"
12 changes: 12 additions & 0 deletions examples/plans/cloud.network.public.add.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
cloud:
network:
public:
add:
- uniq-id: "{{- .Var.uniq_id -}}"
configure-ips: true
new-ips: 2
- uniq-id: "{{- .Var.uniq_id -}}"
configure-ips: true
pool-ips:
- "{{- .Var.poolip1 -}}"
9 changes: 9 additions & 0 deletions examples/plans/cloud.network.public.remove.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
cloud:
network:
public:
remove:
- uniq-id: "{{- .Var.uniq_id -}}"
configure-ips: true
ips:
- "{{- .Var.rmip1 -}}"
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ require (
github.com/pelletier/go-toml v1.8.1 // indirect
github.com/spf13/afero v1.4.1 // indirect
github.com/spf13/cast v1.3.1
github.com/spf13/cobra v1.0.0
github.com/spf13/cobra v1.1.1
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.5.1 // indirect
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0
golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634 // indirect
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897
golang.org/x/sys v0.0.0-20201110211018-35f3e6cf4a65 // indirect
golang.org/x/text v0.3.4 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/yaml.v2 v2.3.0
Expand Down
Loading

0 comments on commit 09d2d77

Please sign in to comment.