Skip to content

Commit 89cbe46

Browse files
authored
Add Delete Network call to NMAgent client (#2050)
* Add DeleteNetwork call to NMAgent client * Clean up comments and unused struct tags --------- Co-authored-by: Diego Becerra <[email protected]>
1 parent c1a1408 commit 89cbe46

File tree

3 files changed

+121
-2
lines changed

3 files changed

+121
-2
lines changed

nmagent/client.go

+19
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,25 @@ func (c *Client) JoinNetwork(ctx context.Context, jnr JoinNetworkRequest) error
7777
return err // nolint:wrapcheck // wrapping this just introduces noise
7878
}
7979

80+
// DeleteNetwork deletes a customer network and it's associated subnets.
81+
func (c *Client) DeleteNetwork(ctx context.Context, dnr DeleteNetworkRequest) error {
82+
req, err := c.buildRequest(ctx, dnr)
83+
if err != nil {
84+
return errors.Wrap(err, "building request")
85+
}
86+
87+
resp, err := c.httpClient.Do(req) // nolint:govet // the shadow is intentional
88+
if err != nil {
89+
return errors.Wrap(err, "submitting request")
90+
}
91+
defer resp.Body.Close()
92+
93+
if resp.StatusCode != http.StatusOK {
94+
return die(resp.StatusCode, resp.Header, resp.Body)
95+
}
96+
return nil
97+
}
98+
8099
// GetNetworkConfiguration retrieves the configuration of a customer's virtual
81100
// network. Only subnets which have been delegated will be returned.
82101
func (c *Client) GetNetworkConfiguration(ctx context.Context, gncr GetNetworkConfigRequest) (VirtualNetwork, error) {

nmagent/client_test.go

+62-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ func TestNMAgentClientJoinNetwork(t *testing.T) {
7878
defer cancel()
7979

8080
// attempt to join network
81-
// TODO(timraymond): need a more realistic network ID, I think
8281
err := client.JoinNetwork(ctx, nmagent.JoinNetworkRequest{test.id})
8382
checkErr(t, err, test.shouldErr)
8483

@@ -123,6 +122,68 @@ func TestNMAgentClientJoinNetworkRetry(t *testing.T) {
123122
}
124123
}
125124

125+
func TestNMAgentClientDeleteNetwork(t *testing.T) {
126+
deleteNetTests := []struct {
127+
name string
128+
id string
129+
exp string
130+
respStatus int
131+
shouldErr bool
132+
}{
133+
{
134+
"happy path",
135+
"00000000-0000-0000-0000-000000000000",
136+
"/machine/plugins?comp=nmagent&type=NetworkManagement%2FjoinedVirtualNetworks%2F00000000-0000-0000-0000-000000000000%2Fapi-version%2F1%2Fmethod%2FDELETE",
137+
http.StatusOK,
138+
false,
139+
},
140+
{
141+
"empty network ID",
142+
"",
143+
"",
144+
http.StatusOK, // this shouldn't be checked
145+
true,
146+
},
147+
{
148+
"internal error",
149+
"00000000-0000-0000-0000-000000000000",
150+
"/machine/plugins?comp=nmagent&type=NetworkManagement%2FjoinedVirtualNetworks%2F00000000-0000-0000-0000-000000000000%2Fapi-version%2F1%2Fmethod%2FDELETE",
151+
http.StatusInternalServerError,
152+
true,
153+
},
154+
}
155+
156+
for _, test := range deleteNetTests {
157+
test := test
158+
t.Run(test.name, func(t *testing.T) {
159+
t.Parallel()
160+
161+
// create a client
162+
var got string
163+
client := nmagent.NewTestClient(&TestTripper{
164+
RoundTripF: func(req *http.Request) (*http.Response, error) {
165+
got = req.URL.RequestURI()
166+
rr := httptest.NewRecorder()
167+
_, _ = fmt.Fprintf(rr, `{"httpStatusCode":"%d"}`, test.respStatus)
168+
rr.WriteHeader(http.StatusOK)
169+
return rr.Result(), nil
170+
},
171+
})
172+
173+
ctx, cancel := testContext(t)
174+
defer cancel()
175+
176+
// attempt to delete network
177+
err := client.DeleteNetwork(ctx, nmagent.DeleteNetworkRequest{test.id})
178+
checkErr(t, err, test.shouldErr)
179+
180+
if got != test.exp {
181+
t.Error("received URL differs from expectation: got", got, "exp:", test.exp)
182+
}
183+
})
184+
}
185+
}
186+
126187
func TestWSError(t *testing.T) {
127188
const wsError string = `
128189
<?xml version="1.0" encoding="utf-8"?>

nmagent/requests.go

+40-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func (p *Policy) UnmarshalJSON(in []byte) error {
239239
var _ Request = JoinNetworkRequest{}
240240

241241
type JoinNetworkRequest struct {
242-
NetworkID string `validate:"presence" json:"-"` // the customer's VNet ID
242+
NetworkID string `json:"-"` // the customer's VNet ID
243243
}
244244

245245
// Path constructs a URL path for invoking a JoinNetworkRequest using the
@@ -273,6 +273,45 @@ func (j JoinNetworkRequest) Validate() error {
273273
return err
274274
}
275275

276+
var _ Request = DeleteNetworkRequest{}
277+
278+
// DeleteNetworkRequest represents all information necessary to request that
279+
// NMAgent delete a particular network
280+
type DeleteNetworkRequest struct {
281+
NetworkID string `json:"-"` // the customer's VNet ID
282+
}
283+
284+
// Path constructs a URL path for invoking a DeleteNetworkRequest using the
285+
// provided parameters
286+
func (d DeleteNetworkRequest) Path() string {
287+
const DeleteNetworkPath = "/NetworkManagement/joinedVirtualNetworks/%s/api-version/1/method/DELETE"
288+
return fmt.Sprintf(DeleteNetworkPath, d.NetworkID)
289+
}
290+
291+
// Body returns nothing, because DeleteNetworkRequest has no request body
292+
func (d DeleteNetworkRequest) Body() (io.Reader, error) {
293+
return nil, nil
294+
}
295+
296+
// Method returns the HTTP request method to submit a DeleteNetworkRequest
297+
func (d DeleteNetworkRequest) Method() string {
298+
return http.MethodPost
299+
}
300+
301+
// Validate ensures that the provided parameters of the request are valid
302+
func (d DeleteNetworkRequest) Validate() error {
303+
err := internal.ValidationError{}
304+
305+
if d.NetworkID == "" {
306+
err.MissingFields = append(err.MissingFields, "NetworkID")
307+
}
308+
309+
if err.IsEmpty() {
310+
return nil
311+
}
312+
return err
313+
}
314+
276315
var _ Request = DeleteContainerRequest{}
277316

278317
// DeleteContainerRequest represents all information necessary to request that

0 commit comments

Comments
 (0)