Skip to content

Commit

Permalink
refactor: type and var names
Browse files Browse the repository at this point in the history
  • Loading branch information
tenstad committed Feb 20, 2025
1 parent c5eb2dc commit 3601ee8
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 54 deletions.
2 changes: 1 addition & 1 deletion internal/controller/accesskey/accesskey.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext

cr.SetConditions(xpv1.Creating())

creds, err := c.cloudianService.CreateUserCredentials(ctx, cloudian.UserID{
creds, err := c.cloudianService.CreateUserCredentials(ctx, cloudian.GroupUserID{
GroupID: cr.Spec.ForProvider.GroupID,
UserID: cr.Spec.ForProvider.UserID,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
return managed.ExternalObservation{}, nil
}

user := cloudian.UserID{
guid := cloudian.GroupUserID{
GroupID: groupID,
UserID: "*",
}
qos, err := c.cloudianService.GetQOS(ctx, user, cr.Spec.ForProvider.Region)
qos, err := c.cloudianService.GetQOS(ctx, guid, cr.Spec.ForProvider.Region)

if errors.Is(err, cloudian.ErrNotFound) {
return managed.ExternalObservation{ResourceExists: false}, nil
Expand Down Expand Up @@ -200,11 +200,11 @@ func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext
return managed.ExternalCreation{}, err
}

user := cloudian.UserID{
guid := cloudian.GroupUserID{
GroupID: cr.Spec.ForProvider.GroupID,
UserID: "*",
}
if err := c.cloudianService.SetQOS(ctx, user, cr.Spec.ForProvider.Region, qos); err != nil {
if err := c.cloudianService.SetQOS(ctx, guid, cr.Spec.ForProvider.Region, qos); err != nil {
return managed.ExternalCreation{}, errors.Wrap(err, errCreateQOS)
}

Expand All @@ -226,11 +226,11 @@ func (c *external) Update(ctx context.Context, mg resource.Managed) (managed.Ext
return managed.ExternalUpdate{}, err
}

user := cloudian.UserID{
guid := cloudian.GroupUserID{
GroupID: cr.Spec.ForProvider.GroupID,
UserID: "*",
}
if err := c.cloudianService.SetQOS(ctx, user, cr.Spec.ForProvider.Region, qos); err != nil {
if err := c.cloudianService.SetQOS(ctx, guid, cr.Spec.ForProvider.Region, qos); err != nil {
return managed.ExternalUpdate{}, errors.Wrap(err, errCreateQOS)
}

Expand All @@ -249,11 +249,11 @@ func (c *external) Delete(ctx context.Context, mg resource.Managed) (managed.Ext

cr.SetConditions(xpv1.Deleting())

user := cloudian.UserID{
guid := cloudian.GroupUserID{
GroupID: cr.Spec.ForProvider.GroupID,
UserID: "*",
}
err := c.cloudianService.DeleteQOS(ctx, user, cr.Spec.ForProvider.Region)
err := c.cloudianService.DeleteQOS(ctx, guid, cr.Spec.ForProvider.Region)
if err != nil && !errors.Is(err, cloudian.ErrNotFound) {
return managed.ExternalDelete{}, errors.Wrap(err, errGetCreds)
}
Expand Down
12 changes: 6 additions & 6 deletions internal/controller/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
return managed.ExternalObservation{}, nil
}

_, err := c.cloudianService.GetUser(ctx, cloudian.UserID{
_, err := c.cloudianService.GetUser(ctx, cloudian.GroupUserID{
GroupID: group,
UserID: externalName})
if errors.Is(err, cloudian.ErrNotFound) {
Expand Down Expand Up @@ -193,7 +193,7 @@ func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext
}

user := cloudian.User{
UserID: cloudian.UserID{
GroupUserID: cloudian.GroupUserID{
GroupID: cr.Spec.ForProvider.GroupID,
UserID: meta.GetExternalName(mg),
},
Expand All @@ -205,7 +205,7 @@ func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext

// When Cloudian creates a user, a single access key is created inside it.
// Delete the access key, so that the user does not have any non-managed access keys.
creds, err := c.cloudianService.ListUserCredentials(ctx, user.UserID)
creds, err := c.cloudianService.ListUserCredentials(ctx, user.GroupUserID)
if err != nil {
return managed.ExternalCreation{}, errors.Wrap(err, "failed to list access keys of user")
}
Expand Down Expand Up @@ -243,20 +243,20 @@ func (c *external) Delete(ctx context.Context, mg resource.Managed) (managed.Ext
return managed.ExternalDelete{}, errors.New(errNotUser)
}

user := cloudian.UserID{
guid := cloudian.GroupUserID{
GroupID: cr.Spec.ForProvider.GroupID,
UserID: meta.GetExternalName(mg),
}

creds, err := c.cloudianService.ListUserCredentials(ctx, user)
creds, err := c.cloudianService.ListUserCredentials(ctx, guid)
if err != nil {
return managed.ExternalDelete{}, err
}
if len(creds) > 0 {
return managed.ExternalDelete{}, errors.New("User has access keys and cannot be deleted")
}

if err := c.cloudianService.DeleteUser(ctx, user); err != nil {
if err := c.cloudianService.DeleteUser(ctx, guid); err != nil {
return managed.ExternalDelete{}, errors.Wrap(err, errDeleteUser)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
return managed.ExternalObservation{}, nil
}

user := cloudian.UserID{
guid := cloudian.GroupUserID{
GroupID: groupID,
UserID: userID,
}
qos, err := c.cloudianService.GetQOS(ctx, user, cr.Spec.ForProvider.Region)
qos, err := c.cloudianService.GetQOS(ctx, guid, cr.Spec.ForProvider.Region)

if errors.Is(err, cloudian.ErrNotFound) {
return managed.ExternalObservation{ResourceExists: false}, nil
Expand Down Expand Up @@ -204,11 +204,11 @@ func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext
return managed.ExternalCreation{}, err
}

user := cloudian.UserID{
guid := cloudian.GroupUserID{
GroupID: cr.Spec.ForProvider.GroupID,
UserID: cr.Spec.ForProvider.UserID,
}
if err := c.cloudianService.SetQOS(ctx, user, cr.Spec.ForProvider.Region, qos); err != nil {
if err := c.cloudianService.SetQOS(ctx, guid, cr.Spec.ForProvider.Region, qos); err != nil {
return managed.ExternalCreation{}, errors.Wrap(err, errCreateQOS)
}

Expand All @@ -230,11 +230,11 @@ func (c *external) Update(ctx context.Context, mg resource.Managed) (managed.Ext
return managed.ExternalUpdate{}, err
}

user := cloudian.UserID{
guid := cloudian.GroupUserID{
GroupID: cr.Spec.ForProvider.GroupID,
UserID: cr.Spec.ForProvider.UserID,
}
if err := c.cloudianService.SetQOS(ctx, user, cr.Spec.ForProvider.Region, qos); err != nil {
if err := c.cloudianService.SetQOS(ctx, guid, cr.Spec.ForProvider.Region, qos); err != nil {
return managed.ExternalUpdate{}, errors.Wrap(err, errCreateQOS)
}

Expand All @@ -253,11 +253,11 @@ func (c *external) Delete(ctx context.Context, mg resource.Managed) (managed.Ext

cr.SetConditions(xpv1.Deleting())

user := cloudian.UserID{
guid := cloudian.GroupUserID{
GroupID: cr.Spec.ForProvider.GroupID,
UserID: cr.Spec.ForProvider.UserID,
}
err := c.cloudianService.DeleteQOS(ctx, user, cr.Spec.ForProvider.Region)
err := c.cloudianService.DeleteQOS(ctx, guid, cr.Spec.ForProvider.Region)
if err != nil && !errors.Is(err, cloudian.ErrNotFound) {
return managed.ExternalDelete{}, errors.Wrap(err, errGetCreds)
}
Expand Down
18 changes: 9 additions & 9 deletions internal/sdk/cloudian/qos.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (qos *QualityOfService) queryParams(params map[string]string) error {
// Default user-level QoS for the whole region (GroupID="*", UserID="ALL")
// Group-level QoS for a specific group (GroupID="<groupId>", UserID="*")
// Default group-level QoS for the whole region (GroupID="ALL", UserID="*")
func (client Client) SetQOS(ctx context.Context, user UserID, region string, qos QualityOfService) error {
func (client Client) SetQOS(ctx context.Context, guid GroupUserID, region string, qos QualityOfService) error {
for _, val := range qos.rawQueryParams() {
if val != nil && *val < -1 {
return fmt.Errorf("QoS limit values must be >= -1")
Expand All @@ -161,8 +161,8 @@ func (client Client) SetQOS(ctx context.Context, user UserID, region string, qos
}

resp, err := client.newRequest(ctx).
SetQueryParam("userId", user.UserID).
SetQueryParam("groupId", user.GroupID).
SetQueryParam("userId", guid.UserID).
SetQueryParam("groupId", guid.GroupID).
SetQueryParams(params).
Post("/qos/limits")
if err != nil {
Expand All @@ -179,15 +179,15 @@ func (client Client) SetQOS(ctx context.Context, user UserID, region string, qos

// SetQOS gets QualityOfService limits for a Group or User, depending on the value of GroupID and UserID.
// See SetQOS for details.
func (client Client) GetQOS(ctx context.Context, user UserID, region string) (*QualityOfService, error) {
func (client Client) GetQOS(ctx context.Context, guid GroupUserID, region string) (*QualityOfService, error) {
params := make(map[string]string)
if region != DefaultRegion {
params["region"] = region
}

resp, err := client.newRequest(ctx).
SetQueryParam("userId", user.UserID).
SetQueryParam("groupId", user.GroupID).
SetQueryParam("userId", guid.UserID).
SetQueryParam("groupId", guid.GroupID).
SetQueryParams(params).
Get("/qos/limits")
if err != nil {
Expand Down Expand Up @@ -218,15 +218,15 @@ func (client Client) GetQOS(ctx context.Context, user UserID, region string) (*Q

// DeleteQOS deletes QualityOfService limits for a Group or User, depending on the value of GroupID and UserID.
// See SetQOS for details.
func (client Client) DeleteQOS(ctx context.Context, user UserID, region string) error {
func (client Client) DeleteQOS(ctx context.Context, guid GroupUserID, region string) error {
params := make(map[string]string)
if region != DefaultRegion {
params["region"] = region
}

resp, err := client.newRequest(ctx).
SetQueryParam("userId", user.UserID).
SetQueryParam("groupId", user.GroupID).
SetQueryParam("userId", guid.UserID).
SetQueryParam("groupId", guid.GroupID).
SetQueryParams(params).
Delete("/qos/limits")
if err != nil {
Expand Down
30 changes: 15 additions & 15 deletions internal/sdk/cloudian/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ const (
UserTypeUser UserType = "User"
)

type UserID struct {
type GroupUserID struct {
GroupID string `json:"groupId"`
UserID string `json:"userId"`
}

type User struct {
UserID `json:",inline"`
UserType UserType `json:"userType"`
GroupUserID `json:",inline"`
UserType UserType `json:"userType"`
}

// SecurityInfo is the Cloudian API's term for secure credentials
Expand Down Expand Up @@ -156,7 +156,7 @@ func (client Client) ListUsers(ctx context.Context, groupID string, userID *stri
// Paginated API endpoint where limit+1 elements indicates more pages
if len(users) > ListLimit {
// Fetch remaining users starting from the user after the limit
moreUsers, err := client.ListUsers(ctx, groupID, &users[ListLimit].UserID.UserID)
moreUsers, err := client.ListUsers(ctx, groupID, &users[ListLimit].GroupUserID.UserID)
if err != nil {
return nil, err
}
Expand All @@ -168,11 +168,11 @@ func (client Client) ListUsers(ctx context.Context, groupID string, userID *stri
}

// Delete a single user. Errors if the user does not exist.
func (client Client) DeleteUser(ctx context.Context, user UserID) error {
func (client Client) DeleteUser(ctx context.Context, guid GroupUserID) error {
resp, err := client.newRequest(ctx).
SetQueryParams(map[string]string{
"groupId": user.GroupID,
"userId": user.UserID,
"groupId": guid.GroupID,
"userId": guid.UserID,
}).
Delete("/user")
if err != nil {
Expand Down Expand Up @@ -207,13 +207,13 @@ func (client Client) CreateUser(ctx context.Context, user User) error {

// GetUser gets a user. Returns an error even in the case of a user not found.
// This error can then be checked against ErrNotFound: errors.Is(err, ErrNotFound)
func (client Client) GetUser(ctx context.Context, userID UserID) (*User, error) {
func (client Client) GetUser(ctx context.Context, guid GroupUserID) (*User, error) {
var user User

resp, err := client.newRequest(ctx).
SetQueryParams(map[string]string{
"groupId": userID.GroupID,
"userId": userID.UserID,
"groupId": guid.GroupID,
"userId": guid.UserID,
}).
SetResult(&user).
Get("/user")
Expand All @@ -233,12 +233,12 @@ func (client Client) GetUser(ctx context.Context, userID UserID) (*User, error)
}

// CreateUserCredentials creates a new set of credentials for a user.
func (client Client) CreateUserCredentials(ctx context.Context, user UserID) (*SecurityInfo, error) {
func (client Client) CreateUserCredentials(ctx context.Context, guid GroupUserID) (*SecurityInfo, error) {
var securityInfo SecurityInfo

resp, err := client.newRequest(ctx).
SetResult(&securityInfo).
SetQueryParams(map[string]string{"groupId": user.GroupID, "userId": user.UserID}).
SetQueryParams(map[string]string{"groupId": guid.GroupID, "userId": guid.UserID}).
Put("/user/credentials")
if err != nil {
return nil, err
Expand Down Expand Up @@ -276,11 +276,11 @@ func (client Client) GetUserCredentials(ctx context.Context, accessKey string) (
}

// ListUserCredentials fetches all the credentials of a user.
func (client Client) ListUserCredentials(ctx context.Context, user UserID) ([]SecurityInfo, error) {
func (client Client) ListUserCredentials(ctx context.Context, guid GroupUserID) ([]SecurityInfo, error) {
var securityInfo []SecurityInfo

resp, err := client.newRequest(ctx).
SetQueryParams(map[string]string{"groupId": user.GroupID, "userId": user.UserID}).
SetQueryParams(map[string]string{"groupId": guid.GroupID, "userId": guid.UserID}).
SetResult(&securityInfo).
Get("/user/credentials/list")
if err != nil {
Expand Down Expand Up @@ -323,7 +323,7 @@ func (client Client) DeleteGroupRecursive(ctx context.Context, groupID string) e
}

for _, user := range users {
if err := client.DeleteUser(ctx, user.UserID); err != nil {
if err := client.DeleteUser(ctx, user.GroupUserID); err != nil {
return fmt.Errorf("error deleting user: %w", err)
}
}
Expand Down
14 changes: 7 additions & 7 deletions internal/sdk/cloudian/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestCreateCredentials(t *testing.T) {
})
defer testServer.Close()

credentials, err := cloudianClient.CreateUserCredentials(context.TODO(), UserID{GroupID: "QA", UserID: "user1"})
credentials, err := cloudianClient.CreateUserCredentials(context.TODO(), GroupUserID{GroupID: "QA", UserID: "user1"})
if err != nil {
t.Errorf("Error creating credentials: %v", err)
}
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestListUserCredentials(t *testing.T) {
defer testServer.Close()

credentials, err := cloudianClient.ListUserCredentials(
context.TODO(), UserID{UserID: "", GroupID: ""},
context.TODO(), GroupUserID{UserID: "", GroupID: ""},
)
if err != nil {
t.Errorf("Error listing credentials: %v", err)
Expand All @@ -118,7 +118,7 @@ func TestListUserCredentials(t *testing.T) {
func TestListUsers(t *testing.T) {
var expected []User
for i := 0; i < 500; i++ {
expected = append(expected, User{UserID: UserID{GroupID: "QA", UserID: strconv.Itoa(i)}})
expected = append(expected, User{GroupUserID: GroupUserID{GroupID: "QA", UserID: strconv.Itoa(i)}})
}

cloudianClient, testServer := mockBy(func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -163,16 +163,16 @@ func TestClient_GetUser(t *testing.T) {
status int
wantErr error
}{
{name: "Exists", user: User{UserID: UserID{UserID: strconv.Itoa(http.StatusOK)}}},
{name: "Not found", user: User{UserID: UserID{UserID: strconv.Itoa(http.StatusNoContent)}}, wantErr: ErrNotFound},
{name: "Exists", user: User{GroupUserID: GroupUserID{UserID: strconv.Itoa(http.StatusOK)}}},
{name: "Not found", user: User{GroupUserID: GroupUserID{UserID: strconv.Itoa(http.StatusNoContent)}}, wantErr: ErrNotFound},
}

client, testServer := mockBy(func(w http.ResponseWriter, r *http.Request) {
userId := r.URL.Query().Get("userId")
statusCode, _ := strconv.Atoi(userId)
if statusCode == http.StatusOK {
for _, tt := range tests {
if tt.user.UserID.UserID == userId {
if tt.user.GroupUserID.UserID == userId {
json.NewEncoder(w).Encode(tt.user)
break
}
Expand All @@ -184,7 +184,7 @@ func TestClient_GetUser(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
user, err := client.GetUser(context.Background(), tt.user.UserID)
user, err := client.GetUser(context.Background(), tt.user.GroupUserID)

if !errors.Is(err, tt.wantErr) {
t.Errorf("GetUser() error = %v, wantErr %v", err, tt.wantErr)
Expand Down

0 comments on commit 3601ee8

Please sign in to comment.