// NewClient creates a new Terraform Enterprise API client.
NewClient creates a new Terraform Enterprise API client.
// NewClient creates a new Terraform Enterprise API client .
func NewClient(cfg *Config) (*Client, error) {
	config := DefaultConfig()
	if cfg != nil {
		if cfg.Address != "" {
			config.Address = cfg.Address
		}
		if cfg.BasePath != "" {
			config.BasePath = cfg.BasePath
		}
		if cfg.Token != "" {
			config.Token = cfg.Token
		}
		for k, v := range cfg.Headers {
			config.Headers[k] = v
		}
		if cfg.HTTPClient != nil {
			config.HTTPClient = cfg.HTTPClient
		}
		if cfg.RetryLogHook != nil {
			config.RetryLogHook = cfg.RetryLogHook
		}
	}
	baseURL, err := url.Parse(config.Address)
	if err != nil {
		return nil, fmt.Errorf("invalid address: %v", err)
	}
	baseURL.Path = config.BasePath
	if !strings.HasSuffix(baseURL.Path, "/") {
		baseURL.Path += "/"
	}
	if config.Token == "" {
		return nil, fmt.Errorf("missing API token")
	}
	client := &Client{
		baseURL:      baseURL,
		token:        config.Token,
		headers:      config.Headers,
		retryLogHook: config.RetryLogHook,
	}
	client.http = &retryablehttp.Client{
		Backoff:      client.retryHTTPBackoff,
		CheckRetry:   client.retryHTTPCheck,
		ErrorHandler: retryablehttp.PassthroughErrorHandler,
		HTTPClient:   config.HTTPClient,
		RetryWaitMin: 100 * time.Millisecond,
		RetryWaitMax: 400 * time.Millisecond,
		RetryMax:     30,
	}
	meta, err := client.getRawAPIMetadata()
	if err != nil {
		return nil, err
	}
	client.configureLimiter(meta.RateLimit)
	client.remoteAPIVersion = meta.APIVersion
	client.Applies = &applies{client: client}
	client.ConfigurationVersions = &configurationVersions{client: client}
	client.CostEstimates = &costEstimates{client: client}
	client.NotificationConfigurations = &notificationConfigurations{client: client}
	client.OAuthClients = &oAuthClients{client: client}
	client.OAuthTokens = &oAuthTokens{client: client}
	client.Organizations = &organizations{client: client}
	client.OrganizationMemberships = &organizationMemberships{client: client}
	client.OrganizationTokens = &organizationTokens{client: client}
	client.Plans = &plans{client: client}
	client.PlanExports = &planExports{client: client}
	client.Policies = &policies{client: client}
	client.PolicyChecks = &policyChecks{client: client}
	client.PolicySetParameters = &policySetParameters{client: client}
	client.PolicySets = &policySets{client: client}
	client.Runs = &runs{client: client}
	client.RunTriggers = &runTriggers{client: client}
	client.SSHKeys = &sshKeys{client: client}
	client.StateVersions = &stateVersions{client: client}
	client.Teams = &teams{client: client}
	client.TeamAccess = &teamAccesses{client: client}
	client.TeamMembers = &teamMembers{client: client}
	client.TeamTokens = &teamTokens{client: client}
	client.Users = &users{client: client}
	client.Variables = &variables{client: client}
	client.Workspaces = &workspaces{client: client}
	return client, nil
}
func NewClient ( cfg * Config ) ( * Client , error ) { config := DefaultConfig ( ) if cfg != nil { if cfg . Address != " " { config . Address = cfg . Address } if cfg . BasePath != " " { config . BasePath = cfg . BasePath } if cfg . Token != " " { config . Token = cfg . Token } for k , v := range cfg . Headers { config . Headers [ k ] = v } if cfg . HTTPClient != nil { config . HTTPClient = cfg . HTTPClient } if cfg . RetryLogHook != nil { config . RetryLogHook = cfg . RetryLogHook } } baseURL , err := url . Parse ( config . Address ) if err != nil { return nil , fmt . Errorf ( " " , err ) } baseURL . Path = config . BasePath if ! strings . HasSuffix ( baseURL . Path , " " ) { baseURL . Path += " " } if config . Token == " " { return nil , fmt . Errorf ( " " ) } client := & Client { baseURL : baseURL , token : config . Token , headers : config . Headers , retryLogHook : config . RetryLogHook , } client . http = & retryablehttp . Client { Backoff : client . retryHTTPBackoff , CheckRetry : client . retryHTTPCheck , ErrorHandler : retryablehttp . PassthroughErrorHandler , HTTPClient : config . HTTPClient , RetryWaitMin : 100 * time . Millisecond , RetryWaitMax : 400 * time . Millisecond , RetryMax : 30 , } meta , err := client . getRawAPIMetadata ( ) if err != nil { return nil , err } client . configureLimiter ( meta . RateLimit ) client . remoteAPIVersion = meta . APIVersion client . Applies = & applies { client : client } client . ConfigurationVersions = & configurationVersions { client : client } client . CostEstimates = & costEstimates { client : client } client . NotificationConfigurations = & notificationConfigurations { client : client } client . OAuthClients = & oAuthClients { client : client } client . OAuthTokens = & oAuthTokens { client : client } client . Organizations = & organizations { client : client } client . OrganizationMemberships = & organizationMemberships { client : client } client . OrganizationTokens = & organizationTokens { client : client } client . Plans = & plans { client : client } client . PlanExports = & planExports { client : client } client . Policies = & policies { client : client } client . PolicyChecks = & policyChecks { client : client } client . PolicySetParameters = & policySetParameters { client : client } client . PolicySets = & policySets { client : client } client . Runs = & runs { client : client } client . RunTriggers = & runTriggers { client : client } client . SSHKeys = & sshKeys { client : client } client . StateVersions = & stateVersions { client : client } client . Teams = & teams { client : client } client . TeamAccess = & teamAccesses { client : client } client . TeamMembers = & teamMembers { client : client } client . TeamTokens = & teamTokens { client : client } client . Users = & users { client : client } client . Variables = & variables { client : client } client . Workspaces = & workspaces { client : client } return client , nil }
NewClient creates a new Terraform Enterprise API client.
NewClient creates a new Terraform Enterprise API client .
// Layer in the provided config for any non-blank values. // Parse the address to make sure its a valid URL. // This value must be provided by the user. // Create the client. // Configure the rate limiter. // Save the API version so we can return it from the RemoteAPIVersion // method later. // Create the services.