Skip to content

Commit 872e903

Browse files
committed
PMM-12913 migrate /v1/management/Service
1 parent 8c5b6ff commit 872e903

31 files changed

+3123
-3345
lines changed

admin/commands/management/management.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@
1616
package management
1717

1818
import (
19-
mservice "github.com/percona/pmm/api/management/v1/json/client/management_service"
19+
"github.com/percona/pmm/api/inventory/v1/types"
2020
)
2121

2222
var (
2323
allNodeTypes = map[string]string{
24-
"generic": mservice.RegisterNodeBodyNodeTypeNODETYPEGENERICNODE,
25-
"container": mservice.RegisterNodeBodyNodeTypeNODETYPECONTAINERNODE,
26-
"remote": mservice.RegisterNodeBodyNodeTypeNODETYPEREMOTENODE,
24+
"generic": types.NodeTypeGenericNode,
25+
"container": types.NodeTypeContainerNode,
26+
"remote": types.NodeTypeRemoteNode,
2727
}
2828

2929
allServiceTypes = map[string]string{
30-
"mysql": mservice.RemoveServiceBodyServiceTypeSERVICETYPEMYSQLSERVICE,
31-
"mongodb": mservice.RemoveServiceBodyServiceTypeSERVICETYPEMONGODBSERVICE,
32-
"postgresql": mservice.RemoveServiceBodyServiceTypeSERVICETYPEPOSTGRESQLSERVICE,
33-
"proxysql": mservice.RemoveServiceBodyServiceTypeSERVICETYPEPROXYSQLSERVICE,
34-
"haproxy": mservice.RemoveServiceBodyServiceTypeSERVICETYPEHAPROXYSERVICE,
35-
"external": mservice.RemoveServiceBodyServiceTypeSERVICETYPEEXTERNALSERVICE,
30+
"mysql": types.ServiceTypeMySQLService,
31+
"mongodb": types.ServiceTypeMongoDBService,
32+
"postgresql": types.ServiceTypePostgreSQLService,
33+
"proxysql": types.ServiceTypeProxySQLService,
34+
"haproxy": types.ServiceTypeHAProxyService,
35+
"external": types.ServiceTypeExternalService,
3636
}
3737

3838
// AllServiceTypesKeys lists all possible service types.

admin/commands/management/remove.go

+21-15
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,16 @@ type RemoveCommand struct {
4747

4848
// RunCmd runs the command for RemoveCommand.
4949
func (cmd *RemoveCommand) RunCmd() (commands.Result, error) {
50-
if cmd.ServiceID == "" && cmd.ServiceName == "" {
50+
// As RemoveService method accepts only one of the service ID or service name in its `serviceID` parameter.
51+
// Therefore, we need to check if both are provided. If only one is provided, we take that one.
52+
// If both are provided, we take the service ID.
53+
var serviceID string
54+
55+
switch {
56+
case cmd.ServiceID == "" && cmd.ServiceName == "":
5157
// Automatic service lookup during removal
5258
//
53-
// Get services and remove it automatically once it's only one
54-
// service registered
59+
// Remove the service automatically as long as it's the only service registered
5560
status, err := agentlocal.GetStatus(agentlocal.DoNotRequestNetworkInfo)
5661
if err != nil {
5762
return nil, err
@@ -67,31 +72,32 @@ func (cmd *RemoveCommand) RunCmd() (commands.Result, error) {
6772
}
6873
switch {
6974
case len(servicesRes.Payload.Mysql) == 1:
70-
cmd.ServiceID = servicesRes.Payload.Mysql[0].ServiceID
75+
serviceID = servicesRes.Payload.Mysql[0].ServiceID
7176
case len(servicesRes.Payload.Mongodb) == 1:
72-
cmd.ServiceID = servicesRes.Payload.Mongodb[0].ServiceID
77+
serviceID = servicesRes.Payload.Mongodb[0].ServiceID
7378
case len(servicesRes.Payload.Postgresql) == 1:
74-
cmd.ServiceID = servicesRes.Payload.Postgresql[0].ServiceID
79+
serviceID = servicesRes.Payload.Postgresql[0].ServiceID
7580
case len(servicesRes.Payload.Proxysql) == 1:
76-
cmd.ServiceID = servicesRes.Payload.Proxysql[0].ServiceID
81+
serviceID = servicesRes.Payload.Proxysql[0].ServiceID
7782
case len(servicesRes.Payload.Haproxy) == 1:
78-
cmd.ServiceID = servicesRes.Payload.Haproxy[0].ServiceID
83+
serviceID = servicesRes.Payload.Haproxy[0].ServiceID
7984
case len(servicesRes.Payload.External) == 1:
80-
cmd.ServiceID = servicesRes.Payload.External[0].ServiceID
85+
serviceID = servicesRes.Payload.External[0].ServiceID
8186
}
8287
if cmd.ServiceID == "" {
8388
//nolint:revive,golint
8489
return nil, errors.New(`We could not find a service associated with the local node. Please provide "Service ID" or "Service name".`)
8590
}
91+
case cmd.ServiceName != "" && cmd.ServiceID == "":
92+
serviceID = cmd.ServiceName
93+
default:
94+
serviceID = cmd.ServiceID
8695
}
8796

8897
params := &mservice.RemoveServiceParams{
89-
Body: mservice.RemoveServiceBody{
90-
ServiceID: cmd.ServiceID,
91-
ServiceName: cmd.ServiceName,
92-
ServiceType: cmd.serviceType(),
93-
},
94-
Context: commands.Ctx,
98+
ServiceID: serviceID,
99+
ServiceType: cmd.serviceType(),
100+
Context: commands.Ctx,
95101
}
96102
_, err := client.Default.ManagementService.RemoveService(params)
97103
if err != nil {

api-tests/management/external_test.go

+10-35
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
agents "github.com/percona/pmm/api/inventory/v1/json/client/agents_service"
2929
nodes "github.com/percona/pmm/api/inventory/v1/json/client/nodes_service"
3030
services "github.com/percona/pmm/api/inventory/v1/json/client/services_service"
31+
"github.com/percona/pmm/api/inventory/v1/types"
3132
"github.com/percona/pmm/api/management/v1/json/client"
3233
mservice "github.com/percona/pmm/api/management/v1/json/client/management_service"
3334
)
@@ -428,11 +429,9 @@ func TestRemoveExternal(t *testing.T) {
428429
defer pmmapitests.RemoveNodes(t, nodeID)
429430

430431
removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
431-
Body: mservice.RemoveServiceBody{
432-
ServiceName: serviceName,
433-
ServiceType: pointer.ToString(mservice.RemoveServiceBodyServiceTypeSERVICETYPEEXTERNALSERVICE),
434-
},
435-
Context: pmmapitests.Context,
432+
ServiceID: serviceName,
433+
ServiceType: pointer.ToString(types.ServiceTypeExternalService),
434+
Context: pmmapitests.Context,
436435
})
437436
noError := assert.NoError(t, err)
438437
notNil := assert.NotNil(t, removeServiceOK)
@@ -456,11 +455,9 @@ func TestRemoveExternal(t *testing.T) {
456455
defer pmmapitests.RemoveNodes(t, nodeID)
457456

458457
removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
459-
Body: mservice.RemoveServiceBody{
460-
ServiceID: serviceID,
461-
ServiceType: pointer.ToString(mservice.RemoveServiceBodyServiceTypeSERVICETYPEEXTERNALSERVICE),
462-
},
463-
Context: pmmapitests.Context,
458+
ServiceID: serviceID,
459+
ServiceType: pointer.ToString(types.ServiceTypeExternalService),
460+
Context: pmmapitests.Context,
464461
})
465462
noError := assert.NoError(t, err)
466463
notNil := assert.NotNil(t, removeServiceOK)
@@ -477,25 +474,6 @@ func TestRemoveExternal(t *testing.T) {
477474
assert.Nil(t, listAgents)
478475
})
479476

480-
t.Run("Both params", func(t *testing.T) {
481-
serviceName := pmmapitests.TestString(t, "service-remove-both-params")
482-
nodeName := pmmapitests.TestString(t, "node-remove-both-params")
483-
nodeID, serviceID := addExternal(t, serviceName, nodeName)
484-
defer pmmapitests.RemoveNodes(t, nodeID)
485-
defer pmmapitests.RemoveServices(t, serviceID)
486-
487-
removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
488-
Body: mservice.RemoveServiceBody{
489-
ServiceID: serviceID,
490-
ServiceName: serviceName,
491-
ServiceType: pointer.ToString(mservice.RemoveServiceBodyServiceTypeSERVICETYPEEXTERNALSERVICE),
492-
},
493-
Context: pmmapitests.Context,
494-
})
495-
assert.Nil(t, removeServiceOK)
496-
pmmapitests.AssertAPIErrorf(t, err, 400, codes.InvalidArgument, "service_id or service_name expected; not both")
497-
})
498-
499477
t.Run("Wrong type", func(t *testing.T) {
500478
serviceName := pmmapitests.TestString(t, "service-remove-wrong-type")
501479
nodeName := pmmapitests.TestString(t, "node-remove-wrong-type")
@@ -504,19 +482,16 @@ func TestRemoveExternal(t *testing.T) {
504482
defer pmmapitests.RemoveServices(t, serviceID)
505483

506484
removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
507-
Body: mservice.RemoveServiceBody{
508-
ServiceID: serviceID,
509-
ServiceType: pointer.ToString(mservice.RemoveServiceBodyServiceTypeSERVICETYPEPOSTGRESQLSERVICE),
510-
},
511-
Context: pmmapitests.Context,
485+
ServiceID: serviceID,
486+
ServiceType: pointer.ToString(types.ServiceTypePostgreSQLService),
487+
Context: pmmapitests.Context,
512488
})
513489
assert.Nil(t, removeServiceOK)
514490
pmmapitests.AssertAPIErrorf(t, err, 400, codes.InvalidArgument, "wrong service type")
515491
})
516492

517493
t.Run("No params", func(t *testing.T) {
518494
removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
519-
Body: mservice.RemoveServiceBody{},
520495
Context: pmmapitests.Context,
521496
})
522497
assert.Nil(t, removeServiceOK)

api-tests/management/haproxy_test.go

+10-35
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
agents "github.com/percona/pmm/api/inventory/v1/json/client/agents_service"
2929
nodes "github.com/percona/pmm/api/inventory/v1/json/client/nodes_service"
3030
services "github.com/percona/pmm/api/inventory/v1/json/client/services_service"
31+
"github.com/percona/pmm/api/inventory/v1/types"
3132
"github.com/percona/pmm/api/management/v1/json/client"
3233
mservice "github.com/percona/pmm/api/management/v1/json/client/management_service"
3334
)
@@ -389,11 +390,9 @@ func TestRemoveHAProxy(t *testing.T) {
389390
defer pmmapitests.RemoveNodes(t, nodeID)
390391

391392
removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
392-
Body: mservice.RemoveServiceBody{
393-
ServiceName: serviceName,
394-
ServiceType: pointer.ToString(mservice.RemoveServiceBodyServiceTypeSERVICETYPEHAPROXYSERVICE),
395-
},
396-
Context: pmmapitests.Context,
393+
ServiceID: serviceName,
394+
ServiceType: pointer.ToString(types.ServiceTypeHAProxyService),
395+
Context: pmmapitests.Context,
397396
})
398397
noError := assert.NoError(t, err)
399398
notNil := assert.NotNil(t, removeServiceOK)
@@ -417,11 +416,9 @@ func TestRemoveHAProxy(t *testing.T) {
417416
defer pmmapitests.RemoveNodes(t, nodeID)
418417

419418
removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
420-
Body: mservice.RemoveServiceBody{
421-
ServiceID: serviceID,
422-
ServiceType: pointer.ToString(mservice.RemoveServiceBodyServiceTypeSERVICETYPEHAPROXYSERVICE),
423-
},
424-
Context: pmmapitests.Context,
419+
ServiceID: serviceID,
420+
ServiceType: pointer.ToString(types.ServiceTypeHAProxyService),
421+
Context: pmmapitests.Context,
425422
})
426423
noError := assert.NoError(t, err)
427424
notNil := assert.NotNil(t, removeServiceOK)
@@ -438,25 +435,6 @@ func TestRemoveHAProxy(t *testing.T) {
438435
assert.Nil(t, listAgents)
439436
})
440437

441-
t.Run("Both params", func(t *testing.T) {
442-
serviceName := pmmapitests.TestString(t, "service-remove-both-params")
443-
nodeName := pmmapitests.TestString(t, "node-remove-both-params")
444-
nodeID, serviceID := addHAProxy(t, serviceName, nodeName)
445-
defer pmmapitests.RemoveNodes(t, nodeID)
446-
defer pmmapitests.RemoveServices(t, serviceID)
447-
448-
removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
449-
Body: mservice.RemoveServiceBody{
450-
ServiceID: serviceID,
451-
ServiceName: serviceName,
452-
ServiceType: pointer.ToString(mservice.RemoveServiceBodyServiceTypeSERVICETYPEHAPROXYSERVICE),
453-
},
454-
Context: pmmapitests.Context,
455-
})
456-
assert.Nil(t, removeServiceOK)
457-
pmmapitests.AssertAPIErrorf(t, err, 400, codes.InvalidArgument, "service_id or service_name expected; not both")
458-
})
459-
460438
t.Run("Wrong type", func(t *testing.T) {
461439
serviceName := pmmapitests.TestString(t, "service-remove-wrong-type")
462440
nodeName := pmmapitests.TestString(t, "node-remove-wrong-type")
@@ -465,19 +443,16 @@ func TestRemoveHAProxy(t *testing.T) {
465443
defer pmmapitests.RemoveServices(t, serviceID)
466444

467445
removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
468-
Body: mservice.RemoveServiceBody{
469-
ServiceID: serviceID,
470-
ServiceType: pointer.ToString(mservice.RemoveServiceBodyServiceTypeSERVICETYPEPOSTGRESQLSERVICE),
471-
},
472-
Context: pmmapitests.Context,
446+
ServiceID: serviceID,
447+
ServiceType: pointer.ToString(types.ServiceTypePostgreSQLService),
448+
Context: pmmapitests.Context,
473449
})
474450
assert.Nil(t, removeServiceOK)
475451
pmmapitests.AssertAPIErrorf(t, err, 400, codes.InvalidArgument, "wrong service type")
476452
})
477453

478454
t.Run("No params", func(t *testing.T) {
479455
removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
480-
Body: mservice.RemoveServiceBody{},
481456
Context: pmmapitests.Context,
482457
})
483458
assert.Nil(t, removeServiceOK)

api-tests/management/mongodb_test.go

+10-35
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
inventoryClient "github.com/percona/pmm/api/inventory/v1/json/client"
2828
agents "github.com/percona/pmm/api/inventory/v1/json/client/agents_service"
2929
services "github.com/percona/pmm/api/inventory/v1/json/client/services_service"
30+
"github.com/percona/pmm/api/inventory/v1/types"
3031
"github.com/percona/pmm/api/management/v1/json/client"
3132
mservice "github.com/percona/pmm/api/management/v1/json/client/management_service"
3233
)
@@ -878,11 +879,9 @@ func TestRemoveMongoDB(t *testing.T) {
878879
defer RemovePMMAgentWithSubAgents(t, pmmAgentID)
879880

880881
removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
881-
Body: mservice.RemoveServiceBody{
882-
ServiceName: serviceName,
883-
ServiceType: pointer.ToString(mservice.RemoveServiceBodyServiceTypeSERVICETYPEMONGODBSERVICE),
884-
},
885-
Context: pmmapitests.Context,
882+
ServiceID: serviceName,
883+
ServiceType: pointer.ToString(types.ServiceTypeMongoDBService),
884+
Context: pmmapitests.Context,
886885
})
887886
noError := assert.NoError(t, err)
888887
notNil := assert.NotNil(t, removeServiceOK)
@@ -907,11 +906,9 @@ func TestRemoveMongoDB(t *testing.T) {
907906
defer RemovePMMAgentWithSubAgents(t, pmmAgentID)
908907

909908
removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
910-
Body: mservice.RemoveServiceBody{
911-
ServiceID: serviceID,
912-
ServiceType: pointer.ToString(mservice.RemoveServiceBodyServiceTypeSERVICETYPEMONGODBSERVICE),
913-
},
914-
Context: pmmapitests.Context,
909+
ServiceID: serviceID,
910+
ServiceType: pointer.ToString(types.ServiceTypeMongoDBService),
911+
Context: pmmapitests.Context,
915912
})
916913
noError := assert.NoError(t, err)
917914
notNil := assert.NotNil(t, removeServiceOK)
@@ -928,26 +925,6 @@ func TestRemoveMongoDB(t *testing.T) {
928925
assert.Nil(t, listAgents)
929926
})
930927

931-
t.Run("Both params", func(t *testing.T) {
932-
serviceName := pmmapitests.TestString(t, "service-remove-both-params")
933-
nodeName := pmmapitests.TestString(t, "node-remove-both-params")
934-
nodeID, pmmAgentID, serviceID := addMongoDB(t, serviceName, nodeName, false)
935-
defer pmmapitests.RemoveNodes(t, nodeID)
936-
defer pmmapitests.RemoveServices(t, serviceID)
937-
defer RemovePMMAgentWithSubAgents(t, pmmAgentID)
938-
939-
removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
940-
Body: mservice.RemoveServiceBody{
941-
ServiceID: serviceID,
942-
ServiceName: serviceName,
943-
ServiceType: pointer.ToString(mservice.RemoveServiceBodyServiceTypeSERVICETYPEMYSQLSERVICE),
944-
},
945-
Context: pmmapitests.Context,
946-
})
947-
assert.Nil(t, removeServiceOK)
948-
pmmapitests.AssertAPIErrorf(t, err, 400, codes.InvalidArgument, "service_id or service_name expected; not both")
949-
})
950-
951928
t.Run("Wrong type", func(t *testing.T) {
952929
serviceName := pmmapitests.TestString(t, "service-remove-wrong-type")
953930
nodeName := pmmapitests.TestString(t, "node-remove-wrong-type")
@@ -957,11 +934,9 @@ func TestRemoveMongoDB(t *testing.T) {
957934
defer RemovePMMAgentWithSubAgents(t, pmmAgentID)
958935

959936
removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
960-
Body: mservice.RemoveServiceBody{
961-
ServiceID: serviceID,
962-
ServiceType: pointer.ToString(mservice.RemoveServiceBodyServiceTypeSERVICETYPEPOSTGRESQLSERVICE),
963-
},
964-
Context: pmmapitests.Context,
937+
ServiceID: serviceID,
938+
ServiceType: pointer.ToString(types.ServiceTypePostgreSQLService),
939+
Context: pmmapitests.Context,
965940
})
966941
assert.Nil(t, removeServiceOK)
967942
pmmapitests.AssertAPIErrorf(t, err, 400, codes.InvalidArgument, "wrong service type")

0 commit comments

Comments
 (0)