Skip to content

Commit df7e844

Browse files
authored
Merge pull request #83 from arangodb-managed/OAS-6283
OAS-6283 | add authentication providers
2 parents 490d6b4 + 606cb12 commit df7e844

File tree

2 files changed

+106
-3
lines changed

2 files changed

+106
-3
lines changed

internal/resource_organization.go

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ const (
3636
organizationNameFieldName = "name"
3737
organizationDescriptionFieldName = "description"
3838
organizationLockFieldName = "locked"
39+
authenticationProvidersFieldName = "authentication_providers"
40+
enableGithubFieldName = "enable_github"
41+
enableGoogleFieldName = "enable_google"
42+
enableUsernamePasswordFieldName = "enable_username_password"
43+
enableMicrosoftFieldName = "enable_microsoft"
3944
)
4045

4146
// resourceOrganization defines an Organization Oasis resource.
@@ -63,6 +68,41 @@ func resourceOrganization() *schema.Resource {
6368
Description: "Organization Resource Organization Lock field",
6469
Optional: true,
6570
},
71+
authenticationProvidersFieldName: {
72+
Type: schema.TypeList,
73+
Description: "Authentication Provider field",
74+
Computed: true,
75+
Optional: true,
76+
Elem: &schema.Resource{
77+
Schema: map[string]*schema.Schema{
78+
79+
enableGithubFieldName: {
80+
Type: schema.TypeBool,
81+
Description: "Organization Resource Enable Github Login field",
82+
Optional: true,
83+
Default: false,
84+
},
85+
enableGoogleFieldName: {
86+
Type: schema.TypeBool,
87+
Description: "Organization Resource Enable Google Login field",
88+
Optional: true,
89+
Default: false,
90+
},
91+
enableUsernamePasswordFieldName: {
92+
Type: schema.TypeBool,
93+
Description: "Organization Resource Enable Username Password Login field",
94+
Optional: true,
95+
Default: false,
96+
},
97+
enableMicrosoftFieldName: {
98+
Type: schema.TypeBool,
99+
Description: "Organization Resource Enable Microsoft Login field",
100+
Optional: true,
101+
Default: false,
102+
},
103+
},
104+
},
105+
},
66106
},
67107
}
68108
}
@@ -132,7 +172,9 @@ func expandOrganizationResource(d *schema.ResourceData) (*rm.Organization, error
132172
if v, ok := d.GetOk(organizationLockFieldName); ok {
133173
ret.Locked = v.(bool)
134174
}
135-
175+
if v, ok := d.GetOk(authenticationProvidersFieldName); ok {
176+
ret.AuthenticationProviders = expandAuthenticationProviders(v.([]interface{}))
177+
}
136178
return ret, nil
137179
}
138180

@@ -178,7 +220,9 @@ func resourceOrganizationUpdate(ctx context.Context, d *schema.ResourceData, m i
178220
if d.HasChange(organizationLockFieldName) {
179221
organization.Locked = d.Get(organizationLockFieldName).(bool)
180222
}
181-
223+
if v, ok := d.GetOk(authenticationProvidersFieldName); ok {
224+
organization.AuthenticationProviders = expandAuthenticationProviders(v.([]interface{}))
225+
}
182226
res, err := rmc.UpdateOrganization(client.ctxWithToken, organization)
183227
if err != nil {
184228
client.log.Error().Err(err).Msg("Failed to update Organization")
@@ -191,9 +235,46 @@ func resourceOrganizationUpdate(ctx context.Context, d *schema.ResourceData, m i
191235

192236
// flattenOrganizationResource will take an Organization object and turn it into a flat map for terraform digestion.
193237
func flattenOrganizationResource(organization *rm.Organization) map[string]interface{} {
194-
return map[string]interface{}{
238+
result := map[string]interface{}{
195239
organizationNameFieldName: organization.GetName(),
196240
organizationDescriptionFieldName: organization.GetDescription(),
197241
organizationLockFieldName: organization.GetLocked(),
198242
}
243+
if organization.GetAuthenticationProviders() != nil {
244+
result[authenticationProvidersFieldName] = flattenAuthenticationProviders(organization.GetAuthenticationProviders())
245+
}
246+
return result
247+
}
248+
249+
// flattenAuthenticationProviders will take a AuthenticationProviders Spec object and turn it into a flat map for terraform digestion.
250+
func flattenAuthenticationProviders(p *rm.AuthenticationProviders) []interface{} {
251+
providers := make(map[string]interface{})
252+
providers[enableGithubFieldName] = p.GetEnableGithub()
253+
providers[enableGoogleFieldName] = p.GetEnableGoogle()
254+
providers[enableMicrosoftFieldName] = p.GetEnableMicrosoft()
255+
providers[enableUsernamePasswordFieldName] = p.GetEnableUsernamePassword()
256+
return []interface{}{
257+
providers,
258+
}
259+
}
260+
261+
// expandAuthenticationProviders will take a terraform flat map schema data and turn it into an ArangoGraph AuthenticationProviders.
262+
func expandAuthenticationProviders(p []interface{}) *rm.AuthenticationProviders {
263+
result := &rm.AuthenticationProviders{}
264+
for _, v := range p {
265+
item := v.(map[string]interface{})
266+
if i, ok := item[enableGithubFieldName]; ok {
267+
result.EnableGithub = i.(bool)
268+
}
269+
if i, ok := item[enableGoogleFieldName]; ok {
270+
result.EnableGoogle = i.(bool)
271+
}
272+
if i, ok := item[enableMicrosoftFieldName]; ok {
273+
result.EnableMicrosoft = i.(bool)
274+
}
275+
if i, ok := item[enableUsernamePasswordFieldName]; ok {
276+
result.EnableUsernamePassword = i.(bool)
277+
}
278+
}
279+
return result
199280
}

internal/resource_organization_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,28 @@ func TestFlattenOrganization(t *testing.T) {
139139
flattened := flattenOrganizationResource(organization)
140140
assert.Equal(tt, expected, flattened)
141141
})
142+
143+
t.Run("with auth providers enabled", func(tt *testing.T) {
144+
authProviderData := []interface{}{
145+
map[string]interface{}{
146+
enableGithubFieldName: true,
147+
enableGoogleFieldName: true,
148+
enableMicrosoftFieldName: true,
149+
enableUsernamePasswordFieldName: true,
150+
},
151+
}
152+
organization.AuthenticationProviders = expandAuthenticationProviders(authProviderData)
153+
expected[authenticationProvidersFieldName] = []interface{}{
154+
map[string]interface{}{
155+
enableGithubFieldName: true,
156+
enableGoogleFieldName: true,
157+
enableMicrosoftFieldName: true,
158+
enableUsernamePasswordFieldName: true,
159+
},
160+
}
161+
flattened := flattenOrganizationResource(organization)
162+
assert.Equal(tt, expected, flattened)
163+
})
142164
}
143165

144166
// TestExpandOrganization tests the Oasis Organization expansion for Terraform schema compatibility.

0 commit comments

Comments
 (0)