Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…outscale into main
  • Loading branch information
outscale-hmi committed May 22, 2023
2 parents 6453316 + 37c013f commit 37ac443
Show file tree
Hide file tree
Showing 23 changed files with 142 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17
go-version: '>=1.19.0'
- name: Describe plugin
id: plugin_describe
run: echo "::set-output name=api_version::$(go run . describe | jq -r '.api_version')"
Expand Down
3 changes: 2 additions & 1 deletion builder/osc/bsu/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
Regions: b.config.OMIRegions,
},
&stepCreateOMI{
RawRegion: b.config.RawRegion,
RawRegion: b.config.RawRegion,
ProductCodes: b.config.ProductCodes,
},
&osccommon.StepUpdateOMIAttributes{
AccountIds: b.config.OMIAccountIDs,
Expand Down
2 changes: 2 additions & 0 deletions builder/osc/bsu/builder.hcl2spec.go

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

32 changes: 32 additions & 0 deletions builder/osc/bsu/builder_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ func TestAccBuilder_basic(t *testing.T) {
acctest.TestPlugin(t, testCase)
}

func TestAccBuilder_GoodProductCode(t *testing.T) {
testCase := &acctest.PluginTestCase{
Name: "bsu_product_code_test",
Template: testBuilderAccWithGoodProductCode,
Check: func(buildCommand *exec.Cmd, logfile string) error {
if buildCommand.ProcessState != nil {
if buildCommand.ProcessState.ExitCode() != 0 {
return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
}
}
return nil
},
}
acctest.TestPlugin(t, testCase)
}

const testBuilderAccBasic = `
{
"builders": [{
Expand All @@ -39,3 +55,19 @@ const testBuilderAccBasic = `
}]
}
`

const testBuilderAccWithGoodProductCode = `
{
"builders": [{
"type": "outscale-bsu",
"region": "eu-west-2",
"vm_type": "t2.micro",
"source_omi": "ami-68ed4301",
"ssh_username": "outscale",
"omi_name": "packer-test",
"product_codes": ["pty-2ffc33dc"],
"associate_public_ip_address": true,
"force_deregister": true
}]
}
`
8 changes: 6 additions & 2 deletions builder/osc/bsu/step_create_omi.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
)

type stepCreateOMI struct {
image *oscgo.Image
RawRegion string
image *oscgo.Image
RawRegion string
ProductCodes []string
}

func (s *stepCreateOMI) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
Expand All @@ -35,6 +36,9 @@ func (s *stepCreateOMI) Run(ctx context.Context, state multistep.StateBag) multi
if config.OMIDescription != "" {
createOpts.Description = &config.OMIDescription
}
if config.ProductCodes != nil {
createOpts.ProductCodes = &config.ProductCodes
}

resp, _, err := oscconn.Api.ImageApi.CreateImage(oscconn.Auth).CreateImageRequest(createOpts).Execute()
if err != nil || resp.GetImage().ImageId == nil {
Expand Down
1 change: 1 addition & 0 deletions builder/osc/bsusurrogate/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
OMIDevices: omiDevices,
LaunchDevices: launchOSCDevices,
RawRegion: b.config.RawRegion,
ProductCodes: b.config.ProductCodes,
},
&osccommon.StepUpdateOMIAttributes{
AccountIds: b.config.OMIAccountIDs,
Expand Down
2 changes: 2 additions & 0 deletions builder/osc/bsusurrogate/builder.hcl2spec.go

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

4 changes: 4 additions & 0 deletions builder/osc/bsusurrogate/step_register_omi.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type StepRegisterOMI struct {
LaunchDevices []oscgo.BlockDeviceMappingVmCreation
image *oscgo.Image
RawRegion string
ProductCodes []string
}

func (s *StepRegisterOMI) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
Expand All @@ -41,6 +42,9 @@ func (s *StepRegisterOMI) Run(ctx context.Context, state multistep.StateBag) mul
if config.OMIDescription != "" {
registerOpts.Description = &config.OMIDescription
}
if config.ProductCodes != nil {
registerOpts.ProductCodes = &config.ProductCodes
}
registerResp, _, err := oscconn.Api.ImageApi.CreateImage(oscconn.Auth).CreateImageRequest(registerOpts).Execute()
if err != nil {
state.Put("error", fmt.Errorf("Error registering OMI: %s", err))
Expand Down
1 change: 1 addition & 0 deletions builder/osc/chroot/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
&StepCreateOMI{
RootVolumeSize: b.config.RootVolumeSize,
RawRegion: b.config.RawRegion,
ProductCodes: b.config.ProductCodes,
},
&osccommon.StepUpdateOMIAttributes{
AccountIds: b.config.OMIAccountIDs,
Expand Down
2 changes: 2 additions & 0 deletions builder/osc/chroot/builder.hcl2spec.go

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

4 changes: 4 additions & 0 deletions builder/osc/chroot/step_create_omi.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type StepCreateOMI struct {
RootVolumeSize int64
RawRegion string
ProductCodes []string
}

func (s *StepCreateOMI) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
Expand Down Expand Up @@ -79,6 +80,9 @@ func (s *StepCreateOMI) Run(ctx context.Context, state multistep.StateBag) multi
if config.OMIDescription != "" {
registerOpts.Description = &config.OMIDescription
}
if config.ProductCodes != nil {
registerOpts.ProductCodes = &config.ProductCodes
}

registerResp, _, err := osconn.Api.ImageApi.CreateImage(osconn.Auth).CreateImageRequest(registerOpts).Execute()
if err != nil {
Expand Down
22 changes: 16 additions & 6 deletions builder/osc/common/access_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"os"

"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
"github.com/outscale/osc-sdk-go/v2"
oscgo "github.com/outscale/osc-sdk-go/v2"
"github.com/outscale/packer-plugin-outscale/version"
)
Expand Down Expand Up @@ -133,13 +132,24 @@ func (c *AccessConfig) NewOSCClientByRegion(region string) *OscClient {
config := oscgo.NewConfiguration()
config.Debug = true
config.UserAgent = fmt.Sprintf("packer-plugin-outscale/%s", version.PluginVersion.String())
auth := context.WithValue(context.Background(), oscgo.ContextAWSv4, osc.AWSv4{
AccessKey: os.Getenv("OSC_ACCESS_KEY"),
SecretKey: os.Getenv("OSC_SECRET_KEY"),
auth := context.WithValue(context.Background(), oscgo.ContextAWSv4, oscgo.AWSv4{
AccessKey: c.AccessKey,
SecretKey: c.SecretKey,
})
config.HTTPClient = skipClient
auth = context.WithValue(auth, oscgo.ContextServerIndex, 0)
auth = context.WithValue(auth, oscgo.ContextServerVariables, map[string]string{"region": os.Getenv("OSC_REGION")})
config.Servers = oscgo.ServerConfigurations{
{
URL: fmt.Sprintf("https://api.%s.%s", region, c.CustomEndpointOAPI),
Description: "Loaded from profile",
Variables: map[string]oscgo.ServerVariable{
"region": {
Description: "Loaded from env variables",
DefaultValue: region,
EnumValues: []string{region},
},
},
},
}

oscClient := &OscClient{
Api: oscgo.NewAPIClient(config),
Expand Down
1 change: 1 addition & 0 deletions builder/osc/common/omi_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type OMIConfig struct {
SnapshotTags TagMap `mapstructure:"snapshot_tags"`
SnapshotAccountIDs []string `mapstructure:"snapshot_account_ids"`
GlobalPermission bool `mapstructure:"global_permission"`
ProductCodes []string `mapstructure:"product_codes"`
}

func (c *OMIConfig) Prepare(accessConfig *AccessConfig, ctx *interpolate.Context) []error {
Expand Down
10 changes: 5 additions & 5 deletions datasource/omi/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ func (d *Datasource) Configure(raws ...interface{}) error {
}

type DatasourceOutput struct {
// The ID of the AMI.
// The ID of the OMI.
ID string `mapstructure:"id"`
// The name of the AMI.
// The name of the OMI.
Name string `mapstructure:"name"`
// The date of creation of the AMI.
// The date of creation of the OMI.
CreationDate string `mapstructure:"creation_date"`
// The AWS account ID of the owner.
// The Outscale account Id of the owner.
Owner string `mapstructure:"owner"`
// The owner alias.
OwnerName string `mapstructure:"owner_name"`
// The key/value combination of the tags assigned to the AMI.
// The key/value combination of the tags assigned to the OMI.
Tags map[string]string `mapstructure:"tags"`
}

Expand Down
15 changes: 15 additions & 0 deletions docs-partials/datasource/omi/DatasourceOutput.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!-- Code generated from the comments of the DatasourceOutput struct in datasource/omi/data.go; DO NOT EDIT MANUALLY -->

- `id` (string) - The ID of the OMI.

- `name` (string) - The name of the OMI.

- `creation_date` (string) - The date of creation of the OMI.

- `owner` (string) - The Outscale account Id of the owner.

- `owner_name` (string) - The owner alias.

- `tags` (map[string]string) - The key/value combination of the tags assigned to the OMI.

<!-- End of code generated from the comments of the DatasourceOutput struct in datasource/omi/data.go; -->
10 changes: 5 additions & 5 deletions docs/builders/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ Packer is able to create Outscale OMIs. To achieve this, Packer comes with
multiple builders depending on the strategy you want to use to build the OMI.
Packer supports the following builders at the moment:

- [outscale-bsu](/docs/builders/outscale-bsu) - Create BSU-backed OMIs by
- [outscale-bsu](/packer/plugins/builders/outscale/outscale-bsu) - Create BSU-backed OMIs by
launching a source OMI and re-packaging it into a new OMI after
provisioning. If in doubt, use this builder, which is the easiest to get
started with.

- [outscale-chroot](/docs/builders/outscale-chroot) - Create EBS-backed OMIs
- [outscale-chroot](/packer/plugins/builders/outscale/outscale-chroot) - Create EBS-backed OMIs
from an existing OUTSCALE VM by mounting the root device and using a
[Chroot](https://en.wikipedia.org/wiki/Chroot) environment to provision
that device. This is an **advanced builder and should not be used by
newcomers**. However, it is also the fastest way to build an EBS-backed OMI
since no new OUTSCALE VM needs to be launched.

- [outscale-bsusurrogate](/docs/builders/outscale-bsusurrogate) - Create BSU-backed OMIs from scratch. Works similarly to the `chroot` builder but does
- [outscale-bsusurrogate](/packer/plugins/builders/outscale/outscale-bsusurrogate) - Create BSU-backed OMIs from scratch. Works similarly to the `chroot` builder but does
not require running in Outscale VM. This is an **advanced builder and should not be
used by newcomers**.

-> **Don't know which builder to use?** If in doubt, use the [outscale-bsu
builder](/docs/builders/outscale-bsu). It is much easier to use and Outscale generally recommends BSU-backed images nowadays.
builder](/packer/plugins/builders/outscale/outscale-bsu). It is much easier to use and Outscale generally recommends BSU-backed images nowadays.

# Outscale BSU Volume Builder

Packer is able to create Outscale BSU Volumes which are preinitialized with a filesystem and data.

- [outscale-bsuvolume](/docs/builders/outscale-bsuvolume) - Create EBS volumes by launching a source OMI with block devices mapped. Provision the VM, then destroy it, retaining the EBS volumes.
- [outscale-bsuvolume](/packer/plugins/builders/outscale/outscale-bsuvolume) - Create EBS volumes by launching a source OMI with block devices mapped. Provision the VM, then destroy it, retaining the EBS volumes.

## Authentication

Expand Down
2 changes: 2 additions & 0 deletions docs/builders/outscale-bsu.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ builder.

- `omi_account_ids` (array of strings) - A list of account IDs that have access to launch the resulting OMI(s). By default no additional users other than the user creating the OMIS has permissions to launch it.

- `product_codes` ([]string) - A list of product codes to associate with the OMI. By default no product codes are associated with the OMI.

- `global_permission` (boolean) - This option is useful to make the OMI publicly accessible.

- `omi_virtualization_type` (string) - The type of virtualization for the OMI you are building. This option must match the supported virtualization type of `source_omi`. Can be `paravirtual` or `hvm`.
Expand Down
2 changes: 2 additions & 0 deletions docs/builders/outscale-bsusurrogate.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ builder.

- `omi_account_ids` (array of strings) - A list of account IDs that have access to launch the resulting OMI(s). By default no additional users other than the user creating the OMIS has permissions to launch it.

- `product_codes` ([]string) - A list of product codes to associate with the OMI. By default no product codes are associated with the OMI.

- `global_permission` (boolean) - This option is useful to make the OMI publicly accessible.

- `omi_virtualization_type` (string) - The type of virtualization for the OMI you are building. This option must match the supported virtualization type of `source_omi`. Can be `paravirtual` or `hvm`.
Expand Down
2 changes: 2 additions & 0 deletions docs/builders/outscale-chroot.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ each category, the available configuration keys are alphabetized.

- `omi_account_ids` (array of strings) - A list of account IDs that have access to launch the resulting OMI(s). By default no additional users other than the user creating the OMIS has permissions to launch it.

- `product_codes` ([]string) - A list of product codes to associate with the OMI. By default no product codes are associated with the OMI.

- `global_permission` (boolean) - This option is useful to make the OMI publicly accessible.

- `omi_virtualization_type` (string) - The type of virtualization for the OMI you are building. This option must match the supported virtualization type of `source_omi`. Can be `paravirtual` or `hvm`.
Expand Down
33 changes: 33 additions & 0 deletions docs/datasources/omi.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
description: |
The Outscale OMI data source provides information from an OMI that will be fetched based
on the filter options provided in the configuration.
page_title: Outscale OMI - Data Source
nav_title: Outscale OMI
---

# Outscale OMI Data Source

Type: `outscale-omi`

The Outscale OMI Data source will filter and fetch an Outscale OMI, and output all the OMI information that will
be then available to use in the [Outscale builders](https://developer.hashicorp.com/packer/plugins/builders/outscale).

-> **Note:** Data sources is a feature exclusively available to HCL2 templates.

Basic example of usage:

```hcl
data "outscale-omi" "basic-example" {
filters = {
virtualization-type = "hvm"
name = "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*"
root-device-type = "ebs"
}
owners = ["099720109477"]
most_recent = true
}
```
This selects the most recent Ubuntu 16.04 HVM EBS OMI from Canonical. Note that the data source will fail unless
*exactly* one OMI is returned. In the above example, `most_recent` will cause this to succeed by selecting the newest image.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/hashicorp/hcl/v2 v2.13.0
github.com/hashicorp/packer-plugin-sdk v0.3.2
github.com/mitchellh/mapstructure v1.5.0
github.com/outscale/osc-sdk-go/v2 v2.12.0
github.com/outscale/osc-sdk-go/v2 v2.17.0
github.com/zclconf/go-cty v1.10.0
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab
)
Expand Down
Loading

0 comments on commit 37ac443

Please sign in to comment.