-
Notifications
You must be signed in to change notification settings - Fork 395
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reserved-ipv6: Add reserved IPv6 commands (#1623)
* 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
Showing
21 changed files
with
1,414 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.