Skip to content

Commit

Permalink
Merge 8ca5d89 into 4b60f08
Browse files Browse the repository at this point in the history
  • Loading branch information
ozerovandrei committed Jan 3, 2019
2 parents 4b60f08 + 8ca5d89 commit a64cdbb
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ You can use this library to work with the following objects of the Selectel VPC
* [vrrp_subnets](https://godoc.org/github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/vrrpsubnets)
* [floating ips](https://godoc.org/github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/floatingips)
* [licenses](https://godoc.org/github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/licenses)
* [keypairs](https://godoc.org/github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/keypairs)
* [traffic](https://godoc.org/github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/traffic)
* [capabilities](https://godoc.org/github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/capabilities)

Expand Down
9 changes: 9 additions & 0 deletions selvpcclient/resell/v2/keypairs/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,14 @@ Example of creating keypairs in all regions with the same options
for _, newKeypair := range newKeypairs {
fmt.Printf("%v\n", newKeypair)
}
Example of deleting a single keypair of a user
keypairName := "my_keypair"
userID := 82a026cae2104e92b999dbe00cdb9435""
_, err = keypairs.Delete(ctx, resellClient, keypairName, userID)
if err != nil {
log.Fatal(err)
}
*/
package keypairs
13 changes: 13 additions & 0 deletions selvpcclient/resell/v2/keypairs/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,16 @@ func Create(ctx context.Context, client *selvpcclient.ServiceClient, createOpts

return result.Keypair, responseResult, nil
}

// Delete deletes a single keypair by its name and user ID.
func Delete(ctx context.Context, client *selvpcclient.ServiceClient, name, userID string) (*selvpcclient.ResponseResult, error) {
url := strings.Join([]string{client.Endpoint, resourceURL, name, "users", userID}, "/")
responseResult, err := client.DoRequest(ctx, http.MethodDelete, url, nil)
if err != nil {
return nil, err
}
if responseResult.Err != nil {
err = responseResult.Err
}
return responseResult, err
}
66 changes: 66 additions & 0 deletions selvpcclient/resell/v2/keypairs/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,69 @@ func TestCreateKeypairsUnmarshalError(t *testing.T) {
t.Fatal("expected error from the Create method")
}
}

func TestDeleteKeypair(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/resell/v2/keypairs/key1/users/82a026cae2104e92b999dbe00cdb9435",
Method: http.MethodDelete,
Status: http.StatusOK,
CallFlag: &endpointCalled,
})

ctx := context.Background()
_, err := keypairs.Delete(ctx, testEnv.Client, "key1", "82a026cae2104e92b999dbe00cdb9435")
if err != nil {
t.Fatal(err)
}
if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
}

func TestDeleteKeypairHTTPError(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/resell/v2/keypairs/key1/users/82a026cae2104e92b999dbe00cdb9435",
Method: http.MethodDelete,
Status: http.StatusBadGateway,
CallFlag: &endpointCalled,
})

ctx := context.Background()
httpResponse, err := keypairs.Delete(ctx, testEnv.Client, "key1", "82a026cae2104e92b999dbe00cdb9435")

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if err == nil {
t.Fatal("expected error from the Delete method")
}
if httpResponse.StatusCode != http.StatusBadGateway {
t.Fatalf("expected %d status in the HTTP response, but got %d", http.StatusBadRequest, httpResponse.StatusCode)
}
}

func TestDeleteKeypairTimeoutError(t *testing.T) {
testEnv := testutils.SetupTestEnv()
testEnv.Server.Close()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()

ctx := context.Background()
_, err := keypairs.Delete(ctx, testEnv.Client, "key1", "82a026cae2104e92b999dbe00cdb9435")

if err == nil {
t.Fatal("expected error from the Delete method")
}
}

0 comments on commit a64cdbb

Please sign in to comment.