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

HPR-1173: Channel Restriction Management #555

Merged
merged 16 commits into from
Jul 31, 2023
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
11 changes: 11 additions & 0 deletions .changelog/555.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```release-note:breaking-change
Removed the `iteration` block from `hcp_packer_channel`, previously deprecated in v0.63.0
```

```release-note:feature
Channel restriction can now be configured with the `restricted` field of `hcp_packer_channel`
```

```release-note:improvement
Add support for HCP Packer Managed channels to `hcp_packer_channel`. Also adds a read-only `managed` attribute.
```
14 changes: 2 additions & 12 deletions docs/resources/packer_channel.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,21 @@ resource "hcp_packer_channel" "staging" {

### Optional

- `iteration` (Block List, Max: 1, Deprecated) The iteration assigned to the channel. This block is deprecated. Please use `hcp_packer_channel_assignment` instead. (see [below for nested schema](#nestedblock--iteration))
- `project_id` (String) The ID of the HCP project where this channel is located.
If not specified, the project specified in the HCP Provider config block will be used, if configured.
If a project is not configured in the HCP Provider config block, the oldest project in the organization will be used.
- `restricted` (Boolean) If true, the channel is only visible to users with permission to create and manage it. If false, the channel is visible to every member of the organization.
- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))

### Read-Only

- `author_id` (String) The author of this channel.
- `created_at` (String) The creation time of this channel.
- `id` (String) The ID of this resource.
- `managed` (Boolean) If true, the channel is an HCP Packer managed channel
- `organization_id` (String) The ID of the HCP organization where this channel is located.
- `restricted` (Boolean) If true, the channel is only visible to users with permission to create and manage it. Otherwise the channel is visible to every member of the organization.
- `updated_at` (String) The time this channel was last updated.

<a id="nestedblock--iteration"></a>
### Nested Schema for `iteration`

Optional:

- `fingerprint` (String) The fingerprint of the iteration assigned to the channel.
- `id` (String) The ID of the iteration assigned to the channel.
- `incremental_version` (Number) The incremental_version of the iteration assigned to the channel.


<a id="nestedblock--timeouts"></a>
### Nested Schema for `timeouts`

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/go-uuid v1.0.3
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/hcp-sdk-go v0.55.0
github.com/hashicorp/hcp-sdk-go v0.56.0
github.com/hashicorp/terraform-plugin-docs v0.16.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.27.0
github.com/stretchr/testify v1.8.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ github.com/hashicorp/hc-install v0.5.2 h1:SfwMFnEXVVirpwkDuSF5kymUOhrUxrTq3udEse
github.com/hashicorp/hc-install v0.5.2/go.mod h1:9QISwe6newMWIfEiXpzuu1k9HAGtQYgnSH8H9T8wmoI=
github.com/hashicorp/hcl/v2 v2.17.0 h1:z1XvSUyXd1HP10U4lrLg5e0JMVz6CPaJvAgxM0KNZVY=
github.com/hashicorp/hcl/v2 v2.17.0/go.mod h1:gJyW2PTShkJqQBKpAmPO3yxMxIuoXkOF2TpqXzrQyx4=
github.com/hashicorp/hcp-sdk-go v0.55.0 h1:T4sQtgQfQJOD0uucT4hS+GZI1FmoHAQMADj277W++xw=
github.com/hashicorp/hcp-sdk-go v0.55.0/go.mod h1:hZqky4HEzsKwvLOt4QJlZUrjeQmb4UCZUhDP2HyQFfc=
github.com/hashicorp/hcp-sdk-go v0.56.0 h1:dh9mDMACpfgsnQ4frfqZiZHiejapAj391sDTUGi2NAk=
github.com/hashicorp/hcp-sdk-go v0.56.0/go.mod h1:hZqky4HEzsKwvLOt4QJlZUrjeQmb4UCZUhDP2HyQFfc=
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.18.1 h1:LAbfDvNQU1l0NOQlTuudjczVhHj061fNX5H8XZxHlH4=
Expand Down
10 changes: 7 additions & 3 deletions golangci-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ issues:
- path: _test\.go
linters:
- bodyclose
- linters:
- staticcheck
text: "SA1019: package github.com/golang/protobuf"
- text: "SA1019: package github.com/golang/protobuf"
linters:
- staticcheck
- text: "SA1019: d.GetOkExists"
linters:
- staticcheck

80 changes: 53 additions & 27 deletions internal/clients/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"errors"
"fmt"
"strings"

"github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/stable/2021-04-30/client/packer_service"
packermodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/stable/2021-04-30/models"
Expand Down Expand Up @@ -39,7 +40,7 @@ func GetPackerChannelBySlug(ctx context.Context, client *Client, loc *sharedmode
// channel associated with the given channel name, using ListBucketChannels
func GetPackerChannelBySlugFromList(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation,
bucketName string, channelName string) (*packermodels.HashicorpCloudPackerChannel, error) {
resp, err := ListBucketChannels(ctx, client, loc, bucketName)
resp, err := ListPackerChannels(ctx, client, loc, bucketName)
aidan-mundy marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -98,59 +99,84 @@ func getIteration(client *Client, params *packer_service.PackerServiceGetIterati
return it.Payload.Iteration, nil
}

// CreateBucketChannel creates a channel on the named bucket.
func CreateBucketChannel(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation, bucketSlug string, channelSlug string,
iteration *packermodels.HashicorpCloudPackerIteration, restriction *packermodels.HashicorpCloudPackerCreateChannelRequestRestriction) (*packermodels.HashicorpCloudPackerChannel, error) {
// CreatePackerChannel creates a channel on the named bucket.
func CreatePackerChannel(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation, bucketSlug string, channelSlug string,
restriction *packermodels.HashicorpCloudPackerCreateChannelRequestRestriction) (*packermodels.HashicorpCloudPackerChannel, error) {
params := packer_service.NewPackerServiceCreateChannelParamsWithContext(ctx)
params.LocationOrganizationID = loc.OrganizationID
params.LocationProjectID = loc.ProjectID
params.BucketSlug = bucketSlug
params.Body.Slug = channelSlug
params.Body.Restriction = restriction

if iteration != nil {
switch {
case iteration.ID != "":
params.Body.IterationID = iteration.ID
case iteration.Fingerprint != "":
params.Body.Fingerprint = iteration.Fingerprint
case iteration.IncrementalVersion > 0:
params.Body.IncrementalVersion = iteration.IncrementalVersion
channel, err := client.Packer.PackerServiceCreateChannel(params, nil)
if err != nil {
if err, ok := err.(*packer_service.PackerServiceCreateChannelDefault); ok {
return nil, err
}
return nil, fmt.Errorf("unexpected error format received by CreateBucketChannel. Got: %v", err)
}

channel, err := client.Packer.PackerServiceCreateChannel(params, nil)
return channel.GetPayload().Channel, nil
}

// UpdatePackerChannel updates the named channel.
func UpdatePackerChannel(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation, bucketSlug string, channelSlug string,
restricted bool) (*packermodels.HashicorpCloudPackerChannel, error) {
params := packer_service.NewPackerServiceUpdateChannelParamsWithContext(ctx)
params.LocationOrganizationID = loc.OrganizationID
params.LocationProjectID = loc.ProjectID
params.BucketSlug = bucketSlug
params.Slug = channelSlug
params.Body.Mask = "restriction"

if restricted {
params.Body.Restriction = packermodels.HashicorpCloudPackerUpdateChannelRequestRestrictionRESTRICTED.Pointer()
} else {
params.Body.Restriction = packermodels.HashicorpCloudPackerUpdateChannelRequestRestrictionUNRESTRICTED.Pointer()
}

channel, err := client.Packer.PackerServiceUpdateChannel(params, nil)
if err != nil {
if err, ok := err.(*packer_service.PackerServiceCreateChannelDefault); ok {
if err, ok := err.(*packer_service.PackerServiceUpdateChannelDefault); ok {
return nil, errors.New(err.Payload.Message)
}
return nil, fmt.Errorf("unexpected error format received by CreateBucketChannel. Got: %v", err)
return nil, fmt.Errorf("unexpected error format received by UpdateBucketChannel. Got: %v", err)
}

return channel.GetPayload().Channel, nil
}

// UpdateBucketChannel updates the named channel.
func UpdateBucketChannel(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation, bucketSlug string, channelSlug string,
iteration *packermodels.HashicorpCloudPackerIteration, restriction *packermodels.HashicorpCloudPackerUpdateChannelRequestRestriction) (*packermodels.HashicorpCloudPackerChannel, error) {
func UpdatePackerChannelAssignment(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation, bucketSlug string, channelSlug string,
iteration *packermodels.HashicorpCloudPackerIteration) (*packermodels.HashicorpCloudPackerChannel, error) {
params := packer_service.NewPackerServiceUpdateChannelParamsWithContext(ctx)
params.LocationOrganizationID = loc.OrganizationID
params.LocationProjectID = loc.ProjectID
params.BucketSlug = bucketSlug
params.Slug = channelSlug
params.Body.Restriction = restriction

maskPaths := []string{}

if iteration != nil {
switch {
case iteration.ID != "":
if iteration.ID != "" {
params.Body.IterationID = iteration.ID
case iteration.Fingerprint != "":
maskPaths = append(maskPaths, "iterationId")
}
if iteration.Fingerprint != "" {
params.Body.Fingerprint = iteration.Fingerprint
case iteration.IncrementalVersion > 0:
maskPaths = append(maskPaths, "fingerprint")
}
if iteration.IncrementalVersion > 0 {
params.Body.IncrementalVersion = iteration.IncrementalVersion
maskPaths = append(maskPaths, "incrementalVersion")
}
}

if len(maskPaths) == 0 {
maskPaths = []string{"iterationId", "fingerprint", "incrementalVersion"}
}
params.Body.Mask = strings.Join(maskPaths, ",")

channel, err := client.Packer.PackerServiceUpdateChannel(params, nil)
if err != nil {
if err, ok := err.(*packer_service.PackerServiceUpdateChannelDefault); ok {
Expand All @@ -162,8 +188,8 @@ func UpdateBucketChannel(ctx context.Context, client *Client, loc *sharedmodels.
return channel.GetPayload().Channel, nil
}

// DeleteBucketChannel deletes a channel from the named bucket.
func DeleteBucketChannel(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation, bucketSlug, channelSlug string) (*packermodels.HashicorpCloudPackerChannel, error) {
// DeletePackerChannel deletes a channel from the named bucket.
func DeletePackerChannel(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation, bucketSlug, channelSlug string) (*packermodels.HashicorpCloudPackerChannel, error) {
params := packer_service.NewPackerServiceDeleteChannelParamsWithContext(ctx)
params.LocationOrganizationID = loc.OrganizationID
params.LocationProjectID = loc.ProjectID
Expand All @@ -185,8 +211,8 @@ func DeleteBucketChannel(ctx context.Context, client *Client, loc *sharedmodels.
return nil, nil
}

// ListBucketChannels queries the HCP Packer registry for channels associated to the specified bucket.
func ListBucketChannels(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation, bucketSlug string) (*packermodels.HashicorpCloudPackerListChannelsResponse, error) {
// ListPackerChannels queries the HCP Packer registry for channels associated to the specified bucket.
func ListPackerChannels(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation, bucketSlug string) (*packermodels.HashicorpCloudPackerListChannelsResponse, error) {
params := packer_service.NewPackerServiceListChannelsParamsWithContext(ctx)
params.LocationOrganizationID = loc.OrganizationID
params.LocationProjectID = loc.ProjectID
Expand Down
4 changes: 4 additions & 0 deletions internal/provider/data_source_packer_bucket_names_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ func TestAcc_dataSourcePackerBucketNames(t *testing.T) {
bucketNames := testAccPackerDataBucketNamesBuilder("all")
config := testConfig(testAccConfigBuildersToString(bucketNames))

// Must not be Parallel
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, map[string]bool{"aws": false, "azure": false}) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
PreConfig: func() {
upsertRegistry(t)
},
Config: config,
// If this check fails, there are probably pre-existing buckets
// in the environment that need to be deleted before testing.
Expand Down
1 change: 1 addition & 0 deletions internal/provider/data_source_packer_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func TestAcc_dataSourcePackerImage(t *testing.T) {
// works.
{
PreConfig: func() {
upsertRegistry(t)
upsertBucket(t, acctestImageBucket)
upsertIteration(t, acctestImageBucket, fingerprint)
itID, err := getIterationIDFromFingerPrint(t, acctestImageBucket, fingerprint)
Expand Down
1 change: 1 addition & 0 deletions internal/provider/data_source_packer_iteration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func TestAcc_dataSourcePackerIteration(t *testing.T) {
// works.
{
PreConfig: func() {
upsertRegistry(t)
upsertBucket(t, acctestIterationBucket)
upsertIteration(t, acctestIterationBucket, fingerprint)
itID, err := getIterationIDFromFingerPrint(t, acctestIterationBucket, fingerprint)
Expand Down
1 change: 1 addition & 0 deletions internal/provider/data_source_packer_run_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestAcc_dataSourcePackerRunTask(t *testing.T) {
runTask := testAccPackerDataRunTaskBuilder("runTask")
config := testConfig(testAccConfigBuildersToString(runTask))

// Must not be Parallel
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t, map[string]bool{"aws": false, "azure": false})
Expand Down
Loading