File: versions.go

package info (click to toggle)
golang-github-hashicorp-go-checkpoint 0.5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 176 kB
  • sloc: makefile: 3
file content (90 lines) | stat: -rw-r--r-- 2,353 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package checkpoint

import (
	"encoding/json"
	"fmt"
	"net/http"
	"net/url"
	"os"
	"strconv"
	"time"

	"github.com/hashicorp/go-cleanhttp"
)

// VersionsParams are the parameters for a versions request.
type VersionsParams struct {
	// Service is used to lookup the correct service.
	Service string

	// Product is used to filter the version contraints.
	Product string

	// Force, if true, will force the check even if CHECKPOINT_DISABLE
	// is set. Within HashiCorp products, this is ONLY USED when the user
	// specifically requests it. This is never automatically done without
	// the user's consent.
	Force bool
}

// VersionsResponse is the response for a versions request.
type VersionsResponse struct {
	Service   string   `json:"service"`
	Product   string   `json:"product"`
	Minimum   string   `json:"minimum"`
	Maximum   string   `json:"maximum"`
	Excluding []string `json:"excluding"`
}

// Versions returns the version constrains for a given service and product.
func Versions(p *VersionsParams) (*VersionsResponse, error) {
	if enabled := os.Getenv("CHECKPOINT_ENABLE"); enabled == "" && !p.Force {
		return &VersionsResponse{}, nil
	}

	// Set a default timeout of 1 sec for the versions request (in milliseconds)
	timeout := 1000
	if _, err := strconv.Atoi(os.Getenv("CHECKPOINT_TIMEOUT")); err == nil {
		timeout, _ = strconv.Atoi(os.Getenv("CHECKPOINT_TIMEOUT"))
	}

	v := url.Values{}
	v.Set("product", p.Product)

	u := &url.URL{
		Scheme:   "https",
		Host:     "checkpoint-api.hashicorp.com",
		Path:     fmt.Sprintf("/v1/versions/%s", p.Service),
		RawQuery: v.Encode(),
	}

	req, err := http.NewRequest("GET", u.String(), nil)
	if err != nil {
		return nil, err
	}
	req.Header.Set("Accept", "application/json")
	req.Header.Set("User-Agent", "HashiCorp/go-checkpoint")

	client := cleanhttp.DefaultClient()

	// We use a short timeout since checking for new versions is not critical
	// enough to block on if checkpoint is broken/slow.
	client.Timeout = time.Duration(timeout) * time.Millisecond

	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		return nil, fmt.Errorf("Unknown status: %d", resp.StatusCode)
	}

	result := &VersionsResponse{}
	if err := json.NewDecoder(resp.Body).Decode(result); err != nil {
		return nil, err
	}

	return result, nil
}