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

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  // Package api provides access to the HCP Packer Registry API.
     5  package api
     6  
     7  import (
     8  	"fmt"
     9  
    10  	packerSvc "github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/stable/2021-04-30/client/packer_service"
    11  	organizationSvc "github.com/hashicorp/hcp-sdk-go/clients/cloud-resource-manager/stable/2019-12-10/client/organization_service"
    12  	projectSvc "github.com/hashicorp/hcp-sdk-go/clients/cloud-resource-manager/stable/2019-12-10/client/project_service"
    13  	"github.com/hashicorp/hcp-sdk-go/httpclient"
    14  	"github.com/hashicorp/packer/version"
    15  )
    16  
    17  // DeprecatedClient is an HCP client capable of making requests on behalf of a service principal
    18  type DeprecatedClient struct {
    19  	Packer         packerSvc.ClientService
    20  	Organization   organizationSvc.ClientService
    21  	Project        projectSvc.ClientService
    22  	OrganizationID string
    23  	ProjectID      string
    24  }
    25  
    26  // NewDeprecatedClient returns an authenticated client to a HCP Packer Registry.
    27  // Upon error a HCPClientError will be returned.
    28  func NewDeprecatedClient() (*DeprecatedClient, error) {
    29  	// Use NewClient to validate HCP configuration provided by user.
    30  	tempClient, err := NewClient()
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	hcpClientCfg := httpclient.Config{
    36  		SourceChannel: fmt.Sprintf("packer/%s", version.PackerVersion.FormattedVersion()),
    37  	}
    38  	cl, err := httpclient.New(hcpClientCfg)
    39  	if err != nil {
    40  		return nil, &ClientError{
    41  			StatusCode: InvalidClientConfig,
    42  			Err:        err,
    43  		}
    44  	}
    45  
    46  	client := DeprecatedClient{
    47  		Packer:         packerSvc.New(cl, nil),
    48  		Organization:   organizationSvc.New(cl, nil),
    49  		Project:        projectSvc.New(cl, nil),
    50  		OrganizationID: tempClient.OrganizationID,
    51  		ProjectID:      tempClient.ProjectID,
    52  	}
    53  	return &client, nil
    54  }