Skip to content

Commit dcc40ec

Browse files
alexbondar92assaf758
authored andcommitted
addressing Felix's comments
1 parent b71cb7a commit dcc40ec

File tree

3 files changed

+70
-53
lines changed

3 files changed

+70
-53
lines changed

pkg/controlplane/http/session.go

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -226,53 +226,22 @@ func (s *session) DeleteAccessKeySync(deleteAccessKeyInput *v3ioc.DeleteAccessKe
226226
&deleteAccessKeyInput.ControlPlaneInput)
227227
}
228228

229-
// DeleteAccessKeySync deletes an access key (blocking)
230-
func (s *session) GetUserNameSync(getUserNameInput *v3ioc.GetUserNameInput) (*v3ioc.GetUserNameOutput, error) {
229+
// GetRunningUserAttributesSync returns user's attributes related to session's access key (blocking)
230+
func (s *session) GetRunningUserAttributesSync(getRunningUserAttributesInput *v3ioc.GetRunningUserAttributesInput) (*v3ioc.GetRunningUserAttributesOutput, error) {
231231

232232
// prepare session response resource
233-
userNameOutput := v3ioc.GetUserNameOutput{}
234-
235-
// allocate request
236-
httpRequest := fasthttp.AcquireRequest()
237-
defer fasthttp.ReleaseRequest(httpRequest)
233+
userNameOutput := v3ioc.GetRunningUserAttributesOutput{}
238234

239-
responseInstance, err := s.sendRequest(getUserNameInput.ControlPlaneInput.Ctx,
240-
&request{
241-
method: http.MethodGet,
242-
path: fmt.Sprintf("api/%s", "self"),
243-
httpRequest: httpRequest,
244-
}, getUserNameInput.ControlPlaneInput.Timeout)
235+
err := s.getResource(getRunningUserAttributesInput.Ctx,
236+
"self",
237+
&getRunningUserAttributesInput.ControlPlaneInput,
238+
&userNameOutput.ControlPlaneOutput,
239+
&userNameOutput.UserAttributes)
245240

246241
if err != nil {
247242
return nil, err
248243
}
249244

250-
// if we got cookies, set them
251-
if len(responseInstance.cookies) > 0 {
252-
s.cookies = responseInstance.cookies
253-
}
254-
255-
var objmap map[string]json.RawMessage
256-
err = json.Unmarshal(responseInstance.body, &objmap)
257-
if err != nil {
258-
return nil, err
259-
}
260-
261-
err = json.Unmarshal(objmap["data"], &objmap)
262-
if err != nil {
263-
return nil, err
264-
}
265-
266-
err = json.Unmarshal(objmap["attributes"], &objmap)
267-
if err != nil {
268-
return nil, err
269-
}
270-
271-
err = json.Unmarshal(objmap["username"], &userNameOutput.Username)
272-
if err != nil {
273-
return nil, err
274-
}
275-
276245
return &userNameOutput, err
277246
}
278247

@@ -386,6 +355,52 @@ func (s *session) createOrUpdateResource(ctx context.Context,
386355
return nil
387356
}
388357

358+
func (s *session) getResource(ctx context.Context,
359+
path string,
360+
controlPlaneInput *v3ioc.ControlPlaneInput,
361+
controlPlaneOutput *v3ioc.ControlPlaneOutput,
362+
responseAttributes interface{}) error {
363+
364+
// allocate request
365+
httpRequest := fasthttp.AcquireRequest()
366+
defer fasthttp.ReleaseRequest(httpRequest)
367+
368+
responseInstance, err := s.sendRequest(ctx,
369+
&request{
370+
method: http.MethodGet,
371+
path: "api/" + path,
372+
httpRequest: httpRequest,
373+
}, controlPlaneInput.Timeout)
374+
375+
if err != nil {
376+
return err
377+
}
378+
379+
// unmarshal
380+
if responseInstance.body != nil && controlPlaneOutput != nil {
381+
responseBuffer := bytes.NewBuffer(responseInstance.body)
382+
383+
jsonAPIResponse := jsonapiResource{
384+
Data: jsonapiData{
385+
Attributes: responseAttributes,
386+
},
387+
}
388+
389+
if err := json.NewDecoder(responseBuffer).Decode(&jsonAPIResponse); err != nil {
390+
return err
391+
}
392+
393+
switch typedResponseID := jsonAPIResponse.Data.ID.(type) {
394+
case string:
395+
controlPlaneOutput.ID = typedResponseID
396+
case float64:
397+
controlPlaneOutput.IDNumeric = int(typedResponseID)
398+
}
399+
}
400+
401+
return nil
402+
}
403+
389404
func (s *session) deleteResource(ctx context.Context,
390405
path string,
391406
kind string,

pkg/controlplane/test/controlplane_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ func (suite *githubClientSuite) SetupSuite() {
3838
suite.Require().NoError(err, fmt.Sprintf("\nInput: %v\n", newSessionInput))
3939

4040
// create a unique user for the tests
41+
username := fmt.Sprintf("testuser-%d", ts)
4142
ts := time.Now().Unix()
4243
createUserInput := v3ioc.CreateUserInput{}
4344
createUserInput.Ctx = suite.ctx
4445
createUserInput.FirstName = fmt.Sprintf("Test-%d", ts)
4546
createUserInput.LastName = fmt.Sprintf("User-%d", ts)
46-
createUserInput.Username = fmt.Sprintf("testuser-%d", ts)
47+
createUserInput.Username = username
4748
createUserInput.Password = fmt.Sprintf("testpasswd-%d", ts)
4849
createUserInput.Email = fmt.Sprintf("testuser-%d@user.com", ts)
4950
createUserInput.Description = "A user created from tests"
@@ -55,6 +56,13 @@ func (suite *githubClientSuite) SetupSuite() {
5556
suite.Require().NotNil(createUserOutput.ID)
5657
suite.userID = createUserOutput.ID
5758

59+
getRunningUserAttributesInput := v3ioc.GetRunningUserAttributesInput{}
60+
getRunningUserAttributesInput.Ctx = suite.ctx
61+
62+
getRunningUserAttributesOutput, err := session.GetRunningUserAttributesSync(&getRunningUserAttributesInput)
63+
suite.Require().NoError(err)
64+
suite.Require().Equal(getRunningUserAttributesOutput.Username, username)
65+
5866
// create a session with that user
5967
newSessionInput.Username = createUserInput.Username
6068
newSessionInput.Password = createUserInput.Password
@@ -172,12 +180,6 @@ func (suite *githubClientSuite) TestCreateEventUsingAccessKey() {
172180
err = accessKeySession.CreateEventSync(&createEventInput)
173181
suite.Require().NoError(err)
174182

175-
getUserNameInput := v3ioc.GetUserNameInput{}
176-
getUserNameInput.Ctx = suite.ctx
177-
178-
_, err = accessKeySession.GetUserNameSync(&getUserNameInput)
179-
suite.Require().NoError(err)
180-
181183
// Delete access key
182184
deleteAccessKeyInput := v3ioc.DeleteAccessKeyInput{}
183185
deleteAccessKeyInput.ID = createAccessKeyOutput.ID

pkg/controlplane/types.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ type Session interface {
4747
// DeleteAccessKeySync deletes an access key (blocking)
4848
DeleteAccessKeySync(*DeleteAccessKeyInput) error
4949

50-
// GetUserNameSync returns username related to session's access key (blocking)
51-
GetUserNameSync(*GetUserNameInput) (*GetUserNameOutput, error)
50+
// GetRunningUserAttributesSync returns user's attributes related to session's access key (blocking)
51+
GetRunningUserAttributesSync(*GetRunningUserAttributesInput) (*GetRunningUserAttributesOutput, error)
5252
}
5353

5454
type ControlPlaneInput struct {
@@ -141,13 +141,13 @@ type DeleteAccessKeyInput struct {
141141
ControlPlaneInput
142142
}
143143

144-
// GetUserNameInput specifies what access key
145-
type GetUserNameInput struct {
144+
// GetRunningUserAttributesInput specifies what access key
145+
type GetRunningUserAttributesInput struct {
146146
ControlPlaneInput
147147
}
148148

149-
// GetUserNameOutput holds the response from get username
150-
type GetUserNameOutput struct {
149+
// GetRunningUserAttributesOutput holds the response from get user's attributes
150+
type GetRunningUserAttributesOutput struct {
151151
ControlPlaneOutput
152-
Username string
152+
UserAttributes
153153
}

0 commit comments

Comments
 (0)