-
Notifications
You must be signed in to change notification settings - Fork 31
fix(compute): allow unlisted template IDs #888
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d1eb683
build(deps): update golang.org/x dependencies
natalie-o-perret 22c0bc7
docs(changelog): note golang.org/x updates
natalie-o-perret 338b079
build(deps): sync integration test module
natalie-o-perret 366d552
fix(compute): allow unlisted template IDs
natalie-o-perret a02d6c7
docs: add changelog entry for #888
natalie-o-perret d9fd010
fix(compute): adjust default disk size to template
natalie-o-perret 4db7e34
Merge remote-tracking branch 'origin/fix/template-disk-size-default' …
natalie-o-perret afb781c
fix(compute): adjust default disk size to template
natalie-o-perret 5f1cf81
Merge remote-tracking branch 'origin/fix/template-disk-size-default' …
natalie-o-perret 97e547b
docs(changelog): add entry for #882
natalie-o-perret 8244aba
Merge remote-tracking branch 'origin/fix/template-disk-size-default' …
natalie-o-perret 889e798
fix(compute): adjust default disk size to template
natalie-o-perret c82f267
docs(changelog): add entry for #882
natalie-o-perret b7bb930
Merge remote-tracking branch 'origin/fix/template-disk-size-default' …
natalie-o-perret 0045d79
Merge remote-tracking branch 'origin/master' into fix/unlisted-templa…
natalie-o-perret 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
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
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,62 @@ | ||
| package compute | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| v3 "github.com/exoscale/egoscale/v3" | ||
| ) | ||
|
|
||
| // ResolveTemplate resolves names and retrieves UUID metadata when available. | ||
| func ResolveTemplate( | ||
| ctx context.Context, | ||
| client *v3.Client, | ||
| nameOrID string, | ||
| visibility string, | ||
| zone v3.ZoneName, | ||
| ) (*v3.Template, error) { | ||
| if id, err := v3.ParseUUID(nameOrID); err == nil { | ||
| template, err := client.GetTemplate(ctx, id) | ||
| if err != nil { | ||
| // Metadata is optional for UUIDs; the mutation API remains authoritative. | ||
| return &v3.Template{ID: id}, nil | ||
| } | ||
| return template, nil | ||
| } | ||
|
|
||
| templates, err := client.ListTemplates(ctx, v3.ListTemplatesWithVisibility(v3.ListTemplatesVisibility(visibility))) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error listing template with visibility %q: %w", visibility, err) | ||
| } | ||
| template, err := templates.FindTemplate(nameOrID) | ||
| if err != nil { | ||
| return nil, fmt.Errorf( | ||
| "no template %q found with visibility %s in zone %s", | ||
| nameOrID, | ||
| visibility, | ||
| zone, | ||
| ) | ||
| } | ||
|
|
||
| return &template, nil | ||
| } | ||
|
|
||
| // ResolveTemplateID passes UUIDs through and resolves template names from the list endpoint. | ||
| func ResolveTemplateID( | ||
| ctx context.Context, | ||
| client *v3.Client, | ||
| nameOrID string, | ||
| visibility string, | ||
| zone v3.ZoneName, | ||
| ) (v3.UUID, error) { | ||
| if id, err := v3.ParseUUID(nameOrID); err == nil { | ||
| return id, nil | ||
| } | ||
|
|
||
| template, err := ResolveTemplate(ctx, client, nameOrID, visibility, zone) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| return template.ID, 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,76 @@ | ||
| package compute | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| v3 "github.com/exoscale/egoscale/v3" | ||
| "github.com/exoscale/egoscale/v3/credentials" | ||
| ) | ||
|
|
||
| func TestResolveTemplateIDPassesUUIDThrough(t *testing.T) { | ||
| const id = "5b473841-fd33-4b88-bcfe-a776abf15034" | ||
|
|
||
| got, err := ResolveTemplateID(context.Background(), nil, id, "public", "ch-gva-2") | ||
| if err != nil { | ||
| t.Fatalf("ResolveTemplateID() error = %v", err) | ||
| } | ||
| if got != v3.UUID(id) { | ||
| t.Errorf("ResolveTemplateID() = %q, want %q", got, id) | ||
| } | ||
| } | ||
|
|
||
| func TestResolveTemplateGetsOptionalUUIDMetadata(t *testing.T) { | ||
| const id = "5b473841-fd33-4b88-bcfe-a776abf15034" | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| status int | ||
| response string | ||
| wantSize int64 | ||
| }{ | ||
| { | ||
| name: "metadata available", | ||
| status: http.StatusOK, | ||
| response: `{"id":"5b473841-fd33-4b88-bcfe-a776abf15034","size":85899345920}`, | ||
| wantSize: 80 << 30, | ||
| }, | ||
| { | ||
| name: "metadata unavailable", | ||
| status: http.StatusNotFound, | ||
| response: `{"message":"not found"}`, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| if r.Method != http.MethodGet || r.URL.Path != "/template/"+id { | ||
| t.Errorf("request = %s %s, want GET /template/%s", r.Method, r.URL.Path, id) | ||
| } | ||
| w.Header().Set("Content-Type", "application/json") | ||
| w.WriteHeader(tt.status) | ||
| _, _ = w.Write([]byte(tt.response)) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| client, err := v3.NewClient( | ||
| credentials.NewStaticCredentials("key", "secret"), | ||
| v3.ClientOptWithEndpoint(v3.Endpoint(server.URL)), | ||
| ) | ||
| if err != nil { | ||
| t.Fatalf("NewClient() error = %v", err) | ||
| } | ||
|
|
||
| got, err := ResolveTemplate(context.Background(), client, id, "public", "ch-gva-2") | ||
| if err != nil { | ||
| t.Fatalf("ResolveTemplate() error = %v", err) | ||
| } | ||
| if got.ID != v3.UUID(id) || got.Size != tt.wantSize { | ||
| t.Errorf("ResolveTemplate() = {ID: %q, Size: %d}, want {ID: %q, Size: %d}", got.ID, got.Size, id, tt.wantSize) | ||
| } | ||
| }) | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.