-
Notifications
You must be signed in to change notification settings - Fork 88
/
upstream.go
79 lines (64 loc) · 2.04 KB
/
upstream.go
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
package replicatedapp
import (
"encoding/base64"
"fmt"
"net/http"
"net/url"
"path"
"strings"
"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
"github.com/replicatedhq/kots/pkg/util"
)
type ReplicatedUpstream struct {
Channel *string
AppSlug string
VersionLabel *string
Sequence *int
}
func ParseReplicatedURL(u *url.URL) (*ReplicatedUpstream, error) {
replicatedUpstream := ReplicatedUpstream{}
if u.User != nil {
if u.User.Username() != "" {
replicatedUpstream.AppSlug = u.User.Username()
versionLabel := u.Hostname()
replicatedUpstream.VersionLabel = &versionLabel
}
}
if replicatedUpstream.AppSlug == "" {
replicatedUpstream.AppSlug = u.Hostname()
if u.Path != "" {
channel := strings.TrimPrefix(u.Path, "/")
replicatedUpstream.Channel = &channel
}
}
return &replicatedUpstream, nil
}
func (r *ReplicatedUpstream) GetRequest(method string, license *kotsv1beta1.License, cursor ReplicatedCursor) (*http.Request, error) {
u, err := url.Parse(license.Spec.Endpoint)
if err != nil {
return nil, errors.Wrap(err, "failed to parse endpoint from license")
}
hostname := u.Hostname()
if u.Port() != "" {
hostname = fmt.Sprintf("%s:%s", u.Hostname(), u.Port())
}
urlPath := path.Join(hostname, "release", license.Spec.AppSlug)
if r.Channel != nil {
urlPath = path.Join(urlPath, *r.Channel)
}
urlValues := url.Values{}
urlValues.Set("channelSequence", cursor.Cursor)
if r.VersionLabel != nil {
urlValues.Set("versionLabel", *r.VersionLabel)
}
urlValues.Add("licenseSequence", fmt.Sprintf("%d", license.Spec.LicenseSequence))
urlValues.Add("isSemverSupported", "true")
url := fmt.Sprintf("%s://%s?%s", u.Scheme, urlPath, urlValues.Encode())
req, err := util.NewRequest(method, url, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to call newrequest")
}
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", license.Spec.LicenseID, license.Spec.LicenseID)))))
return req, nil
}