Skip to content

Commit

Permalink
feat: reimplement mackerel_aws_integration data source
Browse files Browse the repository at this point in the history
  • Loading branch information
tosuke committed Oct 31, 2024
1 parent 27994f0 commit 97b413d
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 0 deletions.
138 changes: 138 additions & 0 deletions internal/provider/data_source_mackerel_aws_integration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package provider

import (
"context"

"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/mackerelio-labs/terraform-provider-mackerel/internal/mackerel"
)

var (
_ datasource.DataSource = (*mackerelAWSIntegrationDataSource)(nil)
_ datasource.DataSourceWithConfigure = (*mackerelAWSIntegrationDataSource)(nil)
)

type mackerelAWSIntegrationDataSource struct {
Client *mackerel.Client
}

func NewMackerelAWSIntegrationDataSource() datasource.DataSource {
return &mackerelAWSIntegrationDataSource{}
}

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

func (d *mackerelAWSIntegrationDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schemaAWSIntegrationDataSource()
}

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

data, err := mackerel.ReadAWSIntegration(ctx, d.Client, config.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Unable to read an AWS integration",
err.Error(),
)
return
}
dataSourceModel := mackerel.AWSIntegrationDataSourceModel(*data)

resp.Diagnostics.Append(resp.State.Set(ctx, &dataSourceModel)...)
}

func schemaAWSIntegrationDataSource() schema.Schema {
attrs := map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: schemaAWSIntegrationIDDesc,
Required: true,
},
"name": schema.StringAttribute{
Description: schemaAWSIntegrationNameDesc,
Computed: true,
},
"memo": schema.StringAttribute{
Description: schemaAWSIntegrationMemoDesc,
Computed: true,
},
"key": schema.StringAttribute{
Description: schemaAWSIntegrationKeyDesc,
Computed: true,
},
"role_arn": schema.StringAttribute{
Description: schemaAWSIntegrationRoleARNDesc,
Computed: true,
},
"external_id": schema.StringAttribute{
Description: schemaAWSIntegrationExternalIDDesc,
Computed: true,
},
"region": schema.StringAttribute{
Description: schemaAWSIntegrationRegionDesc,
Computed: true,
},
"included_tags": schema.StringAttribute{
Description: schemaAWSIntegrationIncludedTagsDesc,
Computed: true,
},
"excluded_tags": schema.StringAttribute{
Description: schemaAWSIntegrationExcludedTagsDesc,
Computed: true,
},
}

for name, spec := range awsIntegrationServices {
if spec.supportsAutoRetire {
attrs[name] = schema.ListAttribute{
Computed: true,
ElementType: types.ObjectType{
AttrTypes: map[string]attr.Type{
"enable": types.BoolType,
"role": types.StringType,
"excluded_metrics": types.ListType{
ElemType: types.StringType,
},
"retire_automatically": types.BoolType,
},
},
}
} else {
attrs[name] = schema.ListAttribute{
Computed: true,
ElementType: types.ObjectType{
AttrTypes: map[string]attr.Type{
"enable": types.BoolType,
"role": types.StringType,
"excluded_metrics": types.ListType{
ElemType: types.StringType,
},
},
},
}
}
}

s := schema.Schema{
Attributes: attrs,
}
return s
}
25 changes: 25 additions & 0 deletions internal/provider/data_source_mackerel_aws_integration_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_MackerelAWSIntegrationDataSource_schema(t *testing.T) {
t.Parallel()

ctx := context.Background()

req := fwdatasource.SchemaRequest{}
resp := fwdatasource.SchemaResponse{}
if provider.NewMackerelAWSIntegrationDataSource().Schema(ctx, req, &resp); 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 @@ -100,6 +100,7 @@ func (m *mackerelProvider) Resources(context.Context) []func() resource.Resource
func (m *mackerelProvider) DataSources(context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
NewMackerelAlertGroupSettingDataSource,
NewMackerelAWSIntegrationDataSource,
NewMackerelChannelDataSource,
NewMackerelDashboardDataSource,
NewMackerelDowntimeDataSource,
Expand Down
1 change: 1 addition & 0 deletions mackerel/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func protoV5ProviderServer(provider *schema.Provider) tfprotov5.ProviderServer {

// Data Sources
delete(provider.DataSourcesMap, "mackerel_alert_group_setting")
delete(provider.DataSourcesMap, "mackerel_aws_integration")
delete(provider.DataSourcesMap, "mackerel_channel")
delete(provider.DataSourcesMap, "mackerel_dashboard")
delete(provider.DataSourcesMap, "mackerel_downtime")
Expand Down

0 comments on commit 97b413d

Please sign in to comment.