Skip to content

Commit

Permalink
Merge 68a69d1 into e264b6b
Browse files Browse the repository at this point in the history
  • Loading branch information
ozerovandrei authored Apr 20, 2018
2 parents e264b6b + 68a69d1 commit e6a0e98
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 16 deletions.
19 changes: 19 additions & 0 deletions selvpcclient/resell/v2/roles/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,24 @@ Example of creating a single role
log.Fatal(err)
}
fmt.Println(myRole)
Example of creating several roles
createOpts := roles.RoleOpts{
Roles: []roles.RoleOpt{
{
ProjectID: "81800a8ec3fc49fca2cf00857de3ae9d",
UserID: "763eecfaeb0c8e9b76ab12a82eb4c11",
},
{
ProjectID: "d7452adc9769422a908edfd2281d7c55",
UserID: "763eecfaeb0c8e9b76ab12a82eb4c11",
},
},
}
allRoles, httpResponse, err := roles.CreateBulk(ctx, testEnv.Client, createOpts)
for _, myRole := range allRoles {
fmt.Println(myRole)
}
*/
package roles
31 changes: 31 additions & 0 deletions selvpcclient/resell/v2/roles/requests.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package roles

import (
"bytes"
"context"
"encoding/json"
"strings"

"github.com/selectel/go-selvpcclient/selvpcclient"
Expand Down Expand Up @@ -77,3 +79,32 @@ func Create(ctx context.Context, client *selvpcclient.ServiceClient, createOpts

return result.Role, responseResult, nil
}

// CreateBulk requests a creation of several roles.
func CreateBulk(ctx context.Context, client *selvpcclient.ServiceClient, createOpts RoleOpts) ([]*Role, *selvpcclient.ResponseResult, error) {
createRolesOpts := &createOpts
requestBody, err := json.Marshal(createRolesOpts)
if err != nil {
return nil, nil, err
}

url := strings.Join([]string{client.Endpoint, resourceURL}, "/")
responseResult, err := client.DoRequest(ctx, "POST", url, bytes.NewReader(requestBody))
if err != nil {
return nil, nil, err
}
if responseResult.Err != nil {
return nil, responseResult, responseResult.Err
}

// Extract role from the response body.
var result struct {
Roles []*Role `json:"roles"`
}
err = responseResult.ExtractResult(&result)
if err != nil {
return nil, responseResult, err
}

return result.Roles, responseResult, nil
}
5 changes: 5 additions & 0 deletions selvpcclient/resell/v2/roles/requests_opts.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package roles

// RoleOpts represents options for several Resell roles.
type RoleOpts struct {
Roles []RoleOpt `json:"roles"`
}

// RoleOpt represents options for a single Resell role.
type RoleOpt struct {
// ProjectID represents needed Resell project.
Expand Down
46 changes: 46 additions & 0 deletions selvpcclient/resell/v2/roles/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,52 @@ var TestCreateRoleResponse = &roles.Role{
UserID: "763eecfaeb0c8e9b76ab12a82eb4c11",
}

// TestCreateRolesOptsRaw represent raw options for the Create request.
const TestCreateRolesOptsRaw = `
{
"roles": [
{
"project_id": "81800a8ec3fc49fca2cf00857de3ae9d",
"user_id": "763eecfaeb0c8e9b76ab12a82eb4c11"
},
{
"project_id": "d7452adc9769422a908edfd2281d7c55",
"user_id": "763eecfaeb0c8e9b76ab12a82eb4c11"
}
]
}
`

// TestCreateRolesOpts represent options for the Create request.
var TestCreateRolesOpts = roles.RoleOpts{
Roles: []roles.RoleOpt{
{
ProjectID: "81800a8ec3fc49fca2cf00857de3ae9d",
UserID: "763eecfaeb0c8e9b76ab12a82eb4c11",
},
{
ProjectID: "d7452adc9769422a908edfd2281d7c55",
UserID: "763eecfaeb0c8e9b76ab12a82eb4c11",
},
},
}

// TestCreateRolesResponseRaw represents a raw response from the Create request.
const TestCreateRolesResponseRaw = `
{
"roles": [
{
"project_id": "81800a8ec3fc49fca2cf00857de3ae9d",
"user_id": "763eecfaeb0c8e9b76ab12a82eb4c11"
},
{
"project_id": "d7452adc9769422a908edfd2281d7c55",
"user_id": "763eecfaeb0c8e9b76ab12a82eb4c11"
}
]
}
`

// TestManyRolesInvalidResponseRaw represents a raw invalid response with several roles.
const TestManyRolesInvalidResponseRaw = `
{
Expand Down
124 changes: 108 additions & 16 deletions selvpcclient/resell/v2/roles/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,7 @@ func TestCreateRole(t *testing.T) {
TestCreateRoleResponseRaw, http.MethodPost, http.StatusOK, &endpointCalled, t)

ctx := context.Background()
createOpts := roles.RoleOpt{
ProjectID: "49338ac045f448e294b25d013f890317",
UserID: "763eecfaeb0c8e9b76ab12a82eb4c11",
}
createOpts := TestCreateRoleOpts
actual, _, err := roles.Create(ctx, testEnv.Client, createOpts)
if err != nil {
t.Fatal(err)
Expand All @@ -299,10 +296,7 @@ func TestCreateRoleHTTPError(t *testing.T) {
TestCreateRoleResponseRaw, http.MethodPost, http.StatusBadGateway, &endpointCalled, t)

ctx := context.Background()
createOpts := roles.RoleOpt{
ProjectID: "49338ac045f448e294b25d013f890317",
UserID: "763eecfaeb0c8e9b76ab12a82eb4c11",
}
createOpts := TestCreateRoleOpts
role, httpResponse, err := roles.Create(ctx, testEnv.Client, createOpts)

if !endpointCalled {
Expand All @@ -327,10 +321,7 @@ func TestCreateRoleTimeoutError(t *testing.T) {
testEnv.NewTestResellV2Client()

ctx := context.Background()
createOpts := roles.RoleOpt{
ProjectID: "49338ac045f448e294b25d013f890317",
UserID: "763eecfaeb0c8e9b76ab12a82eb4c11",
}
createOpts := TestCreateRoleOpts
role, _, err := roles.Create(ctx, testEnv.Client, createOpts)

if role != nil {
Expand All @@ -352,10 +343,7 @@ func TestCreateRoleUnmarshalError(t *testing.T) {
TestSingleRoleInvalidResponseRaw, http.MethodPost, http.StatusOK, &endpointCalled, t)

ctx := context.Background()
createOpts := roles.RoleOpt{
ProjectID: "49338ac045f448e294b25d013f890317",
UserID: "763eecfaeb0c8e9b76ab12a82eb4c11",
}
createOpts := TestCreateRoleOpts
role, _, err := roles.Create(ctx, testEnv.Client, createOpts)

if !endpointCalled {
Expand All @@ -368,3 +356,107 @@ func TestCreateRoleUnmarshalError(t *testing.T) {
t.Fatal("expected error from the Create method")
}
}

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

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/roles",
TestCreateRolesResponseRaw, TestCreateRolesOptsRaw, http.MethodPost, http.StatusOK,
&endpointCalled, t)

ctx := context.Background()
createOpts := TestCreateRolesOpts
actual, _, err := roles.CreateBulk(ctx, testEnv.Client, createOpts)
if err != nil {
t.Fatal(err)
}

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if actual == nil {
t.Fatal("didn't get roles")
}
actualKind := reflect.TypeOf(actual).Kind()
if actualKind != reflect.Slice {
t.Errorf("expected slice of pointers to roles, but got %v", actualKind)
}
if len(actual) != 2 {
t.Errorf("expected 2 roles, but got %d", len(actual))
}
}

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

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/roles",
TestCreateRolesResponseRaw, TestCreateRolesOptsRaw, http.MethodPost,
http.StatusBadGateway, &endpointCalled, t)

ctx := context.Background()
createOpts := TestCreateRolesOpts
allRoles, httpResponse, err := roles.CreateBulk(ctx, testEnv.Client, createOpts)

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

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

ctx := context.Background()
createOpts := TestCreateRolesOpts
allRoles, _, err := roles.CreateBulk(ctx, testEnv.Client, createOpts)

if allRoles != nil {
t.Fatal("expected no role from the CreateBulk method")
}
if err == nil {
t.Fatal("expected error from the CreateBulk method")
}
}

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

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/roles",
TestManyRolesInvalidResponseRaw, TestCreateRolesOptsRaw, http.MethodPost,
http.StatusOK, &endpointCalled, t)

ctx := context.Background()
createOpts := TestCreateRolesOpts
allRoles, _, err := roles.CreateBulk(ctx, testEnv.Client, createOpts)

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if allRoles != nil {
t.Fatal("expected no role from the CreateBulk method")
}
if err == nil {
t.Fatal("expected error from the CreateBulk method")
}
}

0 comments on commit e6a0e98

Please sign in to comment.