Skip to content
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

Add additional options to control storage endpoints on import #83

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@
after the import process has completed. Possible values are: `true` to
leave it in the bucket, `false` to remove it. Default is `false`.

- `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-import/post-processor.go; -->
40 changes: 39 additions & 1 deletion post-processor/yandex-import/post-processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package yandeximport
import (
"context"
"fmt"
"log"

"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer-plugin-sdk/common"
Expand All @@ -17,9 +18,15 @@ import (
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
"github.com/hashicorp/packer-plugin-yandex/builder/yandex"
yandexexport "github.com/hashicorp/packer-plugin-yandex/post-processor/yandex-export"
"github.com/yandex-cloud/go-genproto/yandex/cloud/endpoint"
"github.com/yandex-cloud/go-genproto/yandex/cloud/iam/v1/awscompatibility"
)

const (
defaultStorageEndpoint = "storage.yandexcloud.net"
defaultStorageRegion = "ru-central1"
)

type Config struct {
common.PackerConfig `mapstructure:",squash"`
yandex.AccessConfig `mapstructure:",squash"`
Expand All @@ -42,6 +49,14 @@ type Config struct {
// leave it in the bucket, `false` to remove it. Default is `false`.
SkipClean bool `mapstructure:"skip_clean" required:"false"`

// 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
}

Expand Down Expand Up @@ -124,7 +139,30 @@ func (p *PostProcessor) PostProcess(ctx context.Context, ui packersdk.Ui, artifa
return nil, false, false, err
}

storageClient, err := newYCStorageClient("", respWithKey.GetAccessKey().GetKeyId(), respWithKey.GetSecret())
if p.config.StorageEndpoint == "" {
p.config.StorageEndpoint = defaultStorageEndpoint
}

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

if p.config.StorageEndpointAutoresolve {
ui.Say("Resolving storage endpoint...")
response, err := client.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)

storageClient, err := newYCStorageClient(p.config.StorageEndpoint, respWithKey.GetAccessKey().GetKeyId(), respWithKey.GetSecret())
if err != nil {
return nil, false, false, fmt.Errorf("error create object storage client: %s", err)
}
Expand Down
106 changes: 56 additions & 50 deletions post-processor/yandex-import/post-processor.hcl2spec.go

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

5 changes: 1 addition & 4 deletions post-processor/yandex-import/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ import (
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
)

const defaultS3Region = "ru-central1"
const defaultStorageEndpoint = "storage.yandexcloud.net"

func newYCStorageClient(storageEndpoint, accessKey, secretKey string) (*s3.S3, error) {
var creds *credentials.Credentials

Expand All @@ -28,7 +25,7 @@ func newYCStorageClient(storageEndpoint, accessKey, secretKey string) (*s3.S3, e

s3Config := &aws.Config{
Endpoint: aws.String(storageEndpoint),
Region: aws.String(defaultS3Region),
Region: aws.String(defaultStorageRegion),
}

switch {
Expand Down