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

Implement service resource and data source in the framework provider #214

Merged
merged 12 commits into from
Jun 20, 2024

Conversation

tosuke
Copy link
Contributor

@tosuke tosuke commented Jun 6, 2024

Output from acceptance testing:

$ MACKEREL_EXPERIMENTAL_TFFRAMEWORK=1 make testacc TESTS='"^TestAcc(DataSourceMackerelService(NotMatchAnyService)?|MackerelService)$$"'
TF_ACC=1 go test -v ./mackerel/... -run "^TestAcc(DataSourceMackerelService(NotMatchAnyService)?|MackerelService)$" -timeout 120m
2024/06/12 11:54:56 [INFO] mackerel: use terraform-plugin-framework based implementation
=== RUN   TestAccDataSourceMackerelService
=== PAUSE TestAccDataSourceMackerelService
=== RUN   TestAccDataSourceMackerelServiceNotMatchAnyService
=== PAUSE TestAccDataSourceMackerelServiceNotMatchAnyService
=== RUN   TestAccMackerelService
=== PAUSE TestAccMackerelService
=== CONT  TestAccDataSourceMackerelService
=== CONT  TestAccMackerelService
=== CONT  TestAccDataSourceMackerelServiceNotMatchAnyService
--- PASS: TestAccDataSourceMackerelServiceNotMatchAnyService (2.47s)
--- PASS: TestAccDataSourceMackerelService (5.17s)
--- PASS: TestAccMackerelService (8.64s)
PASS
ok      github.com/mackerelio-labs/terraform-provider-mackerel/mackerel 9.407s

@tosuke tosuke force-pushed the framework-mackerel-service branch from 2aff711 to d8ea5e9 Compare June 6, 2024 09:24
@tosuke tosuke force-pushed the framework-mackerel-service branch from d8ea5e9 to 4b2f20f Compare June 12, 2024 02:55
Copy link
Member

@Arthur1 Arthur1 left a comment

Choose a reason for hiding this comment

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

The review is in process, but I have commented tentatively. I completed the review and made some minor suggestions.

Since we merged another pull request, could you fix conflict?

internal/validatorutil/url_test.go Outdated Show resolved Hide resolved
func ServiceNameValidator() validator.String {
return stringvalidator.All(
stringvalidator.LengthBetween(2, 63),
stringvalidator.RegexMatches(regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9-_]+$`),
Copy link
Member

Choose a reason for hiding this comment

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

[may] Compiling regexp needs some initialize cost, so store the compiled expression as a global parameter is desirable.

var re = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9-_]+$`

func ServiceNameValidator() validator.String {
	return stringvalidator.All(
		stringvalidator.LengthBetween(2, 63),
		stringvalidator.RegexMatches(re, "..."),
	)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As ServiceNameValidator is expected to be called constant times during a provider initialization, hoisting regex compilation may not be necessary. But it is some kind of best practices and there is no reason to deviate it: 91ffd1d

wantError: true,
},
"invalid char": {
val: types.StringValue("v('ω')v"),
Copy link
Member

Choose a reason for hiding this comment

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

so cute lol

internal/mackerel/service_test.go Outdated Show resolved Hide resolved
Validators: []validator.String{mackerel.ServiceNameValidator()},
},
"memo": schema.StringAttribute{
Computed: true,
Copy link
Member

Choose a reason for hiding this comment

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

[memo] Former schema is here.

func dataSourceMackerelService() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceMackerelServiceRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"memo": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

Why memo has been treated as computed attribute?

Copy link
Contributor Author

@tosuke tosuke Jun 20, 2024

Choose a reason for hiding this comment

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

Computed attributes are "output" from the provider.
We can use memo without specifying the value explicitly (an example below), so memo(and id) is a computed attribute.

data "mackerel_service" "svc" {
  name = "service"
}
resource "some_resource" "something" {
  some_value = data.mackerel_service.svc.memo
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

id may be used "input" value in the future (then name and memo are "output"). But it is out of scope.

Copy link
Member

Choose a reason for hiding this comment

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

I understood that the computed attribute would be unnecessary if the datasource could take a memo as input. Thank you for the information.

@tosuke tosuke requested a review from Arthur1 June 20, 2024 09:18
Copy link
Member

@Arthur1 Arthur1 left a comment

Choose a reason for hiding this comment

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

Thank you.

@Arthur1 Arthur1 merged commit 8efe2a4 into mackerelio-labs:main Jun 20, 2024
1 check passed
@tosuke tosuke deleted the framework-mackerel-service branch August 15, 2024 06:08
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