Skip to content

Commit 0ce3c93

Browse files
authored
chore: split cloudian sdk group struct into internal and external (#56)
1 parent 48ce43a commit 0ce3c93

File tree

2 files changed

+77
-32
lines changed

2 files changed

+77
-32
lines changed

internal/sdk/cloudian/sdk.go

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,66 @@ type Client struct {
1919
}
2020

2121
type Group struct {
22-
Active *string `json:"active,omitempty"`
22+
Active bool `json:"active"`
23+
GroupID string `json:"groupId"`
24+
GroupName string `json:"groupName"`
25+
LDAPEnabled bool `json:"ldapEnabled"`
26+
LDAPGroup string `json:"ldapGroup"`
27+
LDAPMatchAttribute string `json:"ldapMatchAttribute"`
28+
LDAPSearch string `json:"ldapSearch"`
29+
LDAPSearchUserBase string `json:"ldapSearchUserBase"`
30+
LDAPServerURL string `json:"ldapServerURL"`
31+
LDAPUserDNTemplate string `json:"ldapUserDNTemplate"`
32+
}
33+
34+
// fields must be exported (uppercase) to allow json marshalling
35+
type groupInternal struct {
36+
Active string `json:"active"`
2337
GroupID string `json:"groupId"`
24-
GroupName *string `json:"groupName,omitempty"`
25-
LDAPEnabled *bool `json:"ldapEnabled,omitempty"`
26-
LDAPGroup *string `json:"ldapGroup,omitempty"`
27-
LDAPMatchAttribute *string `json:"ldapMatchAttribute,omitempty"`
28-
LDAPSearch *string `json:"ldapSearch,omitempty"`
29-
LDAPSearchUserBase *string `json:"ldapSearchUserBase,omitempty"`
30-
LDAPServerURL *string `json:"ldapServerURL,omitempty"`
31-
LDAPUserDNTemplate *string `json:"ldapUserDNTemplate,omitempty"`
32-
S3EndpointsHTTP []string `json:"s3endpointshttp,omitempty"`
33-
S3EndpointsHTTPS []string `json:"s3endpointshttps,omitempty"`
34-
S3WebSiteEndpoints []string `json:"s3websiteendpoints,omitempty"`
38+
GroupName string `json:"groupName"`
39+
LDAPEnabled bool `json:"ldapEnabled"`
40+
LDAPGroup string `json:"ldapGroup"`
41+
LDAPMatchAttribute string `json:"ldapMatchAttribute"`
42+
LDAPSearch string `json:"ldapSearch"`
43+
LDAPSearchUserBase string `json:"ldapSearchUserBase"`
44+
LDAPServerURL string `json:"ldapServerURL"`
45+
LDAPUserDNTemplate string `json:"ldapUserDNTemplate"`
46+
S3EndpointsHTTP []string `json:"s3endpointshttp"`
47+
S3EndpointsHTTPS []string `json:"s3endpointshttps"`
48+
S3WebSiteEndpoints []string `json:"s3websiteendpoints"`
49+
}
50+
51+
func toInternal(g Group) groupInternal {
52+
return groupInternal{
53+
Active: strconv.FormatBool(g.Active),
54+
GroupID: g.GroupID,
55+
GroupName: g.GroupName,
56+
LDAPEnabled: g.LDAPEnabled,
57+
LDAPGroup: g.LDAPGroup,
58+
LDAPMatchAttribute: g.LDAPMatchAttribute,
59+
LDAPSearch: g.LDAPSearch,
60+
LDAPSearchUserBase: g.LDAPSearchUserBase,
61+
LDAPServerURL: g.LDAPServerURL,
62+
LDAPUserDNTemplate: g.LDAPUserDNTemplate,
63+
S3EndpointsHTTP: []string{"ALL"},
64+
S3EndpointsHTTPS: []string{"ALL"},
65+
S3WebSiteEndpoints: []string{"ALL"},
66+
}
67+
}
68+
69+
func fromInternal(g groupInternal) Group {
70+
return Group{
71+
Active: g.Active == "true",
72+
GroupID: g.GroupID,
73+
GroupName: g.GroupName,
74+
LDAPEnabled: g.LDAPEnabled,
75+
LDAPGroup: g.LDAPGroup,
76+
LDAPMatchAttribute: g.LDAPMatchAttribute,
77+
LDAPSearch: g.LDAPSearch,
78+
LDAPSearchUserBase: g.LDAPSearchUserBase,
79+
LDAPServerURL: g.LDAPServerURL,
80+
LDAPUserDNTemplate: g.LDAPUserDNTemplate,
81+
}
3582
}
3683

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

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

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

247-
var group Group
294+
var group groupInternal
248295
if err := json.Unmarshal(body, &group); err != nil {
249296
return nil, fmt.Errorf("GET unmarshal response body failed: %w", err)
250297
}
251298

252-
return &group, nil
299+
retVal := fromInternal(group)
300+
return &retVal, nil
253301
case 204:
254302
// Cloudian-API returns 204 if the group does not exist
255303
return nil, ErrNotFound

internal/sdk/cloudian/sdk_test.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestRealisticGroupSerialization(t *testing.T) {
2222
"s3websiteendpoints": ["ALL"]
2323
}`
2424

25-
var group Group
25+
var group groupInternal
2626
err := json.Unmarshal([]byte(jsonString), &group)
2727
if err != nil {
2828
t.Errorf("Error deserializing from JSON: %v", err)
@@ -88,23 +88,21 @@ func TestUnmarshalUsers(t *testing.T) {
8888

8989
}
9090

91-
func (group Group) Generate(rand *rand.Rand, size int) reflect.Value {
92-
active := "true"
93-
ldapEnabled := true
94-
return reflect.ValueOf(Group{
95-
Active: &active,
96-
GroupID: *randomString(16),
91+
func (group groupInternal) Generate(rand *rand.Rand, size int) reflect.Value {
92+
return reflect.ValueOf(groupInternal{
93+
Active: "true",
94+
GroupID: randomString(16),
9795
GroupName: randomString(32),
98-
LDAPEnabled: &ldapEnabled,
96+
LDAPEnabled: true,
9997
LDAPGroup: randomString(8),
10098
LDAPMatchAttribute: randomString(8),
10199
LDAPSearch: randomString(8),
102100
LDAPSearchUserBase: randomString(8),
103101
LDAPServerURL: randomString(8),
104102
LDAPUserDNTemplate: randomString(8),
105-
S3EndpointsHTTP: []string{*randomString(8), *randomString(8)},
106-
S3EndpointsHTTPS: []string{*randomString(8), *randomString(8)},
107-
S3WebSiteEndpoints: []string{*randomString(8), *randomString(8)},
103+
S3EndpointsHTTP: []string{randomString(8), randomString(8)},
104+
S3EndpointsHTTPS: []string{randomString(8), randomString(8)},
105+
S3WebSiteEndpoints: []string{randomString(8), randomString(8)},
108106
})
109107
}
110108

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

127125
func TestGroupSerialization(t *testing.T) {
128-
f := func(group Group) bool {
126+
f := func(group groupInternal) bool {
129127
data, err := json.Marshal(group)
130128
if err != nil {
131129
return false
132130
}
133131

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

147145
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
148146

149-
func randomString(length int) *string {
147+
func randomString(length int) string {
150148
var sb strings.Builder
151149
runes := []rune(charset)
152150
for i := 0; i < length; i++ {
153151
sb.WriteRune(runes[rand.Intn(len(runes))])
154152
}
155-
str := sb.String()
156-
return &str
153+
return sb.String()
157154
}

0 commit comments

Comments
 (0)