Skip to content

Commit 5574d7c

Browse files
authored
Add database measurements Endpoint (#80)
1 parent 1fbdd98 commit 5574d7c

File tree

5 files changed

+180
-8
lines changed

5 files changed

+180
-8
lines changed

mongodbatlas/mongodbatlas.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const (
2121
libraryVersion = "0.1"
2222
defaultBaseURL = "https://cloud.mongodb.com/api/atlas/v1.0/"
2323
userAgent = "go-mongodbatlas" + libraryVersion
24-
mediaType = "application/json"
24+
jsonMediaType = "application/json"
2525
gzipMediaType = "application/gzip"
2626
)
2727

@@ -85,6 +85,7 @@ type Client struct {
8585
ProcessDisks ProcessDisksService
8686
ProcessDiskMeasurements ProcessDiskMeasurementsService
8787
ProcessDatabases ProcessDatabasesService
88+
ProcessDatabaseMeasurements ProcessDatabaseMeasurementsService
8889
Indexes IndexesService
8990
Logs LogsService
9091

@@ -210,6 +211,7 @@ func NewClient(httpClient *http.Client) *Client {
210211
c.ProcessDisks = &ProcessDisksServiceOp{Client: c}
211212
c.ProcessDiskMeasurements = &ProcessDiskMeasurementsServiceOp{Client: c}
212213
c.ProcessDatabases = &ProcessDatabasesServiceOp{Client: c}
214+
c.ProcessDatabaseMeasurements = &ProcessDatabaseMeasurementsServiceOp{Client: c}
213215
c.Indexes = &IndexesServiceOp{Client: c}
214216
c.Logs = &LogsServiceOp{Client: c}
215217

@@ -273,9 +275,9 @@ func (c *Client) NewRequest(ctx context.Context, method, urlStr string, body int
273275
}
274276

275277
if body != nil {
276-
req.Header.Set("Content-Type", mediaType)
278+
req.Header.Set("Content-Type", jsonMediaType)
277279
}
278-
req.Header.Add("Accept", mediaType)
280+
req.Header.Add("Accept", jsonMediaType)
279281
if c.UserAgent != "" {
280282
req.Header.Set("User-Agent", c.UserAgent)
281283
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package mongodbatlas
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
const processDatabaseMeasurementsPath = processesDatabasesPath + "/%s/measurements"
10+
11+
// ProcessDatabaseMeasurementsService is an interface for interfacing with the process database measurements
12+
// endpoints of the MongoDB Atlas API.
13+
// See more: https://docs.atlas.mongodb.com/reference/api/process-databases-measurements/
14+
type ProcessDatabaseMeasurementsService interface {
15+
List(context.Context, string, string, int, string, *ProcessMeasurementListOptions) (*ProcessDatabaseMeasurements, *Response, error)
16+
}
17+
18+
// ProcessDatabaseMeasurementsServiceOp handles communication with the process database measurements related methods of the
19+
// MongoDB Atlas API
20+
type ProcessDatabaseMeasurementsServiceOp struct {
21+
Client RequestDoer
22+
}
23+
24+
// ProcessDatabaseMeasurements represents a MongoDB process database measurements.
25+
type ProcessDatabaseMeasurements struct {
26+
*ProcessMeasurements
27+
DatabaseName string `json:"databaseName"`
28+
}
29+
30+
var _ ProcessDatabaseMeasurementsService = &ProcessDatabaseMeasurementsServiceOp{}
31+
32+
// List list measurements for a specific Atlas MongoDB database.
33+
// See more: https://docs.atlas.mongodb.com/reference/api/process-databases-measurements/
34+
func (s *ProcessDatabaseMeasurementsServiceOp) List(ctx context.Context, groupID, hostName string, port int, databaseName string, opts *ProcessMeasurementListOptions) (*ProcessDatabaseMeasurements, *Response, error) {
35+
36+
if groupID == "" {
37+
return nil, nil, NewArgError("groupID", "must be set")
38+
}
39+
40+
if hostName == "" {
41+
return nil, nil, NewArgError("hostName", "must be set")
42+
}
43+
44+
if databaseName == "" {
45+
return nil, nil, NewArgError("databaseName", "must be set")
46+
}
47+
48+
basePath := fmt.Sprintf(processDatabaseMeasurementsPath, groupID, hostName, port, databaseName)
49+
50+
//Add query params from listOptions
51+
path, err := setListOptions(basePath, opts)
52+
if err != nil {
53+
return nil, nil, err
54+
}
55+
56+
req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
57+
if err != nil {
58+
return nil, nil, err
59+
}
60+
61+
root := new(ProcessDatabaseMeasurements)
62+
resp, err := s.Client.Do(ctx, req, root)
63+
if err != nil {
64+
return nil, resp, err
65+
}
66+
67+
return root, resp, err
68+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package mongodbatlas
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"testing"
7+
8+
"github.com/go-test/deep"
9+
)
10+
11+
func TestProcessDatabaseMeasurements_List(t *testing.T) {
12+
setup()
13+
defer teardown()
14+
15+
groups := "12345678"
16+
host := "shard-00-00.mongodb.net"
17+
port := 27017
18+
database := "database"
19+
20+
mux.HandleFunc(fmt.Sprintf("/groups/%s/processes/%s:%d/databases/%s/measurements", groups, host, port, database), func(w http.ResponseWriter, r *http.Request) {
21+
testMethod(t, r, http.MethodGet)
22+
fmt.Fprint(w, `{
23+
"end" : "2017-08-22T20:31:14Z",
24+
"granularity" : "PT1M",
25+
"groupId" : "12345678",
26+
"hostId" : "shard-00-00.mongodb.net:27017",
27+
"links" : [ {
28+
"href" : "https://cloud.mongodb.com/api/atlas/v1.0/groups/12345678/processes/shard-00-00.mongodb.net:27017/dataabses/database/measurements?granularity=PT1M&period=PT1M",
29+
"rel" : "self"
30+
}, {
31+
"href" : "https://cloud.mongodb.com/api/atlas/v1.0/groups/12345678/processes/shard-00-00.mongodb.net:27017",
32+
"rel" : "http://mms.mongodb.com/host"
33+
} ],
34+
"measurements" : [ {
35+
"dataPoints" : [ {
36+
"timestamp" : "2017-08-22T20:31:12Z",
37+
"value" : null
38+
}, {
39+
"timestamp" : "2017-08-22T20:31:14Z",
40+
"value" : null
41+
} ],
42+
"name" : "DISK_PARTITION_IOPS_READ",
43+
"units" : "SCALAR_PER_SECOND"
44+
}],
45+
"databaseName":"database",
46+
"processId" : "shard-00-00.mongodb.net:27017",
47+
"start" : "2017-08-22T20:30:45Z"
48+
}`)
49+
})
50+
51+
opts := &ProcessMeasurementListOptions{
52+
Granularity: "PT1M",
53+
Period: "PT1M",
54+
}
55+
56+
measurements, _, err := client.ProcessDatabaseMeasurements.List(ctx, groups, host, port, database, opts)
57+
if err != nil {
58+
t.Fatalf("ProcessDatabaseMeasurements.List returned error: %v", err)
59+
}
60+
61+
expected := &ProcessDatabaseMeasurements{
62+
ProcessMeasurements: &ProcessMeasurements{
63+
End: "2017-08-22T20:31:14Z",
64+
Granularity: "PT1M",
65+
GroupID: "12345678",
66+
HostID: "shard-00-00.mongodb.net:27017",
67+
Links: []*Link{
68+
{
69+
Rel: "self",
70+
Href: "https://cloud.mongodb.com/api/atlas/v1.0/groups/12345678/processes/shard-00-00.mongodb.net:27017/dataabses/database/measurements?granularity=PT1M&period=PT1M",
71+
},
72+
{
73+
Href: "https://cloud.mongodb.com/api/atlas/v1.0/groups/12345678/processes/shard-00-00.mongodb.net:27017",
74+
Rel: "http://mms.mongodb.com/host",
75+
},
76+
},
77+
Measurements: []*Measurements{
78+
{
79+
DataPoints: []*DataPoints{
80+
{
81+
Timestamp: "2017-08-22T20:31:12Z",
82+
Value: nil,
83+
},
84+
{
85+
Timestamp: "2017-08-22T20:31:14Z",
86+
Value: nil,
87+
},
88+
},
89+
Name: "DISK_PARTITION_IOPS_READ",
90+
Units: "SCALAR_PER_SECOND",
91+
},
92+
},
93+
ProcessID: "shard-00-00.mongodb.net:27017",
94+
Start: "2017-08-22T20:30:45Z",
95+
},
96+
DatabaseName: "database",
97+
}
98+
99+
if diff := deep.Equal(measurements, expected); diff != nil {
100+
t.Error(diff)
101+
}
102+
}

mongodbatlas/process_disk_measurements.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"net/http"
77
)
88

9-
const ProcessDiskMeasurementsPath = processesDisksPath + "/%s/measurements"
9+
const processDiskMeasurementsPath = processesDisksPath + "/%s/measurements"
1010

1111
// ProcessDiskMeasurementsService is an interface for interfacing with the Process Disk Measurements
1212
// endpoints of the MongoDB Atlas API.
@@ -29,7 +29,7 @@ type ProcessDiskMeasurements struct {
2929

3030
var _ ProcessDiskMeasurementsService = &ProcessDiskMeasurementsServiceOp{}
3131

32-
// Get gets measurements for a specific Atlas MongoDB disk.
32+
// List lists measurements for a specific Atlas MongoDB disk.
3333
// See more: https://docs.atlas.mongodb.com/reference/api/process-disks-measurements/#get-measurements-of-a-disk-for-a-mongodb-process
3434
func (s *ProcessDiskMeasurementsServiceOp) List(ctx context.Context, groupID, hostName string, port int, diskName string, opts *ProcessMeasurementListOptions) (*ProcessDiskMeasurements, *Response, error) {
3535

@@ -45,7 +45,7 @@ func (s *ProcessDiskMeasurementsServiceOp) List(ctx context.Context, groupID, ho
4545
return nil, nil, NewArgError("diskName", "must be set")
4646
}
4747

48-
basePath := fmt.Sprintf(ProcessDiskMeasurementsPath, groupID, hostName, port, diskName)
48+
basePath := fmt.Sprintf(processDiskMeasurementsPath, groupID, hostName, port, diskName)
4949

5050
//Add query params from listOptions
5151
path, err := setListOptions(basePath, opts)

mongodbatlas/process_disk_measurements_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/go-test/deep"
99
)
1010

11-
func TestDiskMeasurements_List(t *testing.T) {
11+
func TestProcessDiskMeasurements_List(t *testing.T) {
1212
setup()
1313
defer teardown()
1414

@@ -55,7 +55,7 @@ func TestDiskMeasurements_List(t *testing.T) {
5555

5656
measurements, _, err := client.ProcessDiskMeasurements.List(ctx, groups, host, port, disk, opts)
5757
if err != nil {
58-
t.Fatalf("Teams.Get returned error: %v", err)
58+
t.Fatalf("ProcessDiskMeasurements.List returned error: %v", err)
5959
}
6060

6161
expected := &ProcessDiskMeasurements{

0 commit comments

Comments
 (0)