Skip to content

Commit 3a8d3b5

Browse files
committed
Merge remote-tracking branch 'origin/main' into ml-datafeed-state
* origin/main: First take at documented coding standards (#1429) chore(deps): update golangci/golangci-lint-action action to v9 (#1431) chore(deps): update golang:1.25.4 docker digest to 6ca9eb0 (#1432) chore(deps): update kibana-openapi-spec digest to 96ffcd7 (#1430) chore(deps): update kibana-openapi-spec digest to a8e3b64 (#1428) chore(deps): update golang docker tag to v1.25.4 (#1423)
2 parents cab9208 + 4d55f3b commit 3a8d3b5

File tree

8 files changed

+2494
-1220
lines changed

8 files changed

+2494
-1220
lines changed

.buildkite/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
steps:
22
- label: Release
33
agents:
4-
image: "golang:1.25.3@sha256:6d4e5e74f47db00f7f24da5f53c1b4198ae46862a47395e30477365458347bf2"
4+
image: "golang:1.25.4@sha256:6ca9eb0b32a4bd4e8c98a4a2edf2d7c96f3ea6db6eb4fc254eef6c067cf73bb4"
55
cpu: "16"
66
memory: "24G"
77
ephemeralStorage: "20G"

.github/copilot-instructions.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
You will be tasked to fix an issue from an open-source repository. This is a Go based repository hosting a Terrform provider for the elastic stack (elasticsearch and kibana) APIs. This repo currently supports both [plugin framework](https://developer.hashicorp.com/terraform/plugin/framework/getting-started/code-walkthrough) and [sdkv2](https://developer.hashicorp.com/terraform/plugin/sdkv2) resources. Unless you're told otherwise, all new resources _must_ use the plugin framework.
1+
You will be writing or reviewing code for the Terraform provider for Elastic Stack (Elasticsearch, Kibana, Fleet, APM, and Logstash). This is a Go-based repository hosting the provider source.
22

3-
Take your time and think through every step - remember to check your solution rigorously and watch out for boundary cases, especially with the changes you made. Your solution must be perfect. If not, continue working on it. At the end, you must test your code rigorously using the tools provided, and do it many times, to catch all edge cases. If it is not robust, iterate more and make it perfect. Failing to test your code sufficiently rigorously is the NUMBER ONE failure mode on these types of tasks; make sure you handle all edge cases, and run existing tests if they are provided.
3+
When writing code, you must adhere to the coding standards and conventions outlined in the [CODING_STANDARDS.md](../CODING_STANDARDS.md) document in this repository.
4+
5+
When reviewing code, ensure that all changes comply with the coding standards and conventions specified in the [CODING_STANDARDS.md](../CODING_STANDARDS.md) document. Pay special attention to project structure, schema definitions, JSON handling, resource implementation, and testing practices.
6+
7+
Take your time and think through every step - remember to check solutions rigorously and watch out for boundary cases, especially with the changes being made.
8+
9+
When writing code, your solution must be perfect. If not, continue working on it. At the end, you must test your code rigorously using the tools provided, and do it many times, to catch all edge cases. If it is not robust, iterate more and make it perfect. Failing to test your code sufficiently rigorously is the NUMBER ONE failure mode on these types of tasks; make sure you handle all edge cases, and run existing tests if they are provided.
410

511
Please see [README.md](../README.md) and the [CONTRIBUTING.md](../CONTRIBUTING.md) docs before getting started.
612

7-
# Workflow
13+
# Development Workflow
814

915
## High-Level Problem Solving Strategy
1016

CODING_STANDARDS.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Coding Standards
2+
3+
This document outlines the coding standards and conventions used in the terraform-provider-elasticstack repository.
4+
5+
## General Principles
6+
7+
- Write idiomatic Go.
8+
- [Effective Go](https://go.dev/doc/effective_go)
9+
- [Code Review Comments](https://go.dev/wiki/CodeReviewComments)
10+
- The [Google Styleguide](https://google.github.io/styleguide/go/index#about)
11+
12+
## Project Structure
13+
14+
- Use the Plugin Framework for all new resources (not SDKv2)
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.
32+
- Avoid adding extra functionality to the existing `utils` package. Instead:
33+
- Code should live as close to the consumers.
34+
- Resource, area, application specific shared logic should live at that level. For example within `internal/kibana` for Kibana specific shared logic.
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
39+
40+
## Schema Definitions
41+
42+
- Use custom types to model attribute specific behaviour.
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.
46+
- Always include comprehensive descriptions for all resources, and attributes.
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).
48+
- Use schema validation wherever possible. Only perform validation within create/read/update functions as a last resort.
49+
- For example, any validation that relies on the actual Elastic Stack components (e.g Elasticsearch version)
50+
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/)
55+
- Use `EnforceMinVersion` to ensure the backing Elastic Stack applications support the defined fields.
56+
- The provider supports a wide range of Stack versions, and so newer features will not be available in all versions.
57+
- See [`assertKafkaSupport`](./internal/fleet/output/models.go) for an example of how to handle the use of unsupported attributes.
58+
59+
60+
## JSON Handling
61+
62+
- 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.
63+
- Use [`customtypes.JSONWithDefaultsType{}`](./internal/utils/customtypes/json_with_defaults_type.go) if API level defaults may be applied automatically.
64+
65+
## Testing
66+
67+
- Use table-driven unit tests when possible with `t.Run()` for test cases
68+
- Use testify library (`assert`, `require`) for test assertions
69+
- Ensure that *every* resource attribute is covered by at least one acceptance test case whenever possible.
70+
- Features that *require* external services are likely the only excuse to not include acceptance test coverage.
71+
- Organize acceptance tests in `acc_test.go` files
72+
- Test Terraform code should be vanilla, valid Terraform
73+
- Store test Terraform modules in `testdata/<test_name>/<step_description>` directories.
74+
- Define any required variables within the module
75+
- Reference the test code via `ConfigDirectory: acctest.NamedTestCaseDirectory("<step description>")`
76+
- Define any required variables via `ConfigVariables`
77+
78+
## API Client Usage
79+
80+
- Use generated API clients from [`generated/kbapi/`](./generated/kbapi/) for new Kibana API interactions
81+
- Avoid deprecated clients (`libs/go-kibana-rest`, `generated/alerting`, `generated/connectors`, `generated/slo`)

generated/kbapi/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
SHELL := /bin/bash
33
ROOT_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
44

5-
github_ref ?= de217ef5b84c812b4c7c7848405fdf418d50f988
5+
github_ref ?= 96ffcd79c2227e2236811a820f456044b691b2a4
66
oas_url := https://raw.githubusercontent.com/elastic/kibana/$(github_ref)/oas_docs/output/kibana.yaml
77

88
.PHONY: all

0 commit comments

Comments
 (0)