-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide completion, hover and docs links for uninitialized Registry m…
…odules (#924) * Recognize inputs and outputs of uninitialized remote modules * d * progress * add failing tests * progress * make it work * fix test * formatting * fix tests betterer * cleanup and timeout * remove terraform-schema pin * cleanup * make input & output descriptions markup-type-aware * fix typo * bump tfschema to latest & go mod tidy Co-authored-by: Radek Simko <radek.simko@gmail.com>
- Loading branch information
1 parent
be56006
commit 1529e2d
Showing
23 changed files
with
1,617 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package registry | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"sort" | ||
"time" | ||
|
||
"github.com/hashicorp/go-cleanhttp" | ||
"github.com/hashicorp/go-version" | ||
tfaddr "github.com/hashicorp/terraform-registry-address" | ||
) | ||
|
||
const ( | ||
defaultBaseURL = "https://registry.terraform.io" | ||
defaultTimeout = 5 * time.Second | ||
) | ||
|
||
type Client struct { | ||
BaseURL string | ||
Timeout time.Duration | ||
} | ||
|
||
func NewClient() Client { | ||
return Client{ | ||
BaseURL: defaultBaseURL, | ||
Timeout: defaultTimeout, | ||
} | ||
} | ||
|
||
func (c Client) GetModuleData(addr tfaddr.ModuleSourceRegistry, cons version.Constraints) (*ModuleResponse, error) { | ||
var response ModuleResponse | ||
|
||
v, err := c.GetMatchingModuleVersion(addr, cons) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
client := cleanhttp.DefaultClient() | ||
client.Timeout = defaultTimeout | ||
|
||
url := fmt.Sprintf("%s/v1/modules/%s/%s/%s/%s", c.BaseURL, | ||
addr.PackageAddr.Namespace, | ||
addr.PackageAddr.Name, | ||
addr.PackageAddr.TargetSystem, | ||
v.String()) | ||
resp, err := client.Get(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if resp.StatusCode != 200 { | ||
bodyBytes, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
return nil, fmt.Errorf("unexpected response %s: %s", resp.Status, string(bodyBytes)) | ||
} | ||
|
||
err = json.NewDecoder(resp.Body).Decode(&response) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &response, nil | ||
} | ||
|
||
func (c Client) GetMatchingModuleVersion(addr tfaddr.ModuleSourceRegistry, con version.Constraints) (*version.Version, error) { | ||
url := fmt.Sprintf("%s/v1/modules/%s/%s/%s/versions", c.BaseURL, | ||
addr.PackageAddr.Namespace, | ||
addr.PackageAddr.Name, | ||
addr.PackageAddr.TargetSystem) | ||
|
||
client := cleanhttp.DefaultClient() | ||
client.Timeout = defaultTimeout | ||
|
||
resp, err := client.Get(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if resp.StatusCode != 200 { | ||
bodyBytes, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
return nil, fmt.Errorf("unexpected response %s: %s", resp.Status, string(bodyBytes)) | ||
} | ||
|
||
var response ModuleVersionsResponse | ||
err = json.NewDecoder(resp.Body).Decode(&response) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var foundVersions version.Collection | ||
for _, module := range response.Modules { | ||
for _, entry := range module.Versions { | ||
ver, err := version.NewVersion(entry.Version) | ||
if err == nil { | ||
foundVersions = append(foundVersions, ver) | ||
} | ||
} | ||
} | ||
|
||
sort.Sort(foundVersions) | ||
|
||
for _, fv := range foundVersions { | ||
if con.Check(fv) { | ||
return fv, nil | ||
} | ||
} | ||
|
||
return nil, fmt.Errorf("no suitable version found for %q %q", addr, con) | ||
} |
Oops, something went wrong.