Skip to content

Commit cd9d6a0

Browse files
author
saydamir
authored
Refactor response structs (#7)
- Consistent naming - Separate structs for each type of request - Common basic structs
1 parent 030042b commit cd9d6a0

File tree

16 files changed

+264
-239
lines changed

16 files changed

+264
-239
lines changed

examples/group-with-user/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,13 @@ func main() {
6767
}
6868
fmt.Printf("Step 3: Assigned Role %s with scope %s to Group ID: %s\n", roles.Member, roles.Account, group.ID)
6969

70-
group, err = groupsAPI.Update(ctx, group.ID, groups.ModifyRequest{Name: updatedGroupName,
70+
updatedGroup, err := groupsAPI.Update(ctx, group.ID, groups.UpdateRequest{Name: updatedGroupName,
7171
Description: &updatedDescription})
7272
if err != nil {
7373
fmt.Println(err)
7474
return
7575
}
76+
group.Group = updatedGroup.Group
7677
fmt.Printf("Step 4: Group Name and Description updated to: %s and %s\n", group.Name, group.Description)
7778

7879
if deleteAfterRun {

examples/transfer-role/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ func main() {
4343
return
4444
}
4545

46-
var chosenUser *users.UserListResponse
47-
for _, user := range allUsers {
46+
var chosenUser *users.User
47+
for _, user := range allUsers.Users {
4848
for _, role := range user.Roles {
4949
if role.RoleName == roles.Billing && user.ID != "account_root" {
5050
chosenUser = &user

iam.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ type Client struct {
4747
baseClient *baseclient.BaseClient
4848

4949
// Users instance is used to make requests against Selectel IAM API and manage Panel Users.
50-
Users *users.Users
50+
Users *users.Service
5151

5252
// ServiceUsers instance is used to make requests against Selectel IAM API and manage Service Users.
53-
ServiceUsers *serviceusers.ServiceUsers
53+
ServiceUsers *serviceusers.Service
5454

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

5858
// S3Credentials instance is used to make requests against Selectel IAM API and manage S3 Credentials.
59-
S3Credentials *s3credentials.S3Credentials
59+
S3Credentials *s3credentials.Service
6060
}
6161

6262
type AuthOpts struct {

internal/client/testdata/fixtures.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package testdata
22

33
const (
4-
TestToken = "test-token"
5-
TestURL = "http://example.org/"
4+
TestToken = "test-token"
5+
TestURL = "http://example.org/"
66
TestUserAgent = "iam-go/v0.0.1"
77
)
88

@@ -14,4 +14,4 @@ const TestDoRequestRaw = `{
1414
const TestDoRequestErr = `{
1515
"code": "REQUEST_FORBIDDEN",
1616
"message": "You don't have permission to do this"
17-
}`
17+
}`

service/groups/requests.go

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,26 @@ import (
1414

1515
const apiVersion = "iam/v1"
1616

17-
// Groups is used to communicate with the Groups API.
18-
type Groups struct {
17+
// Service is used to communicate with the Groups API.
18+
type Service struct {
1919
baseClient *client.BaseClient
2020
}
2121

22-
// New Initialises Groups with the given client.
23-
func New(baseClient *client.BaseClient) *Groups {
24-
return &Groups{
22+
// New Initialises Service with the given client.
23+
func New(baseClient *client.BaseClient) *Service {
24+
return &Service{
2525
baseClient: baseClient,
2626
}
2727
}
2828

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

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

46-
var groups listResponse
46+
var groups ListResponse
4747
err = client.UnmarshalJSON(response, &groups)
4848
if err != nil {
4949
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
5050
}
51-
return groups.Groups, nil
51+
return &groups, nil
5252
}
5353

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

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

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

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

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

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

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

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

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

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

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

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

185-
return u.manageRoles(ctx, http.MethodPut, groupID, roles)
185+
return s.manageRoles(ctx, http.MethodPut, groupID, roles)
186186
}
187187

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

197-
return u.manageRoles(ctx, http.MethodDelete, groupID, roles)
197+
return s.manageRoles(ctx, http.MethodDelete, groupID, roles)
198198
}
199199

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

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

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

235-
return u.manageUsers(ctx, http.MethodPut, groupID, usersKeystoneIDs)
235+
return s.manageUsers(ctx, http.MethodPut, groupID, usersKeystoneIDs)
236236
}
237237

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

247-
return u.manageUsers(ctx, http.MethodDelete, groupID, usersKeystoneIDs)
247+
return s.manageUsers(ctx, http.MethodDelete, groupID, usersKeystoneIDs)
248248
}
249249

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

262-
_, err = u.baseClient.DoRequest(ctx, client.DoRequestInput{
262+
_, err = s.baseClient.DoRequest(ctx, client.DoRequestInput{
263263
Body: bytes.NewReader(body),
264264
Method: method,
265265
Path: path,

0 commit comments

Comments
 (0)