Skip to content

Commit

Permalink
Add storage_endpoint argument to allow specifying custom post-proce…
Browse files Browse the repository at this point in the history
…ssing storage endpoints (#43)

* Feat: make destination storage endpoint configurable.

* Refact: add autoresolve via YC API option

* Chore: reformat debug log comments

* Refact: make consideration of storage autoresolve flag in force ipv4 option

* Chore: update autogenerated specs

* Add generated docs

Co-authored-by: Wilken Rivera <dev@wilkenrivera.com>
  • Loading branch information
karitra and nywilken authored Jul 5, 2022
1 parent 6b1eb4d commit be7f276
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@
> **Careful!** Increases payment cost.
> See [perfomance](https://cloud.yandex.com/docs/compute/concepts/disk#performance).
- `storage_endpoint` (string) - StorageEndpoint custom Yandex Object Storage endpoint to upload image, Default `storage.yandexcloud.net`.

- `storage_endpoint_autoresolve` (bool) - StorageEndpointAutoresolve auto resolve storage endpoint via YC Public API ListEndpoints call. Option has
precedence over 'storage_endpoint' option.

- `storage_region` (string) - StorageRegion custom Yandex Object region. Default `ru-central1`

<!-- End of code generated from the comments of the Config struct in post-processor/yandex-export/post-processor.go; -->
51 changes: 49 additions & 2 deletions post-processor/yandex-export/post-processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
"github.com/hashicorp/packer-plugin-yandex/builder/yandex"
"github.com/yandex-cloud/go-genproto/yandex/cloud/compute/v1"
"github.com/yandex-cloud/go-genproto/yandex/cloud/endpoint"
"github.com/yandex-cloud/go-genproto/yandex/cloud/iam/v1"
ycsdk "github.com/yandex-cloud/go-sdk"
)
Expand Down Expand Up @@ -65,7 +66,21 @@ type Config struct {
// > **Careful!** Increases payment cost.
// > See [perfomance](https://cloud.yandex.com/docs/compute/concepts/disk#performance).
SourceDiskExtraSize int `mapstructure:"source_disk_extra_size" required:"false"`
ctx interpolate.Context

// StorageEndpoint custom Yandex Object Storage endpoint to upload image, Default `storage.yandexcloud.net`.
StorageEndpoint string `mapstructure:"storage_endpoint" required:"false"`
// StorageEndpointAutoresolve auto resolve storage endpoint via YC Public API ListEndpoints call. Option has
// precedence over 'storage_endpoint' option.
StorageEndpointAutoresolve bool `mapstructure:"storage_endpoint_autoresolve" required:"false"`
// StorageRegion custom Yandex Object region. Default `ru-central1`
StorageRegion string `mapstructure:"storage_region" required:"false"`

ctx interpolate.Context
}

type storageParameters struct {
storageEndpoint string
storageRegion string
}

type PostProcessor struct {
Expand Down Expand Up @@ -140,14 +155,24 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
return errs
}

if p.config.StorageEndpoint == "" {
p.config.StorageEndpoint = defaultStorageEndpoint
}

if p.config.StorageRegion == "" {
p.config.StorageRegion = defaultStorageRegion
}

// Due to the fact that now it's impossible to go to the object storage
// through the internal network - we need access
// to the global Internet: either through ipv4 or ipv6
// TODO: delete this when access appears
if p.config.UseIPv4Nat == false && p.config.UseIPv6 == false {
if !p.config.UseIPv4Nat && !p.config.UseIPv6 &&
!p.config.StorageEndpointAutoresolve && p.config.StorageEndpoint == defaultStorageEndpoint {
log.Printf("[DEBUG] Force use IPv4")
p.config.UseIPv4Nat = true
}

p.config.Preemptible = true //? safety

if p.config.Labels == nil {
Expand Down Expand Up @@ -217,6 +242,7 @@ func (p *PostProcessor) PostProcess(ctx context.Context, ui packersdk.Ui, artifa
if err != nil {
return nil, false, false, err
}

imageDescription, err := driver.SDK().Compute().Image().Get(ctx, &compute.GetImageRequest{
ImageId: imageID,
})
Expand All @@ -240,12 +266,33 @@ func (p *PostProcessor) PostProcess(ctx context.Context, ui packersdk.Ui, artifa
return nil, false, false, err
}

if p.config.StorageEndpointAutoresolve {
ui.Say("Resolving storage endpoint...")
response, err := driver.SDK().ApiEndpoint().ApiEndpoint().Get(ctx, &endpoint.GetApiEndpointRequest{
ApiEndpointId: "storage",
})

if err != nil {
return nil, false, false, err
}

p.config.StorageEndpoint = response.Address
}

log.Printf("[DEBUG] Using storage endpoint: '%s'", p.config.StorageEndpoint)

storageParameters := &storageParameters{
storageEndpoint: p.config.StorageEndpoint,
storageRegion: p.config.StorageRegion,
}

// Set up the state.
state := new(multistep.BasicStateBag)
state.Put("config", &yandexConfig)
state.Put("driver", driver)
state.Put("sdk", driver.SDK())
state.Put("ui", ui)
state.Put("storageParams", storageParameters)

// Build the steps.
steps := []multistep.Step{
Expand Down
Loading

0 comments on commit be7f276

Please sign in to comment.