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

Do not fail packer datasources for iteration with revoke_at set to the future #262

Merged
merged 2 commits into from
Mar 2, 2022
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
5 changes: 3 additions & 2 deletions internal/provider/data_source_packer_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ func dataSourcePackerImageRead(ctx context.Context, d *schema.ResourceData, meta
return diag.FromErr(err)
}

if !time.Time(iteration.RevokeAt).IsZero() {
// If RevokeAt is not a zero date, it means this iteration is revoked and should not be used
revokeAt := time.Time(iteration.RevokeAt)
if !revokeAt.IsZero() && revokeAt.Before(time.Now().UTC()) {
// If RevokeAt is not a zero date and is before NOW, it means this iteration is revoked and should not be used
// to build new images.
return diag.Errorf("the iteration %s is revoked and can not be used. A valid iteration should be used instead.", iteration.ID)
}
Expand Down
5 changes: 3 additions & 2 deletions internal/provider/data_source_packer_image_iteration.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,9 @@ func dataSourcePackerImageIterationRead(ctx context.Context, d *schema.ResourceD

iteration := channel.Iteration

if !time.Time(iteration.RevokeAt).IsZero() {
// If RevokeAt is not a zero date, it means this iteration is revoked and should not be used
revokeAt := time.Time(iteration.RevokeAt)
if !revokeAt.IsZero() && revokeAt.Before(time.Now().UTC()) {
// If RevokeAt is not a zero date and is before NOW, it means this iteration is revoked and should not be used
// to build new images.
return diag.Errorf("the iteration %s assigned to channel %s is revoked and can not be used. A valid iteration"+
" must be assigned to this channel before proceeding", iteration.ID, channelSlug)
Expand Down
53 changes: 50 additions & 3 deletions internal/provider/data_source_packer_image_iteration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import (
)

const (
acctestAlpineBucket = "alpine-acctest"
acctestUbuntuBucket = "ubuntu-acctest"
acctestProductionChannel = "production"
acctestAlpineBucket = "alpine-acctest"
acctestUbuntuBucket = "ubuntu-acctest"
acctestAnotherUbuntuBucket = "another-ubuntu-acctest"
acctestProductionChannel = "production"
)

var (
Expand All @@ -37,6 +38,12 @@ var (
bucket_name = %q
channel = %q
}`, acctestUbuntuBucket, acctestProductionChannel)

testAccPackerAnotherUbuntuProductionImage = fmt.Sprintf(`
data "hcp_packer_image_iteration" "another-ubuntu" {
bucket_name = %q
channel = %q
}`, acctestAnotherUbuntuBucket, acctestProductionChannel)
)

func upsertRegistry(t *testing.T) {
Expand Down Expand Up @@ -512,3 +519,43 @@ func TestAcc_dataSourcePacker_revokedIteration(t *testing.T) {
},
})
}

func TestAcc_dataSourcePacker_iterationScheduledToBeRevoked(t *testing.T) {
resourceName := "data.hcp_packer_image_iteration.another-ubuntu"
fingerprint := fmt.Sprintf("%d", rand.Int())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, map[string]bool{"aws": false, "azure": false}) },
ProviderFactories: providerFactories,
CheckDestroy: func(*terraform.State) error {
deleteChannel(t, acctestAnotherUbuntuBucket, acctestProductionChannel, false)
deleteIteration(t, acctestAnotherUbuntuBucket, fingerprint, false)
deleteBucket(t, acctestAnotherUbuntuBucket, false)
return nil
},
Steps: []resource.TestStep{
// testing that getting an iteration with scheduled revocation works
{
PreConfig: func() {
upsertRegistry(t)
upsertBucket(t, acctestAnotherUbuntuBucket)
upsertIteration(t, acctestAnotherUbuntuBucket, fingerprint)
itID, err := getIterationIDFromFingerPrint(t, acctestAnotherUbuntuBucket, fingerprint)
if err != nil {
t.Fatal(err.Error())
}
upsertBuild(t, acctestAnotherUbuntuBucket, fingerprint, itID)
createChannel(t, acctestAnotherUbuntuBucket, acctestProductionChannel, itID)
// Schedule revocation to the future
revokeIteration(t, itID, acctestAnotherUbuntuBucket, "1d")

},
Config: testConfig(testAccPackerAnotherUbuntuProductionImage),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "organization_id"),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
),
},
},
})
}
6 changes: 4 additions & 2 deletions internal/provider/data_source_packer_iteration.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ func dataSourcePackerIterationRead(ctx context.Context, d *schema.ResourceData,
}

iteration := channel.Iteration
if !time.Time(iteration.RevokeAt).IsZero() {
// If RevokeAt is not a zero date, it means this iteration is revoked and should not be used

revokeAt := time.Time(iteration.RevokeAt)
if !revokeAt.IsZero() && revokeAt.Before(time.Now().UTC()) {
// If RevokeAt is not a zero date and is before NOW, it means this iteration is revoked and should not be used
// to build new images.
return diag.Errorf("the iteration %s assigned to channel %s is revoked and can not be used. A valid iteration"+
" must be assigned to this channel before proceeding", iteration.ID, channelSlug)
Expand Down
57 changes: 47 additions & 10 deletions internal/provider/data_source_packer_iteration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import (
)

const (
acctestIterationBucket = "alpine-acctest-itertest"
acctestIterationUbuntuBucket = "ubuntu-acctest-itertest"
acctestIterationChannel = "production-iter-test"
acctestIterationBucket = "alpine-acctest-itertest"
acctestIterationUbuntuBucket = "ubuntu-acctest-itertest"
acctestIterationAnotherUbuntuBucket = "another-ubuntu-acctest-itertest"
acctestIterationChannel = "production-iter-test"
)

var (
Expand All @@ -28,6 +29,11 @@ var (
bucket_name = %q
channel = %q
}`, acctestIterationUbuntuBucket, acctestIterationChannel)
testAccPackerIterationAnotherUbuntuProduction = fmt.Sprintf(`
data "hcp_packer_iteration" "another-ubuntu" {
bucket_name = %q
channel = %q
}`, acctestIterationAnotherUbuntuBucket, acctestIterationChannel)
)

func TestAcc_dataSourcePackerIteration(t *testing.T) {
Expand Down Expand Up @@ -74,13 +80,6 @@ func TestAcc_dataSourcePackerIteration_revokedIteration(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, map[string]bool{"aws": false, "azure": false}) },
ProviderFactories: providerFactories,
CheckDestroy: func(*terraform.State) error {
deleteChannel(t, acctestIterationUbuntuBucket, acctestIterationChannel, false)
deleteIteration(t, acctestIterationUbuntuBucket, fingerprint, false)
deleteBucket(t, acctestIterationUbuntuBucket, false)
return nil
},

Steps: []resource.TestStep{
// testing that getting the production channel of the alpine image
// works.
Expand Down Expand Up @@ -113,3 +112,41 @@ func TestAcc_dataSourcePackerIteration_revokedIteration(t *testing.T) {
},
})
}

func TestAcc_dataSourcePackerIteration_iterationScheduledToBeRevoked(t *testing.T) {
resourceName := "data.hcp_packer_iteration.another-ubuntu"
fingerprint := fmt.Sprintf("%d", rand.Int())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, map[string]bool{"aws": false, "azure": false}) },
ProviderFactories: providerFactories,
CheckDestroy: func(*terraform.State) error {
deleteChannel(t, acctestIterationAnotherUbuntuBucket, acctestIterationChannel, false)
deleteIteration(t, acctestIterationAnotherUbuntuBucket, fingerprint, false)
deleteBucket(t, acctestIterationAnotherUbuntuBucket, false)
return nil
},
Steps: []resource.TestStep{
// testing that getting an iteration with scheduled revocation works
{
PreConfig: func() {
upsertBucket(t, acctestIterationAnotherUbuntuBucket)
upsertIteration(t, acctestIterationAnotherUbuntuBucket, fingerprint)
itID, err := getIterationIDFromFingerPrint(t, acctestIterationAnotherUbuntuBucket, fingerprint)
if err != nil {
t.Fatal(err.Error())
}
upsertBuild(t, acctestIterationAnotherUbuntuBucket, fingerprint, itID)
createChannel(t, acctestIterationAnotherUbuntuBucket, acctestIterationChannel, itID)
// Schedule revocation to the future
revokeIteration(t, itID, acctestIterationAnotherUbuntuBucket, "1d")
},
Config: testConfig(testAccPackerIterationAnotherUbuntuProduction),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "organization_id"),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
),
},
},
})
}