Skip to content

Commit

Permalink
feat: reimplement "mackerel_role_metadata" data source
Browse files Browse the repository at this point in the history
  • Loading branch information
tosuke committed Jul 11, 2024
1 parent 5f8290e commit c33da35
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
89 changes: 89 additions & 0 deletions internal/provider/data_source_mackerel_role_metadata.go
Original file line number Diff line number Diff line change
@@ -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)...)
}
25 changes: 25 additions & 0 deletions internal/provider/data_source_mackerel_role_metadata_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions mackerel/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit c33da35

Please sign in to comment.