1// Copyright 2021 MongoDB Inc
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package mongodbatlas
16
17import (
18	"context"
19	"fmt"
20	"net/http"
21)
22
23const processesDisksPath = "groups/%s/processes/%s:%d/disks"
24
25// ProcessDisksService is an interface for interfacing with the Process Measurements
26// endpoints of the MongoDB Atlas API.
27// See more: https://docs.atlas.mongodb.com/reference/api/process-disks/
28type ProcessDisksService interface {
29	List(context.Context, string, string, int, *ListOptions) (*ProcessDisksResponse, *Response, error)
30}
31
32// ProcessDisksServiceOp handles communication with the process disks related methods of the
33// MongoDB Atlas API
34type ProcessDisksServiceOp service
35
36var _ ProcessDisksService = &ProcessDisksServiceOp{}
37
38// ProcessDisksResponse is the response from the ProcessDisksService.List.
39type ProcessDisksResponse struct {
40	Links      []*Link        `json:"links"`
41	Results    []*ProcessDisk `json:"results"`
42	TotalCount int            `json:"totalCount"`
43}
44
45// ProcessDisk is the partition information of a process
46type ProcessDisk struct {
47	Links         []*Link `json:"links"`
48	PartitionName string  `json:"partitionName"`
49}
50
51// List gets partitions for a specific Atlas MongoDB process.
52// See more: https://docs.atlas.mongodb.com/reference/api/process-disks/
53func (s *ProcessDisksServiceOp) List(ctx context.Context, groupID, host string, port int, opts *ListOptions) (*ProcessDisksResponse, *Response, error) {
54	if groupID == "" {
55		return nil, nil, NewArgError("groupID", "must be set")
56	}
57
58	if host == "" {
59		return nil, nil, NewArgError("host", "must be set")
60	}
61
62	if port <= 0 {
63		return nil, nil, NewArgError("port", "must be valid")
64	}
65
66	basePath := fmt.Sprintf(processesDisksPath, groupID, host, port)
67
68	// Add query params from listOptions
69	path, err := setListOptions(basePath, opts)
70	if err != nil {
71		return nil, nil, err
72	}
73
74	req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
75	if err != nil {
76		return nil, nil, err
77	}
78
79	root := new(ProcessDisksResponse)
80	resp, err := s.Client.Do(ctx, req, root)
81	return root, resp, err
82}
83