Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix check call vailidy when global_permission is true #193

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ jobs:
fi
test:
strategy:
max-parallel: 1
matrix:
os: [ubuntu-latest, windows-latest]
version: [12, 14, 16]
Expand Down
6 changes: 5 additions & 1 deletion builder/common/step_update_omi.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (s *StepUpdateOMIAttributes) Run(_ context.Context, state multistep.StateBa
valid := false
valid = valid || (s.AccountIds != nil && len(s.AccountIds) > 0)
valid = valid || (s.SnapshotAccountIds != nil && len(s.SnapshotAccountIds) > 0)

valid = valid || s.GlobalPermission == true
if !valid {
return multistep.ActionContinue
}
Expand Down Expand Up @@ -74,6 +74,10 @@ func (s *StepUpdateOMIAttributes) Run(_ context.Context, state multistep.StateBa
}
}

if s.AccountIds == nil || len(s.AccountIds) == 0 {
return multistep.ActionContinue
}

// Updating snapshot attributes
for region, region_snapshots := range snapshots {
for _, snapshot := range region_snapshots {
Expand Down
90 changes: 82 additions & 8 deletions builder/common/step_update_omi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,107 @@ import (
"bytes"
"context"

"os"

packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
oscgo "github.com/outscale/osc-sdk-go/v2"
)

// Create statebag for running test
func tState() multistep.StateBag {
func tState(omiId string, snapId string) (multistep.StateBag, error) {
region := os.Getenv("OSC_REGION")
state := new(multistep.BasicStateBag)
accessConfig := &AccessConfig{}
accessConfig := &AccessConfig{
RawRegion: region,
}

oscConn, err := accessConfig.NewOSCClient()
if err != nil {
return nil, err
}
state.Put("osc", oscConn)
state.Put("ui", &packersdk.BasicUi{
Reader: new(bytes.Buffer),
Writer: new(bytes.Buffer),
})
state.Put("omis", map[string]string{"us-west-2": "omi-12345"})
state.Put("snapshots", map[string][]string{"us-west-2": {"snap-0012345"}})

omis := make(map[string]string, 0)
omis[region] = omiId
state.Put("omis", omis)
snaps := make(map[string][]string, 0)
snaps[region] = []string{snapId}
state.Put("snapshots", snaps)
state.Put("accessConfig", accessConfig)
return state
return state, err
}

func TestUpdateOmi(t *testing.T) {

region := os.Getenv("OSC_REGION")
stepUpdateOMIAttributes := StepUpdateOMIAttributes{
AccountIds: []string{},
SnapshotAccountIds: []string{},
RawRegion: "us-west-2",
RawRegion: region,
GlobalPermission: true,
}
state := tState()
config := &AccessConfig{
RawRegion: region,
}

// Read vms to get 1 to copy
regionconn, _ := config.NewOSCClient()
readRet, _, err := regionconn.Api.VmApi.ReadVms(regionconn.Auth).ReadVmsRequest(oscgo.ReadVmsRequest{}).Execute()

if err != nil {
t.Fatalf("should not error, but: %v", err)
}

vm := readRet.GetVms()[0]
vmId := vm.GetVmId()
volId := vm.GetBlockDeviceMappings()[0].GetBsu().VolumeId

// Create new image
imgName := "ci-packer"
createRet, _, err := regionconn.Api.ImageApi.CreateImage(regionconn.Auth).
CreateImageRequest(oscgo.CreateImageRequest{
ImageName: &imgName,
VmId: &vmId,
}).Execute()

if err != nil {
t.Fatalf("should not error, but: %v", err)
}

imgId := createRet.Image.GetImageId()

// defer delete image
defer func() {
_, _, _ = regionconn.Api.ImageApi.DeleteImage(regionconn.Auth).
DeleteImageRequest(oscgo.DeleteImageRequest{
ImageId: imgId,
}).Execute()
}()

snapshotDesc := "snapshot-ci-packer"
createSnap, _, err := regionconn.Api.SnapshotApi.CreateSnapshot(regionconn.Auth).
CreateSnapshotRequest(oscgo.CreateSnapshotRequest{
Description: &snapshotDesc,
VolumeId: volId,
}).Execute()
if err != nil {
t.Fatalf("should not error, but: %v", err)
}
snapId := createSnap.Snapshot.GetSnapshotId()
defer func() {
_, _, _ = regionconn.Api.SnapshotApi.DeleteSnapshot(regionconn.Auth).
DeleteSnapshotRequest(oscgo.DeleteSnapshotRequest{
SnapshotId: snapId,
}).Execute()
}()

state, err := tState(imgId, snapId)
if state == nil {
t.Fatalf("error retrieving state %s", err.Error())
}

action := stepUpdateOMIAttributes.Run(context.Background(), state)
if err := state.Get("error"); err != nil {
Expand Down
Loading