/
clientUtils.go
152 lines (130 loc) · 5.05 KB
/
clientUtils.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package utils
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
BackplaneApi "github.com/openshift/backplane-api/pkg/client"
logger "github.com/sirupsen/logrus"
"github.com/openshift/backplane-cli/pkg/cli/config"
"github.com/openshift/backplane-cli/pkg/info"
"github.com/openshift/backplane-cli/pkg/ocm"
)
type ClientUtils interface {
MakeBackplaneAPIClient(base string) (BackplaneApi.ClientWithResponsesInterface, error)
MakeBackplaneAPIClientWithAccessToken(base, accessToken string) (BackplaneApi.ClientWithResponsesInterface, error)
MakeRawBackplaneAPIClientWithAccessToken(base, accessToken string) (BackplaneApi.ClientInterface, error)
MakeRawBackplaneAPIClient(base string) (BackplaneApi.ClientInterface, error)
GetBackplaneClient(backplaneURL string, ocmToken string, proxyURL *string) (client BackplaneApi.ClientInterface, err error)
SetClientProxyURL(proxyURL string) error
}
type DefaultClientUtilsImpl struct {
clientProxyURL string
}
var (
DefaultClientUtils ClientUtils = &DefaultClientUtilsImpl{}
)
func (s *DefaultClientUtilsImpl) MakeRawBackplaneAPIClientWithAccessToken(base, accessToken string) (BackplaneApi.ClientInterface, error) {
co := func(client *BackplaneApi.Client) error {
client.RequestEditors = append(client.RequestEditors, func(ctx context.Context, req *http.Request) error {
req.Header.Add("Authorization", "Bearer "+accessToken)
req.Header.Set("User-Agent", "backplane-cli"+info.Version)
return nil
})
return nil
}
// Inject client Proxy Url from config
if s.clientProxyURL == "" {
bpConfig, err := config.GetBackplaneConfiguration()
if err != nil {
return nil, err
}
if bpConfig.ProxyURL != nil {
s.clientProxyURL = *bpConfig.ProxyURL
}
}
// Update http proxy transport
if s.clientProxyURL != "" {
proxyURL, err := url.Parse(s.clientProxyURL)
if err != nil {
return nil, err
}
http.DefaultTransport = &http.Transport{Proxy: http.ProxyURL(proxyURL)}
logger.Debugf("Using backplane Proxy URL: %s\n", s.clientProxyURL)
}
return BackplaneApi.NewClient(base, co)
}
// makeRawBackplaneAPIClientWithAccessTokenCustomProxy creates a BackplaneApi.ClientInterface with a custom proxy url
// proxyURL is optional, the default behavior is no proxy.
func makeRawBackplaneAPIClientWithAccessTokenCustomProxy(server string, accessToken string, proxyURL *string) (BackplaneApi.ClientInterface, error) {
co := func(client *BackplaneApi.Client) error {
client.RequestEditors = append(client.RequestEditors, func(ctx context.Context, req *http.Request) error {
req.Header.Add("Authorization", "Bearer "+accessToken)
req.Header.Set("User-Agent", "backplane-cli"+info.Version)
return nil
})
return nil
}
if proxyURL != nil {
proxy, err := url.Parse(*proxyURL)
if err != nil {
return nil, err
}
http.DefaultTransport = &http.Transport{Proxy: http.ProxyURL(proxy)}
logger.Debugf("Using backplane Proxy URL: %s\n", *proxyURL)
}
return BackplaneApi.NewClient(server, co)
}
func (s *DefaultClientUtilsImpl) MakeRawBackplaneAPIClient(base string) (BackplaneApi.ClientInterface, error) {
token, err := ocm.DefaultOCMInterface.GetOCMAccessToken()
if err != nil {
return nil, err
}
return s.MakeRawBackplaneAPIClientWithAccessToken(base, *token)
}
func (*DefaultClientUtilsImpl) MakeBackplaneAPIClientWithAccessToken(base, accessToken string) (BackplaneApi.ClientWithResponsesInterface, error) {
co := func(client *BackplaneApi.Client) error {
client.RequestEditors = append(client.RequestEditors, func(ctx context.Context, req *http.Request) error {
req.Header.Add("Authorization", "Bearer "+accessToken)
req.Header.Set("User-Agent", "backplane-cli"+info.Version)
return nil
})
return nil
}
return BackplaneApi.NewClientWithResponses(base, co)
}
func (s *DefaultClientUtilsImpl) MakeBackplaneAPIClient(base string) (BackplaneApi.ClientWithResponsesInterface, error) {
token, err := ocm.DefaultOCMInterface.GetOCMAccessToken()
if err != nil {
return nil, err
}
return s.MakeBackplaneAPIClientWithAccessToken(base, *token)
}
// GetBackplaneClient returns authenticated Backplane API client
// Proxy is optional.
func (s *DefaultClientUtilsImpl) GetBackplaneClient(backplaneURL string, ocmToken string, proxyURL *string) (client BackplaneApi.ClientInterface, err error) {
if backplaneURL == "" {
bpConfig, err := config.GetBackplaneConfiguration()
backplaneURL = bpConfig.URL
if err != nil || backplaneURL == "" {
return client, fmt.Errorf("can't find backplane url: %w", err)
}
logger.Infof("Using backplane URL: %s\n", backplaneURL)
}
logger.Debugln("Getting client")
backplaneClient, err := makeRawBackplaneAPIClientWithAccessTokenCustomProxy(backplaneURL, ocmToken, proxyURL)
if err != nil {
return client, fmt.Errorf("unable to create backplane api client: %w", err)
}
logger.Debugln("Got Client")
return backplaneClient, nil
}
// SetClientProxyURL Set client proxy url for http transport
func (s *DefaultClientUtilsImpl) SetClientProxyURL(proxyURL string) error {
if proxyURL == "" {
return errors.New("proxy URL is empty")
}
s.clientProxyURL = proxyURL
return nil
}