From eaec3e55644b875660867b9b3f17e3975c3ac8a6 Mon Sep 17 00:00:00 2001 From: Shaun McEvoy <38361027+shaunmcevoy@users.noreply.github.com> Date: Wed, 7 Apr 2021 02:36:08 -0600 Subject: [PATCH] Add Image Storage Locations field to Google Compute Import post-processor (#10864) * add image storage locations to Google Compute Import --- post-processor/googlecompute-import/post-processor.go | 7 +++++-- .../googlecompute-import/post-processor.hcl2spec.go | 2 ++ .../googlecompute-import/Config-not-required.mdx | 2 ++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/post-processor/googlecompute-import/post-processor.go b/post-processor/googlecompute-import/post-processor.go index 3d92476c6..b36e254e6 100644 --- a/post-processor/googlecompute-import/post-processor.go +++ b/post-processor/googlecompute-import/post-processor.go @@ -59,6 +59,8 @@ type Config struct { ImageLabels map[string]string `mapstructure:"image_labels"` //The unique name of the resulting image. ImageName string `mapstructure:"image_name" required:"true"` + //Specifies a Cloud Storage location, either regional or multi-regional, where image content is to be stored. If not specified, the multi-region location closest to the source is chosen automatically. + ImageStorageLocations []string `mapstructure:"image_storage_locations"` //Skip removing the TAR file uploaded to the GCS //bucket after the import process has completed. "true" means that we should //leave it in the GCS bucket, "false" means to clean it out. Defaults to @@ -182,7 +184,7 @@ func (p *PostProcessor) PostProcess(ctx context.Context, ui packersdk.Ui, artifa return nil, false, false, err } - gceImageArtifact, err := CreateGceImage(opts, ui, p.config.ProjectId, rawImageGcsPath, p.config.ImageName, p.config.ImageDescription, p.config.ImageFamily, p.config.ImageLabels, p.config.ImageGuestOsFeatures, shieldedVMStateConfig) + gceImageArtifact, err := CreateGceImage(opts, ui, p.config.ProjectId, rawImageGcsPath, p.config.ImageName, p.config.ImageDescription, p.config.ImageFamily, p.config.ImageLabels, p.config.ImageGuestOsFeatures, shieldedVMStateConfig, p.config.ImageStorageLocations) if err != nil { return nil, false, false, err } @@ -295,7 +297,7 @@ func UploadToBucket(opts option.ClientOption, ui packersdk.Ui, artifact packersd return storageObject.SelfLink, nil } -func CreateGceImage(opts option.ClientOption, ui packersdk.Ui, project string, rawImageURL string, imageName string, imageDescription string, imageFamily string, imageLabels map[string]string, imageGuestOsFeatures []string, shieldedVMStateConfig *compute.InitialStateConfig) (packersdk.Artifact, error) { +func CreateGceImage(opts option.ClientOption, ui packersdk.Ui, project string, rawImageURL string, imageName string, imageDescription string, imageFamily string, imageLabels map[string]string, imageGuestOsFeatures []string, shieldedVMStateConfig *compute.InitialStateConfig, imageStorageLocations []string) (packersdk.Artifact, error) { service, err := compute.NewService(context.TODO(), opts) if err != nil { @@ -319,6 +321,7 @@ func CreateGceImage(opts option.ClientOption, ui packersdk.Ui, project string, r RawDisk: &compute.ImageRawDisk{Source: rawImageURL}, SourceType: "RAW", ShieldedInstanceInitialState: shieldedVMStateConfig, + StorageLocations: imageStorageLocations, } ui.Say(fmt.Sprintf("Creating GCE image %v...", imageName)) diff --git a/post-processor/googlecompute-import/post-processor.hcl2spec.go b/post-processor/googlecompute-import/post-processor.hcl2spec.go index 151a9c628..af2e4e84b 100644 --- a/post-processor/googlecompute-import/post-processor.hcl2spec.go +++ b/post-processor/googlecompute-import/post-processor.hcl2spec.go @@ -29,6 +29,7 @@ type FlatConfig struct { ImageGuestOsFeatures []string `mapstructure:"image_guest_os_features" cty:"image_guest_os_features" hcl:"image_guest_os_features"` ImageLabels map[string]string `mapstructure:"image_labels" cty:"image_labels" hcl:"image_labels"` ImageName *string `mapstructure:"image_name" required:"true" cty:"image_name" hcl:"image_name"` + ImageStorageLocations []string `mapstructure:"image_storage_locations" cty:"image_storage_locations" hcl:"image_storage_locations"` SkipClean *bool `mapstructure:"skip_clean" cty:"skip_clean" hcl:"skip_clean"` VaultGCPOauthEngine *string `mapstructure:"vault_gcp_oauth_engine" cty:"vault_gcp_oauth_engine" hcl:"vault_gcp_oauth_engine"` ImagePlatformKey *string `mapstructure:"image_platform_key" cty:"image_platform_key" hcl:"image_platform_key"` @@ -68,6 +69,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "image_guest_os_features": &hcldec.AttrSpec{Name: "image_guest_os_features", Type: cty.List(cty.String), Required: false}, "image_labels": &hcldec.AttrSpec{Name: "image_labels", Type: cty.Map(cty.String), Required: false}, "image_name": &hcldec.AttrSpec{Name: "image_name", Type: cty.String, Required: false}, + "image_storage_locations": &hcldec.AttrSpec{Name: "image_storage_locations", Type: cty.List(cty.String), Required: false}, "skip_clean": &hcldec.AttrSpec{Name: "skip_clean", Type: cty.Bool, Required: false}, "vault_gcp_oauth_engine": &hcldec.AttrSpec{Name: "vault_gcp_oauth_engine", Type: cty.String, Required: false}, "image_platform_key": &hcldec.AttrSpec{Name: "image_platform_key", Type: cty.String, Required: false}, diff --git a/website/content/partials/post-processor/googlecompute-import/Config-not-required.mdx b/website/content/partials/post-processor/googlecompute-import/Config-not-required.mdx index 446643a69..2e9929824 100644 --- a/website/content/partials/post-processor/googlecompute-import/Config-not-required.mdx +++ b/website/content/partials/post-processor/googlecompute-import/Config-not-required.mdx @@ -18,6 +18,8 @@ - `image_labels` (map[string]string) - Key/value pair labels to apply to the created image. +- `image_storage_locations` ([]string) - Specifies a Cloud Storage location, either regional or multi-regional, where image content is to be stored. If not specified, the multi-region location closest to the source is chosen automatically. + - `skip_clean` (bool) - Skip removing the TAR file uploaded to the GCS bucket after the import process has completed. "true" means that we should leave it in the GCS bucket, "false" means to clean it out. Defaults to