-
Notifications
You must be signed in to change notification settings - Fork 0
Register the durable extension resource #70
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
IlyaasK
wants to merge
1
commit into
hypeship/extension-import
Choose a base branch
from
hypeship/extension-plan-archive
base: hypeship/extension-import
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.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| --- | ||
| # generated by https://github.com/hashicorp/terraform-plugin-docs | ||
| page_title: "kernel_extension Resource - Kernel" | ||
| subcategory: "" | ||
| description: |- | ||
| Kernel uploaded extension durable configuration. | ||
| --- | ||
|
|
||
| # kernel_extension (Resource) | ||
|
|
||
| Kernel uploaded extension durable configuration. | ||
|
|
||
|
|
||
|
|
||
| <!-- schema generated by tfplugindocs --> | ||
| ## Schema | ||
|
|
||
| ### Optional | ||
|
|
||
| > **NOTE**: [Write-only arguments](https://developer.hashicorp.com/terraform/language/resources/ephemeral#write-only-arguments) are supported in Terraform 1.11 and later. | ||
|
|
||
| - `name` (String) Optional extension name. Must be unique within the project. Adding or changing a configured name replaces the extension; omitting it preserves the remote name because the API cannot clear a name. | ||
| - `project_id` (String) Project this extension belongs to. Defaults to the provider `project_id` when unset; when neither is set, the API key's project binding determines the project. Adding or changing it replaces the extension. | ||
| - `source_path` (String, [Write-only](https://developer.hashicorp.com/terraform/language/resources/ephemeral#write-only-arguments)) Local path to the extension ZIP. Required when creating or replacing the extension and never stored in Terraform plan or state artifacts. Requires Terraform 1.11 or later. | ||
| - `source_sha256` (String) Lowercase hexadecimal SHA-256 checksum of the exact extension ZIP bytes. Configure with `filesha256(source_path)`. Adding or changing it replaces the extension. | ||
|
|
||
| ### Read-Only | ||
|
|
||
| - `id` (String) Unique extension identifier. |
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,82 @@ | ||
| package extension | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-framework/resource" | ||
| "github.com/kernel/terraform-provider-kernel/internal/kernelclient" | ||
| ) | ||
|
|
||
| var _ extensionClient = kernelclient.Clients{} | ||
|
|
||
| func TestExtensionResourceMetadataAndSchema(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| r := NewResource() | ||
| var metadata resource.MetadataResponse | ||
| r.Metadata(context.Background(), resource.MetadataRequest{ProviderTypeName: "kernel"}, &metadata) | ||
| if metadata.TypeName != "kernel_extension" { | ||
| t.Fatalf("type name = %q, want kernel_extension", metadata.TypeName) | ||
| } | ||
|
|
||
| var schema resource.SchemaResponse | ||
| r.Schema(context.Background(), resource.SchemaRequest{}, &schema) | ||
| for _, name := range []string{"id", "name", "project_id", "source_path", "source_sha256"} { | ||
| if _, ok := schema.Schema.Attributes[name]; !ok { | ||
| t.Fatalf("extension schema missing %s attribute", name) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestExtensionResourceConfigure(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("durable client", func(t *testing.T) { | ||
| t.Parallel() | ||
| r := &extensionResource{} | ||
| var resp resource.ConfigureResponse | ||
| r.Configure(context.Background(), resource.ConfigureRequest{ProviderData: kernelclient.Clients{}}, &resp) | ||
| if resp.Diagnostics.HasError() { | ||
| t.Fatalf("unexpected diagnostics: %v", resp.Diagnostics) | ||
| } | ||
| if r.client == nil { | ||
| t.Fatal("extension client was not configured") | ||
| } | ||
| }) | ||
|
|
||
| t.Run("unexpected provider data", func(t *testing.T) { | ||
| t.Parallel() | ||
| r := &extensionResource{} | ||
| var resp resource.ConfigureResponse | ||
| r.Configure(context.Background(), resource.ConfigureRequest{ProviderData: "not a client"}, &resp) | ||
| if len(resp.Diagnostics) != 1 || resp.Diagnostics[0].Summary() != "Unexpected Kernel Client Type" { | ||
| t.Fatalf("diagnostics = %v, want Unexpected Kernel Client Type", resp.Diagnostics) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("nil provider data", func(t *testing.T) { | ||
| t.Parallel() | ||
| r := &extensionResource{} | ||
| var resp resource.ConfigureResponse | ||
| r.Configure(context.Background(), resource.ConfigureRequest{}, &resp) | ||
| if resp.Diagnostics.HasError() { | ||
| t.Fatalf("unexpected diagnostics: %v", resp.Diagnostics) | ||
| } | ||
| if r.client != nil { | ||
| t.Fatal("nil provider data configured a client") | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func TestExtensionResourceRejectsUpdate(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| r := &extensionResource{} | ||
| var resp resource.UpdateResponse | ||
| r.Update(context.Background(), resource.UpdateRequest{}, &resp) | ||
|
|
||
| if len(resp.Diagnostics) != 1 || resp.Diagnostics[0].Summary() != "Unexpected Kernel Extension Update" { | ||
| t.Fatalf("diagnostics = %v, want Unexpected Kernel Extension Update", resp.Diagnostics) | ||
| } | ||
| } |
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,61 @@ | ||
| package extension | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-framework/path" | ||
| "github.com/hashicorp/terraform-plugin-framework/resource" | ||
| "github.com/hashicorp/terraform-plugin-framework/types" | ||
| ) | ||
|
|
||
| func modifyExtensionPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { | ||
| if req.Plan.Raw.IsNull() { | ||
| return | ||
| } | ||
|
|
||
| var config extensionModel | ||
| resp.Diagnostics.Append(req.Config.Get(ctx, &config)...) | ||
| var plan extensionModel | ||
| resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
|
|
||
| requiresArchive := req.State.Raw.IsNull() | ||
| if !requiresArchive { | ||
| var state extensionModel | ||
| resp.Diagnostics.Append(req.State.Get(ctx, &state)...) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
| requiresArchive = extensionReplacementPlanned(state, plan) | ||
| } | ||
| if !requiresArchive { | ||
| return | ||
| } | ||
|
|
||
| if config.SourcePath.IsNull() { | ||
| resp.Diagnostics.AddAttributeError( | ||
| path.Root("source_path"), | ||
| "Missing Extension Source Path", | ||
| "source_path must be configured when creating or replacing a Kernel extension.", | ||
| ) | ||
| } | ||
| if config.SourceSHA256.IsNull() { | ||
| resp.Diagnostics.AddAttributeError( | ||
| path.Root("source_sha256"), | ||
| "Missing Extension Source Checksum", | ||
| "source_sha256 must be configured when creating or replacing a Kernel extension. Use filesha256(source_path) to track the exact archive bytes.", | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| func extensionReplacementPlanned(state, plan extensionModel) bool { | ||
| return extensionStringChanged(state.Name, plan.Name) || | ||
| extensionStringChanged(state.ProjectID, plan.ProjectID) || | ||
| extensionStringChanged(state.SourceSHA256, plan.SourceSHA256) | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| func extensionStringChanged(state, plan types.String) bool { | ||
| return !state.Equal(plan) | ||
| } | ||
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.
Forced replace skips archive checks
Medium Severity
extensionReplacementPlannedonly looks for diffs onname,project_id, andsource_sha256, so a forced replace with unchanged attributes never setsrequiresArchive. Plan can succeed withoutsource_path/source_sha256in config, then destroy runs and create fails afterward, which can delete the remote extension before upload is possible.Additional Locations (1)
internal/resources/extension/modify_plan.go#L23-L35Reviewed by Cursor Bugbot for commit 77e6c31. Configure here.