Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions internal/commands/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,24 +195,29 @@ func newIPReleaseCmd() *cobra.Command {

func newIPStaticNATEnableCmd() *cobra.Command {
var vmSlug string
var networkSlug string

cmd := &cobra.Command{
Use: "enable <ip-slug>",
Short: "Enable static NAT on an IP address",
Args: exactArgs(1),
Example: ` zcp ip static-nat enable 1036521143 --instance my-vm`,
Example: ` zcp ip static-nat enable 1036521143 --instance my-vm --network my-network`,
RunE: func(cmd *cobra.Command, args []string) error {
if vmSlug == "" {
return fmt.Errorf("--instance is required")
}
return runIPStaticNATEnable(cmd, args[0], vmSlug)
if networkSlug == "" {
return fmt.Errorf("--network is required")
}
return runIPStaticNATEnable(cmd, args[0], vmSlug, networkSlug)
},
}
cmd.Flags().StringVar(&vmSlug, "instance", "", "VM slug to associate (required)")
cmd.Flags().StringVar(&networkSlug, "network", "", "Network slug to associate (required)")
return cmd
}

func runIPStaticNATEnable(cmd *cobra.Command, ipSlug, vmSlug string) error {
func runIPStaticNATEnable(cmd *cobra.Command, ipSlug, vmSlug, networkSlug string) error {
_, client, printer, err := buildClientAndPrinter(cmd)
if err != nil {
return err
Expand All @@ -222,19 +227,13 @@ func runIPStaticNATEnable(cmd *cobra.Command, ipSlug, vmSlug string) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(getTimeout(cmd))*time.Second)
defer cancel()

ip, err := svc.EnableStaticNAT(ctx, ipSlug, vmSlug)
ip, err := svc.EnableStaticNAT(ctx, ipSlug, vmSlug, networkSlug)
if err != nil {
return fmt.Errorf("ip static-nat enable: %w", err)
}

headers := []string{"FIELD", "VALUE"}
rows := [][]string{
{"Slug", ip.Slug},
{"IP Address", ip.IPAddress},
{"Strategy", ip.Strategy},
{"VM", ip.VirtualMachineName},
{"Network ID", ip.NetworkID},
}
headers := []string{"STATUS", "MESSAGE"}
rows := [][]string{{ip.Status, ip.Message}}
return printer.PrintTable(headers, rows)
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/api/ipaddress/ipaddress.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type CreateRequest struct {
// StaticNATRequest holds parameters for enabling static NAT.
type StaticNATRequest struct {
VirtualMachine string `json:"virtual_machine"`
Network string `json:"network"`
}

// RemoteAccessVPN represents a remote access VPN entry on an IP address.
Expand Down Expand Up @@ -150,13 +151,13 @@ func (s *Service) Allocate(ctx context.Context, req CreateRequest) (*IPAddress,
// EnableStaticNAT enables static NAT, associating a public IP with a VM.
// ipSlug is the IP address slug (e.g. "1036521143").
// vmSlug is the virtual machine slug.
func (s *Service) EnableStaticNAT(ctx context.Context, ipSlug, vmSlug string) (*IPAddress, error) {
body := StaticNATRequest{VirtualMachine: vmSlug}
func (s *Service) EnableStaticNAT(ctx context.Context, ipSlug, vmSlug, networkSlug string) (*singleResponse, error) {
body := StaticNATRequest{VirtualMachine: vmSlug, Network: networkSlug}
var resp singleResponse
if err := s.client.Post(ctx, "/ipaddresses/"+ipSlug+"/static-nat", body, &resp); err != nil {
return nil, fmt.Errorf("enabling static NAT for IP %s: %w", ipSlug, err)
}
return &resp.Data, nil
return &resp, nil
}

// ListRemoteAccessVPNs returns remote access VPNs for a public IP address.
Expand Down
9 changes: 6 additions & 3 deletions pkg/api/ipaddress/ipaddress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,22 @@ func TestIPEnableStaticNAT(t *testing.T) {
defer srv.Close()

svc := ipaddress.NewService(newClient(srv.URL))
result, err := svc.EnableStaticNAT(context.Background(), "1030011", "my-vm")
result, err := svc.EnableStaticNAT(context.Background(), "1030011", "my-vm", "my-network")
if err != nil {
t.Fatalf("EnableStaticNAT() error = %v", err)
}
if gotPath != "/ipaddresses/1030011/static-nat" {
t.Errorf("path = %q, want %q", gotPath, "/ipaddresses/1030011/static-nat")
}
if result.Strategy != "STATIC-NAT" {
t.Errorf("result.Strategy = %q, want %q", result.Strategy, "STATIC-NAT")
if result.Status != "Success" {
t.Errorf("result.Status = %q, want %q", result.Status, "Success")
}
if gotBody["virtual_machine"] != "my-vm" {
t.Errorf("body virtual_machine = %v, want %q", gotBody["virtual_machine"], "my-vm")
}
if gotBody["network"] != "my-network" {
t.Errorf("body network = %v, want %q", gotBody["network"], "my-network")
}
}

func TestIPListRemoteAccessVPNs(t *testing.T) {
Expand Down
Loading