Skip to content

Commit

Permalink
lookup roles per user, rather than users per role
Browse files Browse the repository at this point in the history
  • Loading branch information
pablito-perez committed Oct 11, 2024
1 parent 802ab67 commit 61a6ee2
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 94 deletions.
55 changes: 21 additions & 34 deletions datadog/fwprovider/resource_datadog_user_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ var (
)

type userRoleResource struct {
Api *datadogV2.RolesApi
Auth context.Context
Api *datadogV2.RolesApi
Auth context.Context
Users *datadogV2.UsersApi
}

type UserRoleModel struct {
Expand All @@ -40,6 +41,7 @@ func (r *userRoleResource) Configure(_ context.Context, request resource.Configu
providerData := request.ProviderData.(*FrameworkProvider)
r.Api = providerData.DatadogApiInstances.GetRolesApiV2()
r.Auth = providerData.Auth
r.Users = providerData.DatadogApiInstances.GetUsersApiV2()
}

func (r *userRoleResource) Metadata(_ context.Context, request resource.MetadataRequest, response *resource.MetadataResponse) {
Expand Down Expand Up @@ -88,48 +90,33 @@ func (r *userRoleResource) Read(ctx context.Context, request resource.ReadReques
if response.Diagnostics.HasError() {
return
}
roleId := state.RoleId.ValueString()

pageSize := int64(100)
pageNumber := int64(0)

var roleUsers []datadogV2.User
for {
resp, httpResp, err := r.Api.ListRoleUsers(r.Auth, roleId, *datadogV2.NewListRoleUsersOptionalParameters().
WithPageSize(pageSize).
WithPageNumber(pageNumber))
if err != nil {
if httpResp != nil && httpResp.StatusCode == 404 {
// Role no longer exists, remove the mapping
response.State.RemoveResource(ctx)
return
}

response.Diagnostics.Append(utils.FrameworkErrorDiag(err, "error retrieving RoleUsers"))
return
}
if err := utils.CheckForUnparsed(resp); err != nil {
response.Diagnostics.AddError("response contains unparsedObject", err.Error())
// get User
userId := state.UserId.ValueString()
resp, httpResp, err := r.Users.GetUser(r.Auth, userId)
if err != nil {
if httpResp != nil && httpResp.StatusCode == 404 {
response.State.RemoveResource(ctx)
return
}

roleUsers = append(roleUsers, resp.GetData()...)
if len(resp.GetData()) < 100 {
break
}

pageNumber++
response.Diagnostics.Append(utils.FrameworkErrorDiag(err, "error retrieving RoleUsers"))
return
}
if err := utils.CheckForUnparsed(resp); err != nil {
response.Diagnostics.AddError("response contains unparsedObject", err.Error())
return
}

for _, user := range roleUsers {
if user.GetId() == state.UserId.ValueString() {
userId := user.GetId()
state.ID = types.StringValue(state.RoleId.ValueString() + ":" + userId)
state.UserId = types.StringValue(userId)
// check if User already has Role
for _, role := range resp.GetData().Relationships.GetRoles().Data {
if roleId := role.GetId(); roleId == state.RoleId.ValueString() {
state.ID = types.StringValue(roleId + ":" + userId)
return
}
}

// User doesn't have Role
response.State.RemoveResource(ctx)
}

Expand Down
2 changes: 1 addition & 1 deletion datadog/tests/cassettes/TestAccUserRoleBasic.freeze
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024-04-02T10:29:40.473628-04:00
2024-10-11T11:22:15.737939+02:00
Loading

0 comments on commit 61a6ee2

Please sign in to comment.