Skip to content

Commit

Permalink
reserved-ipv6: Add reserved IPv6 commands (#1623)
Browse files Browse the repository at this point in the history
* vendor: bump digitalocean/godo to v1.131.1-0.20241127211050-a97e39731918

* Add ReservedIPv6 displayer

* Add ReservedIPv6 service and actions implementation

* Add mock implementations for ReservedIPv6ActionsService and ReservedIPv6sService

* Add ReservedIPv6 and ReservedIPv6Action commands with tests

* Add integration tests for ReservedIPv6 create, delete, get, and list operations

* fix: integration test

* Add integration tests for ReservedIPv6 actions and update response handling

* Apply suggestions from code review

Co-authored-by: Anna Lushnikova <[email protected]>

* fix: standardize indentation in ReservedIPv6 integration tests

---------

Co-authored-by: Anna Lushnikova <[email protected]>
  • Loading branch information
amanoj-do and loosla authored Dec 10, 2024
1 parent 45cd88d commit 7935b48
Show file tree
Hide file tree
Showing 21 changed files with 1,414 additions and 91 deletions.
74 changes: 39 additions & 35 deletions commands/command_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,41 +38,43 @@ type CmdConfig struct {
componentBuilderFactory builder.ComponentBuilderFactory

// services
Keys func() do.KeysService
Sizes func() do.SizesService
Regions func() do.RegionsService
Images func() do.ImagesService
ImageActions func() do.ImageActionsService
LoadBalancers func() do.LoadBalancersService
ReservedIPs func() do.ReservedIPsService
ReservedIPActions func() do.ReservedIPActionsService
Droplets func() do.DropletsService
DropletActions func() do.DropletActionsService
DropletAutoscale func() do.DropletAutoscaleService
Domains func() do.DomainsService
Actions func() do.ActionsService
Account func() do.AccountService
Balance func() do.BalanceService
BillingHistory func() do.BillingHistoryService
Invoices func() do.InvoicesService
Tags func() do.TagsService
UptimeChecks func() do.UptimeChecksService
Volumes func() do.VolumesService
VolumeActions func() do.VolumeActionsService
Snapshots func() do.SnapshotsService
Certificates func() do.CertificatesService
Firewalls func() do.FirewallsService
CDNs func() do.CDNsService
Projects func() do.ProjectsService
Kubernetes func() do.KubernetesService
Databases func() do.DatabasesService
Registry func() do.RegistryService
VPCs func() do.VPCsService
OneClicks func() do.OneClickService
Apps func() do.AppsService
Monitoring func() do.MonitoringService
Serverless func() do.ServerlessService
OAuth func() do.OAuthService
Keys func() do.KeysService
Sizes func() do.SizesService
Regions func() do.RegionsService
Images func() do.ImagesService
ImageActions func() do.ImageActionsService
LoadBalancers func() do.LoadBalancersService
ReservedIPs func() do.ReservedIPsService
ReservedIPv6s func() do.ReservedIPv6sService
ReservedIPv6Actions func() do.ReservedIPv6ActionsService
ReservedIPActions func() do.ReservedIPActionsService
Droplets func() do.DropletsService
DropletActions func() do.DropletActionsService
DropletAutoscale func() do.DropletAutoscaleService
Domains func() do.DomainsService
Actions func() do.ActionsService
Account func() do.AccountService
Balance func() do.BalanceService
BillingHistory func() do.BillingHistoryService
Invoices func() do.InvoicesService
Tags func() do.TagsService
UptimeChecks func() do.UptimeChecksService
Volumes func() do.VolumesService
VolumeActions func() do.VolumeActionsService
Snapshots func() do.SnapshotsService
Certificates func() do.CertificatesService
Firewalls func() do.FirewallsService
CDNs func() do.CDNsService
Projects func() do.ProjectsService
Kubernetes func() do.KubernetesService
Databases func() do.DatabasesService
Registry func() do.RegistryService
VPCs func() do.VPCsService
OneClicks func() do.OneClickService
Apps func() do.AppsService
Monitoring func() do.MonitoringService
Serverless func() do.ServerlessService
OAuth func() do.OAuthService
}

// NewCmdConfig creates an instance of a CmdConfig.
Expand All @@ -98,6 +100,8 @@ func NewCmdConfig(ns string, dc doctl.Config, out io.Writer, args []string, init
c.ImageActions = func() do.ImageActionsService { return do.NewImageActionsService(godoClient) }
c.ReservedIPs = func() do.ReservedIPsService { return do.NewReservedIPsService(godoClient) }
c.ReservedIPActions = func() do.ReservedIPActionsService { return do.NewReservedIPActionsService(godoClient) }
c.ReservedIPv6s = func() do.ReservedIPv6sService { return do.NewReservedIPv6sService(godoClient) }
c.ReservedIPv6Actions = func() do.ReservedIPv6ActionsService { return do.NewReservedIPv6ActionsService(godoClient) }
c.Droplets = func() do.DropletsService { return do.NewDropletsService(godoClient) }
c.DropletActions = func() do.DropletActionsService { return do.NewDropletActionsService(godoClient) }
c.DropletAutoscale = func() do.DropletAutoscaleService { return do.NewDropletAutoscaleService(godoClient) }
Expand Down
85 changes: 50 additions & 35 deletions commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ var (
}
testReservedIPList = do.ReservedIPs{testReservedIP}

testReservedIPv6 = do.ReservedIPv6{
ReservedIPV6: &godo.ReservedIPV6{
Droplet: testDroplet.Droplet,
RegionSlug: testDroplet.Region.Slug,
IP: "5a11:a:b0a7",
},
}
testReservedIPv6List = do.ReservedIPv6s{testReservedIPv6}

testSnapshot = do.Snapshot{
Snapshot: &godo.Snapshot{
ID: "1",
Expand Down Expand Up @@ -220,6 +229,8 @@ type tcMocks struct {
invoices *domocks.MockInvoicesService
reservedIPs *domocks.MockReservedIPsService
reservedIPActions *domocks.MockReservedIPActionsService
reservedIPv6s *domocks.MockReservedIPv6sService
reservedIPv6Actions *domocks.MockReservedIPv6ActionsService
domains *domocks.MockDomainsService
uptimeChecks *domocks.MockUptimeChecksService
volumes *domocks.MockVolumesService
Expand Down Expand Up @@ -264,6 +275,8 @@ func withTestClient(t *testing.T, tFn testFn) {
invoices: domocks.NewMockInvoicesService(ctrl),
reservedIPs: domocks.NewMockReservedIPsService(ctrl),
reservedIPActions: domocks.NewMockReservedIPActionsService(ctrl),
reservedIPv6s: domocks.NewMockReservedIPv6sService(ctrl),
reservedIPv6Actions: domocks.NewMockReservedIPv6ActionsService(ctrl),
droplets: domocks.NewMockDropletsService(ctrl),
dropletActions: domocks.NewMockDropletActionsService(ctrl),
dropletAutoscale: domocks.NewMockDropletAutoscaleService(ctrl),
Expand Down Expand Up @@ -313,41 +326,43 @@ func withTestClient(t *testing.T, tFn testFn) {

componentBuilderFactory: tm.appBuilderFactory,

Keys: func() do.KeysService { return tm.keys },
Sizes: func() do.SizesService { return tm.sizes },
Regions: func() do.RegionsService { return tm.regions },
Images: func() do.ImagesService { return tm.images },
ImageActions: func() do.ImageActionsService { return tm.imageActions },
ReservedIPs: func() do.ReservedIPsService { return tm.reservedIPs },
ReservedIPActions: func() do.ReservedIPActionsService { return tm.reservedIPActions },
Droplets: func() do.DropletsService { return tm.droplets },
DropletActions: func() do.DropletActionsService { return tm.dropletActions },
DropletAutoscale: func() do.DropletAutoscaleService { return tm.dropletAutoscale },
Domains: func() do.DomainsService { return tm.domains },
Actions: func() do.ActionsService { return tm.actions },
Account: func() do.AccountService { return tm.account },
Balance: func() do.BalanceService { return tm.balance },
BillingHistory: func() do.BillingHistoryService { return tm.billingHistory },
Invoices: func() do.InvoicesService { return tm.invoices },
Tags: func() do.TagsService { return tm.tags },
UptimeChecks: func() do.UptimeChecksService { return tm.uptimeChecks },
Volumes: func() do.VolumesService { return tm.volumes },
VolumeActions: func() do.VolumeActionsService { return tm.volumeActions },
Snapshots: func() do.SnapshotsService { return tm.snapshots },
Certificates: func() do.CertificatesService { return tm.certificates },
LoadBalancers: func() do.LoadBalancersService { return tm.loadBalancers },
Firewalls: func() do.FirewallsService { return tm.firewalls },
CDNs: func() do.CDNsService { return tm.cdns },
Projects: func() do.ProjectsService { return tm.projects },
Kubernetes: func() do.KubernetesService { return tm.kubernetes },
Databases: func() do.DatabasesService { return tm.databases },
Registry: func() do.RegistryService { return tm.registry },
VPCs: func() do.VPCsService { return tm.vpcs },
OneClicks: func() do.OneClickService { return tm.oneClick },
Apps: func() do.AppsService { return tm.apps },
Monitoring: func() do.MonitoringService { return tm.monitoring },
Serverless: func() do.ServerlessService { return tm.serverless },
OAuth: func() do.OAuthService { return tm.oauth },
Keys: func() do.KeysService { return tm.keys },
Sizes: func() do.SizesService { return tm.sizes },
Regions: func() do.RegionsService { return tm.regions },
Images: func() do.ImagesService { return tm.images },
ImageActions: func() do.ImageActionsService { return tm.imageActions },
ReservedIPs: func() do.ReservedIPsService { return tm.reservedIPs },
ReservedIPActions: func() do.ReservedIPActionsService { return tm.reservedIPActions },
ReservedIPv6s: func() do.ReservedIPv6sService { return tm.reservedIPv6s },
ReservedIPv6Actions: func() do.ReservedIPv6ActionsService { return tm.reservedIPv6Actions },
Droplets: func() do.DropletsService { return tm.droplets },
DropletActions: func() do.DropletActionsService { return tm.dropletActions },
DropletAutoscale: func() do.DropletAutoscaleService { return tm.dropletAutoscale },
Domains: func() do.DomainsService { return tm.domains },
Actions: func() do.ActionsService { return tm.actions },
Account: func() do.AccountService { return tm.account },
Balance: func() do.BalanceService { return tm.balance },
BillingHistory: func() do.BillingHistoryService { return tm.billingHistory },
Invoices: func() do.InvoicesService { return tm.invoices },
Tags: func() do.TagsService { return tm.tags },
UptimeChecks: func() do.UptimeChecksService { return tm.uptimeChecks },
Volumes: func() do.VolumesService { return tm.volumes },
VolumeActions: func() do.VolumeActionsService { return tm.volumeActions },
Snapshots: func() do.SnapshotsService { return tm.snapshots },
Certificates: func() do.CertificatesService { return tm.certificates },
LoadBalancers: func() do.LoadBalancersService { return tm.loadBalancers },
Firewalls: func() do.FirewallsService { return tm.firewalls },
CDNs: func() do.CDNsService { return tm.cdns },
Projects: func() do.ProjectsService { return tm.projects },
Kubernetes: func() do.KubernetesService { return tm.kubernetes },
Databases: func() do.DatabasesService { return tm.databases },
Registry: func() do.RegistryService { return tm.registry },
VPCs: func() do.VPCsService { return tm.vpcs },
OneClicks: func() do.OneClickService { return tm.oneClick },
Apps: func() do.AppsService { return tm.apps },
Monitoring: func() do.MonitoringService { return tm.monitoring },
Serverless: func() do.ServerlessService { return tm.serverless },
OAuth: func() do.OAuthService { return tm.oauth },
}

tFn(config, tm)
Expand Down
66 changes: 66 additions & 0 deletions commands/displayers/reserved_ipv6.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright 2024 The Doctl Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package displayers

import (
"fmt"
"io"

"github.com/digitalocean/doctl/do"
)

type ReservedIPv6 struct {
ReservedIPv6s do.ReservedIPv6s
}

var _ Displayable = &ReservedIPv6{}

func (rip *ReservedIPv6) JSON(out io.Writer) error {
return writeJSON(rip.ReservedIPv6s, out)
}

func (rip *ReservedIPv6) Cols() []string {
return []string{
"IP", "Region", "DropletID", "DropletName",
}
}

func (rip *ReservedIPv6) ColMap() map[string]string {
return map[string]string{
"IP": "IP", "Region": "Region", "DropletID": "Droplet ID", "DropletName": "Droplet Name",
}
}

func (rip *ReservedIPv6) KV() []map[string]any {
out := make([]map[string]any, 0, len(rip.ReservedIPv6s))

for _, f := range rip.ReservedIPv6s {
var dropletID, dropletName string
if f.Droplet != nil {
dropletID = fmt.Sprintf("%d", f.Droplet.ID)
dropletName = f.Droplet.Name
}

o := map[string]any{
"IP": f.IP,
"Region": f.RegionSlug,
"DropletID": dropletID,
"DropletName": dropletName,
}

out = append(out, o)
}

return out
}
2 changes: 2 additions & 0 deletions commands/doit.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ func computeCmd() *Command {
cmd.AddCommand(Firewall())
cmd.AddCommand(ReservedIP())
cmd.AddCommand(ReservedIPAction())
cmd.AddCommand(ReservedIPv6())
cmd.AddCommand(ReservedIPv6Action())
cmd.AddCommand(Images())
cmd.AddCommand(ImageAction())
cmd.AddCommand(LoadBalancer())
Expand Down
93 changes: 93 additions & 0 deletions commands/reserved_ipv6_actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
Copyright 2024 The Doctl Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package commands

import (
"fmt"
"strconv"

"github.com/digitalocean/doctl"
"github.com/digitalocean/doctl/commands/displayers"
"github.com/digitalocean/doctl/do"
"github.com/spf13/cobra"
)

// ReservedIPv6Action creates the reserved IPv6 action command.
func ReservedIPv6Action() *Command {
cmd := &Command{
Command: &cobra.Command{
Use: "reserved-ipv6-action",
Short: "Display commands to associate reserved IPv6 addresses with Droplets",
Long: "Reserved IP actions are commands that are used to manage DigitalOcean reserved IPv6 addresses.",
Aliases: []string{"reserved-ipv6-actions"},
Hidden: true,
},
}

cmdReservedIPv6ActionsAssign := CmdBuilder(cmd, RunReservedIPv6ActionsAssign,
"assign <reserved-ipv6> <droplet-id>", "Assign a reserved IPv6 address to a Droplet", "Assigns a reserved IPv6 address to the specified Droplet.", Writer,
displayerType(&displayers.Action{}))
cmdReservedIPv6ActionsAssign.Example = `The following example assigns the reserved IPv6 address ` + "`" + `5a11:a:b0a7` + "`" + ` to a Droplet with the ID ` + "`" + `386734086` + "`" + `: doctl compute reserved-ipv6-action assign 5a11:a:b0a7 386734086`

cmdReservedIPv6ActionsUnassign := CmdBuilder(cmd, RunReservedIPv6ActionsUnassign,
"unassign <reserved-ipv6>", "Unassign a reserved IPv6 address from a Droplet", `Unassigns a reserved IPv6 address from a Droplet.`, Writer,
displayerType(&displayers.Action{}))
cmdReservedIPv6ActionsUnassign.Example = `The following example unassigns the reserved IPv6 address ` + "`" + `5a11:a:b0a7` + "`" + ` from a resource: doctl compute reserved-ipv6-action unassign 5a11:a:b0a7`

return cmd
}

// RunReservedIPv6ActionsAssign assigns a reserved IP to a droplet.
func RunReservedIPv6ActionsAssign(c *CmdConfig) error {
if len(c.Args) != 2 {
return doctl.NewMissingArgsErr(c.NS)
}

ip := c.Args[0]

fia := c.ReservedIPv6Actions()

dropletID, err := strconv.Atoi(c.Args[1])
if err != nil {
return err
}

a, err := fia.Assign(ip, dropletID)
if err != nil {
checkErr(fmt.Errorf("could not assign IP to droplet: %v", err))
}

item := &displayers.Action{Actions: do.Actions{*a}}
return c.Display(item)
}

// RunReservedIPActionsUnassign unassigns a reserved IP to a droplet.
func RunReservedIPv6ActionsUnassign(c *CmdConfig) error {
err := ensureOneArg(c)
if err != nil {
return err
}

ip := c.Args[0]

fia := c.ReservedIPv6Actions()

a, err := fia.Unassign(ip)
if err != nil {
checkErr(fmt.Errorf("could not unassign IP to droplet: %v", err))
}

item := &displayers.Action{Actions: do.Actions{*a}}
return c.Display(item)
}
Loading

0 comments on commit 7935b48

Please sign in to comment.