Skip to content

Commit 4979b33

Browse files
imaskmanup-deka
andauthored
Add byoip update method (#1744)
* add byoip update method * update godo version for byoip update prefix * add unit tests * updated godo version --------- Co-authored-by: anup-deka <[email protected]>
1 parent 35bec70 commit 4979b33

File tree

9 files changed

+278
-10
lines changed

9 files changed

+278
-10
lines changed

args.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,9 @@ const (
763763
// ArgSignature is a byoip prefix argument.
764764
ArgSignature = "signature"
765765

766+
// ArgAdvertise is a byoip prefix argument.
767+
ArgAdvertise = "advertise"
768+
766769
// ArgAgentParentID is the ID for the parent agent.
767770
ArgParentAgentId = "parent-agent-id"
768771

commands/byoip_prefix.go

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,14 @@ BYOIP Prefixes can be held in the region they were created in on your account.`,
4545
cmdBYOIPPrefixCreate.Example = `The following example creates a byoip prefix in the ` + "`" + `nyc1` + "`" + ` region: doctl network byoip-prefix create --region nyc1 --prefix "10.1.1.1/24" --signature "signature"`
4646

4747
cmdBYOIPPrefixGet := CmdBuilder(cmd, RunBYOIPPrefixGet, "get <prefix-uuid>", "Retrieve information about a byoip prefix", "Retrieves detailed information about a BYOIP Prefix", Writer,
48-
aliasOpt("g"), displayerType(&displayers.ReservedIPv6{}))
48+
aliasOpt("g"), displayerType(&displayers.BYOIPPrefix{}))
4949
cmdBYOIPPrefixGet.Example = `The following example retrieves information about the byoip prefix ` + "`" + `5ae545c4-0ac4-42bb-9de5-8eca3d17f1c0` + "`" + `: doctl network byoip-prefix get 5ae545c4-0ac4-42bb-9de5-8eca3d17f1c0`
5050

51+
cmdBYOIPPrefixUpdate := CmdBuilder(cmd, RunBYOIPPrefixUpdate, "update <prefix-uuid>", "Update a BYOIP Prefix", "Updates the advertisement status of a BYOIP Prefix", Writer,
52+
aliasOpt("u"), displayerType(&displayers.BYOIPPrefix{}))
53+
AddBoolFlag(cmdBYOIPPrefixUpdate, doctl.ArgAdvertise, "", false, "Enable or disable advertisement of the BYOIP Prefix")
54+
cmdBYOIPPrefixUpdate.Example = `The following example updates the byoip prefix ` + "`" + `5ae545c4-0ac4-42bb-9de5-8eca3d17f1c0` + "`" + ` to enable advertisement: doctl network byoip-prefix update 5ae545c4-0ac4-42bb-9de5-8eca3d17f1c0 --advertise=true`
55+
5156
cmdRunBYOIPPrefixDelete := CmdBuilder(cmd, RunBYOIPPrefixDelete, "delete <prefix-uuid>", "Permanently delete a BYOIP Prefix", "Permanently deletes a BYOIP Prefix. This is irreversible and it needs all IPs of the prefix to be unassigned", Writer, aliasOpt("d", "rm"))
5257
AddBoolFlag(cmdRunBYOIPPrefixDelete, doctl.ArgForce, doctl.ArgShortForce, false, "Deletes the BYOIP Prefix without confirmation")
5358
cmdRunBYOIPPrefixDelete.Example = `The following example deletes the byoip prefix ` + "`" + `5ae545c4-0ac4-42bb-9de5-8eca3d17f1c0` + "`" + `: doctl network byoip-prefix delete 5ae545c4-0ac4-42bb-9de5-8eca3d17f1c0`
@@ -137,6 +142,46 @@ func RunBYOIPPrefixGet(c *CmdConfig) error {
137142
return c.Display(item)
138143
}
139144

145+
// RunBYOIPPrefixUpdate updates a byoip prefix.
146+
func RunBYOIPPrefixUpdate(c *CmdConfig) error {
147+
bp := c.BYOIPPrefixes()
148+
149+
err := ensureOneArg(c)
150+
if err != nil {
151+
return err
152+
}
153+
154+
prefixUUID := c.Args[0]
155+
156+
if len(prefixUUID) < 1 {
157+
return errors.New("invalid BYOIP Prefix UUID")
158+
}
159+
160+
_, err = uuid.Parse(prefixUUID) // Validate UUID format
161+
if err != nil {
162+
return fmt.Errorf("invalid BYOIP Prefix UUID: %s", prefixUUID)
163+
}
164+
165+
advertise, err := c.Doit.GetBool(c.NS, doctl.ArgAdvertise)
166+
if err != nil {
167+
return err
168+
}
169+
170+
req := &godo.BYOIPPrefixUpdateReq{
171+
Advertise: &advertise,
172+
}
173+
174+
byoipPrefix, err := bp.Update(prefixUUID, req)
175+
if err != nil {
176+
return err
177+
}
178+
179+
item := &displayers.BYOIPPrefix{BYOIPPrefixes: do.BYOIPPrefixes{
180+
*byoipPrefix,
181+
}}
182+
return c.Display(item)
183+
}
184+
140185
// RunBYOIPPrefixDelete runs byoip prefix delete.
141186
func RunBYOIPPrefixDelete(c *CmdConfig) error {
142187
bp := c.BYOIPPrefixes()
@@ -185,12 +230,12 @@ func RunBYOIPPrefixResourcesGet(c *CmdConfig) error {
185230
prefixUUID := c.Args[0]
186231

187232
if len(prefixUUID) < 1 {
188-
return errors.New("Invalid BYOIP Prefix UUID")
233+
return errors.New("invalid BYOIP Prefix UUID")
189234
}
190235

191236
_, err = uuid.Parse(prefixUUID) // Validate UUID format
192237
if err != nil {
193-
return fmt.Errorf("Invalid BYOIP Prefix UUID: %s", prefixUUID)
238+
return fmt.Errorf("invalid BYOIP Prefix UUID: %s", prefixUUID)
194239
}
195240

196241
list, err := bp.GetPrefixResources(prefixUUID)

commands/byoip_prefix_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
func TestBYOIPPrefixCommands(t *testing.T) {
2525
cmd := BYOIPPrefix()
2626
assert.NotNil(t, cmd)
27-
assertCommandNames(t, cmd, "create", "delete", "get", "list", "resource")
27+
assertCommandNames(t, cmd, "create", "delete", "get", "list", "resource", "update")
2828
}
2929

3030
func TestBYOIPPrefixList(t *testing.T) {
@@ -79,3 +79,16 @@ func TestBYOIPPrefixResourcesGet(t *testing.T) {
7979
RunBYOIPPrefixResourcesGet(config)
8080
})
8181
}
82+
83+
func TestBYOIPPrefixUpdate(t *testing.T) {
84+
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
85+
uuid := "f00b8f02-11da-424b-b658-ad8cebfc5a56"
86+
updateReq := &godo.BYOIPPrefixUpdateReq{Advertise: godo.PtrTo(true)}
87+
tm.byoipPrefixes.EXPECT().Update(uuid, updateReq).Return(&testBYOIPPrefix, nil)
88+
89+
config.Args = append(config.Args, uuid)
90+
config.Doit.Set(config.NS, doctl.ArgAdvertise, true)
91+
92+
assert.NoError(t, RunBYOIPPrefixUpdate(config))
93+
})
94+
}

commands/displayers/byoip_prefix.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ func (bp *BYOIPPrefix) JSON(out io.Writer) error {
3131

3232
func (bp *BYOIPPrefix) Cols() []string {
3333
return []string{
34-
"Prefix", "Region", "Status", "UUID", "FailureReason", "Validations",
34+
"Prefix", "Region", "Status", "UUID", "Advertised", "FailureReason", "Validations",
3535
}
3636
}
3737

3838
func (bp *BYOIPPrefix) ColMap() map[string]string {
3939
return map[string]string{
4040
"Prefix": "Prefix", "Region": "Region", "Status": "Status", "UUID": "UUID",
41-
"FailureReason": "Failure Reason", "Validations": "Validations",
41+
"Advertised": "Advertised", "FailureReason": "Failure Reason", "Validations": "Validations",
4242
}
4343
}
4444

@@ -52,6 +52,7 @@ func (bp *BYOIPPrefix) KV() []map[string]any {
5252
"Region": f.BYOIPPrefix.Region,
5353
"Status": f.BYOIPPrefix.Status,
5454
"UUID": f.BYOIPPrefix.UUID,
55+
"Advertised": f.BYOIPPrefix.Advertised,
5556
"FailureReason": f.BYOIPPrefix.FailureReason,
5657
"Validations": f.BYOIPPrefix.Validations,
5758
}

do/byoip_prefix.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type BYOIPPrefixsService interface {
4444
List() (BYOIPPrefixes, error)
4545
Get(prefixUUID string) (*BYOIPPrefix, error)
4646
Create(ficr *godo.BYOIPPrefixCreateReq) (*godo.BYOIPPrefixCreateResp, error)
47+
Update(prefixUUID string, ucr *godo.BYOIPPrefixUpdateReq) (*BYOIPPrefix, error)
4748
Delete(prefixUUID string) error
4849

4950
GetPrefixResources(prefixUUID string) (BYOIPPrefixResources, error)
@@ -109,6 +110,15 @@ func (bps *byoipPrefixService) Create(bpcr *godo.BYOIPPrefixCreateReq) (*godo.BY
109110
return prefixCreateResp, nil
110111
}
111112

113+
func (bps *byoipPrefixService) Update(prefixUUID string, bpur *godo.BYOIPPrefixUpdateReq) (*BYOIPPrefix, error) {
114+
byoipPrefix, _, err := bps.client.BYOIPPrefixes.Update(context.TODO(), prefixUUID, bpur)
115+
if err != nil {
116+
return nil, err
117+
}
118+
119+
return &BYOIPPrefix{BYOIPPrefix: byoipPrefix}, nil
120+
}
121+
112122
func (fis *byoipPrefixService) Delete(prefixUUID string) error {
113123
_, err := fis.client.BYOIPPrefixes.Delete(context.TODO(), prefixUUID)
114124
return err

do/mocks/BYOIPPrefixsService.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration/byoip_prefix_get_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ var _ = suite("network/byoip-prefix/get", func(t *testing.T, when spec.G, it spe
7777

7878
const (
7979
byoipPrefixGetOutput = `
80-
Prefix Region Status UUID Failure Reason Validations
81-
10.1.1.1/24 nyc3 active 78d564a7-bc3f-4489-be14-1fb714969213 []
80+
Prefix Region Status UUID Advertised Failure Reason Validations
81+
10.1.1.1/24 nyc3 active 78d564a7-bc3f-4489-be14-1fb714969213 false []
8282
`
8383
byoipPrefixGetResponse = `
8484
{
@@ -87,6 +87,7 @@ Prefix Region Status UUID Fai
8787
"region": "nyc3",
8888
"status": "active",
8989
"prefix": "10.1.1.1/24",
90+
"advertised": false,
9091
"validations": [],
9192
"failure_reason": ""
9293
}

integration/byoip_prefix_list_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ var _ = suite("network/byoip-prefix/list", func(t *testing.T, when spec.G, it sp
7676

7777
const (
7878
byoipPrefixListOutput = `
79-
Prefix Region Status UUID Failure Reason Validations
80-
10.1.1.1/24 nyc3 active 78d564a7-bc3f-4489-be14-1fb714969213 []
79+
Prefix Region Status UUID Advertised Failure Reason Validations
80+
10.1.1.1/24 nyc3 active 78d564a7-bc3f-4489-be14-1fb714969213 false []
8181
`
8282
byoipPrefixListResponse = `
8383
{
@@ -86,6 +86,7 @@ Prefix Region Status UUID Fai
8686
"region": "nyc3",
8787
"status": "active",
8888
"prefix": "10.1.1.1/24",
89+
"advertised": false,
8990
"validations": [],
9091
"failure_reason": ""
9192
}]
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
package integration
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"net/http"
7+
"net/http/httptest"
8+
"net/http/httputil"
9+
"os/exec"
10+
"testing"
11+
12+
"github.com/sclevine/spec"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
var _ = suite("network/byoip-prefix/update", func(t *testing.T, when spec.G, it spec.S) {
17+
var (
18+
expect *require.Assertions
19+
cmd *exec.Cmd
20+
server *httptest.Server
21+
)
22+
23+
it.Before(func() {
24+
expect = require.New(t)
25+
26+
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
27+
switch req.URL.Path {
28+
case "/v2/byoip_prefixes/78d564a7-bc3f-4489-be14-1fb714969213":
29+
auth := req.Header.Get("Authorization")
30+
if auth != "Bearer some-magic-token" {
31+
w.WriteHeader(http.StatusUnauthorized)
32+
return
33+
}
34+
35+
if req.Method != http.MethodPatch {
36+
w.WriteHeader(http.StatusMethodNotAllowed)
37+
return
38+
}
39+
40+
reqBody, err := io.ReadAll(req.Body)
41+
expect.NoError(err)
42+
43+
matchedRequest := byoipPrefixUpdateRequest
44+
responseJSON := byoipPrefixUpdateResponse
45+
46+
expect.JSONEq(matchedRequest, string(reqBody))
47+
48+
w.Write([]byte(responseJSON))
49+
default:
50+
dump, err := httputil.DumpRequest(req, true)
51+
if err != nil {
52+
t.Fatal("failed to dump request")
53+
}
54+
55+
t.Fatalf("received unknown request: %s", dump)
56+
}
57+
}))
58+
})
59+
60+
when("the valid request", func() {
61+
it("updates the byoip prefix", func() {
62+
aliases := []string{"update", "u"}
63+
64+
for _, alias := range aliases {
65+
cmd = exec.Command(builtBinaryPath,
66+
"-t", "some-magic-token",
67+
"-u", server.URL,
68+
"network",
69+
"byoip-prefix",
70+
alias,
71+
"78d564a7-bc3f-4489-be14-1fb714969213",
72+
"--advertise=true",
73+
)
74+
75+
output, err := cmd.CombinedOutput()
76+
expect.NoError(err, fmt.Sprintf("received error output: %s", output))
77+
expect.Equal(norm(byoipPrefixUpdateOutput), norm(string(output)))
78+
}
79+
})
80+
})
81+
82+
when("advertise is set to false", func() {
83+
it("updates the byoip prefix to disable advertisement", func() {
84+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
85+
switch req.URL.Path {
86+
case "/v2/byoip_prefixes/78d564a7-bc3f-4489-be14-1fb714969213":
87+
auth := req.Header.Get("Authorization")
88+
if auth != "Bearer some-magic-token" {
89+
w.WriteHeader(http.StatusUnauthorized)
90+
return
91+
}
92+
93+
if req.Method != http.MethodPatch {
94+
w.WriteHeader(http.StatusMethodNotAllowed)
95+
return
96+
}
97+
98+
reqBody, err := io.ReadAll(req.Body)
99+
expect.NoError(err)
100+
101+
matchedRequest := byoipPrefixUpdateRequestFalse
102+
responseJSON := byoipPrefixUpdateResponseFalse
103+
104+
expect.JSONEq(matchedRequest, string(reqBody))
105+
106+
w.Write([]byte(responseJSON))
107+
default:
108+
dump, err := httputil.DumpRequest(req, true)
109+
if err != nil {
110+
t.Fatal("failed to dump request")
111+
}
112+
113+
t.Fatalf("received unknown request: %s", dump)
114+
}
115+
}))
116+
defer server.Close()
117+
118+
cmd = exec.Command(builtBinaryPath,
119+
"-t", "some-magic-token",
120+
"-u", server.URL,
121+
"network",
122+
"byoip-prefix",
123+
"update",
124+
"78d564a7-bc3f-4489-be14-1fb714969213",
125+
"--advertise=false",
126+
)
127+
128+
output, err := cmd.CombinedOutput()
129+
expect.NoError(err, fmt.Sprintf("received error output: %s", output))
130+
expect.Equal(norm(byoipPrefixUpdateOutputFalse), norm(string(output)))
131+
})
132+
})
133+
})
134+
135+
const (
136+
byoipPrefixUpdateOutput = `
137+
Prefix Region Status UUID Advertised Failure Reason Validations
138+
10.1.1.1/24 nyc3 active 78d564a7-bc3f-4489-be14-1fb714969213 true []
139+
`
140+
byoipPrefixUpdateResponse = `
141+
{
142+
"byoip_prefix": {
143+
"uuid": "78d564a7-bc3f-4489-be14-1fb714969213",
144+
"region": "nyc3",
145+
"status": "active",
146+
"prefix": "10.1.1.1/24",
147+
"advertised": true,
148+
"validations": [],
149+
"failure_reason": ""
150+
}
151+
}
152+
`
153+
154+
byoipPrefixUpdateRequest = `
155+
{"advertise":true}
156+
`
157+
158+
byoipPrefixUpdateOutputFalse = `
159+
Prefix Region Status UUID Advertised Failure Reason Validations
160+
10.1.1.1/24 nyc3 active 78d564a7-bc3f-4489-be14-1fb714969213 false []
161+
`
162+
byoipPrefixUpdateResponseFalse = `
163+
{
164+
"byoip_prefix": {
165+
"uuid": "78d564a7-bc3f-4489-be14-1fb714969213",
166+
"region": "nyc3",
167+
"status": "active",
168+
"prefix": "10.1.1.1/24",
169+
"advertised": false,
170+
"validations": [],
171+
"failure_reason": ""
172+
}
173+
}
174+
`
175+
176+
byoipPrefixUpdateRequestFalse = `
177+
{"advertise":false}
178+
`
179+
)

0 commit comments

Comments
 (0)