generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 141
fix: implement schema patches to fix ECS Service PlatformVersion default #2979
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
Open
AviorSchreiber
wants to merge
1
commit into
hashicorp:main
Choose a base branch
from
AviorSchreiber:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+316
−14
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,59 @@ | ||
| // Copyright IBM Corp. 2021, 2026 | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
|
|
||
| jsonpatch "github.com/evanphx/json-patch" | ||
| ) | ||
|
|
||
| // ApplySchemaPatches applies a list of patch operations to a JSON schema string. | ||
| // It uses RFC 6902 JSON Patch format to apply the patches. | ||
| // Currently only supports "remove" action (default). | ||
| // If no operations are provided, it returns the original schema unchanged. | ||
| func ApplySchemaPatches(schemaJSON string, operations []PatchOperation) (string, error) { | ||
| if len(operations) == 0 { | ||
| return schemaJSON, nil | ||
| } | ||
|
|
||
| // Create JSON Patch document | ||
| patchOps := make([]map[string]interface{}, 0, len(operations)) | ||
| for _, op := range operations { | ||
| if op.Action == "" { | ||
| return "", fmt.Errorf("action is required for path %q", op.JSONPath) | ||
| } | ||
|
|
||
| // Currently only "remove" is supported | ||
| if op.Action != "remove" { | ||
| return "", fmt.Errorf("unsupported action %q for path %q: only \"remove\" is currently supported", op.Action, op.JSONPath) | ||
| } | ||
|
|
||
| patchOps = append(patchOps, map[string]interface{}{ | ||
| "op": op.Action, | ||
| "path": op.JSONPath, | ||
| }) | ||
| } | ||
|
|
||
| // Marshal patch operations to JSON | ||
| patchBytes, err := json.Marshal(patchOps) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to marshal patch operations: %w", err) | ||
| } | ||
|
|
||
| // Decode and apply the patch | ||
| patch, err := jsonpatch.DecodePatch(patchBytes) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to decode patch: %w", err) | ||
| } | ||
|
|
||
| // Apply patch to schema | ||
| patchedBytes, err := patch.Apply([]byte(schemaJSON)) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to apply patch: %w", err) | ||
| } | ||
|
|
||
| return string(patchedBytes), nil | ||
| } |
This file contains hidden or 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,215 @@ | ||
| // Copyright IBM Corp. 2021, 2026 | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestApplySchemaPatches(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| schemaJSON string | ||
| operations []PatchOperation | ||
| wantJSON string | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "remove existing property default", | ||
| schemaJSON: `{ | ||
| "properties": { | ||
| "PlatformVersion": { | ||
| "type": "string", | ||
| "default": "LATEST" | ||
| } | ||
| } | ||
| }`, | ||
| operations: []PatchOperation{ | ||
| {Action: "remove", JSONPath: "/properties/PlatformVersion/default"}, | ||
| }, | ||
| wantJSON: `{ | ||
| "properties": { | ||
| "PlatformVersion": { | ||
| "type": "string" | ||
| } | ||
| } | ||
| }`, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "remove nested property", | ||
| schemaJSON: `{ | ||
| "properties": { | ||
| "Config": { | ||
| "type": "object", | ||
| "properties": { | ||
| "Settings": { | ||
| "default": "value", | ||
| "type": "string" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }`, | ||
| operations: []PatchOperation{ | ||
| {Action: "remove", JSONPath: "/properties/Config/properties/Settings/default"}, | ||
| }, | ||
| wantJSON: `{ | ||
| "properties": { | ||
| "Config": { | ||
| "type": "object", | ||
| "properties": { | ||
| "Settings": { | ||
| "type": "string" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }`, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "multiple remove operations", | ||
| schemaJSON: `{ | ||
| "properties": { | ||
| "Prop1": { | ||
| "type": "string", | ||
| "default": "value1" | ||
| }, | ||
| "Prop2": { | ||
| "type": "number", | ||
| "default": 42 | ||
| } | ||
| } | ||
| }`, | ||
| operations: []PatchOperation{ | ||
| {Action: "remove", JSONPath: "/properties/Prop1/default"}, | ||
| {Action: "remove", JSONPath: "/properties/Prop2/default"}, | ||
| }, | ||
| wantJSON: `{ | ||
| "properties": { | ||
| "Prop1": { | ||
| "type": "string" | ||
| }, | ||
| "Prop2": { | ||
| "type": "number" | ||
| } | ||
| } | ||
| }`, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "no operations returns original", | ||
| schemaJSON: `{"properties": {"Foo": "bar"}}`, | ||
| operations: []PatchOperation{}, | ||
| wantJSON: `{"properties": {"Foo": "bar"}}`, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "nil operations returns original", | ||
| schemaJSON: `{"properties": {"Foo": "bar"}}`, | ||
| operations: nil, | ||
| wantJSON: `{"properties": {"Foo": "bar"}}`, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "remove non-existent path returns error", | ||
| schemaJSON: `{"properties": {}}`, | ||
| operations: []PatchOperation{ | ||
| {Action: "remove", JSONPath: "/properties/NonExistent/default"}, | ||
| }, | ||
| wantJSON: "", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "invalid JSON input returns error", | ||
| schemaJSON: `{invalid json`, | ||
| operations: []PatchOperation{ | ||
| {Action: "remove", JSONPath: "/properties/Foo"}, | ||
| }, | ||
| wantJSON: "", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "remove entire property", | ||
| schemaJSON: `{ | ||
| "properties": { | ||
| "Keep": {"type": "string"}, | ||
| "Remove": {"type": "string"} | ||
| } | ||
| }`, | ||
| operations: []PatchOperation{ | ||
| {Action: "remove", JSONPath: "/properties/Remove"}, | ||
| }, | ||
| wantJSON: `{ | ||
| "properties": { | ||
| "Keep": {"type": "string"} | ||
| } | ||
| }`, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "remove array element", | ||
| schemaJSON: `{ | ||
| "required": ["a", "b", "c"] | ||
| }`, | ||
| operations: []PatchOperation{ | ||
| {Action: "remove", JSONPath: "/required/1"}, | ||
| }, | ||
| wantJSON: `{ | ||
| "required": ["a", "c"] | ||
| }`, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "missing action returns error", | ||
| schemaJSON: `{"properties": {"Foo": "bar"}}`, | ||
| operations: []PatchOperation{ | ||
| {JSONPath: "/properties/Foo"}, | ||
| }, | ||
| wantJSON: "", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "unsupported action returns error", | ||
| schemaJSON: `{"properties": {}}`, | ||
| operations: []PatchOperation{ | ||
| {Action: "add", JSONPath: "/properties/Foo"}, | ||
| }, | ||
| wantJSON: "", | ||
| wantErr: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got, err := ApplySchemaPatches(tt.schemaJSON, tt.operations) | ||
| if (err != nil) != tt.wantErr { | ||
| t.Errorf("ApplySchemaPatches() error = %v, wantErr %v", err, tt.wantErr) | ||
| return | ||
| } | ||
| if tt.wantErr { | ||
| return | ||
| } | ||
|
|
||
| // Compare JSON semantically (ignore formatting differences) | ||
| var gotJSON, wantJSON interface{} | ||
| if err := json.Unmarshal([]byte(got), &gotJSON); err != nil { | ||
| t.Errorf("ApplySchemaPatches() returned invalid JSON: %v", err) | ||
| return | ||
| } | ||
| if err := json.Unmarshal([]byte(tt.wantJSON), &wantJSON); err != nil { | ||
| t.Errorf("Test case has invalid wantJSON: %v", err) | ||
| return | ||
| } | ||
|
|
||
| gotBytes, _ := json.Marshal(gotJSON) | ||
| wantBytes, _ := json.Marshal(wantJSON) | ||
| if string(gotBytes) != string(wantBytes) { | ||
| t.Errorf("ApplySchemaPatches() = %s, want %s", string(gotBytes), string(wantBytes)) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| // Copyright IBM Corp. 2021, 2026 | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| //go:generate go run generators/schema/main.go -config all_schemas.hcl -generated-code-root .. -import-path-root github.com/hashicorp/terraform-provider-awscc/internal -- resources.go singular_data_sources.go plural_data_sources.go import_examples_gen.json | ||
| //go:generate go run generators/schema/main.go generators/schema/patches.go -config all_schemas.hcl -generated-code-root .. -import-path-root github.com/hashicorp/terraform-provider-awscc/internal -- resources.go singular_data_sources.go plural_data_sources.go import_examples_gen.json | ||
|
|
||
| package provider |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Const would be better if there are other uses in schema but if not suppression is fine.