Skip to content

Commit

Permalink
chore: Split cloudian group struct into internal and external
Browse files Browse the repository at this point in the history
  • Loading branch information
mariatsji committed Dec 13, 2024
1 parent 376150c commit e175202
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 42 deletions.
82 changes: 65 additions & 17 deletions internal/sdk/cloudian/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,66 @@ type Client struct {
}

type Group struct {
Active *string `json:"active,omitempty"`
GroupID string `json:"groupId"`
GroupName *string `json:"groupName,omitempty"`
LDAPEnabled *bool `json:"ldapEnabled,omitempty"`
LDAPGroup *string `json:"ldapGroup,omitempty"`
LDAPMatchAttribute *string `json:"ldapMatchAttribute,omitempty"`
LDAPSearch *string `json:"ldapSearch,omitempty"`
LDAPSearchUserBase *string `json:"ldapSearchUserBase,omitempty"`
LDAPServerURL *string `json:"ldapServerURL,omitempty"`
LDAPUserDNTemplate *string `json:"ldapUserDNTemplate,omitempty"`
S3EndpointsHTTP []string `json:"s3endpointshttp,omitempty"`
S3EndpointsHTTPS []string `json:"s3endpointshttps,omitempty"`
S3WebSiteEndpoints []string `json:"s3websiteendpoints,omitempty"`
Active bool `json:"active"`
GroupID string `json:"groupId"`
GroupName string `json:"groupName"`
LDAPEnabled bool `json:"ldapEnabled"`
LDAPGroup string `json:"ldapGroup"`
LDAPMatchAttribute string `json:"ldapMatchAttribute"`
LDAPSearch string `json:"ldapSearch"`
LDAPSearchUserBase string `json:"ldapSearchUserBase"`
LDAPServerURL string `json:"ldapServerURL"`
LDAPUserDNTemplate string `json:"ldapUserDNTemplate"`
}

// fields must be exported (uppercase) to allow json marshalling
type groupInternal struct {
ActiveInternal string `json:"active"`
GroupIDInternal string `json:"groupId"`
GroupNameInternal string `json:"groupName"`
LDAPEnabledInternal bool `json:"ldapEnabled"`
LDAPGroupInternal string `json:"ldapGroup"`
LDAPMatchAttributeInternal string `json:"ldapMatchAttribute"`
LDAPSearchInternal string `json:"ldapSearch"`
LDAPSearchUserBaseInternal string `json:"ldapSearchUserBase"`
LDAPServerURLInternal string `json:"ldapServerURL"`
LDAPUserDNTemplateInternal string `json:"ldapUserDNTemplate"`
S3EndpointsHTTPInternal []string `json:"s3endpointshttp"`
S3EndpointsHTTPSInternal []string `json:"s3endpointshttps"`
S3WebSiteEndpointsInternal []string `json:"s3websiteendpoints"`
}

func toInternal(g Group) groupInternal {
return groupInternal{
ActiveInternal: strconv.FormatBool(g.Active),
GroupIDInternal: g.GroupID,
GroupNameInternal: g.GroupName,
LDAPEnabledInternal: g.LDAPEnabled,
LDAPGroupInternal: g.LDAPGroup,
LDAPMatchAttributeInternal: g.LDAPMatchAttribute,
LDAPSearchInternal: g.LDAPSearch,
LDAPSearchUserBaseInternal: g.LDAPSearchUserBase,
LDAPServerURLInternal: g.LDAPServerURL,
LDAPUserDNTemplateInternal: g.LDAPUserDNTemplate,
S3EndpointsHTTPInternal: []string{"ALL"},
S3EndpointsHTTPSInternal: []string{"ALL"},
S3WebSiteEndpointsInternal: []string{"ALL"},
}
}

func fromInternal(g groupInternal) Group {
return Group{
Active: g.ActiveInternal == "true",
GroupID: g.GroupIDInternal,
GroupName: g.GroupNameInternal,
LDAPEnabled: g.LDAPEnabledInternal,
LDAPGroup: g.LDAPGroupInternal,
LDAPMatchAttribute: g.LDAPMatchAttributeInternal,
LDAPSearch: g.LDAPSearchInternal,
LDAPSearchUserBase: g.LDAPSearchUserBaseInternal,
LDAPServerURL: g.LDAPServerURLInternal,
LDAPUserDNTemplate: g.LDAPUserDNTemplateInternal,
}
}

type User struct {
Expand Down Expand Up @@ -179,7 +226,7 @@ func (client Client) DeleteGroup(ctx context.Context, groupId string) error {
func (client Client) CreateGroup(ctx context.Context, group Group) error {
url := client.baseURL + "/group"

jsonData, err := json.Marshal(group)
jsonData, err := json.Marshal(toInternal(group))
if err != nil {
return fmt.Errorf("error marshaling JSON: %w", err)
}
Expand All @@ -201,7 +248,7 @@ func (client Client) CreateGroup(ctx context.Context, group Group) error {
func (client Client) UpdateGroup(ctx context.Context, group Group) error {
url := client.baseURL + "/group"

jsonData, err := json.Marshal(group)
jsonData, err := json.Marshal(toInternal(group))
if err != nil {
return fmt.Errorf("error marshaling JSON: %w", err)
}
Expand Down Expand Up @@ -244,12 +291,13 @@ func (client Client) GetGroup(ctx context.Context, groupId string) (*Group, erro
return nil, fmt.Errorf("GET reading response body failed: %w", err)
}

var group Group
var group groupInternal
if err := json.Unmarshal(body, &group); err != nil {
return nil, fmt.Errorf("GET unmarshal response body failed: %w", err)
}

return &group, nil
retVal := fromInternal(group)
return &retVal, nil
case 204:
// Cloudian-API returns 204 if the group does not exist
return nil, ErrNotFound
Expand Down
47 changes: 22 additions & 25 deletions internal/sdk/cloudian/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ func TestRealisticGroupSerialization(t *testing.T) {
"s3websiteendpoints": ["ALL"]
}`

var group Group
var group groupInternal
err := json.Unmarshal([]byte(jsonString), &group)
if err != nil {
t.Errorf("Error deserializing from JSON: %v", err)
}

if group.GroupID != "QA" {
t.Errorf("Expected QA, got %v", group.GroupID)
if group.GroupIDInternal != "QA" {
t.Errorf("Expected QA, got %v", group.GroupIDInternal)
}
}

Expand Down Expand Up @@ -88,23 +88,21 @@ func TestUnmarshalUsers(t *testing.T) {

}

func (group Group) Generate(rand *rand.Rand, size int) reflect.Value {
active := "true"
ldapEnabled := true
return reflect.ValueOf(Group{
Active: &active,
GroupID: *randomString(16),
GroupName: randomString(32),
LDAPEnabled: &ldapEnabled,
LDAPGroup: randomString(8),
LDAPMatchAttribute: randomString(8),
LDAPSearch: randomString(8),
LDAPSearchUserBase: randomString(8),
LDAPServerURL: randomString(8),
LDAPUserDNTemplate: randomString(8),
S3EndpointsHTTP: []string{*randomString(8), *randomString(8)},
S3EndpointsHTTPS: []string{*randomString(8), *randomString(8)},
S3WebSiteEndpoints: []string{*randomString(8), *randomString(8)},
func (group groupInternal) Generate(rand *rand.Rand, size int) reflect.Value {
return reflect.ValueOf(groupInternal{
ActiveInternal: "true",
GroupIDInternal: randomString(16),
GroupNameInternal: randomString(32),
LDAPEnabledInternal: true,
LDAPGroupInternal: randomString(8),
LDAPMatchAttributeInternal: randomString(8),
LDAPSearchInternal: randomString(8),
LDAPSearchUserBaseInternal: randomString(8),
LDAPServerURLInternal: randomString(8),
LDAPUserDNTemplateInternal: randomString(8),
S3EndpointsHTTPInternal: []string{randomString(8), randomString(8)},
S3EndpointsHTTPSInternal: []string{randomString(8), randomString(8)},
S3WebSiteEndpointsInternal: []string{randomString(8), randomString(8)},
})
}

Expand All @@ -125,13 +123,13 @@ func TestWrappedErrNotFound(t *testing.T) {
}

func TestGroupSerialization(t *testing.T) {
f := func(group Group) bool {
f := func(group groupInternal) bool {
data, err := json.Marshal(group)
if err != nil {
return false
}

var deserialized Group
var deserialized groupInternal
if err = json.Unmarshal(data, &deserialized); err != nil {
return false
}
Expand All @@ -146,12 +144,11 @@ func TestGroupSerialization(t *testing.T) {

const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"

func randomString(length int) *string {
func randomString(length int) string {
var sb strings.Builder
runes := []rune(charset)
for i := 0; i < length; i++ {
sb.WriteRune(runes[rand.Intn(len(runes))])
}
str := sb.String()
return &str
return sb.String()
}

0 comments on commit e175202

Please sign in to comment.