mirror of
https://github.com/hashicorp/packer.git
synced 2026-10-13 09:42:02 -04:00
* Add working registry pkg * Add custom error for handling the loading of PAR environment variables * Working Publish to Build, with proper error handling for bucket names * Update hcp-sdk-go to use branch instead of mod replace directive * Update Packer build status configuration * Add support for HCP_PACKER_BUILD_FINGERPRINT env * Add support for publishing one or more PARtifacts from a single build * add git shas to this branch * Add ability to set provider name if available * Add working RegistryBuilder type * Add RegistryPostProcessor as wrapper post-processor * When in PAR mode a empty RegistryPostProcessor is added to the end of the post-processor list to publish all final image data. * Add support for updating a build from PAR that is not in a DONE state * Fix a small issue with creation the initial builds for an empty iteration. * Add PAR URL to post-processor display * Implement hcp_packer_registry block (#11168) * Update vendored Amazon plugin to v1.0.1-dev * Fix panic when running a Packer registry build in a clean directory * Remove the publishing of post-processor metadata from the registry post-processor. * Remove metadata add from registry_builder * Update registry builder to skip a build that was found to be DONE Co-authored-by: Megan Marsh <[email protected]> Co-authored-by: Sylvia Moss <[email protected]>
99 lines
3.3 KiB
Go
99 lines
3.3 KiB
Go
package packer_registry
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
packerSvc "github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/preview/2021-04-30/client/packer_service"
|
|
organizationSvc "github.com/hashicorp/hcp-sdk-go/clients/cloud-resource-manager/preview/2019-12-10/client/organization_service"
|
|
projectSvc "github.com/hashicorp/hcp-sdk-go/clients/cloud-resource-manager/preview/2019-12-10/client/project_service"
|
|
rmmodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-resource-manager/preview/2019-12-10/models"
|
|
"github.com/hashicorp/hcp-sdk-go/httpclient"
|
|
"github.com/hashicorp/packer/internal/packer_registry/env"
|
|
)
|
|
|
|
// Client is an HCP client capable of making requests on behalf of a service principal
|
|
type Client struct {
|
|
Packer packerSvc.ClientService
|
|
Organization organizationSvc.ClientService
|
|
Project projectSvc.ClientService
|
|
|
|
// OrganizationID is the organization unique identifier on HCP.
|
|
OrganizationID string
|
|
|
|
// ProjectID is the project unique identifier on HCP.
|
|
ProjectID string
|
|
}
|
|
|
|
// NewClient returns an authenticated client to a HCP Packer Registry.
|
|
// Client authentication requires the following environment variables be set HCP_CLIENT_ID and HCP_CLIENT_SECRET.
|
|
// Upon error a HCPClientError will be returned.
|
|
func NewClient() (*Client, error) {
|
|
if !env.HasHCPCredentials() {
|
|
return nil, &ClientError{
|
|
StatusCode: InvalidClientConfig,
|
|
Err: fmt.Errorf("the client authentication requires both %s and %s environment variables to be set", env.HCPClientID, env.HCPClientSecret),
|
|
}
|
|
}
|
|
|
|
cl, err := httpclient.New(httpclient.Config{})
|
|
if err != nil {
|
|
return nil, &ClientError{
|
|
StatusCode: InvalidClientConfig,
|
|
Err: err,
|
|
}
|
|
}
|
|
|
|
client := &Client{
|
|
Packer: packerSvc.New(cl, nil),
|
|
Organization: organizationSvc.New(cl, nil),
|
|
Project: projectSvc.New(cl, nil),
|
|
}
|
|
|
|
if err := client.loadOrganizationID(); err != nil {
|
|
return nil, &ClientError{
|
|
StatusCode: InvalidClientConfig,
|
|
Err: err,
|
|
}
|
|
}
|
|
if err := client.loadProjectID(); err != nil {
|
|
return nil, &ClientError{
|
|
StatusCode: InvalidClientConfig,
|
|
Err: err,
|
|
}
|
|
}
|
|
|
|
return client, nil
|
|
}
|
|
|
|
func (c *Client) loadOrganizationID() error {
|
|
// Get the organization ID.
|
|
listOrgParams := organizationSvc.NewOrganizationServiceListParams()
|
|
listOrgResp, err := c.Organization.OrganizationServiceList(listOrgParams, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to fetch organization list: %v", err)
|
|
}
|
|
orgLen := len(listOrgResp.Payload.Organizations)
|
|
if orgLen != 1 {
|
|
return fmt.Errorf("unexpected number of organizations: expected 1, actual: %v", orgLen)
|
|
}
|
|
c.OrganizationID = listOrgResp.Payload.Organizations[0].ID
|
|
return nil
|
|
}
|
|
|
|
func (c *Client) loadProjectID() error {
|
|
// Get the project using the organization ID.
|
|
listProjParams := projectSvc.NewProjectServiceListParams()
|
|
listProjParams.ScopeID = &c.OrganizationID
|
|
scopeType := string(rmmodels.HashicorpCloudResourcemanagerResourceIDResourceTypeORGANIZATION)
|
|
listProjParams.ScopeType = &scopeType
|
|
listProjResp, err := c.Project.ProjectServiceList(listProjParams, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to fetch project id: %v", err)
|
|
}
|
|
if len(listProjResp.Payload.Projects) > 1 {
|
|
return fmt.Errorf("this version of Packer does not support multiple projects")
|
|
}
|
|
c.ProjectID = listProjResp.Payload.Projects[0].ID
|
|
return nil
|
|
}
|