Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resell V2 - add roles list method #112

Merged
merged 1 commit into from
Dec 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions selvpcclient/resell/v2/roles/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
Package roles provides the ability to retrieve and manage roles through the
Resell v2 API.

Example of getting roles in the current domain

allRoles, _, err = roles.List(context, resellClient)
if err != nil {
log.Fatal(err)
}
for _, myRole := range allRoles {
fmt.Println(myRole)
}

Example of getting roles in the specified project

allRoles, _, err := roles.ListProject(context, resellClient, projectID)
Expand Down
23 changes: 23 additions & 0 deletions selvpcclient/resell/v2/roles/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@ import (

const resourceURL = "roles"

// List returns all roles in the current domain.
func List(ctx context.Context, client *selvpcclient.ServiceClient) ([]*Role, *selvpcclient.ResponseResult, error) {
url := strings.Join([]string{client.Endpoint, resourceURL}, "/")
responseResult, err := client.DoRequest(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, nil, err
}
if responseResult.Err != nil {
return nil, responseResult, responseResult.Err
}

//Extract roles 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
}

// ListProject returns all roles in the specified project.
func ListProject(ctx context.Context, client *selvpcclient.ServiceClient, id string) ([]*Role, *selvpcclient.ResponseResult, error) {
url := strings.Join([]string{client.Endpoint, resourceURL, "projects", id}, "/")
Expand Down
20 changes: 20 additions & 0 deletions selvpcclient/resell/v2/roles/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@ package testing

import "github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/roles"

// TestListResponseRaw represents a raw response from List requests.
const TestListResponseRaw = `
{
"roles": [
{
"project_id": "49338ac045f448e294b25d013f890317",
"user_id": "b006a55e3a904472824061d64d61be75"
},
{
"project_id": "49338ac045f448e294b25d013f890317",
"user_id": "763eecfaeb0c8e9b76ab12a82eb4c11"
},
{
"project_id": "d7452adc9769422a908edfd2281d7c55",
"user_id": "763eecfaeb0c8e9b76ab12a82eb4c11"
}
]
}
`

// TestListProjectResponseRaw represents a raw response from ListProject requests.
const TestListProjectResponseRaw = `
{
Expand Down
140 changes: 140 additions & 0 deletions selvpcclient/resell/v2/roles/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,146 @@ import (
"github.com/selectel/go-selvpcclient/selvpcclient/testutils"
)

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

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/resell/v2/roles",
RawResponse: TestListResponseRaw,
Method: http.MethodGet,
Status: http.StatusOK,
CallFlag: &endpointCalled,
})

ctx := context.Background()
actual, _, err := roles.List(ctx, testEnv.Client)
if err != nil {
t.Fatal(err)
}
if !endpointCalled {
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) != 3 {
t.Errorf("expected 3 roles, but got %d", len(actual))
}
}

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

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/resell/v2/roles",
RawResponse: TestListResponseSingleRaw,
Method: http.MethodGet,
Status: http.StatusOK,
CallFlag: &endpointCalled,
})

ctx := context.Background()
actual, _, err := roles.List(ctx, testEnv.Client)
if err != nil {
t.Fatal(err)
}
expected := TestListResponseSingle
if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("expected %#v, but got %#v", expected, actual)
}
}

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

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/resell/v2/roles",
RawResponse: TestListResponseRaw,
Method: http.MethodGet,
Status: http.StatusBadGateway,
CallFlag: &endpointCalled,
})

ctx := context.Background()
allRoles, httpResponse, err := roles.List(ctx, testEnv.Client)

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

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

ctx := context.Background()
allRoles, _, err := roles.List(ctx, testEnv.Client)

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

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

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/resell/v2/roles",
RawResponse: TestManyRolesInvalidResponseRaw,
Method: http.MethodGet,
Status: http.StatusOK,
CallFlag: &endpointCalled,
})

ctx := context.Background()
allRoles, _, err := roles.List(ctx, testEnv.Client)

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

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

Expand Down