-
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 mackerel_aws_integration data source
- Loading branch information
Showing
4 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
internal/provider/data_source_mackerel_aws_integration.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,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
25
internal/provider/data_source_mackerel_aws_integration_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,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) | ||
} | ||
} |
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