github.com/hashicorp/packer@v1.14.3/internal/hcp/api/service_build.go (about)

     1  package api
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	hcpPackerAPI "github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/stable/2023-01-01/client/packer_service"
     8  	hcpPackerModels "github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/stable/2023-01-01/models"
     9  	"github.com/hashicorp/packer/packer"
    10  )
    11  
    12  func (c *Client) CreateBuild(
    13  	ctx context.Context, bucketName, runUUID, fingerprint, componentType string,
    14  	buildStatus hcpPackerModels.HashicorpCloudPacker20230101BuildStatus,
    15  ) (*hcpPackerAPI.PackerServiceCreateBuildOK, error) {
    16  
    17  	params := hcpPackerAPI.NewPackerServiceCreateBuildParamsWithContext(ctx)
    18  
    19  	params.LocationOrganizationID = c.OrganizationID
    20  	params.LocationProjectID = c.ProjectID
    21  	params.BucketName = bucketName
    22  	params.Fingerprint = fingerprint
    23  	params.Body = &hcpPackerModels.HashicorpCloudPacker20230101CreateBuildBody{
    24  		ComponentType: componentType,
    25  		PackerRunUUID: runUUID,
    26  		Status:        &buildStatus,
    27  	}
    28  
    29  	return c.Packer.PackerServiceCreateBuild(params, nil)
    30  }
    31  
    32  // ListBuilds queries a Version on HCP Packer registry for all of it's associated builds.
    33  // Currently, all builds are returned regardless of status.
    34  func (c *Client) ListBuilds(
    35  	ctx context.Context, bucketName, fingerprint string,
    36  ) ([]*hcpPackerModels.HashicorpCloudPacker20230101Build, error) {
    37  
    38  	params := hcpPackerAPI.NewPackerServiceListBuildsParamsWithContext(ctx)
    39  	params.LocationOrganizationID = c.OrganizationID
    40  	params.LocationProjectID = c.ProjectID
    41  	params.BucketName = bucketName
    42  	params.Fingerprint = fingerprint
    43  
    44  	resp, err := c.Packer.PackerServiceListBuilds(params, nil)
    45  	if err != nil {
    46  		return []*hcpPackerModels.HashicorpCloudPacker20230101Build{}, err
    47  	}
    48  
    49  	return resp.Payload.Builds, nil
    50  }
    51  
    52  // UpdateBuild updates a single build in a version with the incoming input data.
    53  func (c *Client) UpdateBuild(
    54  	ctx context.Context,
    55  	bucketName, fingerprint string,
    56  	buildID, runUUID, platform, sourceExternalIdentifier string,
    57  	parentVersionID string,
    58  	parentChannelID string,
    59  	buildLabels map[string]string,
    60  	buildStatus hcpPackerModels.HashicorpCloudPacker20230101BuildStatus,
    61  	artifacts []*hcpPackerModels.HashicorpCloudPacker20230101ArtifactCreateBody,
    62  	metadata *hcpPackerModels.HashicorpCloudPacker20230101BuildMetadata,
    63  ) (string, error) {
    64  
    65  	params := hcpPackerAPI.NewPackerServiceUpdateBuildParamsWithContext(ctx)
    66  	params.BuildID = buildID
    67  	params.LocationOrganizationID = c.OrganizationID
    68  	params.LocationProjectID = c.ProjectID
    69  	params.BucketName = bucketName
    70  	params.Fingerprint = fingerprint
    71  
    72  	params.Body = &hcpPackerModels.HashicorpCloudPacker20230101UpdateBuildBody{
    73  		Artifacts:                artifacts,
    74  		Labels:                   buildLabels,
    75  		PackerRunUUID:            runUUID,
    76  		ParentChannelID:          parentChannelID,
    77  		ParentVersionID:          parentVersionID,
    78  		Platform:                 platform,
    79  		SourceExternalIdentifier: sourceExternalIdentifier,
    80  		Status:                   &buildStatus,
    81  		Metadata:                 metadata,
    82  	}
    83  
    84  	resp, err := c.Packer.PackerServiceUpdateBuild(params, nil)
    85  	if err != nil {
    86  		return "", err
    87  	}
    88  
    89  	if resp == nil {
    90  		return "", fmt.Errorf(
    91  			"something went wrong retrieving the build %s from bucket %s", buildID, bucketName,
    92  		)
    93  	}
    94  
    95  	return resp.Payload.Build.ID, nil
    96  }
    97  
    98  func (c *Client) UploadSbom(
    99  	ctx context.Context,
   100  	bucketName, fingerprint string,
   101  	buildID string,
   102  	sbom packer.SBOM,
   103  ) error {
   104  
   105  	params := hcpPackerAPI.NewPackerServiceUploadSbomParamsWithContext(ctx)
   106  	params.BuildID = buildID
   107  	params.LocationOrganizationID = c.OrganizationID
   108  	params.LocationProjectID = c.ProjectID
   109  	params.BucketName = bucketName
   110  	params.Fingerprint = fingerprint
   111  
   112  	params.Body = &hcpPackerModels.HashicorpCloudPacker20230101UploadSbomBody{
   113  		CompressedSbom: sbom.CompressedData,
   114  		Format:         &sbom.Format,
   115  		Name:           sbom.Name,
   116  	}
   117  
   118  	_, err := c.Packer.PackerServiceUploadSbom(params, nil)
   119  	return err
   120  }