-
Notifications
You must be signed in to change notification settings - Fork 0
Import extensions with stable project scope #69
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-delete-framework
Choose a base branch
from
hypeship/extension-import
base: hypeship/extension-delete-framework
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,60 @@ | ||
| package extension | ||
|
|
||
| import ( | ||
| "context" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-framework/resource" | ||
| "github.com/hashicorp/terraform-plugin-framework/types" | ||
| "github.com/kernel/terraform-provider-kernel/internal/projectscope" | ||
| ) | ||
|
|
||
| type extensionImporter interface { | ||
| DefaultProjectID() string | ||
| } | ||
|
|
||
| func importExtensionResource(ctx context.Context, client extensionImporter, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { | ||
| if client == nil { | ||
| resp.Diagnostics.AddError( | ||
| "Missing Kernel Client", | ||
| "The Kernel provider was not configured before importing an extension resource.", | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| projectID, extensionID, ok := parseExtensionImportID(req.ID) | ||
| if !ok { | ||
| resp.Diagnostics.AddError( | ||
| "Invalid Kernel Extension Import ID", | ||
| "Cannot import "+strconv.Quote(req.ID)+": import an extension as \"<extension-id>\" or \"<project-id>/<extension-id>\". "+ | ||
| "The bare form uses the provider project_id when configured, otherwise the API key's project binding. Use the qualified form for a different project.", | ||
| ) | ||
| return | ||
| } | ||
| if projectID == "" { | ||
| projectID = client.DefaultProjectID() | ||
| } | ||
|
|
||
| resp.Diagnostics.Append(resp.State.Set(ctx, extensionModel{ | ||
| ID: types.StringValue(extensionID), | ||
| Name: types.StringUnknown(), | ||
| ProjectID: projectscope.StateValue(projectID), | ||
| SourcePath: types.StringNull(), | ||
| SourceSHA256: types.StringUnknown(), | ||
| })...) | ||
| } | ||
|
|
||
| func parseExtensionImportID(id string) (projectID, extensionID string, ok bool) { | ||
| before, after, found := strings.Cut(id, "/") | ||
| if !found { | ||
| return "", id, id != "" | ||
| } | ||
| if strings.Contains(after, "/") { | ||
| return "", "", false | ||
| } | ||
| if before == "" || after == "" { | ||
| return "", "", false | ||
| } | ||
| return before, after, true | ||
| } | ||
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,140 @@ | ||
| package extension | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-framework/resource" | ||
| "github.com/hashicorp/terraform-plugin-framework/types" | ||
| "github.com/kernel/terraform-provider-kernel/internal/kernelclient" | ||
| ) | ||
|
|
||
| var _ extensionImporter = kernelclient.Clients{} | ||
|
|
||
| type fakeExtensionImporter struct { | ||
| defaultProjectID string | ||
| } | ||
|
|
||
| func (f fakeExtensionImporter) DefaultProjectID() string { | ||
| return f.defaultProjectID | ||
| } | ||
|
|
||
| func TestParseExtensionImportID(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := map[string]struct { | ||
| input string | ||
| projectID string | ||
| extensionID string | ||
| ok bool | ||
| }{ | ||
| "bare id": {input: "extension_123", extensionID: "extension_123", ok: true}, | ||
| "qualified id": {input: "project_123/extension_123", projectID: "project_123", extensionID: "extension_123", ok: true}, | ||
| "empty": {input: ""}, | ||
| "empty project": {input: "/extension_123"}, | ||
| "empty extension": {input: "project_123/"}, | ||
| "extra separator": {input: "project_123/extension_123/extra"}, | ||
| } | ||
|
|
||
| for name, test := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| t.Parallel() | ||
| projectID, extensionID, ok := parseExtensionImportID(test.input) | ||
| if projectID != test.projectID || extensionID != test.extensionID || ok != test.ok { | ||
| t.Fatalf("parseExtensionImportID(%q) = %q, %q, %t; want %q, %q, %t", test.input, projectID, extensionID, ok, test.projectID, test.extensionID, test.ok) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestFrameworkImportExtensionSetsRecoverableState(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := map[string]struct { | ||
| importID string | ||
| defaultProjectID string | ||
| wantProjectID types.String | ||
| }{ | ||
| "bare id inherits provider default": { | ||
| importID: "extension_123", | ||
| defaultProjectID: "project_default", | ||
| wantProjectID: types.StringValue("project_default"), | ||
| }, | ||
| "bare id remains api key scoped": { | ||
| importID: "extension_123", | ||
| wantProjectID: types.StringNull(), | ||
| }, | ||
| "qualified id overrides provider default": { | ||
| importID: "project_explicit/extension_123", | ||
| defaultProjectID: "project_default", | ||
| wantProjectID: types.StringValue("project_explicit"), | ||
| }, | ||
| } | ||
|
|
||
| for name, test := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| t.Parallel() | ||
| resp := frameworkExtensionImportResponse() | ||
| importExtensionResource(context.Background(), fakeExtensionImporter{defaultProjectID: test.defaultProjectID}, resource.ImportStateRequest{ID: test.importID}, resp) | ||
| if resp.Diagnostics.HasError() { | ||
| t.Fatalf("unexpected diagnostics: %v", resp.Diagnostics) | ||
| } | ||
| state := frameworkExtensionImportState(t, resp) | ||
| if got, want := state.ID.ValueString(), "extension_123"; got != want { | ||
| t.Fatalf("id = %q, want %q", got, want) | ||
| } | ||
| if !state.ProjectID.Equal(test.wantProjectID) { | ||
| t.Fatalf("project_id = %v, want %v", state.ProjectID, test.wantProjectID) | ||
| } | ||
| if !state.Name.IsUnknown() || !state.SourceSHA256.IsUnknown() { | ||
| t.Fatalf("name/source_sha256 = %v/%v, want unknown until Read", state.Name, state.SourceSHA256) | ||
| } | ||
| if !state.SourcePath.IsNull() { | ||
| t.Fatalf("source_path = %v, want null write-only state", state.SourcePath) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestFrameworkImportExtensionRejectsInvalidIDAndMissingClient(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := map[string]struct { | ||
| client extensionImporter | ||
| importID string | ||
| summary string | ||
| }{ | ||
| "missing client": {importID: "extension_123", summary: "Missing Kernel Client"}, | ||
| "invalid id": {client: fakeExtensionImporter{}, importID: "project/extension/extra", summary: "Invalid Kernel Extension Import ID"}, | ||
| } | ||
|
|
||
| for name, test := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| t.Parallel() | ||
| resp := frameworkExtensionImportResponse() | ||
| importExtensionResource(context.Background(), test.client, resource.ImportStateRequest{ID: test.importID}, resp) | ||
| if !extensionDiagnosticContains(resp.Diagnostics, test.summary, "") { | ||
| t.Fatalf("diagnostics = %v, want %q", resp.Diagnostics, test.summary) | ||
| } | ||
| if !resp.State.Raw.IsNull() { | ||
| t.Fatalf("state = %v, want absent state", resp.State.Raw) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func frameworkExtensionImportResponse() *resource.ImportStateResponse { | ||
| resp := &resource.ImportStateResponse{} | ||
| resp.State.Schema = extensionSchema() | ||
| resp.State.RemoveResource(context.Background()) | ||
| return resp | ||
| } | ||
|
|
||
| func frameworkExtensionImportState(t *testing.T, resp *resource.ImportStateResponse) extensionModel { | ||
| t.Helper() | ||
| var state extensionModel | ||
| if diags := resp.State.Get(context.Background(), &state); diags.HasError() { | ||
| t.Fatalf("get extension import state: %v", diags) | ||
| } | ||
| return state | ||
| } |
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.
Duplicated project-qualified import parser
Low Severity
parseExtensionImportIDis functionally the same as browserpool’sparseImportID: samestrings.Cutflow, extra-separator rejection, and empty-segment rules. Keeping two copies raises the risk that a future import-edge-case fix lands in only one resource.Reviewed by Cursor Bugbot for commit af1b09e. Configure here.