-
Notifications
You must be signed in to change notification settings - Fork 50
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
Data source for packer registry #169
Changes from 22 commits
5d91f37
e594afe
d381e02
9092ab2
d1867aa
3be9aca
7500dac
76bb967
bfe5099
1d3ed90
ff12e1e
66254ef
7afcdc7
8063413
be602f5
4dbb8bd
5cb2338
2db3cba
aa1be36
9874c8b
9e5728f
63d18cf
630880f
280ff02
9d00c67
ca6d036
7397f83
73d79d8
5512bac
31760fc
6707024
f4bcb91
3c2cb91
34b5f2c
7ae2cd9
14ad128
b835c06
d9eacad
6343175
cdc0c02
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "hcp_packer_image_iteration Data Source - terraform-provider-hcp" | ||
subcategory: "" | ||
description: |- | ||
The Packer Image data source iteration gets the most recent iteration (or build) of an image, given a channel. | ||
--- | ||
|
||
# hcp_packer_image_iteration (Data Source) | ||
|
||
The Packer Image data source iteration gets the most recent iteration (or build) of an image, given a channel. | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- **bucket** (String) The slug of the HCP Packer Registry image bucket to pull from. | ||
- **channel** (String) The channel that points to the version of the image you want. | ||
|
||
### Optional | ||
|
||
- **timeouts** (Block, Optional) (see [below for nested schema](#nestedblock--timeouts)) | ||
|
||
### Read-Only | ||
|
||
- **builds** (List of Object) Builds for this iteration. An iteration can have more than one build if it took more than one go to build all images. (see [below for nested schema](#nestedatt--builds)) | ||
- **created_at** (String) Creation time of this iteration | ||
- **id** (String) HCP ID of this iteration | ||
- **incremental_version** (Number) Incremental version of this 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. | ||
|
||
<a id="nestedblock--timeouts"></a> | ||
### Nested Schema for `timeouts` | ||
|
||
Optional: | ||
|
||
- **default** (String) | ||
|
||
|
||
<a id="nestedatt--builds"></a> | ||
### Nested Schema for `builds` | ||
|
||
Read-Only: | ||
|
||
- **cloud_provider** (String) | ||
- **component_type** (String) | ||
- **created_at** (String) | ||
- **id** (String) | ||
- **images** (List of Object) (see [below for nested schema](#nestedobjatt--builds--images)) | ||
- **labels** (Map of String) | ||
- **packer_run_uuid** (String) | ||
- **status** (String) | ||
- **updated_at** (String) | ||
|
||
<a id="nestedobjatt--builds--images"></a> | ||
### Nested Schema for `builds.images` | ||
|
||
Read-Only: | ||
|
||
- **created_at** (String) | ||
- **id** (String) | ||
- **image_id** (String) | ||
- **region** (String) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package clients | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/preview/2021-04-30/client/packer_service" | ||
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" | ||
) | ||
|
||
func GetPackerChannelBySlug(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation, | ||
bucketName string, channel string) (*packermodels.HashicorpCloudPackerChannel, error) { | ||
|
||
getParams := packer_service.NewGetChannelParams() | ||
getParams.BucketSlug = bucketName | ||
getParams.Slug = channel | ||
getParams.LocationOrganizationID = loc.OrganizationID | ||
getParams.LocationProjectID = loc.ProjectID | ||
|
||
getResp, err := client.Packer.GetChannel(getParams, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return getResp.Payload.Channel, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
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" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-provider-hcp/internal/clients" | ||
) | ||
|
||
var defaultPackerTimeout = time.Minute | ||
|
||
func dataSourcePackerImageIteration() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "The Packer Image data source iteration gets the most recent iteration (or build) of an image, given a channel.", | ||
ReadContext: dataSourcePackerImageRead, | ||
Timeouts: &schema.ResourceTimeout{ | ||
Default: &defaultPackerTimeout, | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
// Required inputs | ||
"bucket": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there any plans to make |
||
Description: "The slug of the HCP Packer Registry image bucket to pull from.", | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateDiagFunc: validateSlugID, | ||
}, | ||
"channel": { | ||
Description: "The channel that points to the version of the image you want.", | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateDiagFunc: validateSlugID, | ||
}, | ||
// computed outputs | ||
"organization_id": { | ||
Description: "The ID of the organization this HCP Packer registry is located in.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"project_id": { | ||
Description: "The ID of the project this HCP Packer registry is located in.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
// Actual iteration: | ||
|
||
"id": { | ||
azr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Description: "HCP ID of this iteration", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"incremental_version": { | ||
Description: "Incremental version of this iteration", | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"created_at": { | ||
Description: "Creation time of this iteration", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"builds": { | ||
Description: "Builds for this iteration. An iteration can have more than one build if it took more than one go to build all images.", | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"cloud_provider": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"component_type": { | ||
Description: "Name of the builder that built this. Ex: 'amazon-ebs.example'", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"created_at": { | ||
Description: "Creation time of this build.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"id": { | ||
Description: "HCP ID of this build.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"images": { | ||
Description: "Output of the build. This will contain the location or cloud identifier for your images.", | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"created_at": { | ||
Description: "Creation time of this image.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"id": { | ||
Description: "HCP ID of this image.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"image_id": { | ||
Description: "Cloud Image ID, URL string identifying this image for the builder that built it.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"region": { | ||
Description: "Region this image was built from. If any.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"labels": { | ||
Description: "Labels for this build.", | ||
Type: schema.TypeMap, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
"packer_run_uuid": { | ||
Description: "UUID of this build.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"status": { | ||
Description: "Status this build. DONE means that all images tied to this build were successfully built.", | ||
azr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"updated_at": { | ||
Description: "Time this build was last updated.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourcePackerImageRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
bucketName := d.Get("bucket").(string) | ||
channelSlug := d.Get("channel").(string) | ||
client := meta.(*clients.Client) | ||
|
||
loc := &sharedmodels.HashicorpCloudLocationLocation{ | ||
OrganizationID: client.Config.OrganizationID, | ||
ProjectID: client.Config.ProjectID, | ||
} | ||
|
||
if err := setLocationData(d, loc); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
log.Printf("[INFO] Reading HCP Packer registry (%s) [project_id=%s, organization_id=%s, channel=%s]", bucketName, loc.ProjectID, loc.OrganizationID, channelSlug) | ||
|
||
channel, err := clients.GetPackerChannelBySlug(ctx, client, loc, bucketName, channelSlug) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
iteration := channel.Pointer.Iteration | ||
|
||
d.SetId(channel.Pointer.Iteration.ID) | ||
|
||
if err := d.Set("incremental_version", iteration.IncrementalVersion); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if err := d.Set("created_at", iteration.CreatedAt.String()); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if err := d.Set("builds", flattenPackerBuildList(iteration.Builds)); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func setLocationData(d *schema.ResourceData, loc *sharedmodels.HashicorpCloudLocationLocation) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ooo, could make this a shared helper 👀 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll maybe do that in another PR, if time ! |
||
if err := d.Set("organization_id", loc.OrganizationID); err != nil { | ||
return err | ||
} | ||
|
||
if err := d.Set("project_id", loc.ProjectID); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func flattenPackerBuildList(builds []*packermodels.HashicorpCloudPackerBuild) (flattened []map[string]interface{}) { | ||
for _, build := range builds { | ||
out := map[string]interface{}{ | ||
"cloud_provider": build.CloudProvider, | ||
"component_type": build.ComponentType, | ||
"created_at": build.CreatedAt.String(), | ||
"id": build.ID, | ||
"images": flattenPackerBuildImagesList(build.Images), | ||
"labels": build.Labels, | ||
"packer_run_uuid": build.PackerRunUUID, | ||
"status": build.Status, | ||
"updated_at": build.UpdatedAt.String(), | ||
} | ||
flattened = append(flattened, out) | ||
} | ||
return | ||
} | ||
|
||
func flattenPackerBuildImagesList(images []*packermodels.HashicorpCloudPackerImage) (flattened []map[string]interface{}) { | ||
for _, image := range images { | ||
out := map[string]interface{}{ | ||
"created_at": image.CreatedAt.String(), | ||
"id": image.ID, | ||
"image_id": image.ImageID, | ||
"region": image.Region, | ||
} | ||
flattened = append(flattened, out) | ||
} | ||
return | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is auto generated but, can I edit things here ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To edit the generated doc, you'll want to add a
packer_image_iteration
template to https://github.com/hashicorp/terraform-provider-hcp/tree/main/templates/data-sources. You can follow the example of HVN route, and add whatever you need.Follow-up question, is this feature generally available? If not, you'll want to add a banner in the template:
-> **Note:** This feature is currently in private/public beta.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's private beta ! I added that, and tweaked things accordingly 👍🏼 thanks !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a way to preview docs ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, some nested attributes — like the
builds
one — are not documented, even though I did document the fields in the provider code; is there something I can do to fix that ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use this tool to preview docs, just have to copy-paste the markdown into the form: https://registry.terraform.io/tools/doc-preview
The nested object fields get displayed below in a Nested Schema section: https://github.com/hashicorp/terraform-provider-hcp/blob/3c2cb9185ca3fc616922556fa13d17231337779b/docs/data-sources/packer_image_iteration.md#nested-schema-for-builds
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dope, thanks ! Sorry I missed that info.
Nice, also, I documented them in here:
terraform-provider-hcp/internal/provider/data_source_packer_image_iteration.go
Lines 61 to 76 in 7ae2cd9
But they are not fully populated here:
terraform-provider-hcp/docs/data-sources/packer_image_iteration.md
Lines 52 to 59 in 6707024
Is it normal ? Is there something I could do for this ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, I looked and long story short, because of the way TF schema works, this is not possible.