Skip to content

Commit

Permalink
Merge pull request #1496 from k8s-infra-cherrypick-robot/cherry-pick-…
Browse files Browse the repository at this point in the history
…1491-to-release-1.23

[release-1.23] Add DeletePEConnection to plsClient
  • Loading branch information
k8s-ci-robot committed Apr 15, 2022
2 parents bfece08 + 526e29c commit 6605fd5
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ import (
var _ Interface = &Client{}

const (
PLSResourceType = "Microsoft.Network/privatelinkservices"
PLSResourceType = "Microsoft.Network/privatelinkservices"
PEConnResourceType = "privateEndpointConnections"
)

// Client implements privatelinkservice Interface.
Expand Down Expand Up @@ -331,6 +332,50 @@ func (c *Client) deletePLS(ctx context.Context, resourceGroupName string, privat
return c.armClient.DeleteResource(ctx, resourceID, "")
}

func (c *Client) DeletePEConnection(ctx context.Context, resourceGroupName string, privateLinkServiceName string, privateEndpointConnectionName string) *retry.Error {
mc := metrics.NewMetricContext("private_endpoint_connection", "delete", resourceGroupName, c.subscriptionID, "")

// Report errors if the client is rate limited.
if !c.rateLimiterWriter.TryAccept() {
mc.RateLimitedCount()
return retry.GetRateLimitError(true, "PEConnDelete")
}

// Report errors if the client is throttled.
if c.RetryAfterWriter.After(time.Now()) {
mc.ThrottledCount()
rerr := retry.GetThrottlingError("PEConnDelete", "client throttled", c.RetryAfterWriter)
return rerr
}

rerr := c.deletePEConn(ctx, resourceGroupName, privateLinkServiceName, privateEndpointConnectionName)
mc.Observe(rerr)
if rerr != nil {
if rerr.IsThrottled() {
// Update RetryAfterReader so that no more requests would be sent until RetryAfter expires.
c.RetryAfterWriter = rerr.RetryAfter
}

return rerr
}

return nil
}

// deletePLS deletes a private endpoint connection by name.
func (c *Client) deletePEConn(ctx context.Context, resourceGroupName string, privateLinkServiceName string, privateEndpointConnectionName string) *retry.Error {
resourceID := armclient.GetChildResourceID(
c.subscriptionID,
resourceGroupName,
PLSResourceType,
privateLinkServiceName,
PEConnResourceType,
privateEndpointConnectionName,
)

return c.armClient.DeleteResource(ctx, resourceID, "")
}

func (c *Client) listResponder(resp *http.Response) (result network.PrivateLinkServiceListResult, err error) {
err = autorest.Respond(
resp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,52 @@ func TestDelete(t *testing.T) {
}
}

func TestDeletePEConnection(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

tests := []struct {
description string
armClientErr *retry.Error
expectedErr *retry.Error
}{
{
description: "Delete should report the throttling error",
armClientErr: &retry.Error{HTTPStatusCode: http.StatusTooManyRequests},
expectedErr: &retry.Error{HTTPStatusCode: http.StatusTooManyRequests},
},
{
description: "Delete should not report any error if there's no error from arm client",
},
}

peConn := getTestPrivateEndpointConnection("pls1", "peconn")

for _, test := range tests {
armClient := mockarmclient.NewMockInterface(ctrl)
armClient.EXPECT().DeleteResource(gomock.Any(), to.String(peConn.ID), "").Return(test.armClientErr)

plsClient := getTestPrivateLinkServiceClient(armClient)
rerr := plsClient.DeletePEConnection(context.TODO(), "rg", "pls1", "peconn")
assert.Equal(t, test.expectedErr, rerr)
}
}

func getTestPrivateLinkService(name string) network.PrivateLinkService {
return network.PrivateLinkService{
ID: to.StringPtr(fmt.Sprintf("/subscriptions/subscriptionID/resourceGroups/rg/providers/"+PLSResourceType+"/%s", name)),
ID: to.StringPtr(fmt.Sprintf("/subscriptions/subscriptionID/resourceGroups/rg/providers/%s/%s", PLSResourceType, name)),
Name: to.StringPtr(name),
Location: to.StringPtr("eastus"),
}
}

func getTestPrivateEndpointConnection(PLSName string, PEConnName string) network.PrivateEndpointConnection {
return network.PrivateEndpointConnection{
ID: to.StringPtr(fmt.Sprintf("/subscriptions/subscriptionID/resourceGroups/rg/providers/%s/%s/%s/%s", PLSResourceType, PLSName, PEConnResourceType, PEConnName)),
Name: to.StringPtr(PEConnName),
}
}

func getTestPrivateLinkServiceClient(armClient armclient.Interface) *Client {
rateLimiterReader, rateLimiterWriter := azclients.NewRateLimiter(&azclients.RateLimitConfig{})
return &Client{
Expand Down
3 changes: 3 additions & 0 deletions pkg/azureclients/privatelinkserviceclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,7 @@ type Interface interface {

// Delete deletes a private link service by name.
Delete(ctx context.Context, resourceGroupName string, privateLinkServiceName string) *retry.Error

// Delete deletes a private endpoint connection to the private link service by name
DeletePEConnection(ctx context.Context, resourceGroupName string, privateLinkServiceName string, privateEndpointConnectionName string) *retry.Error
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6605fd5

Please sign in to comment.