github.com/hashicorp/packer@v1.14.3/internal/hcp/api/service_version.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 ) 10 11 const incompleteVersionName = "v0" 12 13 // IsVersionComplete returns if the given version is completed or not. 14 // 15 // The best way to know if the version is completed or not is from the name of the version. All version that are 16 // incomplete are named "v0". 17 func (c *Client) IsVersionComplete(version *hcpPackerModels.HashicorpCloudPacker20230101Version) bool { 18 return version.Name != incompleteVersionName 19 } 20 21 func (c *Client) CreateVersion( 22 ctx context.Context, 23 bucketName, 24 fingerprint string, 25 templateType hcpPackerModels.HashicorpCloudPacker20230101TemplateType, 26 ) (*hcpPackerAPI.PackerServiceCreateVersionOK, error) { 27 28 params := hcpPackerAPI.NewPackerServiceCreateVersionParamsWithContext(ctx) 29 params.LocationOrganizationID = c.OrganizationID 30 params.LocationProjectID = c.ProjectID 31 params.BucketName = bucketName 32 params.Body = &hcpPackerModels.HashicorpCloudPacker20230101CreateVersionBody{ 33 Fingerprint: fingerprint, 34 TemplateType: templateType.Pointer(), 35 } 36 37 return c.Packer.PackerServiceCreateVersion(params, nil) 38 } 39 40 func (c *Client) GetVersion( 41 ctx context.Context, bucketName string, fingerprint string, 42 ) (*hcpPackerModels.HashicorpCloudPacker20230101Version, error) { 43 params := hcpPackerAPI.NewPackerServiceGetVersionParams() 44 params.LocationOrganizationID = c.OrganizationID 45 params.LocationProjectID = c.ProjectID 46 params.BucketName = bucketName 47 params.Fingerprint = fingerprint 48 49 resp, err := c.Packer.PackerServiceGetVersion(params, nil) 50 if err != nil { 51 return nil, err 52 } 53 54 if resp.Payload.Version != nil { 55 return resp.Payload.Version, nil 56 } 57 58 return nil, fmt.Errorf( 59 "something went wrong retrieving the version for bucket %s", bucketName, 60 ) 61 }