2023-05-02 10:33:06 -05:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2019-12-19 19:24:14 -06:00
|
|
|
package getproviders
|
|
|
|
|
|
|
|
import (
|
2020-09-28 19:13:32 -05:00
|
|
|
"context"
|
2019-12-19 19:24:14 -06:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
svchost "github.com/hashicorp/terraform-svchost"
|
|
|
|
disco "github.com/hashicorp/terraform-svchost/disco"
|
|
|
|
|
2023-09-20 06:35:35 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/addrs"
|
2019-12-19 19:24:14 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// RegistrySource is a Source that knows how to find and install providers from
|
|
|
|
// their originating provider registries.
|
|
|
|
type RegistrySource struct {
|
|
|
|
services *disco.Disco
|
|
|
|
}
|
|
|
|
|
2020-01-08 18:24:41 -06:00
|
|
|
var _ Source = (*RegistrySource)(nil)
|
|
|
|
|
2019-12-19 19:24:14 -06:00
|
|
|
// NewRegistrySource creates and returns a new source that will install
|
|
|
|
// providers from their originating provider registries.
|
|
|
|
func NewRegistrySource(services *disco.Disco) *RegistrySource {
|
|
|
|
return &RegistrySource{
|
|
|
|
services: services,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AvailableVersions returns all of the versions available for the provider
|
|
|
|
// with the given address, or an error if that result cannot be determined.
|
|
|
|
//
|
|
|
|
// If the request fails, the returned error might be an value of
|
|
|
|
// ErrHostNoProviders, ErrHostUnreachable, ErrUnauthenticated,
|
|
|
|
// ErrProviderNotKnown, or ErrQueryFailed. Callers must be defensive and
|
|
|
|
// expect errors of other types too, to allow for future expansion.
|
2020-09-28 19:13:32 -05:00
|
|
|
func (s *RegistrySource) AvailableVersions(ctx context.Context, provider addrs.Provider) (VersionList, Warnings, error) {
|
2019-12-19 19:24:14 -06:00
|
|
|
client, err := s.registryClient(provider.Hostname)
|
|
|
|
if err != nil {
|
2020-06-25 09:49:48 -05:00
|
|
|
return nil, nil, err
|
2019-12-19 19:24:14 -06:00
|
|
|
}
|
|
|
|
|
2020-09-28 19:13:32 -05:00
|
|
|
versionsResponse, warnings, err := client.ProviderVersions(ctx, provider)
|
2019-12-19 19:24:14 -06:00
|
|
|
if err != nil {
|
2020-06-25 09:49:48 -05:00
|
|
|
return nil, nil, err
|
2019-12-19 19:24:14 -06:00
|
|
|
}
|
|
|
|
|
2020-06-25 09:49:48 -05:00
|
|
|
if len(versionsResponse) == 0 {
|
|
|
|
return nil, warnings, nil
|
2019-12-19 19:24:14 -06:00
|
|
|
}
|
|
|
|
|
2020-06-25 09:49:48 -05:00
|
|
|
// We ignore protocols here because our goal is to find out which versions
|
|
|
|
// are available _at all_. Which ones are compatible with the current
|
|
|
|
// Terraform becomes relevant only once we've selected one, at which point
|
|
|
|
// we'll return an error if the selected one is incompatible.
|
2020-05-11 12:49:12 -05:00
|
|
|
//
|
|
|
|
// We intentionally produce an error on incompatibility, rather than
|
|
|
|
// silently ignoring an incompatible version, in order to give the user
|
|
|
|
// explicit feedback about why their selection wasn't valid and allow them
|
|
|
|
// to decide whether to fix that by changing the selection or by some other
|
|
|
|
// action such as upgrading Terraform, using a different OS to run
|
2020-06-25 09:49:48 -05:00
|
|
|
// Terraform, etc. Changes that affect compatibility are considered breaking
|
|
|
|
// changes from a provider API standpoint, so provider teams should change
|
|
|
|
// compatibility only in new major versions.
|
|
|
|
ret := make(VersionList, 0, len(versionsResponse))
|
|
|
|
for str := range versionsResponse {
|
2019-12-19 19:24:14 -06:00
|
|
|
v, err := ParseVersion(str)
|
|
|
|
if err != nil {
|
2020-06-25 09:49:48 -05:00
|
|
|
return nil, nil, ErrQueryFailed{
|
2019-12-19 19:24:14 -06:00
|
|
|
Provider: provider,
|
2023-09-18 07:53:49 -05:00
|
|
|
Wrapped: fmt.Errorf("registry response includes invalid version string %q: %w", str, err),
|
2019-12-19 19:24:14 -06:00
|
|
|
}
|
|
|
|
}
|
2020-05-11 12:49:12 -05:00
|
|
|
ret = append(ret, v)
|
2019-12-19 19:24:14 -06:00
|
|
|
}
|
|
|
|
ret.Sort() // lowest precedence first, preserving order when equal precedence
|
2020-06-25 09:49:48 -05:00
|
|
|
return ret, warnings, nil
|
2019-12-19 19:24:14 -06:00
|
|
|
}
|
|
|
|
|
2020-01-08 18:24:41 -06:00
|
|
|
// PackageMeta returns metadata about the location and capabilities of
|
2019-12-19 19:24:14 -06:00
|
|
|
// a distribution package for a particular provider at a particular version
|
|
|
|
// targeting a particular platform.
|
|
|
|
//
|
2020-01-08 18:24:41 -06:00
|
|
|
// Callers of PackageMeta should first call AvailableVersions and pass
|
2019-12-19 19:24:14 -06:00
|
|
|
// one of the resulting versions to this function. This function cannot
|
|
|
|
// distinguish between a version that is not available and an unsupported
|
|
|
|
// target platform, so if it encounters either case it will return an error
|
|
|
|
// suggesting that the target platform isn't supported under the assumption
|
|
|
|
// that the caller already checked that the version is available at all.
|
|
|
|
//
|
|
|
|
// To find a package suitable for the platform where the provider installation
|
|
|
|
// process is running, set the "target" argument to
|
2020-01-08 18:24:41 -06:00
|
|
|
// getproviders.CurrentPlatform.
|
2019-12-19 19:24:14 -06:00
|
|
|
//
|
|
|
|
// If the request fails, the returned error might be an value of
|
|
|
|
// ErrHostNoProviders, ErrHostUnreachable, ErrUnauthenticated,
|
|
|
|
// ErrPlatformNotSupported, or ErrQueryFailed. Callers must be defensive and
|
|
|
|
// expect errors of other types too, to allow for future expansion.
|
2020-09-28 19:13:32 -05:00
|
|
|
func (s *RegistrySource) PackageMeta(ctx context.Context, provider addrs.Provider, version Version, target Platform) (PackageMeta, error) {
|
2019-12-19 19:24:14 -06:00
|
|
|
client, err := s.registryClient(provider.Hostname)
|
|
|
|
if err != nil {
|
|
|
|
return PackageMeta{}, err
|
|
|
|
}
|
|
|
|
|
2020-09-28 19:13:32 -05:00
|
|
|
return client.PackageMeta(ctx, provider, version, target)
|
2019-12-19 19:24:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RegistrySource) registryClient(hostname svchost.Hostname) (*registryClient, error) {
|
|
|
|
host, err := s.services.Discover(hostname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrHostUnreachable{
|
|
|
|
Hostname: hostname,
|
|
|
|
Wrapped: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
url, err := host.ServiceURL("providers.v1")
|
|
|
|
switch err := err.(type) {
|
|
|
|
case nil:
|
|
|
|
// okay! We'll fall through and return below.
|
|
|
|
case *disco.ErrServiceNotProvided:
|
|
|
|
return nil, ErrHostNoProviders{
|
|
|
|
Hostname: hostname,
|
|
|
|
}
|
|
|
|
case *disco.ErrVersionNotSupported:
|
|
|
|
return nil, ErrHostNoProviders{
|
|
|
|
Hostname: hostname,
|
|
|
|
HasOtherVersion: true,
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, ErrHostUnreachable{
|
|
|
|
Hostname: hostname,
|
|
|
|
Wrapped: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we have credentials configured for this hostname.
|
|
|
|
creds, err := s.services.CredentialsForHost(hostname)
|
|
|
|
if err != nil {
|
|
|
|
// This indicates that a credentials helper failed, which means we
|
|
|
|
// can't do anything better than just pass through the helper's
|
|
|
|
// own error message.
|
2023-09-18 07:53:49 -05:00
|
|
|
return nil, fmt.Errorf("failed to retrieve credentials for %s: %w", hostname, err)
|
2019-12-19 19:24:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return newRegistryClient(url, creds), nil
|
|
|
|
}
|
2020-05-14 13:04:13 -05:00
|
|
|
|
|
|
|
func (s *RegistrySource) ForDisplay(provider addrs.Provider) string {
|
|
|
|
return fmt.Sprintf("registry %s", provider.Hostname.ForDisplay())
|
|
|
|
}
|