github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/registry/regsrc/terraform_provider.go (about)

     1  package regsrc
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform-svchost"
     9  )
    10  
    11  var (
    12  	// DefaultProviderNamespace represents the namespace for canonical
    13  	// HashiCorp-controlled providers.
    14  	DefaultProviderNamespace = "-"
    15  )
    16  
    17  // TerraformProvider describes a Terraform Registry Provider source.
    18  type TerraformProvider struct {
    19  	RawHost      *FriendlyHost
    20  	RawNamespace string
    21  	RawName      string
    22  	OS           string
    23  	Arch         string
    24  }
    25  
    26  // NewTerraformProvider constructs a new provider source.
    27  func NewTerraformProvider(name, os, arch string) *TerraformProvider {
    28  	if os == "" {
    29  		os = runtime.GOOS
    30  	}
    31  	if arch == "" {
    32  		arch = runtime.GOARCH
    33  	}
    34  
    35  	// separate namespace if included
    36  	namespace := DefaultProviderNamespace
    37  	if names := strings.SplitN(name, "/", 2); len(names) == 2 {
    38  		namespace, name = names[0], names[1]
    39  	}
    40  	p := &TerraformProvider{
    41  		RawHost:      PublicRegistryHost,
    42  		RawNamespace: namespace,
    43  		RawName:      name,
    44  		OS:           os,
    45  		Arch:         arch,
    46  	}
    47  
    48  	return p
    49  }
    50  
    51  // Provider returns just the registry ID of the provider
    52  func (p *TerraformProvider) TerraformProvider() string {
    53  	return fmt.Sprintf("%s/%s", p.RawNamespace, p.RawName)
    54  }
    55  
    56  // SvcHost returns the svchost.Hostname for this provider. The
    57  // default PublicRegistryHost is returned.
    58  func (p *TerraformProvider) SvcHost() (svchost.Hostname, error) {
    59  	return svchost.ForComparison(PublicRegistryHost.Raw)
    60  }