Skip to content

Commit 852fb88

Browse files
Added support for Private Image Sharing (#391)
1 parent e410d03 commit 852fb88

6 files changed

Lines changed: 78 additions & 0 deletions

File tree

.web-docs/components/builder/linode/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ can also be supplied to override the typical auto-generated key:
127127

128128
- `image_regions` ([]string) - The regions where the outcome image will be replicated to.
129129

130+
- `image_share_group_ids` ([]int) - Image Share Group IDs to add the newly created private image to
131+
immediately after image creation.
132+
130133
- `interface_generation` (string) - Specifies the interface type for the Linode. The value can be either
131134
`legacy_config` or `linode`. The default value is determined by the
132135
`interfaces_for_new_linodes` setting in the account settings.
@@ -426,6 +429,7 @@ source "linode" "example" {
426429
image = "linode/debian11"
427430
image_description = "My Private Image"
428431
image_label = "private-image-${local.timestamp}"
432+
image_share_group_ids = [12345]
429433
instance_label = "temporary-linode-${local.timestamp}"
430434
instance_type = "g6-nanode-1"
431435
linode_token = "YOUR API TOKEN"
@@ -453,6 +457,7 @@ build {
453457
"instance_label": "temporary-linode-{{timestamp}}",
454458
"image_label": "private-image-{{timestamp}}",
455459
"image_description": "My Private Image",
460+
"image_share_group_ids": [12345],
456461
"ssh_username": "root"
457462
}
458463
}

builder/linode/builder_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,3 +696,34 @@ func TestBuilderPrepare_MetadataTagsFirewallID(t *testing.T) {
696696
t.Errorf("got %v, expected %v", b.config.Tags, expectedTags)
697697
}
698698
}
699+
700+
func TestBuilderPrepare_ImageShareGroupIDs(t *testing.T) {
701+
var b Builder
702+
config := testConfig()
703+
704+
delete(config, "image_share_group_ids")
705+
_, warnings, err := b.Prepare(config)
706+
if len(warnings) > 0 {
707+
t.Fatalf("bad: %#v", warnings)
708+
}
709+
if err != nil {
710+
t.Fatalf("unexpected error: %v", err)
711+
}
712+
if len(b.config.ImageShareGroupIDs) != 0 {
713+
t.Errorf("expected nil or empty, got %v", b.config.ImageShareGroupIDs)
714+
}
715+
716+
expected := []int{101, 202, 303}
717+
config["image_share_group_ids"] = expected
718+
b = Builder{}
719+
_, warnings, err = b.Prepare(config)
720+
if len(warnings) > 0 {
721+
t.Fatalf("bad: %#v", warnings)
722+
}
723+
if err != nil {
724+
t.Fatalf("should not have error: %s", err)
725+
}
726+
if !reflect.DeepEqual(b.config.ImageShareGroupIDs, expected) {
727+
t.Errorf("got %v, expected %v", b.config.ImageShareGroupIDs, expected)
728+
}
729+
}

builder/linode/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ type Config struct {
159159
// The regions where the outcome image will be replicated to.
160160
ImageRegions []string `mapstructure:"image_regions" required:"false"`
161161

162+
// Image Share Group IDs to add the newly created private image to
163+
// immediately after image creation.
164+
ImageShareGroupIDs []int `mapstructure:"image_share_group_ids" required:"false"`
165+
162166
// Specifies the interface type for the Linode. The value can be either
163167
// `legacy_config` or `linode`. The default value is determined by the
164168
// `interfaces_for_new_linodes` setting in the account settings.

builder/linode/config.hcl2spec.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builder/linode/step_create_image.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package linode
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/hashicorp/packer-plugin-sdk/multistep"
78
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
@@ -45,6 +46,39 @@ func (s *stepCreateImage) Run(ctx context.Context, state multistep.StateBag) mul
4546
return handleError("Failed to wait for image creation", err)
4647
}
4748

49+
// Add the image to Image Share Groups, if configured
50+
if len(c.ImageShareGroupIDs) > 0 {
51+
for _, shareGroupID := range c.ImageShareGroupIDs {
52+
ui.Say(fmt.Sprintf(
53+
"Adding image %s to image share group %d...",
54+
image.ID,
55+
shareGroupID,
56+
))
57+
58+
_, err := s.client.ImageShareGroupAddImages(
59+
ctx,
60+
shareGroupID,
61+
linodego.ImageShareGroupAddImagesOptions{
62+
Images: []linodego.ImageShareGroupImage{
63+
{
64+
ID: image.ID,
65+
},
66+
},
67+
},
68+
)
69+
if err != nil {
70+
return handleError(
71+
fmt.Sprintf(
72+
"Failed to add image %s to image share group %d",
73+
image.ID,
74+
shareGroupID,
75+
),
76+
err,
77+
)
78+
}
79+
}
80+
}
81+
4882
if len(c.ImageRegions) > 0 {
4983
image, err = s.client.ReplicateImage(ctx, image.ID, linodego.ImageReplicateOptions{
5084
Regions: c.ImageRegions,

docs/builders/linode.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ source "linode" "example" {
186186
image = "linode/debian11"
187187
image_description = "My Private Image"
188188
image_label = "private-image-${local.timestamp}"
189+
image_share_group_ids = [12345]
189190
instance_label = "temporary-linode-${local.timestamp}"
190191
instance_type = "g6-nanode-1"
191192
linode_token = "YOUR API TOKEN"
@@ -213,6 +214,7 @@ build {
213214
"instance_label": "temporary-linode-{{timestamp}}",
214215
"image_label": "private-image-{{timestamp}}",
215216
"image_description": "My Private Image",
217+
"image_share_group_ids": [12345],
216218
"ssh_username": "root"
217219
}
218220
}

0 commit comments

Comments
 (0)