From 1155ab8e3c3482352bb864900fe7fd4e11e09a00 Mon Sep 17 00:00:00 2001 From: GennadySpb Date: Mon, 20 Apr 2020 16:20:05 +0300 Subject: [PATCH] Add 'target_image_folder_id' for builder/yandex (#9080) Add 'target_image_folder_id' for builder/yandex Closes #9079 --- CHANGELOG.md | 2 ++ builder/yandex/config.go | 9 +++++++++ builder/yandex/config.hcl2spec.go | 2 ++ builder/yandex/config_test.go | 5 +++++ builder/yandex/step_create_image.go | 2 +- .../partials/builder/yandex/Config-not-required.mdx | 3 +++ .../pages/partials/builder/yandex/Config-required.mdx | 2 ++ 7 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b9089c051..e201fbad9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,8 @@ * provisioner/powershell: Add cleanup step to remove temporarily created scripts; cleanup can be skipped by setting the `skip_clean` option [GH-8908] +* builder/yandex: Allow set `target_image_folder_id ` where save built + image in [GH-9079] ### Bug Fixes: * builder/amazon: Fix bug with launch_block_device_mappings in spot instances. diff --git a/builder/yandex/config.go b/builder/yandex/config.go index cbc07bdfe..f45c53637 100644 --- a/builder/yandex/config.go +++ b/builder/yandex/config.go @@ -36,6 +36,8 @@ type Config struct { Endpoint string `mapstructure:"endpoint" required:"false"` // The folder ID that will be used to launch instances and store images. // Alternatively you may set value by environment variable YC_FOLDER_ID. + // To use a different folder for looking up the source image or saving the target image to + // check options 'source_image_folder_id' and 'target_image_folder_id'. FolderID string `mapstructure:"folder_id" required:"true"` // Path to file with Service Account key in json format. This // is an alternative method to authenticate to Yandex.Cloud. Alternatively you may set environment variable @@ -104,6 +106,9 @@ type Config struct { // the launched instance. Note, the zone of the subnet must match the // zone in which the VM is launched. SubnetID string `mapstructure:"subnet_id" required:"false"` + // The ID of the folder to save built image in. + // This defaults to value of 'folder_id'. + TargetImageFolderID string `mapstructure:"target_image_folder_id" required:"false"` // If set to true, then launched instance will have external internet // access. UseIPv4Nat bool `mapstructure:"use_ipv4_nat" required:"false"` @@ -274,6 +279,10 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) { errs, errors.New("a folder_id must be specified")) } + if c.TargetImageFolderID == "" { + c.TargetImageFolderID = c.FolderID + } + for key, file := range c.MetadataFromFile { if _, err := os.Stat(file); err != nil { errs = packer.MultiErrorAppend( diff --git a/builder/yandex/config.hcl2spec.go b/builder/yandex/config.hcl2spec.go index 5cf52a379..c0410523d 100644 --- a/builder/yandex/config.hcl2spec.go +++ b/builder/yandex/config.hcl2spec.go @@ -86,6 +86,7 @@ type FlatConfig struct { SourceImageID *string `mapstructure:"source_image_id" required:"false" cty:"source_image_id"` SourceImageName *string `mapstructure:"source_image_name" cty:"source_image_name"` SubnetID *string `mapstructure:"subnet_id" required:"false" cty:"subnet_id"` + TargetImageFolderID *string `mapstructure:"target_image_folder_id" required:"false" cty:"target_image_folder_id"` UseIPv4Nat *bool `mapstructure:"use_ipv4_nat" required:"false" cty:"use_ipv4_nat"` UseIPv6 *bool `mapstructure:"use_ipv6" required:"false" cty:"use_ipv6"` UseInternalIP *bool `mapstructure:"use_internal_ip" required:"false" cty:"use_internal_ip"` @@ -182,6 +183,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "source_image_id": &hcldec.AttrSpec{Name: "source_image_id", Type: cty.String, Required: false}, "source_image_name": &hcldec.AttrSpec{Name: "source_image_name", Type: cty.String, Required: false}, "subnet_id": &hcldec.AttrSpec{Name: "subnet_id", Type: cty.String, Required: false}, + "target_image_folder_id": &hcldec.AttrSpec{Name: "target_image_folder_id", Type: cty.String, Required: false}, "use_ipv4_nat": &hcldec.AttrSpec{Name: "use_ipv4_nat", Type: cty.Bool, Required: false}, "use_ipv6": &hcldec.AttrSpec{Name: "use_ipv6", Type: cty.Bool, Required: false}, "use_internal_ip": &hcldec.AttrSpec{Name: "use_internal_ip", Type: cty.Bool, Required: false}, diff --git a/builder/yandex/config_test.go b/builder/yandex/config_test.go index 611474718..ab9ebfc3f 100644 --- a/builder/yandex/config_test.go +++ b/builder/yandex/config_test.go @@ -180,6 +180,11 @@ func TestConfigDefaults(t *testing.T) { func(c *Config) interface{} { return c.Communicator.SSHPort }, 22, }, + + { + func(c *Config) interface{} { return c.TargetImageFolderID }, + "hashicorp", + }, } for _, tc := range cases { diff --git a/builder/yandex/step_create_image.go b/builder/yandex/step_create_image.go index 4e40f188c..d16a1aa5e 100644 --- a/builder/yandex/step_create_image.go +++ b/builder/yandex/step_create_image.go @@ -26,7 +26,7 @@ func (stepCreateImage) Run(ctx context.Context, state multistep.StateBag) multis defer cancel() op, err := sdk.WrapOperation(sdk.Compute().Image().Create(ctx, &compute.CreateImageRequest{ - FolderId: c.FolderID, + FolderId: c.TargetImageFolderID, Name: c.ImageName, Family: c.ImageFamily, Description: c.ImageDescription, diff --git a/website/pages/partials/builder/yandex/Config-not-required.mdx b/website/pages/partials/builder/yandex/Config-not-required.mdx index 36daf5a77..8c627091a 100644 --- a/website/pages/partials/builder/yandex/Config-not-required.mdx +++ b/website/pages/partials/builder/yandex/Config-not-required.mdx @@ -62,6 +62,9 @@ the launched instance. Note, the zone of the subnet must match the zone in which the VM is launched. +- `target_image_folder_id` (string) - The ID of the folder to save built image in. + This defaults to value of 'folder_id'. + - `use_ipv4_nat` (bool) - If set to true, then launched instance will have external internet access. diff --git a/website/pages/partials/builder/yandex/Config-required.mdx b/website/pages/partials/builder/yandex/Config-required.mdx index c9dba971c..139ba285d 100644 --- a/website/pages/partials/builder/yandex/Config-required.mdx +++ b/website/pages/partials/builder/yandex/Config-required.mdx @@ -2,6 +2,8 @@ - `folder_id` (string) - The folder ID that will be used to launch instances and store images. Alternatively you may set value by environment variable YC_FOLDER_ID. + To use a different folder for looking up the source image or saving the target image to + check options 'source_image_folder_id' and 'target_image_folder_id'. - `token` (string) - OAuth token to use to authenticate to Yandex.Cloud. Alternatively you may set value by environment variable YC_TOKEN.