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

Refactor response structs #7

Merged
merged 1 commit into from
Jun 7, 2024
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
3 changes: 2 additions & 1 deletion examples/group-with-user/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,13 @@ func main() {
}
fmt.Printf("Step 3: Assigned Role %s with scope %s to Group ID: %s\n", roles.Member, roles.Account, group.ID)

group, err = groupsAPI.Update(ctx, group.ID, groups.ModifyRequest{Name: updatedGroupName,
updatedGroup, err := groupsAPI.Update(ctx, group.ID, groups.UpdateRequest{Name: updatedGroupName,
Description: &updatedDescription})
if err != nil {
fmt.Println(err)
return
}
group.Group = updatedGroup.Group
fmt.Printf("Step 4: Group Name and Description updated to: %s and %s\n", group.Name, group.Description)

if deleteAfterRun {
Expand Down
4 changes: 2 additions & 2 deletions examples/transfer-role/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func main() {
return
}

var chosenUser *users.UserListResponse
for _, user := range allUsers {
var chosenUser *users.User
for _, user := range allUsers.Users {
for _, role := range user.Roles {
if role.RoleName == roles.Billing && user.ID != "account_root" {
chosenUser = &user
Expand Down
8 changes: 4 additions & 4 deletions iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ type Client struct {
baseClient *baseclient.BaseClient

// Users instance is used to make requests against Selectel IAM API and manage Panel Users.
Users *users.Users
Users *users.Service

// ServiceUsers instance is used to make requests against Selectel IAM API and manage Service Users.
ServiceUsers *serviceusers.ServiceUsers
ServiceUsers *serviceusers.Service

// Groups instance is used to make requests against Selectel IAM API and manage Groups of users.
Groups *groups.Groups
Groups *groups.Service

// S3Credentials instance is used to make requests against Selectel IAM API and manage S3 Credentials.
S3Credentials *s3credentials.S3Credentials
S3Credentials *s3credentials.Service
}

type AuthOpts struct {
Expand Down
6 changes: 3 additions & 3 deletions internal/client/testdata/fixtures.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package testdata

const (
TestToken = "test-token"
TestURL = "http://example.org/"
TestToken = "test-token"
TestURL = "http://example.org/"
TestUserAgent = "iam-go/v0.0.1"
)

Expand All @@ -14,4 +14,4 @@ const TestDoRequestRaw = `{
const TestDoRequestErr = `{
"code": "REQUEST_FORBIDDEN",
"message": "You don't have permission to do this"
}`
}`
64 changes: 32 additions & 32 deletions service/groups/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@ import (

const apiVersion = "iam/v1"

// Groups is used to communicate with the Groups API.
type Groups struct {
// Service is used to communicate with the Groups API.
type Service struct {
baseClient *client.BaseClient
}

// New Initialises Groups with the given client.
func New(baseClient *client.BaseClient) *Groups {
return &Groups{
// New Initialises Service with the given client.
func New(baseClient *client.BaseClient) *Service {
return &Service{
baseClient: baseClient,
}
}

// List returns a list of Groups for the account.
func (u *Groups) List(ctx context.Context) ([]GroupListResponse, error) {
func (s *Service) List(ctx context.Context) (*ListResponse, error) {
path, err := url.JoinPath(apiVersion, "groups")
if err != nil {
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
}

response, err := u.baseClient.DoRequest(ctx, client.DoRequestInput{
response, err := s.baseClient.DoRequest(ctx, client.DoRequestInput{
Body: nil,
Method: http.MethodGet,
Path: path,
Expand All @@ -43,16 +43,16 @@ func (u *Groups) List(ctx context.Context) ([]GroupListResponse, error) {
return nil, err
}

var groups listResponse
var groups ListResponse
err = client.UnmarshalJSON(response, &groups)
if err != nil {
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
}
return groups.Groups, nil
return &groups, nil
}

// Get returns an info of Group with groupID.
func (u *Groups) Get(ctx context.Context, groupID string) (*Group, error) {
func (s *Service) Get(ctx context.Context, groupID string) (*GetResponse, error) {
if groupID == "" {
return nil, iamerrors.Error{Err: iamerrors.ErrGroupIDRequired, Desc: "No groupID was provided."}
}
Expand All @@ -62,7 +62,7 @@ func (u *Groups) Get(ctx context.Context, groupID string) (*Group, error) {
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
}

response, err := u.baseClient.DoRequest(ctx, client.DoRequestInput{
response, err := s.baseClient.DoRequest(ctx, client.DoRequestInput{
Body: nil,
Method: http.MethodGet,
Path: path,
Expand All @@ -72,7 +72,7 @@ func (u *Groups) Get(ctx context.Context, groupID string) (*Group, error) {
return nil, err
}

var group Group
var group GetResponse
err = client.UnmarshalJSON(response, &group)
if err != nil {
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
Expand All @@ -81,7 +81,7 @@ func (u *Groups) Get(ctx context.Context, groupID string) (*Group, error) {
}

// Create creates a new Group.
func (u *Groups) Create(ctx context.Context, input CreateRequest) (*Group, error) {
func (s *Service) Create(ctx context.Context, input CreateRequest) (*CreateResponse, error) {
if input.Name == "" {
return nil, iamerrors.Error{Err: iamerrors.ErrGroupNameRequired, Desc: "No Name for Group was provided."}
}
Expand All @@ -96,7 +96,7 @@ func (u *Groups) Create(ctx context.Context, input CreateRequest) (*Group, error
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
}

response, err := u.baseClient.DoRequest(ctx, client.DoRequestInput{
response, err := s.baseClient.DoRequest(ctx, client.DoRequestInput{
Body: bytes.NewReader(body),
Method: http.MethodPost,
Path: path,
Expand All @@ -106,7 +106,7 @@ func (u *Groups) Create(ctx context.Context, input CreateRequest) (*Group, error
return nil, err
}

var group Group
var group CreateResponse
err = client.UnmarshalJSON(response, &group)
if err != nil {
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
Expand All @@ -115,7 +115,7 @@ func (u *Groups) Create(ctx context.Context, input CreateRequest) (*Group, error
}

// Update updates exists Group.
func (u *Groups) Update(ctx context.Context, groupID string, input ModifyRequest) (*Group, error) {
func (s *Service) Update(ctx context.Context, groupID string, input UpdateRequest) (*UpdateResponse, error) {
if groupID == "" {
return nil, iamerrors.Error{Err: iamerrors.ErrGroupIDRequired, Desc: "No groupID was provided."}
}
Expand All @@ -130,7 +130,7 @@ func (u *Groups) Update(ctx context.Context, groupID string, input ModifyRequest
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
}

response, err := u.baseClient.DoRequest(ctx, client.DoRequestInput{
response, err := s.baseClient.DoRequest(ctx, client.DoRequestInput{
Body: bytes.NewReader(body),
Method: http.MethodPatch,
Path: path,
Expand All @@ -140,7 +140,7 @@ func (u *Groups) Update(ctx context.Context, groupID string, input ModifyRequest
return nil, err
}

var group Group
var group UpdateResponse
err = client.UnmarshalJSON(response, &group)
if err != nil {
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
Expand All @@ -149,7 +149,7 @@ func (u *Groups) Update(ctx context.Context, groupID string, input ModifyRequest
}

// Delete deletes a Group from the account.
func (u *Groups) Delete(ctx context.Context, groupID string) error {
func (s *Service) Delete(ctx context.Context, groupID string) error {
if groupID == "" {
return iamerrors.Error{Err: iamerrors.ErrGroupIDRequired, Desc: "No groupID was provided."}
}
Expand All @@ -159,7 +159,7 @@ func (u *Groups) Delete(ctx context.Context, groupID string) error {
return iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
}

_, err = u.baseClient.DoRequest(ctx, client.DoRequestInput{
_, err = s.baseClient.DoRequest(ctx, client.DoRequestInput{
Body: nil,
Method: http.MethodDelete,
Path: path,
Expand All @@ -173,7 +173,7 @@ func (u *Groups) Delete(ctx context.Context, groupID string) error {
}

// AssignRoles adds new roles for a Group with the given groupID.
func (u *Groups) AssignRoles(ctx context.Context, groupID string, roles []roles.Role) error {
func (s *Service) AssignRoles(ctx context.Context, groupID string, roles []roles.Role) error {
if groupID == "" {
return iamerrors.Error{Err: iamerrors.ErrGroupIDRequired, Desc: "No groupID was provided."}
}
Expand All @@ -182,22 +182,22 @@ func (u *Groups) AssignRoles(ctx context.Context, groupID string, roles []roles.
return iamerrors.Error{Err: iamerrors.ErrGroupRolesRequired, Desc: "No roles for Group was provided."}
}

return u.manageRoles(ctx, http.MethodPut, groupID, roles)
return s.manageRoles(ctx, http.MethodPut, groupID, roles)
}

// UnassignRoles removes roles from a Group with the given groupID.
func (u *Groups) UnassignRoles(ctx context.Context, groupID string, roles []roles.Role) error {
func (s *Service) UnassignRoles(ctx context.Context, groupID string, roles []roles.Role) error {
if groupID == "" {
return iamerrors.Error{Err: iamerrors.ErrGroupIDRequired, Desc: "No groupID was provided."}
}
if len(roles) == 0 {
return iamerrors.Error{Err: iamerrors.ErrGroupRolesRequired, Desc: "No roles for Group was provided."}
}

return u.manageRoles(ctx, http.MethodDelete, groupID, roles)
return s.manageRoles(ctx, http.MethodDelete, groupID, roles)
}

func (u *Groups) manageRoles(ctx context.Context, method string, groupID string, roles []roles.Role) error {
func (s *Service) manageRoles(ctx context.Context, method string, groupID string, roles []roles.Role) error {
path, err := url.JoinPath(apiVersion, "groups", groupID, "roles")
if err != nil {
return iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
Expand All @@ -209,7 +209,7 @@ func (u *Groups) manageRoles(ctx context.Context, method string, groupID string,
return iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
}

_, err = u.baseClient.DoRequest(ctx, client.DoRequestInput{
_, err = s.baseClient.DoRequest(ctx, client.DoRequestInput{
Body: bytes.NewReader(body),
Method: method,
Path: path,
Expand All @@ -223,7 +223,7 @@ func (u *Groups) manageRoles(ctx context.Context, method string, groupID string,
}

// AddUsers adds new users to a Group with the given groupID.
func (u *Groups) AddUsers(ctx context.Context, groupID string, usersKeystoneIDs []string) error {
func (s *Service) AddUsers(ctx context.Context, groupID string, usersKeystoneIDs []string) error {
if groupID == "" {
return iamerrors.Error{Err: iamerrors.ErrGroupIDRequired, Desc: "No groupID was provided."}
}
Expand All @@ -232,22 +232,22 @@ func (u *Groups) AddUsers(ctx context.Context, groupID string, usersKeystoneIDs
return iamerrors.Error{Err: iamerrors.ErrGroupUserIDsRequired, Desc: "No users for Group was provided."}
}

return u.manageUsers(ctx, http.MethodPut, groupID, usersKeystoneIDs)
return s.manageUsers(ctx, http.MethodPut, groupID, usersKeystoneIDs)
}

// DeleteUsers removes users from a Group with the given groupID.
func (u *Groups) DeleteUsers(ctx context.Context, groupID string, usersKeystoneIDs []string) error {
func (s *Service) DeleteUsers(ctx context.Context, groupID string, usersKeystoneIDs []string) error {
if groupID == "" {
return iamerrors.Error{Err: iamerrors.ErrGroupIDRequired, Desc: "No groupID was provided."}
}
if len(usersKeystoneIDs) == 0 {
return iamerrors.Error{Err: iamerrors.ErrGroupUserIDsRequired, Desc: "No users for Group was provided."}
}

return u.manageUsers(ctx, http.MethodDelete, groupID, usersKeystoneIDs)
return s.manageUsers(ctx, http.MethodDelete, groupID, usersKeystoneIDs)
}

func (u *Groups) manageUsers(ctx context.Context, method string, groupID string, usersKeystoneIDs []string) error {
func (s *Service) manageUsers(ctx context.Context, method string, groupID string, usersKeystoneIDs []string) error {
path, err := url.JoinPath(apiVersion, "groups", groupID, "users")
if err != nil {
return iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
Expand All @@ -259,7 +259,7 @@ func (u *Groups) manageUsers(ctx context.Context, method string, groupID string,
return iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
}

_, err = u.baseClient.DoRequest(ctx, client.DoRequestInput{
_, err = s.baseClient.DoRequest(ctx, client.DoRequestInput{
Body: bytes.NewReader(body),
Method: method,
Path: path,
Expand Down
Loading
Loading