Skip to content

Commit

Permalink
Add other computed properties
Browse files Browse the repository at this point in the history
* Add author_id to output
* Add updated_at computed property
  • Loading branch information
nywilken committed Jan 26, 2023
1 parent ffbb496 commit e31543e
Show file tree
Hide file tree
Showing 11 changed files with 869 additions and 308 deletions.
3 changes: 3 additions & 0 deletions .changelog/435.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feature
New resource `hcp_packer_channel`
```
112 changes: 112 additions & 0 deletions docs/resources/packer_channel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
---
page_title: "Resource hcp_packer_channel - terraform-provider-hcp"
subcategory: ""
description: |-
The Packer Channel resource allows you to manage image bucket channels within an active HCP Packer Registry.
---

# hcp_packer_channel (Resource)

The Packer Channel resource allows you to manage image bucket channels within an active HCP Packer Registry.

## Example Usage

To create a channel with no assigned iteration.
```terraform
resource "hcp_packer_channel" "staging" {
name = "staging"
bucket_name = "alpine"
}
```

To create, or update an existing, channel with an assigned iteration.
```terraform
resource "hcp_packer_channel" "staging" {
name = "staging"
bucket_name = "alpine"
iteration_assignment {
id = "iteration-id"
}
}
# Update assigned iteration using an iteration fingerprint
resource "hcp_packer_channel" "staging" {
name = "staging"
bucket_name = "alpine"
iteration_assignment {
fingerprint = "fingerprint-associated-to-iteration"
}
}
# Update assigned iteration using an iteration incremental version
resource "hcp_packer_channel" "staging" {
name = "staging"
bucket_name = "alpine"
iteration_assignment {
// incremental_version is the version number assigned to a completed iteration.
incremental_version = 1
}
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `bucket_name` (String) The slug of the HCP Packer Registry image bucket where the channel should be managed in.
- `name` (String) The name of the channel being managed.

### Optional

- `iteration_assignment` (Block List, Max: 1) The iteration assignment information that will be used to assign a completed iteration to the channel. (see [below for nested schema](#nestedblock--iteration_assignment))
- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))

### Read-Only

- `author_id` (String) The author of the channel.
- `created_at` (String) Creation time of this build.
- `id` (String) The ID of this resource.
- `iteration` (List of Object) The iteration assigned to the channel. (see [below for nested schema](#nestedatt--iteration))
- `organization_id` (String) The ID of the organization this HCP Packer registry is located in.
- `project_id` (String) The ID of the project this HCP Packer registry is located in.
- `updated_at` (String) The author of the channel.

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

Optional:

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


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

Optional:

- `create` (String)
- `default` (String)
- `delete` (String)
- `update` (String)


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

Read-Only:

- `fingerprint` (String)
- `id` (String)
- `incremental_version` (Number)

## Import

Import is supported using the following syntax:

```shell
# The import ID requires the bucket and channel name in the following format {bucket_name}:{name}
terraform import hcp_packer_channel.staging alpine:staging
```
2 changes: 2 additions & 0 deletions examples/resources/hcp_packer_channel/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# The import ID requires the bucket and channel name in the following format {bucket_name}:{name}
terraform import hcp_packer_channel.staging alpine:staging
4 changes: 4 additions & 0 deletions examples/resources/hcp_packer_channel/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "hcp_packer_channel" "staging" {
name = "staging"
bucket_name = "alpine"
}
27 changes: 27 additions & 0 deletions examples/resources/hcp_packer_channel/resource_assignment.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
resource "hcp_packer_channel" "staging" {
name = "staging"
bucket_name = "alpine"
iteration_assignment {
id = "iteration-id"
}
}

# Update assigned iteration using an iteration fingerprint
resource "hcp_packer_channel" "staging" {
name = "staging"
bucket_name = "alpine"
iteration_assignment {
fingerprint = "fingerprint-associated-to-iteration"
}
}

# Update assigned iteration using an iteration incremental version
resource "hcp_packer_channel" "staging" {
name = "staging"
bucket_name = "alpine"
iteration_assignment {
// incremental_version is the version number assigned to a completed iteration.
incremental_version = 1
}
}

87 changes: 54 additions & 33 deletions internal/clients/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func GetPackerChannelBySlug(ctx context.Context, client *Client, loc *sharedmode
return getResp.Payload.Channel, nil
}

// GetIteration queries the HCP Packer registry for an existing bucket iteration.
// GetIterationFromID queries the HCP Packer registry for an existing bucket iteration.
func GetIterationFromID(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation,
bucketslug string, iterationID string) (*packermodels.HashicorpCloudPackerIteration, error) {
params := packer_service.NewPackerServiceGetIterationParamsWithContext(ctx)
Expand All @@ -48,21 +48,32 @@ func GetIterationFromID(ctx context.Context, client *Client, loc *sharedmodels.H
return it.Payload.Iteration, nil
}

type ChannelResourceInput struct {
BucketName string
Name string
Description string
AssignedIterationID string
// ChannelIterationAssignment
type ChannelIterationAssignment struct {
IterationID string
IterationFingerprint string
IterationIncrementalVersion int
}

// CreateBucketChannel creates channel on the named bucket
func CreateBucketChannel(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation, in ChannelResourceInput) (*packermodels.HashicorpCloudPackerChannel, error) {
// CreateBucketChannel creates a channel on the named bucket.
func CreateBucketChannel(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation, bucketSlug string, channelSlug string,
assignment *ChannelIterationAssignment) (*packermodels.HashicorpCloudPackerChannel, error) {
params := packer_service.NewPackerServiceCreateChannelParamsWithContext(ctx)
params.LocationOrganizationID = loc.OrganizationID
params.LocationProjectID = loc.ProjectID
params.Body.Slug = in.Name
params.BucketSlug = in.BucketName
params.Body.IterationID = in.AssignedIterationID
params.BucketSlug = bucketSlug
params.Body.Slug = channelSlug

if assignment != nil {
switch {
case assignment.IterationID != "":
params.Body.IterationID = assignment.IterationID
case assignment.IterationFingerprint != "":
params.Body.Fingerprint = assignment.IterationFingerprint
case assignment.IterationIncrementalVersion > 0:
params.Body.IncrementalVersion = int32(assignment.IterationIncrementalVersion)
}
}

channel, err := client.Packer.PackerServiceCreateChannel(params, nil)
if err != nil {
Expand All @@ -73,52 +84,62 @@ func CreateBucketChannel(ctx context.Context, client *Client, loc *sharedmodels.
return channel.GetPayload().Channel, nil
}

// DeleteBucketChannel creates channel on the named bucket
func DeleteBucketChannel(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation, bucketSlug string, channelSlug string) (*packermodels.HashicorpCloudPackerChannel, error) {
params := packer_service.NewPackerServiceDeleteChannelParamsWithContext(ctx)
// UpdateBucketChannel updates the named channel.
func UpdateBucketChannel(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation, bucketSlug string, channelSlug string,
assignment *ChannelIterationAssignment) (*packermodels.HashicorpCloudPackerChannel, error) {
params := packer_service.NewPackerServiceUpdateChannelParamsWithContext(ctx)
params.LocationOrganizationID = loc.OrganizationID
params.LocationProjectID = loc.ProjectID
params.BucketSlug = bucketSlug
params.Slug = channelSlug

req, err := client.Packer.PackerServiceDeleteChannel(params, nil)
if err != nil {
err := err.(*packer_service.PackerServiceDeleteChannelDefault)
return nil, errors.New(err.Payload.Message)
if assignment != nil {
switch {
case assignment.IterationID != "":
params.Body.IterationID = assignment.IterationID
case assignment.IterationFingerprint != "":
params.Body.Fingerprint = assignment.IterationFingerprint
case assignment.IterationIncrementalVersion > 0:
params.Body.IncrementalVersion = int32(assignment.IterationIncrementalVersion)
}
}

if !req.IsSuccess() {
return nil, errors.New("failed to delete channel")
channel, err := client.Packer.PackerServiceUpdateChannel(params, nil)
if err != nil {
err := err.(*packer_service.PackerServiceUpdateChannelDefault)
return nil, errors.New(err.Payload.Message)
}

return nil, nil
return channel.GetPayload().Channel, nil
}

// UpdateBucketChannel creates channel on the named bucket
func UpdateBucketChannel(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation, in ChannelResourceInput) (*packermodels.HashicorpCloudPackerChannel, error) {
params := packer_service.NewPackerServiceUpdateChannelParamsWithContext(ctx)
// DeleteBucketChannel deletes a channel from the named bucket.
func DeleteBucketChannel(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
params.BucketSlug = in.BucketName
params.Slug = in.Name
params.Body.IterationID = in.AssignedIterationID
params.BucketSlug = bucketSlug
params.Slug = channelSlug

channel, err := client.Packer.PackerServiceUpdateChannel(params, nil)
req, err := client.Packer.PackerServiceDeleteChannel(params, nil)
if err != nil {
err := err.(*packer_service.PackerServiceUpdateChannelDefault)
err := err.(*packer_service.PackerServiceDeleteChannelDefault)
return nil, errors.New(err.Payload.Message)
}

return channel.GetPayload().Channel, nil
if !req.IsSuccess() {
return nil, errors.New("failed to delete channel")
}

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, bucketName string) (*packermodels.HashicorpCloudPackerListChannelsResponse, error) {

func ListBucketChannels(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation, bucketSlug string) (*packermodels.HashicorpCloudPackerListChannelsResponse, error) {
params := packer_service.NewPackerServiceListChannelsParams()
params.BucketSlug = bucketName
params.LocationOrganizationID = loc.OrganizationID
params.LocationProjectID = loc.ProjectID
params.BucketSlug = bucketSlug

req, err := client.Packer.PackerServiceListChannels(params, nil)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func New() func() *schema.Provider {
"hcp_hvn": resourceHvn(),
"hcp_hvn_peering_connection": resourceHvnPeeringConnection(),
"hcp_hvn_route": resourceHvnRoute(),
"hcp_packer_bucket_channel": resourcePackerBucketChannel(),
"hcp_packer_channel": resourcePackerChannel(),
"hcp_vault_cluster": resourceVaultCluster(),
"hcp_vault_cluster_admin_token": resourceVaultClusterAdminToken(),
},
Expand Down
Loading

0 comments on commit e31543e

Please sign in to comment.