Skip to content

Commit 205bf73

Browse files
committed
PR feedback
1 parent 899aec9 commit 205bf73

File tree

3 files changed

+38
-20
lines changed

3 files changed

+38
-20
lines changed

CODING_STANDARDS.md

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,52 @@ This document outlines the coding standards and conventions used in the terrafor
1212
## Project Structure
1313

1414
- Use the Plugin Framework for all new resources (not SDKv2)
15-
- Follow the code organization pattern of `internal/elasticsearch/security/system_user` for new Plugin Framework resources
15+
- Follow the code organization pattern of [the `system_user` resource](./internal/elasticsearch/security/system_user) for new Plugin Framework resources
16+
- [`testdata/`](./internal/elasticsearch/security/system_user/testdata) - This directory contains Terraform definitions used within the resource acceptance tests. In most cases, this will contain a subdirectory for each test, which then contain subdirectories for individual named test steps.
17+
- [`acc_test.go`](./internal/elasticsearch/security/system_user/acc_test.go) - Contains acceptance tests for the resource
18+
- [`create.go`](./internal/elasticsearch/security/system_user/create.go) - Contains the resources `Create` method and any required logic. Depending on the underlying API, the create and update handlers may share a single code path.
19+
- [`delete.go`](./internal/elasticsearch/security/system_user/delete.go) - Contains the resources `Delete` method.
20+
- [`models.go`](./internal/elasticsearch/security/system_user/models.go) - Contains Golang models used by the resource. At a minimum this will contain a model for reading plan/config/state from the Terraform plugin framework. Any non-trivial models should also define receivers for translating between Terraform models and API client models.
21+
- [`read.go`](./internal/elasticsearch/security/system_user/read.go) - Contains the resources `Read` method. This should also define an internal `read` function that can be re-used by the create/update paths to populate the final Terraform state after performing the create/update operation.
22+
- [`resource.go`](./internal/elasticsearch/security/system_user/resource.go) - Contains:
23+
- A factory function for creating the resource (e.g `NewSystemUserResource`)
24+
- `Metadata`, `Configure`, and optionally `ImportState` functions.
25+
- Type assertions ensuring the resource fully implement the relevant Plugin Framework interfaces (e.g `var _ resource.ResourceWithConfigure = &systemUserResource{}`)
26+
- [`schema.go`](./internal/elasticsearch/security/system_user/schema.go) - Contains the `Schema` function fully defining the resources schema
27+
- [`update.go`](./internal/elasticsearch/security/system_user/update.go) - Contains the `Update` method. Depending on the underlying API this may share significant logic with the `Create` method.
28+
- Some resources may define other files, for example:
29+
- [`models_*.go`](./internal/kibana/security_detection_rule/) - Complex APIs may result in significant model related logic. Split these files as appropriate if they become large.
30+
- Custom [plan modifiers](./internal/elasticsearch/security/api_key/set_unknown_if_access_has_changes.go), [validators](./internal/elasticsearch/security/api_key/validators.go) and [types](./internal/elasticsearch/security/api_key/role_descriptor_defaults.go) - Resource specific plan modifiers and custom types should be contained within the resource package.
31+
- [`state_upgrade.go`](./internal/elasticsearch/security/api_key/state_upgrade.go) - Resources requiring state upgrades should place the `UpgradeState` method within this file.
1632
- Avoid adding extra functionality to the existing `utils` package. Instead:
1733
- Code should live as close to the consumers.
1834
- Resource, area, application specific shared logic should live at that level. For example within `internal/kibana` for Kibana specific shared logic.
19-
- Provider wide shared logic should be packaged together by a logical concept. For example `internal/diagutil` contains shared code for managing Terraform Diagnostics, and translating between errors, SDKv2 diags, and Plugin Framework diags.
35+
- Provider wide shared logic should be packaged together by a logical concept. For example [diagutil](./internal/diagutil) contains shared code for managing Terraform Diagnostics, and translating between errors, SDKv2 diags, and Plugin Framework diags.
36+
- Prefer using existing util functions over longer form, duplicated code:
37+
- `utils.IsKnown(val)` instead of `!val.IsNull() && !val.IsUnknown()`
38+
- `utils.ListTypeAs` instead of `val.ElementsAs` or similar for other collection types
2039

2140
## Schema Definitions
2241

2342
- Use custom types to model attribute specific behaviour.
24-
- Use `jsontypes.NormalizedType{}` custom type for string attributes containing JSON blobs.
25-
- Use `customtypes.DurationType{}` for duration-based string attributes.
26-
- Use `customtypes.JSONWithDefaultsType{}` to allow users to specify only a subset of a JSON blob.
43+
- Use [`jsontypes.NormalizedType{}`](https://github.com/hashicorp/terraform-plugin-framework-jsontypes/blob/main/jsontypes/normalized_type.go) custom type for string attributes containing JSON blobs.
44+
- Use [`customtypes.DurationType{}`](./internal/utils/customtypes/duration_type.go) for duration-based string attributes.
45+
- Use [`customtypes.JSONWithDefaultsType{}`](./internal/utils/customtypes/json_with_defaults_type.go) to allow users to specify only a subset of a JSON blob.
2746
- Always include comprehensive descriptions for all resources, and attributes.
28-
- Long, multiline descriptions should be stored in an external markdown file, which is imported via Golang embedding. See `internal/elasticsearch/security/system_user/resource-description.md` for an example location.
47+
- Long, multiline descriptions should be stored in an external markdown file, which is imported via Golang embedding. For [example](./internal/elasticsearch/security/system_user/resource-description.md).
2948
- Use schema validation wherever possible. Only perform validation within create/read/update functions as a last resort.
3049
- For example, any validation that relies on the actual Elastic Stack components (e.g Elasticsearch version)
3150
can only be performed during the create/read/update phase.
51+
- Kibana and Fleet resources will be backed by the Kibana API. The schema definition should closely follow the defined API request/response models defined in the [OpenAPI specification](./generated/kbapi/oas-filtered.yaml).
52+
- Further details may be found in the [API documentation](https://www.elastic.co/docs/api/doc/kibana/v9/)
53+
- Elasticsearch resources will be backed by the [go-elasticsearch](https://github.com/elastic/go-elasticsearch) client.
54+
- Further details may be found in the [API documentation](https://www.elastic.co/docs/api/doc/elasticsearch/)
3255

33-
## JSON Handling
34-
35-
- Use `jsontypes.NormalizedType{}` for JSON string attributes to ensure proper normalization and comparison.
36-
- Use `customtypes.JSONWithDefaultsType{}` if API level defaults may be applied automatically.
3756

38-
## Resource Implementation
57+
## JSON Handling
3958

40-
- Follow the pattern: `resource.go`, `schema.go`, `models.go`, `create.go`, `read.go`, `update.go`, `delete.go`
41-
- Use factory functions like `NewSystemUserResource()` to create resource instances
42-
- Ensure appropriate interface assertions are included alongside the resource definition.
43-
- For example, if a resource supports imports, include `var _ resource.ResourceWithImportState = &resource{}` or similar.
44-
- Prefer using existing util functions over longer form, duplicated code:
45-
- `utils.IsKnown(val)` instead of `!val.IsNull() && !val.IsUnknown()`
46-
- `utils.ListTypeAs` instead of `val.ElementsAs` or similar for other collection types
59+
- Use [`jsontypes.NormalizedType{}`](https://github.com/hashicorp/terraform-plugin-framework-jsontypes/blob/main/jsontypes/normalized_type.go) for JSON string attributes to ensure proper normalization and comparison.
60+
- Use [`customtypes.JSONWithDefaultsType{}`](./internal/utils/customtypes/json_with_defaults_type.go) if API level defaults may be applied automatically.
4761

4862
## Testing
4963

@@ -60,5 +74,5 @@ This document outlines the coding standards and conventions used in the terrafor
6074

6175
## API Client Usage
6276

63-
- Use generated API clients from `generated/kbapi/` for new Kibana API interactions
77+
- Use generated API clients from [`generated/kbapi/`](./generated/kbapi/) for new Kibana API interactions
6478
- Avoid deprecated clients (`libs/go-kibana-rest`, `generated/alerting`, `generated/connectors`, `generated/slo`)

internal/elasticsearch/security/system_user/resource.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import (
77
"github.com/hashicorp/terraform-plugin-framework/resource"
88
)
99

10+
// Ensure provider defined types fully satisfy framework interfaces
11+
var _ resource.Resource = &systemUserResource{}
12+
var _ resource.ResourceWithConfigure = &systemUserResource{}
13+
1014
func NewSystemUserResource() resource.Resource {
1115
return &systemUserResource{}
1216
}

renovate.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
],
1717
"postUpgradeTasks": {
1818
"commands": ["make -C generated/kbapi all"],
19-
"fileFilters": ["generated/kbapi/kibana.gen.go"]
19+
"fileFilters": ["generated/kbapi/kibana.gen.go", "generated/kbapi/oas-filtered.yaml"]
2020
},
2121
"automerge": true,
2222
"automergeStrategy": "squash",

0 commit comments

Comments
 (0)