Files
Packer-Cn/internal/hcp/api/service.go
T

254 lines
7.6 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
2023-08-10 15:53:29 -07:00
// SPDX-License-Identifier: BUSL-1.1
2022-11-07 14:16:04 -05:00
package api
import (
"context"
"errors"
"fmt"
"github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/stable/2021-04-30/client/packer_service"
"github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/stable/2021-04-30/models"
"google.golang.org/grpc/codes"
)
2021-12-01 15:58:33 +01:00
func (client *Client) CreateBucket(
ctx context.Context,
bucketSlug,
bucketDescription string,
bucketLabels map[string]string,
) (*packer_service.PackerServiceCreateBucketOK, error) {
2021-12-01 15:58:33 +01:00
createBktParams := packer_service.NewPackerServiceCreateBucketParams()
createBktParams.LocationOrganizationID = client.OrganizationID
createBktParams.LocationProjectID = client.ProjectID
2022-11-28 16:14:30 -05:00
createBktParams.Body = packer_service.PackerServiceCreateBucketBody{
2021-12-01 15:58:33 +01:00
BucketSlug: bucketSlug,
Description: bucketDescription,
Labels: bucketLabels,
}
2021-12-01 15:58:33 +01:00
return client.Packer.PackerServiceCreateBucket(createBktParams, nil)
}
2021-12-01 15:58:33 +01:00
func (client *Client) DeleteBucket(
ctx context.Context,
bucketSlug string,
) (*packer_service.PackerServiceDeleteBucketOK, error) {
2021-12-01 15:58:33 +01:00
deleteBktParams := packer_service.NewPackerServiceDeleteBucketParamsWithContext(ctx)
deleteBktParams.LocationOrganizationID = client.OrganizationID
deleteBktParams.LocationProjectID = client.ProjectID
deleteBktParams.BucketSlug = bucketSlug
return client.Packer.PackerServiceDeleteBucket(deleteBktParams, nil)
}
// UpsertBucket tries to create a bucket on a HCP Packer Registry. If the bucket
// exists it will handle the error and update the bucket with the provided
// details.
func (client *Client) UpsertBucket(
ctx context.Context,
bucketSlug,
bucketDescription string,
bucketLabels map[string]string,
) error {
// Create bucket if exist we continue as is, eventually we want to treat
// this like an upsert
_, err := client.CreateBucket(ctx, bucketSlug, bucketDescription, bucketLabels)
2022-11-07 14:16:04 -05:00
if err != nil && !CheckErrorCode(err, codes.AlreadyExists) {
return err
}
if err == nil {
return nil
}
2021-12-01 15:58:33 +01:00
params := packer_service.NewPackerServiceUpdateBucketParamsWithContext(ctx)
params.LocationOrganizationID = client.OrganizationID
params.LocationProjectID = client.ProjectID
2021-12-01 15:58:33 +01:00
params.BucketSlug = bucketSlug
2022-11-28 16:14:30 -05:00
params.Body = packer_service.PackerServiceUpdateBucketBody{
2021-12-01 15:58:33 +01:00
Description: bucketDescription,
Labels: bucketLabels,
}
2021-10-01 12:53:37 -07:00
_, err = client.Packer.PackerServiceUpdateBucket(params, nil)
return err
}
2021-12-01 15:58:33 +01:00
func (client *Client) CreateIteration(
ctx context.Context,
bucketSlug,
fingerprint string,
templateType models.HashicorpCloudPackerIterationTemplateType,
2021-12-01 15:58:33 +01:00
) (*packer_service.PackerServiceCreateIterationOK, error) {
params := packer_service.NewPackerServiceCreateIterationParamsWithContext(ctx)
params.LocationOrganizationID = client.OrganizationID
params.LocationProjectID = client.ProjectID
2021-12-01 15:58:33 +01:00
params.BucketSlug = bucketSlug
2022-11-28 16:14:30 -05:00
params.Body = packer_service.PackerServiceCreateIterationBody{
Fingerprint: fingerprint,
TemplateType: templateType.Pointer(),
}
2021-12-01 15:58:33 +01:00
return client.Packer.PackerServiceCreateIteration(params, nil)
}
2021-12-01 15:58:33 +01:00
type GetIterationOption func(*packer_service.PackerServiceGetIterationParams)
var (
GetIteration_byID = func(id string) GetIterationOption {
return func(params *packer_service.PackerServiceGetIterationParams) {
params.IterationID = &id
}
}
GetIteration_byFingerprint = func(fingerprint string) GetIterationOption {
return func(params *packer_service.PackerServiceGetIterationParams) {
params.Fingerprint = &fingerprint
}
}
)
2022-01-13 12:28:50 +01:00
func (client *Client) GetIteration(ctx context.Context, bucketSlug string, opts ...GetIterationOption) (*models.HashicorpCloudPackerIteration, error) {
2021-12-01 15:58:33 +01:00
getItParams := packer_service.NewPackerServiceGetIterationParams()
getItParams.LocationOrganizationID = client.OrganizationID
getItParams.LocationProjectID = client.ProjectID
getItParams.BucketSlug = bucketSlug
for _, opt := range opts {
opt(getItParams)
}
2022-01-13 12:28:50 +01:00
resp, err := client.Packer.PackerServiceGetIteration(getItParams, nil)
if err != nil {
return nil, err
}
if resp.Payload.Iteration != nil {
return resp.Payload.Iteration, nil
}
return nil, fmt.Errorf("something went wrong retrieving the iteration for bucket %s", bucketSlug)
2021-12-01 15:58:33 +01:00
}
func (client *Client) CreateBuild(
ctx context.Context,
bucketSlug,
runUUID,
iterationID,
fingerprint,
componentType string,
status models.HashicorpCloudPackerBuildStatus,
) (*packer_service.PackerServiceCreateBuildOK, error) {
params := packer_service.NewPackerServiceCreateBuildParamsWithContext(ctx)
params.LocationOrganizationID = client.OrganizationID
params.LocationProjectID = client.ProjectID
2021-12-01 15:58:33 +01:00
params.BucketSlug = bucketSlug
params.IterationID = iterationID
2022-11-28 16:14:30 -05:00
params.Body = packer_service.PackerServiceCreateBuildBody{
2021-12-01 15:58:33 +01:00
Fingerprint: fingerprint,
Build: &models.HashicorpCloudPackerBuildCreateBody{
ComponentType: componentType,
PackerRunUUID: runUUID,
2022-11-28 16:14:30 -05:00
Status: status.Pointer(),
2021-12-01 15:58:33 +01:00
},
}
2021-12-01 15:58:33 +01:00
return client.Packer.PackerServiceCreateBuild(params, nil)
}
2021-12-01 15:58:33 +01:00
// ListBuilds queries an Iteration on HCP Packer registry for all of it's
// associated builds. Currently all builds are returned regardless of status.
func (client *Client) ListBuilds(
ctx context.Context,
bucketSlug string,
iterationID string,
) ([]*models.HashicorpCloudPackerBuild, error) {
2021-12-01 15:58:33 +01:00
params := packer_service.NewPackerServiceListBuildsParamsWithContext(ctx)
params.LocationOrganizationID = client.OrganizationID
params.LocationProjectID = client.ProjectID
params.BucketSlug = bucketSlug
params.IterationID = iterationID
2021-10-01 12:53:37 -07:00
resp, err := client.Packer.PackerServiceListBuilds(params, nil)
if err != nil {
return []*models.HashicorpCloudPackerBuild{}, err
}
return resp.Payload.Builds, nil
}
2021-12-01 15:58:33 +01:00
// UpdateBuild updates a single iteration build entry with the incoming input
// data.
func (client *Client) UpdateBuild(
ctx context.Context,
buildID,
runUUID,
cloudProvider,
sourceImageID string,
sourceIterationID string,
2022-07-29 15:15:29 +02:00
sourceChannelID string,
2021-12-01 15:58:33 +01:00
labels map[string]string,
status models.HashicorpCloudPackerBuildStatus,
images []*models.HashicorpCloudPackerImageCreateBody,
) (string, error) {
params := packer_service.NewPackerServiceUpdateBuildParamsWithContext(ctx)
params.BuildID = buildID
params.LocationOrganizationID = client.OrganizationID
params.LocationProjectID = client.ProjectID
2021-12-01 15:58:33 +01:00
2022-11-28 16:14:30 -05:00
params.Body = packer_service.PackerServiceUpdateBuildBody{
2021-12-01 15:58:33 +01:00
Updates: &models.HashicorpCloudPackerBuildUpdates{
Images: images,
PackerRunUUID: runUUID,
Labels: labels,
2022-11-28 16:14:30 -05:00
Status: status.Pointer(),
CloudProvider: cloudProvider,
SourceImageID: sourceImageID,
SourceIterationID: sourceIterationID,
2022-07-29 15:15:29 +02:00
SourceChannelID: sourceChannelID,
2021-12-01 15:58:33 +01:00
},
}
2021-10-01 12:53:37 -07:00
resp, err := client.Packer.PackerServiceUpdateBuild(params, nil)
if err != nil {
return "", err
}
if resp == nil {
return "", errors.New("Not sure why response is nil")
}
return resp.Payload.Build.ID, nil
}
2022-07-29 15:15:29 +02:00
// GetChannel loads the named channel that is associated to the bucket slug . If the
2021-12-01 15:58:33 +01:00
// channel does not exist in HCP Packer, GetChannel returns an error.
2022-07-29 15:15:29 +02:00
func (client *Client) GetChannel(ctx context.Context, bucketSlug string, channelName string) (*models.HashicorpCloudPackerChannel, error) {
2021-12-01 15:58:33 +01:00
params := packer_service.NewPackerServiceGetChannelParamsWithContext(ctx)
params.LocationOrganizationID = client.OrganizationID
params.LocationProjectID = client.ProjectID
params.BucketSlug = bucketSlug
params.Slug = channelName
2021-10-01 12:53:37 -07:00
resp, err := client.Packer.PackerServiceGetChannel(params, nil)
if err != nil {
return nil, err
}
2022-07-29 15:15:29 +02:00
if resp.Payload.Channel == nil {
return nil, fmt.Errorf("there is no channel with the name %s associated with the bucket %s",
channelName, bucketSlug)
}
2022-07-29 15:15:29 +02:00
return resp.Payload.Channel, nil
}