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

Add check for revoked iterations to HCP Packer datasources #240

Merged
merged 5 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/go-version v1.3.0
github.com/hashicorp/hcl/v2 v2.8.2 // indirect
github.com/hashicorp/hcp-sdk-go v0.14.0
github.com/hashicorp/hcp-sdk-go v0.15.1-0.20220112153249-f565607d7cc4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to get some network updates into the go SDK, so I'll try to release 0.16.0 with your changes tomorrow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! So I'll wait for the release to update this one with 0.16.0

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

github.com/hashicorp/terraform-plugin-docs v0.5.1
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.0
github.com/posener/complete v1.2.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,8 @@ github.com/hashicorp/hcl/v2 v2.8.2 h1:wmFle3D1vu0okesm8BTLVDyJ6/OL9DCLUwn0b2Opti
github.com/hashicorp/hcl/v2 v2.8.2/go.mod h1:bQTN5mpo+jewjJgh8jr0JUguIi7qPHUF6yIfAEN3jqY=
github.com/hashicorp/hcp-sdk-go v0.14.0 h1:0+elHACiRu4L+rR+1k9khlu7zaGYq9e/8gy4N4pz0NU=
github.com/hashicorp/hcp-sdk-go v0.14.0/go.mod h1:z0I0eZ+TVJJ7pycnCzMM/ouOw5D5Qnp/zylNXkqGEX0=
github.com/hashicorp/hcp-sdk-go v0.15.1-0.20220112153249-f565607d7cc4 h1:H4V7J/mUKzMpmTqnnDloH0r7mk2Jwn4oKUvealKE9cQ=
github.com/hashicorp/hcp-sdk-go v0.15.1-0.20220112153249-f565607d7cc4/go.mod h1:z0I0eZ+TVJJ7pycnCzMM/ouOw5D5Qnp/zylNXkqGEX0=
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/terraform-exec v0.15.0 h1:cqjh4d8HYNQrDoEmlSGelHmg2DYDh5yayckvJ5bV18E=
Expand Down
6 changes: 6 additions & 0 deletions internal/provider/data_source_packer_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ 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
// to build new images.
return diag.Errorf("the iteration %s is revoked and can not be used", iteration.ID)
}

found := false
for _, build := range iteration.Builds {
if build.CloudProvider != cloudProvider {
Expand Down
13 changes: 10 additions & 3 deletions internal/provider/data_source_packer_image_iteration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"context"
"log"
"time"

packermodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/preview/2021-04-30/models"
sharedmodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-shared/v1/models"
Expand Down Expand Up @@ -161,13 +162,19 @@ func dataSourcePackerImageIterationRead(ctx context.Context, d *schema.ResourceD
return diag.FromErr(err)
}

if channel.Pointer == nil {
if channel.Iteration == nil {
return diag.Errorf("no iteration information found for the specified channel %s", channelSlug)
}

iteration := channel.Pointer.Iteration
iteration := channel.Iteration

d.SetId(channel.Pointer.Iteration.ID)
if !time.Time(iteration.RevokeAt).IsZero() {
// If RevokeAt is not a zero date, 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", iteration.ID)
}

d.SetId(iteration.ID)

if err := d.Set("incremental_version", iteration.IncrementalVersion); err != nil {
return diag.FromErr(err)
Expand Down
60 changes: 60 additions & 0 deletions internal/provider/data_source_packer_image_iteration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"testing"
"time"

"github.com/google/uuid"
"github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/preview/2021-04-30/client/packer_service"
Expand Down Expand Up @@ -92,6 +93,26 @@ func upsertIteration(t *testing.T, bucketSlug, fingerprint string) {
t.Errorf("unexpected CreateIteration error, expected nil or 409. Got %v", err)
}

func revokeIteration(t *testing.T, iterationID, revokeIn string) {
t.Helper()
client := testAccProvider.Meta().(*clients.Client)
loc := &sharedmodels.HashicorpCloudLocationLocation{
OrganizationID: client.Config.OrganizationID,
ProjectID: client.Config.ProjectID,
}

params := packer_service.NewPackerServiceUpdateIterationParams()
params.LocationOrganizationID = loc.OrganizationID
params.LocationProjectID = loc.ProjectID
params.IterationID = iterationID
params.Body = &models.HashicorpCloudPackerUpdateIterationRequest{RevokeIn: revokeIn}

_, err := client.Packer.PackerServiceUpdateIteration(params, nil)
if err != nil {
t.Fatal(err)
}
}

func getIterationIDFromFingerPrint(t *testing.T, bucketSlug, fingerprint string) string {
t.Helper()

Expand Down Expand Up @@ -342,3 +363,42 @@ func TestAcc_dataSourcePacker(t *testing.T) {
},
})
}

func TestAcc_dataSourcePacker_revokedIteration(t *testing.T) {
resourceName := "data.hcp_packer_image_iteration.alpine"
fingerprint := "43"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, false) },
ProviderFactories: providerFactories,
CheckDestroy: func(*terraform.State) error {
itID := getIterationIDFromFingerPrint(t, acctestBucket, fingerprint)
deleteChannel(t, acctestBucket, acctestChannel)
deleteIteration(t, acctestBucket, itID)
deleteBucket(t, acctestBucket)
return nil
},
Steps: []resource.TestStep{
// testing that getting a revoked iteration fails properly
{
PreConfig: func() {
upsertBucket(t, acctestBucket)
upsertIteration(t, acctestBucket, fingerprint)
itID := getIterationIDFromFingerPrint(t, acctestBucket, fingerprint)
upsertBuild(t, acctestBucket, fingerprint, itID)
createChannel(t, acctestBucket, acctestChannel)
// Schedule revocation to the future, otherwise we won't be able to revoke an iteration that
// it's assigned to a channel
revokeIteration(t, itID, "5s")
// Sleep to make sure the iteration is revoked when we test
time.Sleep(5 * time.Second)
},
Config: testConfig(testAccPackerAlpineProductionImage),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "organization_id"),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
),
},
},
})
}
12 changes: 9 additions & 3 deletions internal/provider/data_source_packer_iteration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"context"
"log"
"time"

sharedmodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-shared/v1/models"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -98,13 +99,18 @@ func dataSourcePackerIterationRead(ctx context.Context, d *schema.ResourceData,
return diag.FromErr(err)
}

if channel.Pointer == nil {
if channel.Iteration == nil {
return diag.Errorf("no iteration information found for the specified channel %s", channelSlug)
}

iteration := channel.Pointer.Iteration
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
// to build new images.
return diag.Errorf("the iteration %s is revoked and can not be used", iteration.ID)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we advise the user what to do/change when they run into this error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can, I will try to think of a better text!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! I also improved some of the tests

}

d.SetId(channel.Pointer.Iteration.ID)
d.SetId(iteration.ID)

if err := d.Set("author_id", iteration.AuthorID); err != nil {
return diag.FromErr(err)
Expand Down