-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reimplement data source "mackerel_service_metric_names"
- Loading branch information
Showing
4 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
internal/provider/data_source_mackerel_service_metric_names.go
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,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)...) | ||
} |
27 changes: 27 additions & 0 deletions
27
internal/provider/data_source_mackerel_service_metric_names_test.go
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,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) | ||
} | ||
} |
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