This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
/
mongodb.go
137 lines (118 loc) · 4.1 KB
/
mongodb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// pmm-managed
// Copyright (C) 2017 Percona LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package management
import (
"context"
"github.com/AlekSi/pointer"
"github.com/percona/pmm/api/inventorypb"
"github.com/percona/pmm/api/managementpb"
"gopkg.in/reform.v1"
"github.com/percona/pmm-managed/models"
"github.com/percona/pmm-managed/services"
)
// MongoDBService MongoDB Management Service.
//nolint:unused
type MongoDBService struct {
db *reform.DB
registry agentsRegistry
}
// NewMongoDBService creates new MongoDB Management Service.
func NewMongoDBService(db *reform.DB, registry agentsRegistry) *MongoDBService {
return &MongoDBService{db, registry}
}
// Add adds "MongoDB Service", "MongoDB Exporter Agent" and "QAN MongoDB Profiler".
func (s *MongoDBService) Add(ctx context.Context, req *managementpb.AddMongoDBRequest) (*managementpb.AddMongoDBResponse, error) {
res := new(managementpb.AddMongoDBResponse)
if e := s.db.InTransaction(func(tx *reform.TX) error {
nodeID, err := nodeID(tx, req.NodeId, req.NodeName, req.AddNode, req.Address)
if err != nil {
return err
}
service, err := models.AddNewService(tx.Querier, models.MongoDBServiceType, &models.AddDBMSServiceParams{
ServiceName: req.ServiceName,
NodeID: nodeID,
Environment: req.Environment,
Cluster: req.Cluster,
ReplicationSet: req.ReplicationSet,
Address: pointer.ToStringOrNil(req.Address),
Port: pointer.ToUint16OrNil(uint16(req.Port)),
Socket: pointer.ToStringOrNil(req.Socket),
CustomLabels: req.CustomLabels,
})
if err != nil {
return err
}
invService, err := services.ToAPIService(service)
if err != nil {
return err
}
res.Service = invService.(*inventorypb.MongoDBService)
mongoDBOptions := models.MongoDBOptionsFromRequest(req)
req.MetricsMode, err = supportedMetricsMode(tx.Querier, req.MetricsMode, req.PmmAgentId)
if err != nil {
return err
}
row, err := models.CreateAgent(tx.Querier, models.MongoDBExporterType, &models.CreateAgentParams{
PMMAgentID: req.PmmAgentId,
ServiceID: service.ServiceID,
Username: req.Username,
Password: req.Password,
TLS: req.Tls,
TLSSkipVerify: req.TlsSkipVerify,
MongoDBOptions: mongoDBOptions,
PushMetrics: isPushMode(req.MetricsMode),
DisableCollectors: req.DisableCollectors,
})
if err != nil {
return err
}
if !req.SkipConnectionCheck {
if err = s.registry.CheckConnectionToService(ctx, tx.Querier, service, row); err != nil {
return err
}
}
agent, err := services.ToAPIAgent(tx.Querier, row)
if err != nil {
return err
}
res.MongodbExporter = agent.(*inventorypb.MongoDBExporter)
if req.QanMongodbProfiler {
row, err = models.CreateAgent(tx.Querier, models.QANMongoDBProfilerAgentType, &models.CreateAgentParams{
PMMAgentID: req.PmmAgentId,
ServiceID: service.ServiceID,
Username: req.Username,
Password: req.Password,
TLS: req.Tls,
TLSSkipVerify: req.TlsSkipVerify,
MongoDBOptions: mongoDBOptions,
// TODO QueryExamplesDisabled https://jira.percona.com/browse/PMM-4650
})
if err != nil {
return err
}
agent, err := services.ToAPIAgent(tx.Querier, row)
if err != nil {
return err
}
res.QanMongodbProfiler = agent.(*inventorypb.QANMongoDBProfilerAgent)
}
return nil
}); e != nil {
return nil, e
}
s.registry.RequestStateUpdate(ctx, req.PmmAgentId)
return res, nil
}