-
Notifications
You must be signed in to change notification settings - Fork 0
component: add oxide-image data source #13
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| // file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
|
||
| //go:generate packer-sdc mapstructure-to-hcl2 -type Config,Output | ||
| //go:generate packer-sdc struct-markdown | ||
|
|
||
| package image | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/hashicorp/hcl/v2/hcldec" | ||
| "github.com/hashicorp/packer-plugin-sdk/hcl2helper" | ||
| "github.com/hashicorp/packer-plugin-sdk/packer" | ||
| "github.com/hashicorp/packer-plugin-sdk/template/config" | ||
| "github.com/oxidecomputer/oxide.go/oxide" | ||
| "github.com/zclconf/go-cty/cty" | ||
| ) | ||
|
|
||
| // Config represents the Packer configuration for this plugin component. | ||
| type Config struct { | ||
| // Oxide API URL (e.g., silo.sys.example.com). | ||
| Host string `mapstructure:"host"` | ||
|
|
||
| // Oxide API token. | ||
| Token string `mapstructure:"token"` | ||
|
|
||
| // Name of the image to fetch. | ||
| Name string `mapstructure:"name"` | ||
|
|
||
| // Name or ID of the project containing the image to fetch. Leave blank to fetch | ||
| // a silo image instead of a project image. | ||
| Project string `mapstructure:"project"` | ||
| } | ||
|
|
||
| // Output represents the information returned by this plugin component for use | ||
| // in other Packer plugin components. | ||
| type Output struct { | ||
| // ID of the image that was fetched. | ||
| ImageID string `mapstructure:"image_id"` | ||
| } | ||
|
|
||
| // Compile-time assertion to ensure the DataSource type implements the Packer | ||
| // data source component interface. | ||
| var _ packer.Datasource = (*DataSource)(nil) | ||
|
|
||
| // DataSource is the concrete type that implements the Packer data source | ||
| // component interface. | ||
| type DataSource struct { | ||
| config Config | ||
| } | ||
|
|
||
| // ConfigSpec returns the HCL specification that Packer uses to validate and | ||
| // configure this plugin component. | ||
| func (d *DataSource) ConfigSpec() hcldec.ObjectSpec { | ||
| return d.config.FlatMapstructure().HCL2Spec() | ||
| } | ||
|
|
||
| // Configure decodes the configuration for this plugin component, checks whether | ||
| // the configuration is valid, and stores any necessary state for future methods | ||
| // to use during execution. | ||
| func (d *DataSource) Configure(args ...any) error { | ||
| if err := config.Decode(&d.config, nil, args...); err != nil { | ||
| return fmt.Errorf("failed decoding configuration: %w", err) | ||
| } | ||
|
|
||
| var multiErr *packer.MultiError | ||
|
|
||
| if d.config.Host == "" { | ||
| d.config.Host = os.Getenv("OXIDE_HOST") | ||
| } | ||
|
|
||
| if d.config.Token == "" { | ||
| d.config.Token = os.Getenv("OXIDE_TOKEN") | ||
| } | ||
|
|
||
| if d.config.Host == "" { | ||
| multiErr = packer.MultiErrorAppend(multiErr, errors.New("host is required")) | ||
| } | ||
|
|
||
| if d.config.Token == "" { | ||
| multiErr = packer.MultiErrorAppend(multiErr, errors.New("token is required")) | ||
| } | ||
|
|
||
| if d.config.Name == "" { | ||
| multiErr = packer.MultiErrorAppend(multiErr, errors.New("name is required")) | ||
| } | ||
|
|
||
| if multiErr != nil && len(multiErr.Errors) > 0 { | ||
| return multiErr | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // Execute fetches image information from the Oxide API and returns that | ||
| // information in the format specified by [OutputSpec]. | ||
| func (d *DataSource) Execute() (cty.Value, error) { | ||
| oxideClient, err := oxide.NewClient(&oxide.Config{ | ||
| Host: d.config.Host, | ||
| Token: d.config.Token, | ||
| }) | ||
| if err != nil { | ||
| return cty.NullVal(cty.EmptyObject), fmt.Errorf("failed creating oxide client: %w", err) | ||
| } | ||
|
|
||
| image, err := oxideClient.ImageView(context.TODO(), oxide.ImageViewParams{ | ||
| Image: oxide.NameOrId(d.config.Name), | ||
| Project: oxide.NameOrId(d.config.Project), // This relies on the Go SDK omitting empty strings from serialization to fetch silo images. | ||
| }) | ||
| if err != nil { | ||
| return cty.NullVal(cty.EmptyObject), fmt.Errorf("failed fetching image %q within project %q: %w", d.config.Name, d.config.Project, err) | ||
| } | ||
|
|
||
| output := Output{ | ||
| ImageID: image.Id, | ||
| } | ||
|
|
||
| return hcl2helper.HCL2ValueFromConfig(output, d.OutputSpec()), nil | ||
| } | ||
|
|
||
| // OutputSpec returns the HCL specification that Packer uses to populate output | ||
| // values for this plugin component. | ||
| func (d *DataSource) OutputSpec() hcldec.ObjectSpec { | ||
| return (&Output{}).FlatMapstructure().HCL2Spec() | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
docs-partials/component/data-source/image/Config-not-required.mdx
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,12 @@ | ||
| <!-- Code generated from the comments of the Config struct in component/data-source/image/data_source.go; DO NOT EDIT MANUALLY --> | ||
|
|
||
| - `host` (string) - Oxide API URL (e.g., silo.sys.example.com). | ||
|
|
||
| - `token` (string) - Oxide API token. | ||
|
|
||
| - `name` (string) - Name of the image to fetch. | ||
|
|
||
| - `project` (string) - Name or ID of the project containing the image to fetch. Leave blank to fetch | ||
| a silo image instead of a project image. | ||
|
|
||
| <!-- End of code generated from the comments of the Config struct in component/data-source/image/data_source.go; --> |
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,5 @@ | ||
| <!-- Code generated from the comments of the Config struct in component/data-source/image/data_source.go; DO NOT EDIT MANUALLY --> | ||
|
|
||
| Config represents the Packer configuration for this plugin component. | ||
|
|
||
| <!-- End of code generated from the comments of the Config struct in component/data-source/image/data_source.go; --> |
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,6 @@ | ||
| <!-- Code generated from the comments of the DataSource struct in component/data-source/image/data_source.go; DO NOT EDIT MANUALLY --> | ||
|
|
||
| DataSource is the concrete type that implements the Packer data source | ||
| component interface. | ||
|
|
||
| <!-- End of code generated from the comments of the DataSource struct in component/data-source/image/data_source.go; --> |
5 changes: 5 additions & 0 deletions
5
docs-partials/component/data-source/image/Output-not-required.mdx
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,5 @@ | ||
| <!-- Code generated from the comments of the Output struct in component/data-source/image/data_source.go; DO NOT EDIT MANUALLY --> | ||
|
|
||
| - `image_id` (string) - ID of the image that was fetched. | ||
|
|
||
| <!-- End of code generated from the comments of the Output struct in component/data-source/image/data_source.go; --> |
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,6 @@ | ||
| <!-- Code generated from the comments of the Output struct in component/data-source/image/data_source.go; DO NOT EDIT MANUALLY --> | ||
|
|
||
| Output represents the information returned by this plugin component for use | ||
| in other Packer plugin components. | ||
|
|
||
| <!-- End of code generated from the comments of the Output struct in component/data-source/image/data_source.go; --> |
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
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.