Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions component/builder/instance/step_instance_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (o *stepInstanceCreate) Run(ctx context.Context, stateBag multistep.StateBa
instance, err := oxideClient.InstanceCreate(ctx, oxide.InstanceCreateParams{
Project: oxide.NameOrId(config.Project),
Body: &oxide.InstanceCreate{
AntiAffinityGroups: []oxide.NameOrId{},
BootDisk: &oxide.InstanceDiskAttachment{
Description: "Created by Packer.",
DiskSource: oxide.DiskSource{
Expand Down
130 changes: 130 additions & 0 deletions component/data-source/image/data_source.go
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()
}
60 changes: 60 additions & 0 deletions component/data-source/image/data_source.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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; -->
5 changes: 5 additions & 0 deletions docs-partials/component/data-source/image/Config.mdx
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; -->
6 changes: 6 additions & 0 deletions docs-partials/component/data-source/image/DataSource.mdx
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; -->
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; -->
6 changes: 6 additions & 0 deletions docs-partials/component/data-source/image/Output.mdx
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; -->
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/packer-plugin-sdk/plugin"
"github.com/hashicorp/packer-plugin-sdk/version"
"github.com/oxidecomputer/packer-plugin-oxide/component/builder/instance"
"github.com/oxidecomputer/packer-plugin-oxide/component/data-source/image"
)

var (
Expand All @@ -30,6 +31,7 @@ var (
func main() {
pluginSet := plugin.NewSet()
pluginSet.RegisterBuilder("instance", new(instance.Builder))
pluginSet.RegisterDatasource("image", new(image.DataSource))
pluginSet.SetVersion(
version.NewPluginVersion(Version, VersionPreRelease, VersionMetadata),
)
Expand Down
8 changes: 6 additions & 2 deletions template.pkr.hcl
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
packer {
required_plugins {
oxide = {
version = ">=v0.0.1"
version = ">= 0.0.1"
source = "github.com/oxidecomputer/oxide"
}
}
}

data "oxide-image" "ubuntu" {
name = "noble"
}

source "oxide-instance" "example" {
project = "matthewsanabria"
image_id = "feb2c8ee-5a1d-4d66-beeb-289b860561bf"
image_id = data.oxide-image.ubuntu.image_id

ssh_username = "ubuntu"
ssh_agent_auth = true
Expand Down