Skip to content

Commit 6c046e0

Browse files
authored
CLOUDP-82836: Parse service version (#122)
1 parent 3cb50de commit 6c046e0

File tree

4 files changed

+90
-5
lines changed

4 files changed

+90
-5
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/openlyinc/pointy v1.1.2
1010
github.com/stretchr/testify v1.7.0
1111
github.com/xdg-go/stringprep v1.0.2
12-
go.mongodb.org/atlas v0.11.1-0.20210811161401-47f8a8c02572
12+
go.mongodb.org/atlas v0.12.1-0.20210902121637-a40fdd6c5807
1313
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
1414
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
1515
)

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
2222
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
2323
github.com/xdg-go/stringprep v1.0.2 h1:6iq84/ryjjeRmMJwxutI51F2GIPlP5BfTvXHeYjyhBc=
2424
github.com/xdg-go/stringprep v1.0.2/go.mod h1:8F9zXuvzgwmyT5DUm4GUfZGDdT3W+LCvS6+da4O5kxM=
25-
go.mongodb.org/atlas v0.11.1-0.20210811161401-47f8a8c02572 h1:V/9fqSuNtNOecclmodf3TSbAfYFSS4MOJOA8pO7iDP8=
26-
go.mongodb.org/atlas v0.11.1-0.20210811161401-47f8a8c02572/go.mod h1:MMWDsc2akjTDSG4tVQrxv/82p3QbBnqeELbtTl45sbg=
25+
go.mongodb.org/atlas v0.12.1-0.20210902121637-a40fdd6c5807 h1:a3UaBaiyEFIKOV23O3eAFzfmnZT2ClLJAELLuyOcNLE=
26+
go.mongodb.org/atlas v0.12.1-0.20210902121637-a40fdd6c5807/go.mod h1:wVCnHcm/7/IfTjEB6K8K35PLG70yGz8BdkRwX0oK9/M=
2727
golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
2828
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
2929
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

opsmngr/opsmngr.go

+30
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ const (
3636
userAgent = "go-ops-manager"
3737
jsonMediaType = "application/json"
3838
gzipMediaType = "application/gzip"
39+
plainMediaType = "text/plain"
3940
)
4041

4142
type (
4243
Response = atlas.Response
4344
RequestCompletionCallback = atlas.RequestCompletionCallback
45+
ServiceVersion = atlas.ServiceVersion
4446
)
4547

4648
// Client manages communication with Ops Manager API.
@@ -94,6 +96,7 @@ type Client struct {
9496
ServerUsage ServerUsageService
9597
ServerUsageReport ServerUsageReportService
9698
LiveMigration LiveDataMigrationService
99+
ServiceVersion atlas.ServiceVersionService
97100

98101
onRequestCompleted RequestCompletionCallback
99102
}
@@ -161,6 +164,7 @@ func NewClient(httpClient *http.Client) *Client {
161164
c.ServerUsage = &ServerUsageServiceOp{Client: c}
162165
c.ServerUsageReport = &ServerUsageReportServiceOp{Client: c}
163166
c.LiveMigration = &LiveDataMigrationServiceOp{Client: c}
167+
c.ServiceVersion = &atlas.ServiceVersionServiceOp{Client: c}
164168

165169
return c
166170
}
@@ -289,6 +293,32 @@ func (c *Client) NewGZipRequest(ctx context.Context, method, urlStr string) (*ht
289293
return req, nil
290294
}
291295

296+
// NewPlainRequest creates an API request that accepts plain text.
297+
// A relative URL can be provided in urlStr, which will be resolved to the
298+
// BaseURL of the Client. Relative URLS should always be specified without a preceding slash.
299+
func (c *Client) NewPlainRequest(ctx context.Context, method, urlStr string) (*http.Request, error) {
300+
if !strings.HasSuffix(c.BaseURL.Path, "/") {
301+
return nil, fmt.Errorf("base URL must have a trailing slash, but %q does not", c.BaseURL)
302+
}
303+
rel, err := url.Parse(urlStr)
304+
if err != nil {
305+
return nil, err
306+
}
307+
308+
u := c.BaseURL.ResolveReference(rel)
309+
310+
req, err := http.NewRequestWithContext(ctx, method, u.String(), nil)
311+
if err != nil {
312+
return nil, err
313+
}
314+
req.Header.Add("Accept", plainMediaType)
315+
if c.UserAgent != "" {
316+
req.Header.Set("User-Agent", c.UserAgent)
317+
}
318+
319+
return req, nil
320+
}
321+
292322
// OnRequestCompleted sets the DO API request completion callback.
293323
func (c *Client) OnRequestCompleted(rc atlas.RequestCompletionCallback) {
294324
c.onRequestCompleted = rc

opsmngr/opsmngr_test.go

+57-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const (
4040
// baseURLPath is a non-empty Client.BaseURL path to use during tests,
4141
// to ensure relative URLs are used for all endpoints.
4242
baseURLPath = "/api-v1"
43+
requestPath = "foo"
4344
)
4445

4546
// setup sets up a test HTTP server along with a opsmngr.Client that is
@@ -138,8 +139,6 @@ type testRequestBody struct {
138139
func TestNewRequest_withUserData(t *testing.T) {
139140
c := NewClient(nil)
140141

141-
requestPath := "foo"
142-
143142
inURL, outURL := requestPath, defaultBaseURL+requestPath
144143
inBody, outBody := &testRequestBody{TestName: "l", TestUserData: "u"},
145144
`{"testName":"l","testCounter":0,`+
@@ -233,6 +232,62 @@ func TestNewRequest_errorForNoTrailingSlash(t *testing.T) {
233232
}
234233
}
235234

235+
func TestNewPlainRequest_emptyBody(t *testing.T) {
236+
c := NewClient(nil)
237+
req, err := c.NewPlainRequest(ctx, http.MethodGet, ".")
238+
if err != nil {
239+
t.Fatalf("NewPlainRequest returned unexpected error: %v", err)
240+
}
241+
if req.Body != nil {
242+
t.Fatalf("constructed request contains a non-nil Body")
243+
}
244+
}
245+
246+
func TestNewPlainRequest_withCustomUserAgent(t *testing.T) {
247+
c, err := New(nil, SetUserAgent(ua))
248+
249+
if err != nil {
250+
t.Fatalf("New() unexpected error: %v", err)
251+
}
252+
253+
req, _ := c.NewPlainRequest(ctx, http.MethodGet, "/foo")
254+
255+
expected := fmt.Sprintf("%s %s", ua, userAgent)
256+
if got := req.Header.Get("User-Agent"); got != expected {
257+
t.Errorf("NewPlainRequest() UserAgent = %s; expected %s", got, expected)
258+
}
259+
}
260+
261+
func TestNewPlainRequest_badURL(t *testing.T) {
262+
c := NewClient(nil)
263+
_, err := c.NewPlainRequest(ctx, http.MethodGet, ":")
264+
testURLParseError(t, err)
265+
}
266+
267+
func TestNewPlainRequest(t *testing.T) {
268+
c := NewClient(nil)
269+
270+
inURL, outURL := requestPath, defaultBaseURL+requestPath
271+
req, _ := c.NewPlainRequest(ctx, http.MethodGet, inURL)
272+
273+
// test relative URL was expanded
274+
if req.URL.String() != outURL {
275+
t.Errorf("NewPlainRequest(%v) URL = %v, expected %v", inURL, req.URL, outURL)
276+
}
277+
278+
// test accept content type is correct
279+
accept := req.Header.Get("Accept")
280+
if plainMediaType != accept {
281+
t.Errorf("NewPlainRequest() Accept = %v, expected %v", accept, gzipMediaType)
282+
}
283+
284+
// test default user-agent is attached to the request
285+
uA := req.Header.Get("User-Agent")
286+
if c.UserAgent != uA {
287+
t.Errorf("NewPlainRequest() User-Agent = %v, expected %v", uA, c.UserAgent)
288+
}
289+
}
290+
236291
const testResponse = `{"A":"a"}`
237292

238293
func TestClient_Do(t *testing.T) {

0 commit comments

Comments
 (0)