diff --git a/internal/commands/ip.go b/internal/commands/ip.go index a0cc275..9c92529 100644 --- a/internal/commands/ip.go +++ b/internal/commands/ip.go @@ -195,24 +195,29 @@ func newIPReleaseCmd() *cobra.Command { func newIPStaticNATEnableCmd() *cobra.Command { var vmSlug string + var networkSlug string cmd := &cobra.Command{ Use: "enable ", 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 @@ -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) } diff --git a/pkg/api/ipaddress/ipaddress.go b/pkg/api/ipaddress/ipaddress.go index a390f6c..bfb8dbc 100644 --- a/pkg/api/ipaddress/ipaddress.go +++ b/pkg/api/ipaddress/ipaddress.go @@ -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. @@ -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. diff --git a/pkg/api/ipaddress/ipaddress_test.go b/pkg/api/ipaddress/ipaddress_test.go index 883ccc8..48d41d8 100644 --- a/pkg/api/ipaddress/ipaddress_test.go +++ b/pkg/api/ipaddress/ipaddress_test.go @@ -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) {