Skip to content
This repository was archived by the owner on Jun 11, 2021. It is now read-only.

Commit ef0c395

Browse files
committed
Finalise module registry refactor
1 parent 4d8a0ae commit ef0c395

File tree

5 files changed

+9
-59
lines changed

5 files changed

+9
-59
lines changed

Terrafile.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
terrafile-test-registry:
22
source: "terraform-digitalocean-modules/droplet/digitalocean"
3-
version: "0.1.7"
3+
version: "0.1.x"
44
terrafile-test-https:
55
source: "github.com/terraform-digitalocean-modules/terraform-digitalocean-droplet.git"
66
terrafile-test-tag:

cmd/install.go

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,9 @@
2121
package cmd
2222

2323
import (
24-
"errors"
25-
"fmt"
26-
"net/http"
2724
"net/url"
2825
"os"
2926
"path"
30-
"regexp"
3127
"strings"
3228
"sync"
3329

@@ -86,7 +82,7 @@ func getModule(moduleName string, moduleMeta module, wg *sync.WaitGroup) {
8682
case xt.IsLocalSourceAddr(moduleSource):
8783
xt.CopyFile(moduleName, moduleSource, directory)
8884
case xt.IsRegistrySourceAddr(moduleSource):
89-
source, version := getRegistrySource(moduleName, moduleSource, moduleVersion)
85+
source, version := xt.GetRegistrySource(moduleName, moduleSource, moduleVersion, nil)
9086
getWithGoGetter(moduleName, source, version, directory)
9187
default:
9288
getWithGoGetter(moduleName, moduleSource, moduleVersion, directory)
@@ -108,54 +104,6 @@ func getModule(moduleName string, moduleMeta module, wg *sync.WaitGroup) {
108104
os.RemoveAll(path.Join(directory, ".git"))
109105
}
110106

111-
// Handle modules from Terraform registry
112-
var registryBaseURL = "https://registry.terraform.io/v1/modules"
113-
var githubDownloadURLRe = regexp.MustCompile(`https://[^/]+/repos/([^/]+)/([^/]+)/tarball/([^/]+)/.*`)
114-
115-
func getRegistrySource(name string, source string, version string) (string, string) {
116-
logVersion := "latest"
117-
if len(version) > 0 {
118-
logVersion = version
119-
}
120-
121-
jww.INFO.Printf("[%s] Looking up %s version %s in Terraform registry", name, source, logVersion)
122-
123-
src := strings.Split(source, "/")
124-
namespace, name, provider := src[0], src[1], src[2]
125-
126-
registryDownloadURL := fmt.Sprintf("%s/%s/%s/%s/%s/download",
127-
registryBaseURL,
128-
namespace,
129-
name,
130-
provider,
131-
version)
132-
133-
client := &http.Client{}
134-
req, err := http.NewRequest("GET", registryDownloadURL, nil)
135-
xt.CheckIfError(name, err)
136-
137-
req.Header.Set("User-Agent", "XTerrafile (https://github.com/devopsmakers/xterrafile)")
138-
resp, err := client.Do(req)
139-
xt.CheckIfError(name, err)
140-
defer resp.Body.Close()
141-
142-
var githubDownloadURL = ""
143-
if len(resp.Header["X-Terraform-Get"]) > 0 {
144-
githubDownloadURL = resp.Header["X-Terraform-Get"][0]
145-
}
146-
jww.INFO.Printf("[%s] %v", name, registryDownloadURL)
147-
148-
if githubDownloadURLRe.MatchString(githubDownloadURL) {
149-
matches := githubDownloadURLRe.FindStringSubmatch(githubDownloadURL)
150-
user, repo, version := matches[1], matches[2], matches[3]
151-
source = fmt.Sprintf("github.com/%s/%s.git", user, repo)
152-
return source, version
153-
}
154-
err = errors.New("Unable to find module or version download url")
155-
xt.CheckIfError(name, err)
156-
return "", "" // Never reaches here
157-
}
158-
159107
// Handle modules from other sources to reflect:
160108
// https://www.terraform.io/docs/modules/sources.html
161109
//

main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ func TestTerraformWithTerrafilePath(t *testing.T) {
4040
"[terrafile-test-https] Fetching git::https://github.com/terraform-digitalocean-modules/terraform-digitalocean-droplet.git",
4141
"[terrafile-test-branch] Fetching git::ssh://git@github.com/terraform-digitalocean-modules/terraform-digitalocean-droplet.git?ref=branch_test",
4242
"[terrafile-test-tag] Fetching git::ssh://git@github.com/terraform-digitalocean-modules/terraform-digitalocean-droplet.git?ref=v0.1.7",
43-
"[terrafile-test-registry] Looking up terraform-digitalocean-modules/droplet/digitalocean version 0.1.7 in Terraform registry",
44-
"[terrafile-test-registry] Fetching git::https://github.com/terraform-digitalocean-modules/terraform-digitalocean-droplet.git?ref=v0.1.7",
43+
"[terrafile-test-registry] Found module version 0.1.7 at registry.terraform.io",
44+
"[terrafile-test-registry] Fetching https://api.github.com/repos/terraform-digitalocean-modules/terraform-digitalocean-droplet/tarball/v0.1.7//*?archive=tar.gz&ref=0.1.7",
4545
} {
4646
assert.Contains(t, testcli.Stdout(), output)
4747
}

pkg/xterrafile/registry.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func IsRegistrySourceAddr(addr string) bool {
4141
}
4242

4343
// GetRegistrySource retrieves a modules download source from a Terraform registry
44-
func GetRegistrySource(name string, source string, version string, services *disco.Disco) string {
44+
func GetRegistrySource(name string, source string, version string, services *disco.Disco) (string, string) {
4545

4646
modSrc, err := getModSrc(source)
4747
CheckIfError(name, err)
@@ -56,7 +56,7 @@ func GetRegistrySource(name string, source string, version string, services *dis
5656
CheckIfError(name, err)
5757
jww.INFO.Printf("[%s] Downloading from source URL %s", name, regSrc)
5858

59-
return regSrc
59+
return regSrc, version
6060
}
6161

6262
// Helper function to return a valid version

pkg/xterrafile/registry_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ func TestGetRegistrySource(t *testing.T) {
2626
server := test.Registry()
2727
defer server.Close()
2828

29-
module1Src := GetRegistrySource("droplet", "example.com/test-versions/name/provider", "2.1.x", test.Disco(server))
29+
module1Src, module1Version := GetRegistrySource("droplet", "example.com/test-versions/name/provider", "2.1.x", test.Disco(server))
3030
assert.IsType(t, "string", module1Src, "download URL should be a string")
31+
assert.IsType(t, "string", module1Version, "download version should be a string")
32+
3133
}
3234

3335
func TestGetModSrc(t *testing.T) {

0 commit comments

Comments
 (0)