Skip to content
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

[data_source_datadog_team_memberships] add exact match support #2118

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions datadog/fwprovider/data_source_datadog_team_memberships.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type datadogTeamMembershipsDataSourceModel struct {
// Query Parameters
TeamID types.String `tfsdk:"team_id"`
FilterKeyword types.String `tfsdk:"filter_keyword"`
ExactMatch types.Bool `tfsdk:"exact_match"`
// Results
ID types.String `tfsdk:"id"`
TeamMemberships []*TeamMembershipModel `tfsdk:"team_memberships"`
Expand All @@ -31,13 +32,15 @@ func NewDatadogTeamMembershipsDataSource() datasource.DataSource {
}

type datadogTeamMembershipsDataSource struct {
Api *datadogV2.TeamsApi
Auth context.Context
Api *datadogV2.TeamsApi
UsersApi *datadogV2.UsersApi
Auth context.Context
}

func (r *datadogTeamMembershipsDataSource) Configure(_ context.Context, request datasource.ConfigureRequest, response *datasource.ConfigureResponse) {
providerData, _ := request.ProviderData.(*FrameworkProvider)
r.Api = providerData.DatadogApiInstances.GetTeamsApiV2()
r.UsersApi = providerData.DatadogApiInstances.GetUsersApiV2()
r.Auth = providerData.Auth
}

Expand All @@ -60,6 +63,10 @@ func (d *datadogTeamMembershipsDataSource) Schema(_ context.Context, _ datasourc
Description: "Search query, can be user email or name.",
Optional: true,
},
"exact_match": schema.BoolAttribute{
Description: "When true, `filter_keyword` string is exact matched against the user's `email`, followed by `name`.",
Optional: true,
},
// Computed values
"team_memberships": schema.ListAttribute{
Computed: true,
Expand Down Expand Up @@ -121,16 +128,35 @@ func (d *datadogTeamMembershipsDataSource) Read(ctx context.Context, req datasou
func (r *datadogTeamMembershipsDataSource) updateState(state *datadogTeamMembershipsDataSourceModel, teamData *[]datadogV2.UserTeam) {
state.ID = types.StringValue(fmt.Sprintf("%s:%s", state.TeamID.ValueString(), state.FilterKeyword.ValueString()))

exactMatch := state.ExactMatch.ValueBool()
filterKeywod := state.FilterKeyword.ValueString()
var teamMemberships []*TeamMembershipModel
for _, user := range *teamData {
membership := TeamMembershipModel{
ID: types.StringValue(user.GetId()),
TeamId: types.StringValue(state.TeamID.ValueString()),
UserId: types.StringValue(user.Relationships.User.Data.GetId()),
Role: types.StringValue(string(user.Attributes.GetRole())),
if exactMatch {
if u, _, err := r.UsersApi.GetUser(r.Auth, user.Relationships.User.Data.GetId()); err == nil {
attributes := u.Data.GetAttributes()
if attributes.GetEmail() == filterKeywod || attributes.GetName() == filterKeywod {
membership := TeamMembershipModel{
ID: types.StringValue(user.GetId()),
TeamId: types.StringValue(state.TeamID.ValueString()),
UserId: types.StringValue(user.Relationships.User.Data.GetId()),
Role: types.StringValue(string(user.Attributes.GetRole())),
}

teamMemberships = append(teamMemberships, &membership)
}
}
} else {
membership := TeamMembershipModel{
ID: types.StringValue(user.GetId()),
TeamId: types.StringValue(state.TeamID.ValueString()),
UserId: types.StringValue(user.Relationships.User.Data.GetId()),
Role: types.StringValue(string(user.Attributes.GetRole())),
}

teamMemberships = append(teamMemberships, &membership)
}

teamMemberships = append(teamMemberships, &membership)
}

state.TeamMemberships = teamMemberships
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/team_memberships.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ data "datadog_team_memberships" "foo" {

### Optional

- `exact_match` (Boolean) When true, `filter_keyword` string is exact matched against the user's `email`, followed by `name`.
- `filter_keyword` (String) Search query, can be user email or name.

### Read-Only
Expand Down