Class: TerraspaceBundler::Mod::Registry

Inherits:
Object
  • Object
show all
Includes:
TB::Util::Logging, Http::Concern
Defined in:
lib/terraspace_bundler/mod/registry.rb

Instance Method Summary collapse

Methods included from Http::Concern

#http_request, #http_request_error_message

Constructor Details

#initialize(source, version) ⇒ Registry

Returns a new instance of Registry.



6
7
8
# File 'lib/terraspace_bundler/mod/registry.rb', line 6

def initialize(source, version)
  @source, @version = source, version
end

Instance Method Details

#get_version(base_url) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/terraspace_bundler/mod/registry.rb', line 109

def get_version(base_url)
  return @version.sub(/^v/,'') if @version # v1.0 => 1.0
  api_url = [base_url, @source].join('/')
  resp = http_request(api_url)
  data = JSON.load(resp.body)
  data['version']
end

#github_urlObject



10
11
12
13
14
15
16
# File 'lib/terraspace_bundler/mod/registry.rb', line 10

def github_url
  if @source.split('/').size > 3 # IE: app.terraform.io/demo-qa/s3-webapp/aws
    private_github_url
  else # assume size is 3. IE: terraform-aws-modules/security-group/aws
    public_github_url
  end
end

#private_github_urlObject

www.terraform.io/docs/cloud/api/modules.html#get-a-module GET /organizations/:organization_name/registry-modules/:registry_name/:namespace/:name/:provider GET /organizations/demo-qa/registry-modules/private/demo-qa/s3-webapp/aws

:organization_name	The name of the organization the module belongs to.
:namespace	        The namespace of the module. For private modules this is the name of the organization that owns the module.
:name	              The module name.
:provider	          The module provider.
:registry-name	    Either public or private.

Example: app.terraform.io/demo-qa/s3-webapp/aws

domain: app.terraform.io (private registry indicator)
org: demo-qa
module: s3-webapp
provider: aws


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/terraspace_bundler/mod/registry.rb', line 35

def private_github_url
  domain, org, name, provider = @source.split('/')
  registry_name = 'private'
  namespace = org

  base_site = "https://#{domain}" # IE: domain = 'app.terraform.io'
  base_url = "#{base_site}/api/v2"
  api_url = "#{base_url}/organizations/#{org}/registry-modules/#{registry_name}/#{namespace}/#{name}/#{provider}"

  resp = http_request(api_url, auth_domain: domain)

  repo_url = case resp.code.to_i
  when 200
    result = JSON.load(resp.body)
    result['data']['attributes']['vcs-repo']['repository-http-url'] # repo_url
  when 401
    auth_error_exit!(resp)
  else
    logger.error "ERROR: Unable to lookup up module in Terraform Registry: #{@source}".color(:red)
    puts "resp.code #{resp.code}"
    pp resp
    exit 1
  end

  clone_with = 'git' # TODO: make configurable
  unless clone_with == 'https'
    repo_url.sub!('https://github.com/', '[email protected]:')
  end

  repo_url
end

#public_github_urlObject

Terrafile example

mod "sg", source: "terraform-aws-modules/security-group/aws", version: "3.10.0"

Resources: www.terraform.io/docs/registry/api.html

Latest version:

https://registry.terraform.io/v1/modules/terraform-aws-modules/sqs/aws/2.1.0/download

The latest version returns an 302 and contains a location header that is followed and then downloaded the same way the specific version is downloaded.

Specific version:

https://registry.terraform.io/v1/modules/terraform-aws-modules/sqs/aws/download

The specific version returns an 204 and then we grab the download url info form the x-terraform-get header.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/terraspace_bundler/mod/registry.rb', line 85

def public_github_url
  base_site = "https://registry.terraform.io"
  base_url = "#{base_site}/v1/modules"
  version = get_version(base_url)
  api_url = [base_url, @source, version, "download"].compact.join('/')

  resp = http_request(api_url)

  case resp.code.to_i
  when 204 # IE: curl -v https://registry.terraform.io/v1/modules/terraform-aws-modules/security-group/aws/3.10.0/download
    download_url = resp.header["x-terraform-get"]
  else
    logger.error "ERROR: Unable to lookup up module in Terraform Registry: #{@source}".color(:red)
    logger.error "api_url: #{api_url}"
    puts "resp.code #{resp.code}"
    pp resp
    exit 1
  end

  url = download_url.sub(%r{/archive/.*},'')
  # IE: git::https://github.com/terraform-aws-modules/terraform-aws-security-group?ref=v3.10.0
  url.sub(/^git::/,'').sub(/\?.*/,'')
end