Skip to content

Commit

Permalink
Merge pull request #39 from max-soe/master
Browse files Browse the repository at this point in the history
Share the snapshots of private stemcells
  • Loading branch information
rkoster committed May 16, 2024
2 parents 4345f2c + acf67f3 commit 5f7466a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
23 changes: 19 additions & 4 deletions driver/copy_ami_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,6 @@ func (d *SDKCopyAmiDriver) Create(driverConfig resources.AmiDriverConfig) (resou
}
}

if driverConfig.Encrypted {
return resources.Ami{ID: *amiIDptr, Region: dstRegion}, nil
}

var snapshotIDptr *string
var snapshotErr error

Expand Down Expand Up @@ -221,6 +217,25 @@ func (d *SDKCopyAmiDriver) Create(driverConfig resources.AmiDriverConfig) (resou
d.logger.Printf("Error tagging Snapshot: %s, Error: %s ", *snapshotIDptr, err.Error())
}

for _, account := range driverConfig.SharedWithAccounts {
modifySnapshotAttributeInput := &ec2.ModifySnapshotAttributeInput{
SnapshotId: snapshotIDptr,
Attribute: aws.String("createVolumePermission"),
OperationType: aws.String("add"),
UserIds: []*string{
aws.String(account),
},
}
_, err = ec2Client.ModifySnapshotAttribute(modifySnapshotAttributeInput)
if err != nil {
return resources.Ami{}, fmt.Errorf("sharing snapshot with id %s with account %s: %v", *snapshotIDptr, account, err)
}
}

if driverConfig.Encrypted {
return resources.Ami{ID: *amiIDptr, Region: dstRegion}, nil
}

modifySnapshotAttributeInput := &ec2.ModifySnapshotAttributeInput{
SnapshotId: snapshotIDptr,
Attribute: aws.String("createVolumePermission"),
Expand Down
13 changes: 13 additions & 0 deletions driver/create_ami_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,19 @@ func (d *SDKCreateAmiDriver) Create(driverConfig resources.AmiDriverConfig) (res
if err != nil {
return resources.Ami{}, fmt.Errorf("failed to share AMI '%s' with account '%s': %w", *amiIDptr, account, err)
}

modifySnapshotAttributeInput := &ec2.ModifySnapshotAttributeInput{
SnapshotId: aws.String(driverConfig.SnapshotID),
Attribute: aws.String("createVolumePermission"),
OperationType: aws.String("add"),
UserIds: []*string{
aws.String(account),
},
}
_, err = d.ec2Client.ModifySnapshotAttribute(modifySnapshotAttributeInput)
if err != nil {
return resources.Ami{}, fmt.Errorf("sharing snapshot with id %s with account %s: %v", driverConfig.SnapshotID, account, err)
}
}

d.logger.Printf("waiting for AMI: %s to be available\n", *amiIDptr)
Expand Down

0 comments on commit 5f7466a

Please sign in to comment.