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

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package api
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  
    10  	hcpPackerDeprecatedAPI "github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/stable/2021-04-30/client/packer_service"
    11  	hcpPackerDeprecatedModels "github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/stable/2021-04-30/models"
    12  )
    13  
    14  type GetIterationOption func(*hcpPackerDeprecatedAPI.PackerServiceGetIterationParams)
    15  
    16  var (
    17  	GetIteration_byID = func(id string) GetIterationOption {
    18  		return func(params *hcpPackerDeprecatedAPI.PackerServiceGetIterationParams) {
    19  			params.IterationID = &id
    20  		}
    21  	}
    22  	GetIteration_byFingerprint = func(fingerprint string) GetIterationOption {
    23  		return func(params *hcpPackerDeprecatedAPI.PackerServiceGetIterationParams) {
    24  			params.Fingerprint = &fingerprint
    25  		}
    26  	}
    27  )
    28  
    29  func (client *DeprecatedClient) GetIteration(
    30  	ctx context.Context, bucketSlug string, opts ...GetIterationOption,
    31  ) (*hcpPackerDeprecatedModels.HashicorpCloudPackerIteration, error) {
    32  	getItParams := hcpPackerDeprecatedAPI.NewPackerServiceGetIterationParams()
    33  	getItParams.LocationOrganizationID = client.OrganizationID
    34  	getItParams.LocationProjectID = client.ProjectID
    35  	getItParams.BucketSlug = bucketSlug
    36  
    37  	for _, opt := range opts {
    38  		opt(getItParams)
    39  	}
    40  
    41  	resp, err := client.Packer.PackerServiceGetIteration(getItParams, nil)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	if resp.Payload.Iteration != nil {
    47  		return resp.Payload.Iteration, nil
    48  	}
    49  
    50  	return nil, fmt.Errorf(
    51  		"something went wrong retrieving the iteration for bucket %s", bucketSlug,
    52  	)
    53  }
    54  
    55  // GetChannel loads the named channel that is associated to the bucket slug . If the
    56  // channel does not exist in HCP Packer, GetChannel returns an error.
    57  func (client *DeprecatedClient) GetChannel(
    58  	ctx context.Context, bucketSlug string, channelName string,
    59  ) (*hcpPackerDeprecatedModels.HashicorpCloudPackerChannel, error) {
    60  	params := hcpPackerDeprecatedAPI.NewPackerServiceGetChannelParamsWithContext(ctx)
    61  	params.LocationOrganizationID = client.OrganizationID
    62  	params.LocationProjectID = client.ProjectID
    63  	params.BucketSlug = bucketSlug
    64  	params.Slug = channelName
    65  
    66  	resp, err := client.Packer.PackerServiceGetChannel(params, nil)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  
    71  	if resp.Payload.Channel == nil {
    72  		return nil, fmt.Errorf(
    73  			"there is no channel with the name %s associated with the bucket %s",
    74  			channelName, bucketSlug,
    75  		)
    76  	}
    77  
    78  	return resp.Payload.Channel, nil
    79  }