-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
grafana_oncall_user_notification_rule
: Remove hardcoded user in test (
#1671) * `grafana_oncall_user_notification_rule`: Remove hardcoded user in test To do so, I added a new datasource that lists the oncall users. We can use that datasource to grab the first user and use them for tests I also converted the current `_user` resource to the new plugin framework while I was at it * Generate docs again
- Loading branch information
1 parent
6beb516
commit 07d2b91
Showing
6 changed files
with
220 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "grafana_oncall_users Data Source - terraform-provider-grafana" | ||
subcategory: "OnCall" | ||
description: |- | ||
HTTP API https://grafana.com/docs/oncall/latest/oncall-api-reference/users/ | ||
--- | ||
|
||
# grafana_oncall_users (Data Source) | ||
|
||
* [HTTP API](https://grafana.com/docs/oncall/latest/oncall-api-reference/users/) | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
- `users` (List of Object) (see [below for nested schema](#nestedatt--users)) | ||
|
||
<a id="nestedatt--users"></a> | ||
### Nested Schema for `users` | ||
|
||
Read-Only: | ||
|
||
- `email` (String) | ||
- `id` (String) | ||
- `role` (String) | ||
- `username` (String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package oncall | ||
|
||
import ( | ||
"context" | ||
|
||
onCallAPI "github.com/grafana/amixr-api-go-client" | ||
"github.com/grafana/terraform-provider-grafana/v3/internal/common" | ||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/hashicorp/terraform-plugin-framework/types/basetypes" | ||
) | ||
|
||
var dataSourceUsersName = "grafana_oncall_users" | ||
|
||
func dataSourceUsers() *common.DataSource { | ||
return common.NewDataSource(common.CategoryOnCall, dataSourceUsersName, &usersDataSource{}) | ||
} | ||
|
||
type usersDataSource struct { | ||
basePluginFrameworkDataSource | ||
} | ||
|
||
func (r *usersDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = dataSourceUsersName | ||
} | ||
|
||
func (r *usersDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
MarkdownDescription: "* [HTTP API](https://grafana.com/docs/oncall/latest/oncall-api-reference/users/)", | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"users": schema.ListAttribute{ | ||
ElementType: types.ObjectType{ | ||
AttrTypes: map[string]attr.Type{ | ||
"id": types.StringType, | ||
"username": types.StringType, | ||
"email": types.StringType, | ||
"role": types.StringType, | ||
}, | ||
}, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
type userDataSourceModel struct { | ||
ID basetypes.StringValue `tfsdk:"id"` | ||
Username basetypes.StringValue `tfsdk:"username"` | ||
Email basetypes.StringValue `tfsdk:"email"` | ||
Role basetypes.StringValue `tfsdk:"role"` | ||
} | ||
|
||
type usersDataSourceModel struct { | ||
ID basetypes.StringValue `tfsdk:"id"` | ||
Users []userDataSourceModel `tfsdk:"users"` | ||
} | ||
|
||
func (r *usersDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
// Read Terraform state data into the model | ||
var data usersDataSourceModel | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) | ||
|
||
allUsers := []userDataSourceModel{} | ||
page := 1 | ||
for { | ||
options := &onCallAPI.ListUserOptions{ | ||
ListOptions: onCallAPI.ListOptions{ | ||
Page: page, | ||
}, | ||
} | ||
usersResponse, _, err := r.client.Users.ListUsers(options) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Failed to list users", err.Error()) | ||
return | ||
} | ||
|
||
for _, user := range usersResponse.Users { | ||
allUsers = append(allUsers, userDataSourceModel{ | ||
ID: basetypes.NewStringValue(user.ID), | ||
Username: basetypes.NewStringValue(user.Username), | ||
Email: basetypes.NewStringValue(user.Email), | ||
Role: basetypes.NewStringValue(user.Role), | ||
}) | ||
} | ||
|
||
if usersResponse.PaginatedResponse.Next == nil { | ||
break | ||
} | ||
} | ||
|
||
data.ID = basetypes.NewStringValue("oncall_users") // singleton | ||
data.Users = allUsers | ||
|
||
// Save data into Terraform state | ||
resp.Diagnostics.Append(resp.State.Set(ctx, data)...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters