From f0e1b719d896145643e2abc621567b92f6d56d93 Mon Sep 17 00:00:00 2001 From: Gennady Lipenkov Date: Thu, 9 Jul 2020 15:33:00 +0300 Subject: [PATCH] Forms urls and support get url for yandex-export artifact --- post-processor/yandex-export/artifact.go | 6 +++ .../yandex-export/post-processor.go | 20 ++++++-- .../yandex-export/post-processor_test.go | 48 +++++++++++++++++++ 3 files changed, 71 insertions(+), 3 deletions(-) diff --git a/post-processor/yandex-export/artifact.go b/post-processor/yandex-export/artifact.go index 3c85f572c..cd117e695 100644 --- a/post-processor/yandex-export/artifact.go +++ b/post-processor/yandex-export/artifact.go @@ -8,6 +8,7 @@ const BuilderId = "packer.post-processor.yandex-export" type Artifact struct { paths []string + urls []string } func (*Artifact) BuilderId() string { @@ -28,6 +29,11 @@ func (a *Artifact) String() string { return fmt.Sprintf("Exported artifacts in: %s", a.paths) } +func (a *Artifact) Url() string { + // print url for first path + return a.urls[0] +} + func (*Artifact) State(name string) interface{} { return nil } diff --git a/post-processor/yandex-export/post-processor.go b/post-processor/yandex-export/post-processor.go index 6aa1252b8..cd8bf3c3f 100644 --- a/post-processor/yandex-export/post-processor.go +++ b/post-processor/yandex-export/post-processor.go @@ -24,6 +24,8 @@ import ( "github.com/hashicorp/packer/template/interpolate" ) +const defaultStorageEndpoint = "storage.yandexcloud.net" + type Config struct { common.PackerConfig `mapstructure:",squash"` @@ -36,7 +38,7 @@ type Config struct { // Alternatively you may set value by environment variable YC_FOLDER_ID. FolderID string `mapstructure:"folder_id" required:"true"` // Service Account ID with proper permission to modify an instance, create and attach disk and - // make upload to specific Yandex Object Storage paths + // make upload to specific Yandex Object Storage paths. ServiceAccountID string `mapstructure:"service_account_id" required:"true"` // The size of the disk in GB. This defaults to `100`, which is 100GB. DiskSizeGb int `mapstructure:"disk_size" required:"false"` @@ -234,7 +236,7 @@ func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact steps := []multistep.Step{ &yandex.StepCreateSSHKey{ Debug: p.config.PackerDebug, - DebugKeyPath: fmt.Sprintf("yc_pp_%s.pem", p.config.PackerBuildName), + DebugKeyPath: fmt.Sprintf("yc_export_pp_%s.pem", p.config.PackerBuildName), }, &yandex.StepCreateInstance{ Debug: p.config.PackerDebug, @@ -248,7 +250,10 @@ func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact p.runner = common.NewRunner(steps, p.config.PackerConfig, ui) p.runner.Run(ctx, state) - result := &Artifact{paths: p.config.Paths} + result := &Artifact{ + paths: p.config.Paths, + urls: formUrls(p.config.Paths), + } return result, false, false, nil } @@ -271,3 +276,12 @@ func ycSaneDefaults() yandex.Config { StateTimeout: 3 * time.Minute, } } + +func formUrls(paths []string) []string { + result := []string{} + for _, path := range paths { + url := fmt.Sprintf("https://%s/%s", defaultStorageEndpoint, strings.TrimPrefix(path, "s3://")) + result = append(result, url) + } + return result +} diff --git a/post-processor/yandex-export/post-processor_test.go b/post-processor/yandex-export/post-processor_test.go index d538504b0..e35381b7a 100644 --- a/post-processor/yandex-export/post-processor_test.go +++ b/post-processor/yandex-export/post-processor_test.go @@ -3,6 +3,8 @@ package yandexexport import ( "testing" + "github.com/stretchr/testify/require" + "github.com/hashicorp/packer/helper/multistep" ) @@ -64,3 +66,49 @@ func TestPostProcessor_Configure(t *testing.T) { }) } } + +func Test_formUrls(t *testing.T) { + type args struct { + paths []string + } + tests := []struct { + name string + args args + wantResult []string + }{ + { + name: "empty list", + args: args{ + paths: []string{}, + }, + wantResult: []string{}, + }, + { + name: "one element", + args: args{ + paths: []string{"s3://bucket1/object1"}, + }, + wantResult: []string{"https://" + defaultStorageEndpoint + "/bucket1/object1"}, + }, + { + name: "several elements", + args: args{ + paths: []string{ + "s3://bucket1/object1", + "s3://bucket-name/object-with/prefix/filename.blob", + "s3://bucket-too/foo/bar.test", + }, + }, + wantResult: []string{ + "https://" + defaultStorageEndpoint + "/bucket1/object1", + "https://" + defaultStorageEndpoint + "/bucket-name/object-with/prefix/filename.blob", + "https://" + defaultStorageEndpoint + "/bucket-too/foo/bar.test", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + require.Equal(t, tt.wantResult, formUrls(tt.args.paths)) + }) + } +}