Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplement mackerel_aws_integration resource with terraform-plugin-framework #246

Merged
merged 5 commits into from
Oct 31, 2024

Conversation

tosuke
Copy link
Contributor

@tosuke tosuke commented Oct 29, 2024

Part of #243

Output from acceptance testing:

$ MACKEREL_EXPERIMENTAL_TFFRAMEWORK=1 make testacc TESTS='"AccMackerelAWSIntegration"'
TF_ACC=1 go test -v ./mackerel/... -run "AccMackerelAWSIntegration" -timeout 120m
2024/10/29 16:17:00 [INFO] mackerel: use terraform-plugin-framework based implementation
=== RUN   TestAccMackerelAWSIntegrationIAMRole
=== PAUSE TestAccMackerelAWSIntegrationIAMRole
=== RUN   TestAccMackerelAWSIntegrationCredentials
=== PAUSE TestAccMackerelAWSIntegrationCredentials
=== CONT  TestAccMackerelAWSIntegrationIAMRole
=== CONT  TestAccMackerelAWSIntegrationCredentials
--- PASS: TestAccMackerelAWSIntegrationCredentials (6.27s)
--- PASS: TestAccMackerelAWSIntegrationIAMRole (6.62s)
PASS
ok      github.com/mackerelio-labs/terraform-provider-mackerel/mackerel 7.743s

@@ -303,7 +303,6 @@ resource "mackerel_aws_integration" "foo" {

ec2 {
enable = true
role = ""
Copy link
Contributor Author

@tosuke tosuke Oct 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The role field accepts a role ID($service_name: $role_name) or null. Empty string is some kind of pathological case and is not supported currently.

Comment on lines +177 to +232
serviceSchema := schema.SetNestedBlock{
Description: schemaAWSIntegrationServiceDesc,
Validators: []validator.Set{
setvalidator.SizeAtMost(1),
},
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"enable": schema.BoolAttribute{
Description: schemaAWSIntegrationServiceEnableDesc,
Optional: true,
Computed: true,
Default: booldefault.StaticBool(true),
},
"role": schema.StringAttribute{
Description: schemaAWSIntegrationServiceRoleDesc,
Optional: true,
},
"excluded_metrics": schema.ListAttribute{
Description: schemaAWSIntegrationServiceExcludedMetricsDesc,
ElementType: types.StringType,
Optional: true,
},
},
},
}
serviceSchemaWithRetireAutomatically := schema.SetNestedBlock{
Description: schemaAWSIntegrationServiceDesc,
Validators: []validator.Set{
setvalidator.SizeAtMost(1),
},
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"enable": schema.BoolAttribute{
Description: schemaAWSIntegrationServiceEnableDesc,
Optional: true,
Computed: true,
Default: booldefault.StaticBool(true),
},
"role": schema.StringAttribute{
Description: schemaAWSIntegrationServiceRoleDesc,
Optional: true,
},
"excluded_metrics": schema.ListAttribute{
Description: schemaAWSIntegrationServiceExcludedMetricsDesc,
ElementType: types.StringType,
Optional: true,
},
"retire_automatically": schema.BoolAttribute{
Description: schemaAWSIntegrationServiceRetireAutomaticallyDesc,
Optional: true,
Computed: true,
Default: booldefault.StaticBool(false),
},
},
},
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original schema:

var awsIntegrationServiceResourceWithRetireAutomatically = &schema.Resource{
Schema: map[string]*schema.Schema{
"enable": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"role": {
Type: schema.TypeString,
Optional: true,
},
"excluded_metrics": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"retire_automatically": {
Type: schema.TypeBool,
Optional: true,
},
},
}
var awsIntegrationServiceSchemaWithRetireAutomatically = &schema.Schema{
Type: schema.TypeSet,
Optional: true,
MaxItems: 1,
Elem: awsIntegrationServiceResourceWithRetireAutomatically,
}
var awsIntegrationServiceResource = &schema.Resource{
Schema: map[string]*schema.Schema{
"enable": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"role": {
Type: schema.TypeString,
Optional: true,
},
"excluded_metrics": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
var awsIntegrationServiceSchema = &schema.Schema{
Type: schema.TypeSet,
Optional: true,
MaxItems: 1,
Elem: awsIntegrationServiceResource,
}

Comment on lines +247 to +321
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: schemaAWSIntegrationIDDesc,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(), // immutable
},
},
"name": schema.StringAttribute{
Description: schemaAWSIntegrationNameDesc,
Required: true,
},
"memo": schema.StringAttribute{
Description: schemaAWSIntegrationMemoDesc,
Optional: true,
Computed: true,
Default: stringdefault.StaticString(""),
},
"key": schema.StringAttribute{
Description: schemaAWSIntegrationKeyDesc,
Optional: true,
Sensitive: true,
Computed: true,
Default: stringdefault.StaticString(""),
Validators: []validator.String{
// With Access Key, secret access key is need too.
stringvalidator.AlsoRequires(path.MatchRoot("secret_key")),
},
},
"secret_key": schema.StringAttribute{
Description: schemaAWSIntegrationSecretKeyDesc,
Optional: true,
Sensitive: true,
Computed: true,
Default: stringdefault.StaticString(""),
Validators: []validator.String{
// Secret access key cannot be set alone
stringvalidator.AlsoRequires(path.MatchRoot("key")),
},
},
"role_arn": schema.StringAttribute{
Description: schemaAWSIntegrationRoleARNDesc,
Optional: true,
Computed: true,
Default: stringdefault.StaticString(""),
},
"external_id": schema.StringAttribute{
Description: schemaAWSIntegrationExternalIDDesc,
Optional: true,
Computed: true,
Default: stringdefault.StaticString(""),
Validators: []validator.String{
// External ID cannot be set alone
stringvalidator.AlsoRequires(path.MatchRoot("role_arn")),
},
},
"region": schema.StringAttribute{
Description: schemaAWSIntegrationRegionDesc,
Optional: true,
Computed: true,
Default: stringdefault.StaticString(""),
},
"included_tags": schema.StringAttribute{
Description: schemaAWSIntegrationIncludedTagsDesc,
Optional: true,
Computed: true,
Default: stringdefault.StaticString(""),
},
"excluded_tags": schema.StringAttribute{
Description: schemaAWSIntegrationExcludedTagsDesc,
Optional: true,
Computed: true,
Default: stringdefault.StaticString(""),
},
},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original schema:

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"memo": {
Type: schema.TypeString,
Optional: true,
},
"key": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
},
"secret_key": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
},
"role_arn": {
Type: schema.TypeString,
Optional: true,
},
"external_id": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
},
"region": {
Type: schema.TypeString,
Optional: true,
},
"included_tags": {
Type: schema.TypeString,
Optional: true,
},
"excluded_tags": {
Type: schema.TypeString,
Optional: true,
},
},

Copy link
Member

@ne-sachirou ne-sachirou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙆‍♀️

@tosuke tosuke merged commit 0f86842 into main Oct 31, 2024
1 check passed
@tosuke tosuke deleted the framework-aws-integration-resource branch October 31, 2024 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants