Class: Dependabot::Terraform::RegistryClient

Inherits:
Object
  • Object
show all
Defined in:
lib/dependabot/terraform/registry_client.rb

Overview

Terraform::RegistryClient is a basic API client to interact with a terraform registry: www.terraform.io/docs/registry/api.html

Constant Summary collapse

PUBLIC_HOSTNAME =
"registry.terraform.io"

Instance Method Summary collapse

Constructor Details

#initialize(hostname:) ⇒ RegistryClient

Returns a new instance of RegistryClient.



14
15
16
# File 'lib/dependabot/terraform/registry_client.rb', line 14

def initialize(hostname:)
  @hostname = hostname
end

Instance Method Details

#all_module_versions(identifier:) ⇒ Array<Dependabot::Terraform::Version>

Fetch all the versions of a module, and return a Version representation of them.

“hashicorp/consul/aws”

Parameters:

  • identifier (String)

    the identifier for the dependency, i.e:

Returns:

Raises:

  • (RuntimeError)

    when the versions cannot be retrieved



43
44
45
46
47
48
49
50
51
52
# File 'lib/dependabot/terraform/registry_client.rb', line 43

def all_module_versions(identifier:)
  # TODO: Implement service discovery for custom registries
  return [] unless hostname == PUBLIC_HOSTNAME

  response = get(endpoint: "modules/#{identifier}/versions")

  JSON.parse(response).
    fetch("modules").first.fetch("versions").
    map { |release| version_class.new(release.fetch("version")) }
end

#all_provider_versions(identifier:) ⇒ Array<Dependabot::Terraform::Version>

Fetch all the versions of a provider, and return a Version representation of them.

“hashicorp/aws”

Parameters:

  • identifier (String)

    the identifier for the dependency, i.e:

Returns:

Raises:

  • (RuntimeError)

    when the versions cannot be retrieved



25
26
27
28
29
30
31
32
33
34
# File 'lib/dependabot/terraform/registry_client.rb', line 25

def all_provider_versions(identifier:)
  # TODO: Implement service discovery for custom registries
  return [] unless hostname == PUBLIC_HOSTNAME

  response = get(endpoint: "providers/#{identifier}/versions")

  JSON.parse(response).
    fetch("versions").
    map { |release| version_class.new(release.fetch("version")) }
end

#source(dependency:) ⇒ Object

Fetch the “source” for a module or provider. We use the API to fetch the source for a dependency, this typically points to a source code repository, and then instantiate a Dependabot::Source object that we can use to fetch Metadata about a specific version of the dependency.

we’re attempting to find

Parameters:

  • dependency (Dependabot::Dependency)

    the dependency who’s source

Returns:

  • Dependabot::Source

Raises:

  • (RuntimeError)

    when the source cannot be retrieved



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dependabot/terraform/registry_client.rb', line 63

def source(dependency:)
  # TODO: Implement service discovery for custom registries
  return unless hostname == PUBLIC_HOSTNAME

  type = dependency.requirements.first[:source][:type]
  endpoint = if type == "registry"
               "modules/#{dependency.name}/#{dependency.version}"
             elsif type == "provider"
               "providers/#{dependency.name}/#{dependency.version}"
             else
               raise "Invalid source type"
             end
  response = get(endpoint: endpoint)

  source_url = JSON.parse(response).fetch("source")
  Source.from_url(source_url) if source_url
end