Skip to content

Commit

Permalink
CLOUDP-82836: Parse service version (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmenezes committed Sep 2, 2021
1 parent 3cb50de commit 6c046e0
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/openlyinc/pointy v1.1.2
github.com/stretchr/testify v1.7.0
github.com/xdg-go/stringprep v1.0.2
go.mongodb.org/atlas v0.11.1-0.20210811161401-47f8a8c02572
go.mongodb.org/atlas v0.12.1-0.20210902121637-a40fdd6c5807
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/xdg-go/stringprep v1.0.2 h1:6iq84/ryjjeRmMJwxutI51F2GIPlP5BfTvXHeYjyhBc=
github.com/xdg-go/stringprep v1.0.2/go.mod h1:8F9zXuvzgwmyT5DUm4GUfZGDdT3W+LCvS6+da4O5kxM=
go.mongodb.org/atlas v0.11.1-0.20210811161401-47f8a8c02572 h1:V/9fqSuNtNOecclmodf3TSbAfYFSS4MOJOA8pO7iDP8=
go.mongodb.org/atlas v0.11.1-0.20210811161401-47f8a8c02572/go.mod h1:MMWDsc2akjTDSG4tVQrxv/82p3QbBnqeELbtTl45sbg=
go.mongodb.org/atlas v0.12.1-0.20210902121637-a40fdd6c5807 h1:a3UaBaiyEFIKOV23O3eAFzfmnZT2ClLJAELLuyOcNLE=
go.mongodb.org/atlas v0.12.1-0.20210902121637-a40fdd6c5807/go.mod h1:wVCnHcm/7/IfTjEB6K8K35PLG70yGz8BdkRwX0oK9/M=
golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
30 changes: 30 additions & 0 deletions opsmngr/opsmngr.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ const (
userAgent = "go-ops-manager"
jsonMediaType = "application/json"
gzipMediaType = "application/gzip"
plainMediaType = "text/plain"
)

type (
Response = atlas.Response
RequestCompletionCallback = atlas.RequestCompletionCallback
ServiceVersion = atlas.ServiceVersion
)

// Client manages communication with Ops Manager API.
Expand Down Expand Up @@ -94,6 +96,7 @@ type Client struct {
ServerUsage ServerUsageService
ServerUsageReport ServerUsageReportService
LiveMigration LiveDataMigrationService
ServiceVersion atlas.ServiceVersionService

onRequestCompleted RequestCompletionCallback
}
Expand Down Expand Up @@ -161,6 +164,7 @@ func NewClient(httpClient *http.Client) *Client {
c.ServerUsage = &ServerUsageServiceOp{Client: c}
c.ServerUsageReport = &ServerUsageReportServiceOp{Client: c}
c.LiveMigration = &LiveDataMigrationServiceOp{Client: c}
c.ServiceVersion = &atlas.ServiceVersionServiceOp{Client: c}

return c
}
Expand Down Expand Up @@ -289,6 +293,32 @@ func (c *Client) NewGZipRequest(ctx context.Context, method, urlStr string) (*ht
return req, nil
}

// NewPlainRequest creates an API request that accepts plain text.
// A relative URL can be provided in urlStr, which will be resolved to the
// BaseURL of the Client. Relative URLS should always be specified without a preceding slash.
func (c *Client) NewPlainRequest(ctx context.Context, method, urlStr string) (*http.Request, error) {
if !strings.HasSuffix(c.BaseURL.Path, "/") {
return nil, fmt.Errorf("base URL must have a trailing slash, but %q does not", c.BaseURL)
}
rel, err := url.Parse(urlStr)
if err != nil {
return nil, err
}

u := c.BaseURL.ResolveReference(rel)

req, err := http.NewRequestWithContext(ctx, method, u.String(), nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", plainMediaType)
if c.UserAgent != "" {
req.Header.Set("User-Agent", c.UserAgent)
}

return req, nil
}

// OnRequestCompleted sets the DO API request completion callback.
func (c *Client) OnRequestCompleted(rc atlas.RequestCompletionCallback) {
c.onRequestCompleted = rc
Expand Down
59 changes: 57 additions & 2 deletions opsmngr/opsmngr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const (
// baseURLPath is a non-empty Client.BaseURL path to use during tests,
// to ensure relative URLs are used for all endpoints.
baseURLPath = "/api-v1"
requestPath = "foo"
)

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

requestPath := "foo"

inURL, outURL := requestPath, defaultBaseURL+requestPath
inBody, outBody := &testRequestBody{TestName: "l", TestUserData: "u"},
`{"testName":"l","testCounter":0,`+
Expand Down Expand Up @@ -233,6 +232,62 @@ func TestNewRequest_errorForNoTrailingSlash(t *testing.T) {
}
}

func TestNewPlainRequest_emptyBody(t *testing.T) {
c := NewClient(nil)
req, err := c.NewPlainRequest(ctx, http.MethodGet, ".")
if err != nil {
t.Fatalf("NewPlainRequest returned unexpected error: %v", err)
}
if req.Body != nil {
t.Fatalf("constructed request contains a non-nil Body")
}
}

func TestNewPlainRequest_withCustomUserAgent(t *testing.T) {
c, err := New(nil, SetUserAgent(ua))

if err != nil {
t.Fatalf("New() unexpected error: %v", err)
}

req, _ := c.NewPlainRequest(ctx, http.MethodGet, "/foo")

expected := fmt.Sprintf("%s %s", ua, userAgent)
if got := req.Header.Get("User-Agent"); got != expected {
t.Errorf("NewPlainRequest() UserAgent = %s; expected %s", got, expected)
}
}

func TestNewPlainRequest_badURL(t *testing.T) {
c := NewClient(nil)
_, err := c.NewPlainRequest(ctx, http.MethodGet, ":")
testURLParseError(t, err)
}

func TestNewPlainRequest(t *testing.T) {
c := NewClient(nil)

inURL, outURL := requestPath, defaultBaseURL+requestPath
req, _ := c.NewPlainRequest(ctx, http.MethodGet, inURL)

// test relative URL was expanded
if req.URL.String() != outURL {
t.Errorf("NewPlainRequest(%v) URL = %v, expected %v", inURL, req.URL, outURL)
}

// test accept content type is correct
accept := req.Header.Get("Accept")
if plainMediaType != accept {
t.Errorf("NewPlainRequest() Accept = %v, expected %v", accept, gzipMediaType)
}

// test default user-agent is attached to the request
uA := req.Header.Get("User-Agent")
if c.UserAgent != uA {
t.Errorf("NewPlainRequest() User-Agent = %v, expected %v", uA, c.UserAgent)
}
}

const testResponse = `{"A":"a"}`

func TestClient_Do(t *testing.T) {
Expand Down

0 comments on commit 6c046e0

Please sign in to comment.