-
-
Notifications
You must be signed in to change notification settings - Fork 986
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding registry url discovery for Terragrunt Provider Cache (#3299)
* chore: registry discovery url * chore: default urls for registry without well-known endpoint * chore: building url if registry download url is a filename * Update cli/provider_cache.go Co-authored-by: Yousif Akbar <[email protected]> --------- Co-authored-by: Yousif Akbar <[email protected]>
- Loading branch information
1 parent
d7423b8
commit e951157
Showing
8 changed files
with
171 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/gruntwork-io/go-commons/errors" | ||
) | ||
|
||
const ( | ||
// well-known address for discovery URLs | ||
wellKnownURL = ".well-known/terraform.json" | ||
) | ||
|
||
var ( | ||
DefaultRegistryURLs = &RegistryURLs{ | ||
ModulesV1: "/v1/modules", | ||
ProvidersV1: "/v1/providers", | ||
} | ||
) | ||
|
||
type RegistryURLs struct { | ||
ModulesV1 string `json:"modules.v1"` | ||
ProvidersV1 string `json:"providers.v1"` | ||
} | ||
|
||
func (urls *RegistryURLs) String() string { | ||
b, _ := json.Marshal(urls) //nolint:errcheck | ||
return string(b) | ||
} | ||
|
||
func DiscoveryURL(ctx context.Context, registryName string) (*RegistryURLs, error) { | ||
url := fmt.Sprintf("https://%s/%s", registryName, wellKnownURL) | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) | ||
if err != nil { | ||
return nil, errors.WithStackTrace(err) | ||
} | ||
|
||
resp, err := (&http.Client{}).Do(req) | ||
if err != nil { | ||
return nil, errors.WithStackTrace(err) | ||
} | ||
defer resp.Body.Close() //nolint:errcheck | ||
|
||
switch resp.StatusCode { | ||
case http.StatusNotFound: | ||
return nil, errors.WithStackTrace(NotFoundWellKnownURL{wellKnownURL}) | ||
case http.StatusOK: | ||
default: | ||
return nil, fmt.Errorf("%s returned %s", url, resp.Status) | ||
} | ||
|
||
content, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, errors.WithStackTrace(err) | ||
} | ||
|
||
urls := new(RegistryURLs) | ||
if err := json.Unmarshal(content, urls); err != nil { | ||
return nil, errors.WithStackTrace(err) | ||
} | ||
return urls, nil | ||
} | ||
|
||
type NotFoundWellKnownURL struct { | ||
url string | ||
} | ||
|
||
func (err NotFoundWellKnownURL) Error() string { | ||
return fmt.Sprintf("%s not found", err.url) | ||
} |
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
Oops, something went wrong.