Skip to content

Commit

Permalink
[Do not merge] Add hcp_packer_bucket, `hcp_packer_bucket_iam_bindin…
Browse files Browse the repository at this point in the history
…g`, and `hcp_packer_bucket_iam_policy` resources #866  (#852)
  • Loading branch information
JenGoldstrich authored Jul 2, 2024
1 parent a99d1a0 commit d3ec11f
Show file tree
Hide file tree
Showing 25 changed files with 989 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changelog/852.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
```release-note:feature
New resource: Add `hcp_packer_bucket` resource for managing HCP Packer buckets
New Resource: Add `hcp_packer_bucket_iam_policy` resource for assigning a list of policy bindings to multiple principals for a HCP Packer Bucket
New resource: Add `hcp_packer_bucket_iam_binding` resource for assigning a single role to a principal for a HCP Packer Bucket
```
55 changes: 55 additions & 0 deletions docs/guides/packer-bucket-rbac.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
subcategory: ""
page_title: "Managing HCP Packer Bucket IAM Policies"
description: |-
A guide to using HCP Packer bucket resource along with binding or policy resource to manage bucket level access.
---

# Managing HCP Packer Bucket IAM Policies

You can grant specific users, service principals, or groups contributor or admin level access to a specific HCP Packer bucket using either a `hcp_packer_bucket_iam_binding` or `hcp_packer_bucket_iam_policy` resource. Whenever a user is invited to a project they will have read level access to all resources, but you can restrict which of the principals in your project can maintain specific buckets.

A resource's policy is a list of bindings to assign roles to multiple users, groups, or service principals. The `hcp_packer_bucket_iam_policy` resource sets the Bucket IAM policy and replaces any existing policy.

The following example assigns the role `contributor` to a user principal and a service principal for the `production` bucket.

```terraform
data "hcp_iam_policy" "mypolicy" {
bindings = [
{
role = "roles/contributor"
principals = [
"user-principal-id-1",
"service-principal-id-1",
]
},
]
}
resource "hcp_packer_bucket" "production" {
name = "production"
}
resource "hcp_packer_bucket_iam_policy" "example" {
resource_name = hcp_packer_bucket.production.resource_name
policy_data = data.hcp_iam_policy.mypolicy.policy_data
}
```

The following example assigns role contriubtor for a service principal to the production bucket, and also preserves existing bindings.

```terraform
resource "hcp_service_principal" "my-sp" {
name = "my-sp"
}
resource "hcp_packer_bucket" "production" {
name = "production"
}
resource "hcp_packer_bucket_iam_binding" "example" {
resource_name = hcp_packer_bucket.production.resource_name
principal_id = hcp_service_principal.my-sp.resource_id
role = "roles/contributor"
}
```
46 changes: 46 additions & 0 deletions docs/resources/packer_bucket.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "hcp_packer_bucket Resource - terraform-provider-hcp"
subcategory: ""
description: |-
The Packer Bucket resource allows you to manage a bucket within an active HCP Packer Registry.
---

# hcp_packer_bucket (Resource)

The Packer Bucket resource allows you to manage a bucket within an active HCP Packer Registry.

## Example Usage

```terraform
resource "hcp_packer_bucket" "staging" {
name = "alpine"
}
```

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

### Required

- `name` (String) The bucket's name.

### Optional

- `project_id` (String) The ID of the project to create the bucket under. If unspecified, the bucket will be created in the project the provider is configured with.

### Read-Only

- `created_at` (String) The creation time of this bucket
- `organization_id` (String) The ID of the HCP organization where this bucket is located.
- `resource_name` (String) The buckets's HCP resource name in the format `packer/project/<project_id>/packer/<name>`.

## Import

Import is supported using the following syntax:

```shell
# Using a HCP Packer Bucket Resource Name
# packer/project/{project_id}/bucket/{bucket_name}
terraform import hcp_packer_bucket.alpine packer/project/f709ec73-55d4-46d8-897d-816ebba28778/bucket/alpine
```
38 changes: 38 additions & 0 deletions docs/resources/packer_bucket_iam_binding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "hcp_packer_bucket_iam_binding Resource - terraform-provider-hcp"
subcategory: ""
description: |-
Updates the HCP Packer Bucket IAM policy to bind a role to a new member. Existing bindings are preserved.
---

# hcp_packer_bucket_iam_binding (Resource)

Updates the HCP Packer Bucket IAM policy to bind a role to a new member. Existing bindings are preserved.

## Example Usage

```terraform
resource "hcp_service_principal" "my-sp" {
name = "my-sp"
}
resource "hcp_packer_bucket" "production" {
name = "production"
}
resource "hcp_packer_bucket_iam_binding" "example" {
resource_name = hcp_packer_bucket.production.resource_name
principal_id = hcp_service_principal.my-sp.resource_id
role = "roles/contributor"
}
```

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

### Required

- `principal_id` (String) The principal to bind to the given role.
- `resource_name` (String) The bucket's resource name in the format packer/project/<project ID>/bucket/<bucket name>.
- `role` (String) The role name to bind to the given principal.
58 changes: 58 additions & 0 deletions docs/resources/packer_bucket_iam_policy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "hcp_packer_bucket_iam_policy Resource - terraform-provider-hcp"
subcategory: ""
description: |-
Sets the HCP Packer Bucket IAM policy and replaces any existing policy.
---

# hcp_packer_bucket_iam_policy (Resource)

Sets the HCP Packer Bucket IAM policy and replaces any existing policy.

## Example Usage

```terraform
data "hcp_iam_policy" "mypolicy" {
bindings = [
{
role = "roles/contributor"
principals = [
"user-principal-id-1",
"service-principal-id-1",
]
},
]
}
resource "hcp_packer_bucket" "production" {
name = "production"
}
resource "hcp_packer_bucket_iam_policy" "example" {
resource_name = hcp_packer_bucket.production.resource_name
policy_data = data.hcp_iam_policy.mypolicy.policy_data
}
```

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

### Required

- `policy_data` (String) The policy to apply.
- `resource_name` (String) The bucket's resource name in the format packer/project/<project ID>/bucket/<bucket name>.

### Read-Only

- `etag` (String) The etag captures the existing state of the policy.

## Import

Import is supported using the following syntax:

```shell
# Using a HCP Packer Bucket Resource Name
# packer/project/{project_id}/bucket/{bucket_name}
terraform import hcp_packer_bucket.alpine packer/project/f709ec73-55d4-46d8-897d-816ebba28778/bucket/alpine
```
13 changes: 13 additions & 0 deletions examples/guides/packer_bucket_rbac/iam_binding_resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resource "hcp_service_principal" "my-sp" {
name = "my-sp"
}

resource "hcp_packer_bucket" "production" {
name = "production"
}

resource "hcp_packer_bucket_iam_binding" "example" {
resource_name = hcp_packer_bucket.production.resource_name
principal_id = hcp_service_principal.my-sp.resource_id
role = "roles/contributor"
}
20 changes: 20 additions & 0 deletions examples/guides/packer_bucket_rbac/iam_policy_resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
data "hcp_iam_policy" "mypolicy" {
bindings = [
{
role = "roles/contributor"
principals = [
"user-principal-id-1",
"service-principal-id-1",
]
},
]
}

resource "hcp_packer_bucket" "production" {
name = "production"
}

resource "hcp_packer_bucket_iam_policy" "example" {
resource_name = hcp_packer_bucket.production.resource_name
policy_data = data.hcp_iam_policy.mypolicy.policy_data
}
4 changes: 4 additions & 0 deletions examples/resources/hcp_packer_bucket/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Using a HCP Packer Bucket Resource Name
# packer/project/{project_id}/bucket/{bucket_name}
terraform import hcp_packer_bucket.alpine packer/project/f709ec73-55d4-46d8-897d-816ebba28778/bucket/alpine

3 changes: 3 additions & 0 deletions examples/resources/hcp_packer_bucket/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "hcp_packer_bucket" "staging" {
name = "alpine"
}
13 changes: 13 additions & 0 deletions examples/resources/hcp_packer_bucket_iam_binding/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resource "hcp_service_principal" "my-sp" {
name = "my-sp"
}

resource "hcp_packer_bucket" "production" {
name = "production"
}

resource "hcp_packer_bucket_iam_binding" "example" {
resource_name = hcp_packer_bucket.production.resource_name
principal_id = hcp_service_principal.my-sp.resource_id
role = "roles/contributor"
}
4 changes: 4 additions & 0 deletions examples/resources/hcp_packer_bucket_iam_policy/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Using a HCP Packer Bucket Resource Name
# packer/project/{project_id}/bucket/{bucket_name}
terraform import hcp_packer_bucket.alpine packer/project/f709ec73-55d4-46d8-897d-816ebba28778/bucket/alpine

20 changes: 20 additions & 0 deletions examples/resources/hcp_packer_bucket_iam_policy/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
data "hcp_iam_policy" "mypolicy" {
bindings = [
{
role = "roles/contributor"
principals = [
"user-principal-id-1",
"service-principal-id-1",
]
},
]
}

resource "hcp_packer_bucket" "production" {
name = "production"
}

resource "hcp_packer_bucket_iam_policy" "example" {
resource_name = hcp_packer_bucket.production.resource_name
policy_data = data.hcp_iam_policy.mypolicy.policy_data
}
16 changes: 16 additions & 0 deletions internal/clients/packerv2/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,19 @@ func ListBuckets(ctx context.Context, client *clients.Client, loc *sharedmodels.
nextPage = pagination.NextPageToken
}
}

func CreateBucket(ctx context.Context, client *clients.Client, loc *sharedmodels.HashicorpCloudLocationLocation, name string) (*Bucket, error) {
params := packerservice.NewPackerServiceCreateBucketParams()
params.SetLocationOrganizationID(loc.OrganizationID)
params.SetLocationProjectID(loc.ProjectID)
params.Body = &packermodels.HashicorpCloudPacker20230101CreateBucketBody{
Name: name,
}

resp, err := client.PackerV2.PackerServiceCreateBucket(params, nil)

if err != nil {
return nil, formatGRPCError[*packerservice.PackerServiceGetBucketDefault](err)
}
return resp.GetPayload().Bucket, nil
}
11 changes: 8 additions & 3 deletions internal/provider/packer/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ package packer
import (
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-provider-hcp/internal/provider/packer/sources/artifact"
"github.com/hashicorp/terraform-provider-hcp/internal/provider/packer/sources/version"
"github.com/hashicorp/terraform-provider-hcp/internal/provider/packer/datasources/artifact"
"github.com/hashicorp/terraform-provider-hcp/internal/provider/packer/datasources/version"
"github.com/hashicorp/terraform-provider-hcp/internal/provider/packer/resources/bucket"
)

// ResourceSchemaBuilders is a list of all HCP Packer resources exposed by the
// Framework provider. To add a new resource, add a new function to this list.
var ResourceSchemaBuilders []func() resource.Resource = []func() resource.Resource{}
var ResourceSchemaBuilders []func() resource.Resource = []func() resource.Resource{
bucket.NewPackerBucketResource,
bucket.NewPackerBucketIAMPolicyResource,
bucket.NewPackerBucketAppIAMBindingResource,
}

// DataSourceSchemaBuilders is a list of all HCP Packer data sources exposed by the
// Framework provider. To add a new data source, add a new function to this list.
Expand Down
Loading

0 comments on commit d3ec11f

Please sign in to comment.