Skip to content

Commit 7b3a0d1

Browse files
authored
feat: add GET process (#311)
1 parent 819fb54 commit 7b3a0d1

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

mongodbatlas/processes.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const processesPath = "api/atlas/v1.0/groups/%s/processes"
2626
//
2727
// See more: https://docs.atlas.mongodb.com/reference/api/monitoring-and-logs/
2828
type ProcessesService interface {
29+
Get(context.Context, string, string, int) (*Process, *Response, error)
2930
List(context.Context, string, *ProcessesListOptions) ([]*Process, *Response, error)
3031
}
3132

@@ -64,6 +65,36 @@ type ProcessesListOptions struct {
6465
ClusterID string `url:"clusterId,omitempty"` // ClusterID is only available for Ops Manager and CLoud Manager.
6566
}
6667

68+
// Get information for the specified Atlas MongoDB process in the specified project.
69+
// An Atlas MongoDB process can be either a mongod or a mongos.
70+
//
71+
// See more: https://www.mongodb.com/docs/atlas/reference/api/processes-get-one/
72+
func (s *ProcessesServiceOp) Get(ctx context.Context, groupID, hostname string, port int) (*Process, *Response, error) {
73+
if groupID == "" {
74+
return nil, nil, NewArgError("groupID", "must be set")
75+
}
76+
if hostname == "" {
77+
return nil, nil, NewArgError("hostname", "must be set")
78+
}
79+
if port == 0 {
80+
return nil, nil, NewArgError("port", "must be set")
81+
}
82+
path := fmt.Sprintf(processesPath, groupID)
83+
path = fmt.Sprintf("%s/%s:%d", path, hostname, port)
84+
req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
85+
if err != nil {
86+
return nil, nil, err
87+
}
88+
89+
root := new(Process)
90+
resp, err := s.Client.Do(ctx, req, root)
91+
if err != nil {
92+
return nil, resp, err
93+
}
94+
95+
return root, resp, nil
96+
}
97+
6798
// List lists all processes in the project associated to {GROUP-ID}.
6899
//
69100
// See more: https://docs.atlas.mongodb.com/reference/api/processes-get-all/

mongodbatlas/processes_test.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func TestProcesses_RetrievePageByNumber(t *testing.T) {
221221
}
222222
`
223223

224-
mux.HandleFunc("/api/atlas/v1.0/groups/1/processes", func(w http.ResponseWriter, r *http.Request) {
224+
mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.0/groups/%s/processes", groupID), func(w http.ResponseWriter, r *http.Request) {
225225
testMethod(t, r, http.MethodGet)
226226
expectedQuery := "pageNum=2"
227227
if r.URL.RawQuery != expectedQuery {
@@ -240,3 +240,48 @@ func TestProcesses_RetrievePageByNumber(t *testing.T) {
240240

241241
checkCurrentPage(t, resp, 2)
242242
}
243+
244+
func TestProcessesServiceOp_Get(t *testing.T) {
245+
client, mux, teardown := setup()
246+
defer teardown()
247+
const hostname = "atlas-abcdef-shard-00-00.nta8e.mongodb.net"
248+
const port = 27017
249+
path := fmt.Sprintf("/api/atlas/v1.0/groups/%s/processes/%s:%d", groupID, hostname, port)
250+
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
251+
testMethod(t, r, http.MethodGet)
252+
_, _ = fmt.Fprint(w, `{
253+
"created" : "2020-08-25T18:44:13Z",
254+
"groupId" : "1",
255+
"hostname" : "atlas-abcdef-shard-00-00.nta8e.mongodb.net",
256+
"id" : "atlas-abcdef-shard-00-00.nta8e.mongodb.net:27017",
257+
"lastPing" : "2020-09-01T18:40:06Z",
258+
"port" : 27017,
259+
"replicaSetName" : "atlas-abcdef-shard-0",
260+
"typeName" : "REPLICA_PRIMARY",
261+
"userAlias" : "testcluster-shard-00-00.nta8e.mongodb.net",
262+
"version" : "4.4.0"
263+
}`)
264+
})
265+
266+
cluster, _, err := client.Processes.Get(ctx, groupID, hostname, port)
267+
if err != nil {
268+
t.Fatalf("Processes.Get returned error: %v", err)
269+
}
270+
271+
expected := &Process{
272+
ID: "atlas-abcdef-shard-00-00.nta8e.mongodb.net:27017",
273+
GroupID: groupID,
274+
Hostname: hostname,
275+
TypeName: "REPLICA_PRIMARY",
276+
Created: "2020-08-25T18:44:13Z",
277+
LastPing: "2020-09-01T18:40:06Z",
278+
Port: port,
279+
ReplicaSetName: "atlas-abcdef-shard-0",
280+
UserAlias: "testcluster-shard-00-00.nta8e.mongodb.net",
281+
Version: "4.4.0",
282+
}
283+
284+
if diff := deep.Equal(cluster, expected); diff != nil {
285+
t.Error(diff)
286+
}
287+
}

0 commit comments

Comments
 (0)