-
Notifications
You must be signed in to change notification settings - Fork 333
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
Fix identity provider update and service account role management #171
Changes from 18 commits
c4e3038
09522c1
3a42bf5
0878dfe
6927624
454a4f7
4fac288
68f628f
68c6818
c7a9f43
f76fe74
ff749d4
8d7b078
c61ec46
b3fd03b
327e7ba
c69b1ff
5691d3f
3a362d9
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 | ||||
---|---|---|---|---|---|---|
|
@@ -40,18 +40,32 @@ func resourceKeycloakOpenidClientServiceAccountRole() *schema.Resource { | |||||
} | ||||||
} | ||||||
|
||||||
func getOpenidClientServiceAccountRoleFromData(data *schema.ResourceData) *keycloak.OpenidClientServiceAccountRole { | ||||||
return &keycloak.OpenidClientServiceAccountRole{ | ||||||
Id: data.Id(), | ||||||
ContainerId: data.Get("client_id").(string), | ||||||
Name: data.Get("role").(string), | ||||||
RealmId: data.Get("realm_id").(string), | ||||||
ServiceAccountUserId: data.Get("service_account_user_id").(string), | ||||||
func getOpenidClientServiceAccountRoleFromData(data *schema.ResourceData, keycloakClient *keycloak.KeycloakClient) (*keycloak.OpenidClientServiceAccountRole, error) { | ||||||
containerId := data.Get("client_id").(string) | ||||||
roleName := data.Get("role").(string) | ||||||
realmId := data.Get("realm_id").(string) | ||||||
serviceAccountRoleId := data.Get("service_account_user_id").(string) | ||||||
|
||||||
role, err := keycloakClient.GetRoleByName(realmId, containerId, roleName) | ||||||
if err != nil { | ||||||
if handleNotFoundError(err, data) == nil { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, your logic makes sense, thanks for the explanation. I think, however, that all you really want to do is check if the error is a 404 and not necessarily remove the resource from state.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mrparkers just updated |
||||||
role = &keycloak.Role{Id: ""} | ||||||
} else { | ||||||
return nil, err | ||||||
} | ||||||
} | ||||||
|
||||||
return &keycloak.OpenidClientServiceAccountRole{ | ||||||
Id: role.Id, | ||||||
ContainerId: containerId, | ||||||
Name: roleName, | ||||||
RealmId: realmId, | ||||||
ServiceAccountUserId: serviceAccountRoleId, | ||||||
}, nil | ||||||
} | ||||||
|
||||||
func setOpenidClientServiceAccountRoleData(data *schema.ResourceData, serviceAccountRole *keycloak.OpenidClientServiceAccountRole) { | ||||||
data.SetId(serviceAccountRole.Id) | ||||||
data.SetId(fmt.Sprintf("%s/%s", serviceAccountRole.ServiceAccountUserId, serviceAccountRole.Id)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be a breaking change since the ID for this resource is being changed. Can you verify that these resources aren't being recreated because of this change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just new resources will be created. In context of service account roles it'll try to apply existing changes once to add service account roles with ned IDs but POST action to service account roles endpoint always returns 204 even if role was already added |
||||||
data.Set("realm_id", serviceAccountRole.RealmId) | ||||||
data.Set("client_id", serviceAccountRole.ContainerId) | ||||||
data.Set("service_account_user_id", serviceAccountRole.ServiceAccountUserId) | ||||||
|
@@ -60,8 +74,12 @@ func setOpenidClientServiceAccountRoleData(data *schema.ResourceData, serviceAcc | |||||
|
||||||
func resourceKeycloakOpenidClientServiceAccountRoleCreate(data *schema.ResourceData, meta interface{}) error { | ||||||
keycloakClient := meta.(*keycloak.KeycloakClient) | ||||||
serviceAccountRole := getOpenidClientServiceAccountRoleFromData(data) | ||||||
err := keycloakClient.NewOpenidClientServiceAccountRole(serviceAccountRole) | ||||||
serviceAccountRole, err := getOpenidClientServiceAccountRoleFromData(data, keycloakClient) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
err = keycloakClient.NewOpenidClientServiceAccountRole(serviceAccountRole) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
@@ -72,12 +90,12 @@ func resourceKeycloakOpenidClientServiceAccountRoleCreate(data *schema.ResourceD | |||||
func resourceKeycloakOpenidClientServiceAccountRoleRead(data *schema.ResourceData, meta interface{}) error { | ||||||
keycloakClient := meta.(*keycloak.KeycloakClient) | ||||||
|
||||||
realmId := data.Get("realm_id").(string) | ||||||
serviceAccountUserId := data.Get("service_account_user_id").(string) | ||||||
clientId := data.Get("client_id").(string) | ||||||
id := data.Id() | ||||||
serviceAccountRole, err := getOpenidClientServiceAccountRoleFromData(data, keycloakClient) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
serviceAccountRole, err := keycloakClient.GetOpenidClientServiceAccountRole(realmId, serviceAccountUserId, clientId, id) | ||||||
serviceAccountRole, err = keycloakClient.GetOpenidClientServiceAccountRole(serviceAccountRole.RealmId, serviceAccountRole.ServiceAccountUserId, serviceAccountRole.ContainerId, serviceAccountRole.Id) | ||||||
if err != nil { | ||||||
return handleNotFoundError(err, data) | ||||||
} | ||||||
|
@@ -90,23 +108,27 @@ func resourceKeycloakOpenidClientServiceAccountRoleRead(data *schema.ResourceDat | |||||
func resourceKeycloakOpenidClientServiceAccountRoleDelete(data *schema.ResourceData, meta interface{}) error { | ||||||
keycloakClient := meta.(*keycloak.KeycloakClient) | ||||||
|
||||||
realmId := data.Get("realm_id").(string) | ||||||
serviceAccountUserId := data.Get("service_account_user_id").(string) | ||||||
clientId := data.Get("client_id").(string) | ||||||
id := data.Id() | ||||||
serviceAccountRole, err := getOpenidClientServiceAccountRoleFromData(data, keycloakClient) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
return keycloakClient.DeleteOpenidClientServiceAccountRole(realmId, serviceAccountUserId, clientId, id) | ||||||
err = keycloakClient.DeleteOpenidClientServiceAccountRole(serviceAccountRole.RealmId, serviceAccountRole.ServiceAccountUserId, serviceAccountRole.ContainerId, serviceAccountRole.Id) | ||||||
if err != nil { | ||||||
return handleNotFoundError(err, data) | ||||||
} | ||||||
return nil | ||||||
} | ||||||
|
||||||
func resourceKeycloakOpenidClientServiceAccountRoleImport(d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData, error) { | ||||||
parts := strings.Split(d.Id(), "/") | ||||||
if len(parts) != 3 { | ||||||
return nil, fmt.Errorf("Invalid import. Supported import formats: {{realmId}}/{{serviceAccountUserId}}/{{clientId}}/{{role}}") | ||||||
return nil, fmt.Errorf("Invalid import. Supported import formats: {{realmId}}/{{serviceAccountUserId}}/{{clientId}}/{{roleId}}") | ||||||
} | ||||||
d.Set("realm_id", parts[0]) | ||||||
d.Set("service_account_user_id", parts[1]) | ||||||
d.Set("client_id", parts[2]) | ||||||
d.SetId(parts[3]) | ||||||
d.SetId(fmt.Sprintf("%s/%s", parts[1], parts[3])) | ||||||
|
||||||
return []*schema.ResourceData{d}, nil | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is going to call
data.SetId("")
if the error is s 404, which removes this resource from state. Is this what you want?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, cause if role doesn't exist yet it cannot be attached/removed to/from a client.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a get request before delete action
https://github.com/mrparkers/terraform-provider-keycloak/blob/master/keycloak/openid_client_service_account_role.go#L33
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I mean is that the
keycloak_openid_client_service_account_role
resource will be removed from state if theGetRoleByName
call fails with a 404. That seems problematic since ultimately this function won't return an error and the creation will appear to be successful.If the
keycloak_openid_client_service_account_role
resource isn't valid without an existing role, shouldn't create just fail?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When terraform plans resources creation it uses empty values for attributes of resource which is not yet created. Don't know how it should work, but at least have noticed such a behaviour for this provider. It'll cause errors during every plan when resources are not created yet. Without this check tests are always failing
https://circleci.com/gh/mrparkers/terraform-provider-keycloak/1152?utm_campaign=vcs-integration-link&utm_medium=referral&utm_source=github-build-link