// AvailableVersions retrieves the available versions for the given provider
// from the object's underlying HTTP mirror service.
AvailableVersions retrieves the available versions for the given provider
from the object's underlying HTTP mirror service.
// AvailableVersions retrieves the available versions for the given provider // from the object ' s underlying HTTP mirror service .
func (s *HTTPMirrorSource) AvailableVersions(ctx context.Context, provider addrs.Provider) (VersionList, Warnings, error) {
	log.Printf("[DEBUG] Querying available versions of provider %s at network mirror %s", provider.String(), s.baseURL.String())
	endpointPath := path.Join(
		provider.Hostname.String(),
		provider.Namespace,
		provider.Type,
		"index.json",
	)
	statusCode, body, finalURL, err := s.get(ctx, endpointPath)
	defer func() {
		if body != nil {
			body.Close()
		}
	}()
	if err != nil {
		return nil, nil, s.errQueryFailed(provider, err)
	}
	switch statusCode {
	case http.StatusOK:
	case http.StatusNotFound:
		return nil, nil, ErrProviderNotFound{
			Provider: provider,
		}
	case http.StatusUnauthorized, http.StatusForbidden:
		return nil, nil, s.errUnauthorized(finalURL)
	default:
		return nil, nil, s.errQueryFailed(provider, fmt.Errorf("server returned unsuccessful status %d", statusCode))
	}
	type ResponseBody struct {
		Versions map[string]struct{} `json:"versions"`
	}
	var bodyContent ResponseBody
	dec := json.NewDecoder(body)
	if err := dec.Decode(&bodyContent); err != nil {
		return nil, nil, s.errQueryFailed(provider, fmt.Errorf("invalid response content from mirror server: %s", err))
	}
	if len(bodyContent.Versions) == 0 {
		return nil, nil, nil
	}
	ret := make(VersionList, 0, len(bodyContent.Versions))
	for versionStr := range bodyContent.Versions {
		version, err := ParseVersion(versionStr)
		if err != nil {
			log.Printf("[WARN] Ignoring invalid %s version string %q in provider mirror response", provider, versionStr)
			continue
		}
		ret = append(ret, version)
	}
	ret.Sort()
	return ret, nil, nil
}
func ( s * HTTPMirrorSource ) AvailableVersions ( ctx context . Context , provider addrs . Provider ) ( VersionList , Warnings , error ) { log . Printf ( " " , provider . String ( ) , s . baseURL . String ( ) ) endpointPath := path . Join ( provider . Hostname . String ( ) , provider . Namespace , provider . Type , " " , ) statusCode , body , finalURL , err := s . get ( ctx , endpointPath ) defer func ( ) { if body != nil { body . Close ( ) } } ( ) if err != nil { return nil , nil , s . errQueryFailed ( provider , err ) } switch statusCode { case http . StatusOK : case http . StatusNotFound : return nil , nil , ErrProviderNotFound { Provider : provider , } case http . StatusUnauthorized , http . StatusForbidden : return nil , nil , s . errUnauthorized ( finalURL ) default : return nil , nil , s . errQueryFailed ( provider , fmt . Errorf ( " " , statusCode ) ) } type ResponseBody struct { Versions map [ string ] struct { } `json:"versions"` } var bodyContent ResponseBody dec := json . NewDecoder ( body ) if err := dec . Decode ( & bodyContent ) ; err != nil { return nil , nil , s . errQueryFailed ( provider , fmt . Errorf ( " " , err ) ) } if len ( bodyContent . Versions ) == 0 { return nil , nil , nil } ret := make ( VersionList , 0 , len ( bodyContent . Versions ) ) for versionStr := range bodyContent . Versions { version , err := ParseVersion ( versionStr ) if err != nil { log . Printf ( " " , provider , versionStr ) continue } ret = append ( ret , version ) } ret . Sort ( ) return ret , nil , nil }
AvailableVersions retrieves the available versions for the given provider
from the object's underlying HTTP mirror service.
AvailableVersions retrieves the available versions for the given provider from the object ' s underlying HTTP mirror service .
// Great! // If we got here then the response had status OK and so our body // will be non-nil and should contain some JSON for us to parse.