-
Notifications
You must be signed in to change notification settings - Fork 59
feat: Add is_primary_for_scope attribute to boundary_auth_method and boundary_auth_method_password resources
#753
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| terraform import boundary_auth_method_password.foo <my-id> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| resource "boundary_scope" "org" { | ||
| name = "organization_one" | ||
| description = "My first scope!" | ||
| scope_id = "global" | ||
| auto_create_admin_role = true | ||
| auto_create_default_role = true | ||
| } | ||
|
|
||
| resource "boundary_auth_method_password" "password" { | ||
| scope_id = boundary_scope.org.id | ||
| } | ||
|
|
||
| resource "boundary_auth_method_password" "password_is_primary" { | ||
| scope_id = boundary_scope.org.id | ||
| is_primary_for_scope = true | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,9 +15,10 @@ import ( | |
| ) | ||
|
|
||
| const ( | ||
| authmethodTypePassword = "password" | ||
| authmethodMinLoginNameLengthKey = "min_login_name_length" | ||
| authmethodMinPasswordLengthKey = "min_password_length" | ||
| authmethodTypePassword = "password" | ||
| authmethodMinLoginNameLengthKey = "min_login_name_length" | ||
| authmethodMinPasswordLengthKey = "min_password_length" | ||
| authmethodIsPrimaryAuthMethodForScopeKey = "is_primary_for_scope" | ||
| ) | ||
|
|
||
| func resourceAuthMethodPassword() *schema.Resource { | ||
|
|
@@ -72,6 +73,11 @@ func resourceAuthMethodPassword() *schema.Resource { | |
| Optional: true, | ||
| Computed: true, | ||
| }, | ||
| authmethodIsPrimaryAuthMethodForScopeKey: { | ||
| Description: "When true, makes this auth method the primary auth method for the scope in which it resides.", | ||
| Type: schema.TypeBool, | ||
| Optional: true, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
@@ -82,6 +88,10 @@ func setFromPasswordAuthMethodResponseMap(d *schema.ResourceData, raw map[string | |
| d.Set(ScopeIdKey, raw[ScopeIdKey]) | ||
| d.Set(TypeKey, raw[TypeKey]) | ||
|
|
||
| if p, ok := raw[authmethodIsPrimaryAuthMethodForScopeKey]; ok { | ||
| d.Set(authmethodIsPrimaryAuthMethodForScopeKey, p.(bool)) | ||
| } | ||
|
|
||
| if attrsVal, ok := raw["attributes"]; ok { | ||
| attrs := attrsVal.(map[string]interface{}) | ||
|
|
||
|
|
@@ -158,6 +168,19 @@ func resourceAuthMethodPasswordCreate(ctx context.Context, d *schema.ResourceDat | |
| return diag.Errorf("nil auth method after create") | ||
| } | ||
|
|
||
| amid := amcr.GetResponse().Map["id"].(string) | ||
|
|
||
| // update scope when set to primary | ||
| if p, ok := d.GetOk(authmethodIsPrimaryAuthMethodForScopeKey); ok { | ||
| if p.(bool) { | ||
| if err := updateScopeWithPrimaryAuthMethodId(ctx, scopeId, amid, meta); err != nil { | ||
| return diag.Errorf("%v", err) | ||
| } | ||
|
|
||
| amcr.GetResponse().Map[authmethodIsPrimaryAuthMethodForScopeKey] = true | ||
| } | ||
| } | ||
|
|
||
| return setFromPasswordAuthMethodResponseMap(d, amcr.GetResponse().Map) | ||
| } | ||
|
|
||
|
|
@@ -177,6 +200,13 @@ func resourceAuthMethodPasswordRead(ctx context.Context, d *schema.ResourceData, | |
| return diag.Errorf("auth method nil after read") | ||
| } | ||
|
|
||
| serr, isPrimary := readScopeIsPrimaryAuthMethodId(ctx, amrr.GetResponse().Map["scope_id"].(string), amrr.GetResponse().Map["id"].(string), meta) | ||
| if serr != nil { | ||
| return diag.Errorf("%v", serr) | ||
| } | ||
|
|
||
| amrr.GetResponse().Map[authmethodIsPrimaryAuthMethodForScopeKey] = isPrimary | ||
moduli marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return setFromPasswordAuthMethodResponseMap(d, amrr.GetResponse().Map) | ||
| } | ||
|
|
||
|
|
@@ -218,15 +248,47 @@ func resourceAuthMethodPasswordUpdate(ctx context.Context, d *schema.ResourceDat | |
| } | ||
| } | ||
|
|
||
| if d.HasChange(authmethodIsPrimaryAuthMethodForScopeKey) { | ||
| amrr, err := amClient.Read(ctx, d.Id()) | ||
| if err != nil { | ||
| return diag.Errorf("error updating auth method: %v", err) | ||
| } | ||
| if amrr == nil { | ||
| return diag.Errorf("error updating auth method: nil resource") | ||
| } | ||
| scopeId := amrr.GetResponse().Map["scope_id"].(string) | ||
| authMethodId := amrr.GetResponse().Map["id"].(string) | ||
|
|
||
| if d.Get(authmethodIsPrimaryAuthMethodForScopeKey).(bool) { | ||
| if err := updateScopeWithPrimaryAuthMethodId(ctx, scopeId, authMethodId, meta); err != nil { | ||
| return diag.Errorf("%v", err) | ||
| } | ||
| } else { | ||
|
||
| if err := updateScopeWithPrimaryAuthMethodId(ctx, scopeId, "", meta); err != nil { | ||
| return diag.Errorf("%v", err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if len(opts) > 0 { | ||
| opts = append(opts, authmethods.WithAutomaticVersioning(true)) | ||
| amur, err := amClient.Update(ctx, d.Id(), 0, opts...) | ||
| if err != nil { | ||
| return diag.Errorf("error updating auth method: %v", err) | ||
| } | ||
|
|
||
| if d.HasChange(authmethodIsPrimaryAuthMethodForScopeKey) { | ||
| amur.GetResponse().Map[authmethodIsPrimaryAuthMethodForScopeKey] = d.Get(authmethodIsPrimaryAuthMethodForScopeKey).(bool) | ||
| } | ||
|
|
||
| return setFromPasswordAuthMethodResponseMap(d, amur.GetResponse().Map) | ||
| } | ||
|
|
||
| // If only is_primary_for_scope changed | ||
| if d.HasChange(authmethodIsPrimaryAuthMethodForScopeKey) { | ||
| return resourceAuthMethodPasswordRead(ctx, d, meta) | ||
| } | ||
|
|
||
moduli marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return nil | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.