Skip to content

Commit

Permalink
feat: reimplement data source "mackerel_service_metric_names"
Browse files Browse the repository at this point in the history
  • Loading branch information
tosuke committed Jun 20, 2024
1 parent 9c865dc commit 9fd62f1
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
85 changes: 85 additions & 0 deletions internal/provider/data_source_mackerel_service_metric_names.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package provider

import (
"context"
"fmt"

"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/hashicorp/terraform-plugin-framework/types"
"github.com/mackerelio-labs/terraform-provider-mackerel/internal/mackerel"
)

var (
_ datasource.DataSourceWithConfigure = (*mackerelServiceMetricNamesDataSource)(nil)
)

func NewMackerelServiceMetricNamesDataSource() datasource.DataSource {
return &mackerelServiceMetricNamesDataSource{}
}

type mackerelServiceMetricNamesDataSource struct {
Client *mackerel.Client
}

func (_ *mackerelServiceMetricNamesDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_service_metric_names"
}

func (_ *mackerelServiceMetricNamesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "This data source allows access to details of a specific Service metric names.",

Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
},
"name": schema.StringAttribute{
Description: "The name of the service.",

Required: true,
Validators: []validator.String{mackerel.ServiceNameValidator()},
},
"prefix": schema.StringAttribute{
Description: "Prefix of the metric names.",

Optional: true,
},
"metric_names": schema.SetAttribute{
Description: "Set of the service metric names.",

ElementType: types.StringType,
Computed: true,
},
},
}
}

func (d *mackerelServiceMetricNamesDataSource) 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 *mackerelServiceMetricNamesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var config mackerel.ServiceMetricNamesModel
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
if resp.Diagnostics.HasError() {
return
}

data, err := mackerel.ReadServiceMetricNames(ctx, d.Client, config)
if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Unable to read Service Metric Names from Service: %s", config.Name.ValueString()),
err.Error(),
)
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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_MackerelServiceMetricNamesDataSource_schema(t *testing.T) {
t.Parallel()

ctx := context.Background()

req := fwdatasource.SchemaRequest{}
resp := &fwdatasource.SchemaResponse{}
provider.NewMackerelServiceMetricNamesDataSource().Schema(ctx, req, resp)
if resp.Diagnostics.HasError() {
t.Errorf("schema method: %+v", resp.Diagnostics)
return
}

if diags := resp.Schema.ValidateImplementation(ctx); diags.HasError() {
t.Errorf("schema validation: %+v", diags)
}
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func (m *mackerelProvider) Resources(context.Context) []func() resource.Resource
func (m *mackerelProvider) DataSources(context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
NewMackerelServiceDataSource,
NewMackerelServiceMetricNamesDataSource,
}
}

Expand Down
1 change: 1 addition & 0 deletions mackerel/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func protoV5ProviderServer(provider *schema.Provider) tfprotov5.ProviderServer {

// Data Sources
delete(provider.DataSourcesMap, "mackerel_service")
delete(provider.DataSourcesMap, "mackerel_service_metric_names")

mux, err := tf5muxserver.NewMuxServer(
context.Background(),
Expand Down

0 comments on commit 9fd62f1

Please sign in to comment.