diff --git a/internal/provider/data_source_mackerel_role_metadata.go b/internal/provider/data_source_mackerel_role_metadata.go new file mode 100644 index 0000000..b57934e --- /dev/null +++ b/internal/provider/data_source_mackerel_role_metadata.go @@ -0,0 +1,89 @@ +package provider + +import ( + "context" + "fmt" + + "github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes" + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/schema/validator" + "github.com/mackerelio-labs/terraform-provider-mackerel/internal/mackerel" +) + +var ( + _ datasource.DataSourceWithConfigure = (*mackerelRoleMetadataDataSource)(nil) +) + +func NewMackerelRoleMetadataDataSource() datasource.DataSource { + return &mackerelRoleMetadataDataSource{} +} + +type mackerelRoleMetadataDataSource struct { + Client *mackerel.Client +} + +func (d *mackerelRoleMetadataDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_role_metadata" +} + +func (d *mackerelRoleMetadataDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = schema.Schema{ + Description: "This data source accesses to details of a specific Role Metadata.", + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + Computed: true, + }, + "service": schema.StringAttribute{ + Description: "The name of the service.", + Required: true, + Validators: []validator.String{mackerel.ServiceNameValidator()}, + }, + "role": schema.StringAttribute{ + Description: "The name of the role.", + Required: true, + Validators: []validator.String{mackerel.RoleNameValidator()}, + }, + "namespace": schema.StringAttribute{ + Description: "The identifier for the metadata.", + Required: true, + }, + "metadata_json": schema.StringAttribute{ + Description: "Arbitrary JSON data for the role.", + Computed: true, + CustomType: jsontypes.NormalizedType{}, + }, + }, + } +} + +func (d *mackerelRoleMetadataDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { + client, diags := retrieveClient(ctx, req.ProviderData) + resp.Diagnostics.Append(diags...) + if diags.HasError() { + return + } + d.Client = client +} + +func (d *mackerelRoleMetadataDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var config mackerel.RoleMetadataModel + resp.Diagnostics.Append(req.Config.Get(ctx, &config)...) + if resp.Diagnostics.HasError() { + return + } + + serviceName := config.ServiceName.ValueString() + roleName := config.RoleName.ValueString() + namespace := config.Namespace.ValueString() + data, err := mackerel.ReadRoleMetadata(ctx, d.Client, serviceName, roleName, namespace) + if err != nil { + resp.Diagnostics.AddError( + fmt.Sprintf("Unable to read Role Metadata: service=%s role=%s namespace=%s", serviceName, roleName, namespace), + err.Error(), + ) + return + } + + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) +} diff --git a/internal/provider/data_source_mackerel_role_metadata_test.go b/internal/provider/data_source_mackerel_role_metadata_test.go new file mode 100644 index 0000000..8c3aee7 --- /dev/null +++ b/internal/provider/data_source_mackerel_role_metadata_test.go @@ -0,0 +1,25 @@ +package provider_test + +import ( + "context" + "testing" + + fwdatasource "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/mackerelio-labs/terraform-provider-mackerel/internal/provider" +) + +func Test_MackerelRoleMetadataDataSource_schema(t *testing.T) { + t.Parallel() + + ctx := context.Background() + req := fwdatasource.SchemaRequest{} + resp := fwdatasource.SchemaResponse{} + provider.NewMackerelRoleMetadataDataSource().Schema(ctx, req, &resp) + if resp.Diagnostics.HasError() { + t.Fatalf("schema diagnostics: %+v", resp.Diagnostics) + } + + if diags := resp.Schema.ValidateImplementation(ctx); diags.HasError() { + t.Fatalf("schema validation diagnostics: %+v", diags) + } +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 0ba4bc1..55d61c7 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -92,6 +92,7 @@ func (m *mackerelProvider) Resources(context.Context) []func() resource.Resource func (m *mackerelProvider) DataSources(context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ NewMackerelNotificationGroupDataSource, + NewMackerelRoleMetadataDataSource, NewMackerelServiceDataSource, NewMackerelServiceMetadataDataSource, NewMackerelServiceMetricNamesDataSource, diff --git a/mackerel/provider.go b/mackerel/provider.go index 0abc2b5..0b74c95 100644 --- a/mackerel/provider.go +++ b/mackerel/provider.go @@ -88,6 +88,7 @@ func protoV5ProviderServer(provider *schema.Provider) tfprotov5.ProviderServer { // Data Sources delete(provider.DataSourcesMap, "mackerel_notification_group") + delete(provider.DataSourcesMap, "mackerel_role_metadata") delete(provider.DataSourcesMap, "mackerel_service") delete(provider.DataSourcesMap, "mackerel_service_metadata") delete(provider.DataSourcesMap, "mackerel_service_metric_names")