diff --git a/mongodbatlas/mongodbatlas.go b/mongodbatlas/mongodbatlas.go index c67a130d8..2ef6d6488 100644 --- a/mongodbatlas/mongodbatlas.go +++ b/mongodbatlas/mongodbatlas.go @@ -21,7 +21,7 @@ const ( libraryVersion = "0.1" defaultBaseURL = "https://cloud.mongodb.com/api/atlas/v1.0/" userAgent = "go-mongodbatlas" + libraryVersion - mediaType = "application/json" + jsonMediaType = "application/json" gzipMediaType = "application/gzip" ) @@ -85,6 +85,7 @@ type Client struct { ProcessDisks ProcessDisksService ProcessDiskMeasurements ProcessDiskMeasurementsService ProcessDatabases ProcessDatabasesService + ProcessDatabaseMeasurements ProcessDatabaseMeasurementsService Indexes IndexesService Logs LogsService @@ -210,6 +211,7 @@ func NewClient(httpClient *http.Client) *Client { c.ProcessDisks = &ProcessDisksServiceOp{Client: c} c.ProcessDiskMeasurements = &ProcessDiskMeasurementsServiceOp{Client: c} c.ProcessDatabases = &ProcessDatabasesServiceOp{Client: c} + c.ProcessDatabaseMeasurements = &ProcessDatabaseMeasurementsServiceOp{Client: c} c.Indexes = &IndexesServiceOp{Client: c} c.Logs = &LogsServiceOp{Client: c} @@ -273,9 +275,9 @@ func (c *Client) NewRequest(ctx context.Context, method, urlStr string, body int } if body != nil { - req.Header.Set("Content-Type", mediaType) + req.Header.Set("Content-Type", jsonMediaType) } - req.Header.Add("Accept", mediaType) + req.Header.Add("Accept", jsonMediaType) if c.UserAgent != "" { req.Header.Set("User-Agent", c.UserAgent) } diff --git a/mongodbatlas/process_database_measurements.go b/mongodbatlas/process_database_measurements.go new file mode 100644 index 000000000..b82ef6ac5 --- /dev/null +++ b/mongodbatlas/process_database_measurements.go @@ -0,0 +1,68 @@ +package mongodbatlas + +import ( + "context" + "fmt" + "net/http" +) + +const processDatabaseMeasurementsPath = processesDatabasesPath + "/%s/measurements" + +// ProcessDatabaseMeasurementsService is an interface for interfacing with the process database measurements +// endpoints of the MongoDB Atlas API. +// See more: https://docs.atlas.mongodb.com/reference/api/process-databases-measurements/ +type ProcessDatabaseMeasurementsService interface { + List(context.Context, string, string, int, string, *ProcessMeasurementListOptions) (*ProcessDatabaseMeasurements, *Response, error) +} + +// ProcessDatabaseMeasurementsServiceOp handles communication with the process database measurements related methods of the +// MongoDB Atlas API +type ProcessDatabaseMeasurementsServiceOp struct { + Client RequestDoer +} + +// ProcessDatabaseMeasurements represents a MongoDB process database measurements. +type ProcessDatabaseMeasurements struct { + *ProcessMeasurements + DatabaseName string `json:"databaseName"` +} + +var _ ProcessDatabaseMeasurementsService = &ProcessDatabaseMeasurementsServiceOp{} + +// List list measurements for a specific Atlas MongoDB database. +// See more: https://docs.atlas.mongodb.com/reference/api/process-databases-measurements/ +func (s *ProcessDatabaseMeasurementsServiceOp) List(ctx context.Context, groupID, hostName string, port int, databaseName string, opts *ProcessMeasurementListOptions) (*ProcessDatabaseMeasurements, *Response, error) { + + if groupID == "" { + return nil, nil, NewArgError("groupID", "must be set") + } + + if hostName == "" { + return nil, nil, NewArgError("hostName", "must be set") + } + + if databaseName == "" { + return nil, nil, NewArgError("databaseName", "must be set") + } + + basePath := fmt.Sprintf(processDatabaseMeasurementsPath, groupID, hostName, port, databaseName) + + //Add query params from listOptions + path, err := setListOptions(basePath, opts) + if err != nil { + return nil, nil, err + } + + req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil) + if err != nil { + return nil, nil, err + } + + root := new(ProcessDatabaseMeasurements) + resp, err := s.Client.Do(ctx, req, root) + if err != nil { + return nil, resp, err + } + + return root, resp, err +} diff --git a/mongodbatlas/process_database_measurements_test.go b/mongodbatlas/process_database_measurements_test.go new file mode 100644 index 000000000..46bbede4b --- /dev/null +++ b/mongodbatlas/process_database_measurements_test.go @@ -0,0 +1,102 @@ +package mongodbatlas + +import ( + "fmt" + "net/http" + "testing" + + "github.com/go-test/deep" +) + +func TestProcessDatabaseMeasurements_List(t *testing.T) { + setup() + defer teardown() + + groups := "12345678" + host := "shard-00-00.mongodb.net" + port := 27017 + database := "database" + + mux.HandleFunc(fmt.Sprintf("/groups/%s/processes/%s:%d/databases/%s/measurements", groups, host, port, database), func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodGet) + fmt.Fprint(w, `{ + "end" : "2017-08-22T20:31:14Z", + "granularity" : "PT1M", + "groupId" : "12345678", + "hostId" : "shard-00-00.mongodb.net:27017", + "links" : [ { + "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", + "rel" : "self" + }, { + "href" : "https://cloud.mongodb.com/api/atlas/v1.0/groups/12345678/processes/shard-00-00.mongodb.net:27017", + "rel" : "http://mms.mongodb.com/host" + } ], + "measurements" : [ { + "dataPoints" : [ { + "timestamp" : "2017-08-22T20:31:12Z", + "value" : null + }, { + "timestamp" : "2017-08-22T20:31:14Z", + "value" : null + } ], + "name" : "DISK_PARTITION_IOPS_READ", + "units" : "SCALAR_PER_SECOND" + }], + "databaseName":"database", + "processId" : "shard-00-00.mongodb.net:27017", + "start" : "2017-08-22T20:30:45Z" + }`) + }) + + opts := &ProcessMeasurementListOptions{ + Granularity: "PT1M", + Period: "PT1M", + } + + measurements, _, err := client.ProcessDatabaseMeasurements.List(ctx, groups, host, port, database, opts) + if err != nil { + t.Fatalf("ProcessDatabaseMeasurements.List returned error: %v", err) + } + + expected := &ProcessDatabaseMeasurements{ + ProcessMeasurements: &ProcessMeasurements{ + End: "2017-08-22T20:31:14Z", + Granularity: "PT1M", + GroupID: "12345678", + HostID: "shard-00-00.mongodb.net:27017", + Links: []*Link{ + { + Rel: "self", + 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", + }, + { + Href: "https://cloud.mongodb.com/api/atlas/v1.0/groups/12345678/processes/shard-00-00.mongodb.net:27017", + Rel: "http://mms.mongodb.com/host", + }, + }, + Measurements: []*Measurements{ + { + DataPoints: []*DataPoints{ + { + Timestamp: "2017-08-22T20:31:12Z", + Value: nil, + }, + { + Timestamp: "2017-08-22T20:31:14Z", + Value: nil, + }, + }, + Name: "DISK_PARTITION_IOPS_READ", + Units: "SCALAR_PER_SECOND", + }, + }, + ProcessID: "shard-00-00.mongodb.net:27017", + Start: "2017-08-22T20:30:45Z", + }, + DatabaseName: "database", + } + + if diff := deep.Equal(measurements, expected); diff != nil { + t.Error(diff) + } +} diff --git a/mongodbatlas/process_disk_measurements.go b/mongodbatlas/process_disk_measurements.go index 8d02f664e..9f2d1c143 100644 --- a/mongodbatlas/process_disk_measurements.go +++ b/mongodbatlas/process_disk_measurements.go @@ -6,7 +6,7 @@ import ( "net/http" ) -const ProcessDiskMeasurementsPath = processesDisksPath + "/%s/measurements" +const processDiskMeasurementsPath = processesDisksPath + "/%s/measurements" // ProcessDiskMeasurementsService is an interface for interfacing with the Process Disk Measurements // endpoints of the MongoDB Atlas API. @@ -29,7 +29,7 @@ type ProcessDiskMeasurements struct { var _ ProcessDiskMeasurementsService = &ProcessDiskMeasurementsServiceOp{} -// Get gets measurements for a specific Atlas MongoDB disk. +// List lists measurements for a specific Atlas MongoDB disk. // See more: https://docs.atlas.mongodb.com/reference/api/process-disks-measurements/#get-measurements-of-a-disk-for-a-mongodb-process func (s *ProcessDiskMeasurementsServiceOp) List(ctx context.Context, groupID, hostName string, port int, diskName string, opts *ProcessMeasurementListOptions) (*ProcessDiskMeasurements, *Response, error) { @@ -45,7 +45,7 @@ func (s *ProcessDiskMeasurementsServiceOp) List(ctx context.Context, groupID, ho return nil, nil, NewArgError("diskName", "must be set") } - basePath := fmt.Sprintf(ProcessDiskMeasurementsPath, groupID, hostName, port, diskName) + basePath := fmt.Sprintf(processDiskMeasurementsPath, groupID, hostName, port, diskName) //Add query params from listOptions path, err := setListOptions(basePath, opts) diff --git a/mongodbatlas/process_disk_measurements_test.go b/mongodbatlas/process_disk_measurements_test.go index 498b68572..1cdb4890c 100644 --- a/mongodbatlas/process_disk_measurements_test.go +++ b/mongodbatlas/process_disk_measurements_test.go @@ -8,7 +8,7 @@ import ( "github.com/go-test/deep" ) -func TestDiskMeasurements_List(t *testing.T) { +func TestProcessDiskMeasurements_List(t *testing.T) { setup() defer teardown() @@ -55,7 +55,7 @@ func TestDiskMeasurements_List(t *testing.T) { measurements, _, err := client.ProcessDiskMeasurements.List(ctx, groups, host, port, disk, opts) if err != nil { - t.Fatalf("Teams.Get returned error: %v", err) + t.Fatalf("ProcessDiskMeasurements.List returned error: %v", err) } expected := &ProcessDiskMeasurements{