Skip to content

Commit

Permalink
PMM-12913 migrate /v1/management/Service
Browse files Browse the repository at this point in the history
  • Loading branch information
ademidoff committed Mar 27, 2024
1 parent 8c5b6ff commit afe6a51
Show file tree
Hide file tree
Showing 31 changed files with 3,120 additions and 3,342 deletions.
20 changes: 10 additions & 10 deletions admin/commands/management/management.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@
package management

import (
mservice "github.com/percona/pmm/api/management/v1/json/client/management_service"
"github.com/percona/pmm/api/inventory/v1/types"
)

var (
allNodeTypes = map[string]string{
"generic": mservice.RegisterNodeBodyNodeTypeNODETYPEGENERICNODE,
"container": mservice.RegisterNodeBodyNodeTypeNODETYPECONTAINERNODE,
"remote": mservice.RegisterNodeBodyNodeTypeNODETYPEREMOTENODE,
"generic": types.NodeTypeGenericNode,
"container": types.NodeTypeContainerNode,
"remote": types.NodeTypeRemoteNode,
}

allServiceTypes = map[string]string{
"mysql": mservice.RemoveServiceBodyServiceTypeSERVICETYPEMYSQLSERVICE,
"mongodb": mservice.RemoveServiceBodyServiceTypeSERVICETYPEMONGODBSERVICE,
"postgresql": mservice.RemoveServiceBodyServiceTypeSERVICETYPEPOSTGRESQLSERVICE,
"proxysql": mservice.RemoveServiceBodyServiceTypeSERVICETYPEPROXYSQLSERVICE,
"haproxy": mservice.RemoveServiceBodyServiceTypeSERVICETYPEHAPROXYSERVICE,
"external": mservice.RemoveServiceBodyServiceTypeSERVICETYPEEXTERNALSERVICE,
"mysql": types.ServiceTypeMySQLService,
"mongodb": types.ServiceTypeMongoDBService,
"postgresql": types.ServiceTypePostgreSQLService,
"proxysql": types.ServiceTypeProxySQLService,
"haproxy": types.ServiceTypeHAProxyService,
"external": types.ServiceTypeExternalService,
}

// AllServiceTypesKeys lists all possible service types.
Expand Down
36 changes: 21 additions & 15 deletions admin/commands/management/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,16 @@ type RemoveCommand struct {

// RunCmd runs the command for RemoveCommand.
func (cmd *RemoveCommand) RunCmd() (commands.Result, error) {
if cmd.ServiceID == "" && cmd.ServiceName == "" {
// As RemoveService method accepts only one of the service ID or service name in its `serviceID` parameter.
// Therefore, we need to check if both are provided. If only one is provided, we take that one.
// If both are provided, we take the service ID.
var serviceID string

switch {
case cmd.ServiceID == "" && cmd.ServiceName == "":
// Automatic service lookup during removal
//
// Get services and remove it automatically once it's only one
// service registered
// Remove the service automatically as long as it's the only service registered
status, err := agentlocal.GetStatus(agentlocal.DoNotRequestNetworkInfo)
if err != nil {
return nil, err
Expand All @@ -67,31 +72,32 @@ func (cmd *RemoveCommand) RunCmd() (commands.Result, error) {
}
switch {
case len(servicesRes.Payload.Mysql) == 1:
cmd.ServiceID = servicesRes.Payload.Mysql[0].ServiceID
serviceID = servicesRes.Payload.Mysql[0].ServiceID
case len(servicesRes.Payload.Mongodb) == 1:
cmd.ServiceID = servicesRes.Payload.Mongodb[0].ServiceID
serviceID = servicesRes.Payload.Mongodb[0].ServiceID
case len(servicesRes.Payload.Postgresql) == 1:
cmd.ServiceID = servicesRes.Payload.Postgresql[0].ServiceID
serviceID = servicesRes.Payload.Postgresql[0].ServiceID
case len(servicesRes.Payload.Proxysql) == 1:
cmd.ServiceID = servicesRes.Payload.Proxysql[0].ServiceID
serviceID = servicesRes.Payload.Proxysql[0].ServiceID
case len(servicesRes.Payload.Haproxy) == 1:
cmd.ServiceID = servicesRes.Payload.Haproxy[0].ServiceID
serviceID = servicesRes.Payload.Haproxy[0].ServiceID
case len(servicesRes.Payload.External) == 1:
cmd.ServiceID = servicesRes.Payload.External[0].ServiceID
serviceID = servicesRes.Payload.External[0].ServiceID
}
if cmd.ServiceID == "" {
//nolint:revive,golint
return nil, errors.New(`We could not find a service associated with the local node. Please provide "Service ID" or "Service name".`)
}
case cmd.ServiceName != "" && cmd.ServiceID == "":
serviceID = cmd.ServiceName
default:
serviceID = cmd.ServiceID
}

params := &mservice.RemoveServiceParams{
Body: mservice.RemoveServiceBody{
ServiceID: cmd.ServiceID,
ServiceName: cmd.ServiceName,
ServiceType: cmd.serviceType(),
},
Context: commands.Ctx,
ServiceID: serviceID,
ServiceType: cmd.serviceType(),
Context: commands.Ctx,
}
_, err := client.Default.ManagementService.RemoveService(params)
if err != nil {
Expand Down
45 changes: 10 additions & 35 deletions api-tests/management/external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
agents "github.com/percona/pmm/api/inventory/v1/json/client/agents_service"
nodes "github.com/percona/pmm/api/inventory/v1/json/client/nodes_service"
services "github.com/percona/pmm/api/inventory/v1/json/client/services_service"
"github.com/percona/pmm/api/inventory/v1/types"
"github.com/percona/pmm/api/management/v1/json/client"
mservice "github.com/percona/pmm/api/management/v1/json/client/management_service"
)
Expand Down Expand Up @@ -428,11 +429,9 @@ func TestRemoveExternal(t *testing.T) {
defer pmmapitests.RemoveNodes(t, nodeID)

removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
Body: mservice.RemoveServiceBody{
ServiceName: serviceName,
ServiceType: pointer.ToString(mservice.RemoveServiceBodyServiceTypeSERVICETYPEEXTERNALSERVICE),
},
Context: pmmapitests.Context,
ServiceID: serviceName,
ServiceType: pointer.ToString(types.ServiceTypeExternalService),
Context: pmmapitests.Context,
})
noError := assert.NoError(t, err)
notNil := assert.NotNil(t, removeServiceOK)
Expand All @@ -456,11 +455,9 @@ func TestRemoveExternal(t *testing.T) {
defer pmmapitests.RemoveNodes(t, nodeID)

removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
Body: mservice.RemoveServiceBody{
ServiceID: serviceID,
ServiceType: pointer.ToString(mservice.RemoveServiceBodyServiceTypeSERVICETYPEEXTERNALSERVICE),
},
Context: pmmapitests.Context,
ServiceID: serviceID,
ServiceType: pointer.ToString(types.ServiceTypeExternalService),
Context: pmmapitests.Context,
})
noError := assert.NoError(t, err)
notNil := assert.NotNil(t, removeServiceOK)
Expand All @@ -477,25 +474,6 @@ func TestRemoveExternal(t *testing.T) {
assert.Nil(t, listAgents)
})

t.Run("Both params", func(t *testing.T) {
serviceName := pmmapitests.TestString(t, "service-remove-both-params")
nodeName := pmmapitests.TestString(t, "node-remove-both-params")
nodeID, serviceID := addExternal(t, serviceName, nodeName)
defer pmmapitests.RemoveNodes(t, nodeID)
defer pmmapitests.RemoveServices(t, serviceID)

removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
Body: mservice.RemoveServiceBody{
ServiceID: serviceID,
ServiceName: serviceName,
ServiceType: pointer.ToString(mservice.RemoveServiceBodyServiceTypeSERVICETYPEEXTERNALSERVICE),
},
Context: pmmapitests.Context,
})
assert.Nil(t, removeServiceOK)
pmmapitests.AssertAPIErrorf(t, err, 400, codes.InvalidArgument, "service_id or service_name expected; not both")
})

t.Run("Wrong type", func(t *testing.T) {
serviceName := pmmapitests.TestString(t, "service-remove-wrong-type")
nodeName := pmmapitests.TestString(t, "node-remove-wrong-type")
Expand All @@ -504,19 +482,16 @@ func TestRemoveExternal(t *testing.T) {
defer pmmapitests.RemoveServices(t, serviceID)

removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
Body: mservice.RemoveServiceBody{
ServiceID: serviceID,
ServiceType: pointer.ToString(mservice.RemoveServiceBodyServiceTypeSERVICETYPEPOSTGRESQLSERVICE),
},
Context: pmmapitests.Context,
ServiceID: serviceID,
ServiceType: pointer.ToString(types.ServiceTypePostgreSQLService),
Context: pmmapitests.Context,
})
assert.Nil(t, removeServiceOK)
pmmapitests.AssertAPIErrorf(t, err, 400, codes.InvalidArgument, "wrong service type")
})

t.Run("No params", func(t *testing.T) {
removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
Body: mservice.RemoveServiceBody{},
Context: pmmapitests.Context,
})
assert.Nil(t, removeServiceOK)
Expand Down
45 changes: 10 additions & 35 deletions api-tests/management/haproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
agents "github.com/percona/pmm/api/inventory/v1/json/client/agents_service"
nodes "github.com/percona/pmm/api/inventory/v1/json/client/nodes_service"
services "github.com/percona/pmm/api/inventory/v1/json/client/services_service"
"github.com/percona/pmm/api/inventory/v1/types"
"github.com/percona/pmm/api/management/v1/json/client"
mservice "github.com/percona/pmm/api/management/v1/json/client/management_service"
)
Expand Down Expand Up @@ -389,11 +390,9 @@ func TestRemoveHAProxy(t *testing.T) {
defer pmmapitests.RemoveNodes(t, nodeID)

removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
Body: mservice.RemoveServiceBody{
ServiceName: serviceName,
ServiceType: pointer.ToString(mservice.RemoveServiceBodyServiceTypeSERVICETYPEHAPROXYSERVICE),
},
Context: pmmapitests.Context,
ServiceID: serviceName,
ServiceType: pointer.ToString(types.ServiceTypeHAProxyService),
Context: pmmapitests.Context,
})
noError := assert.NoError(t, err)
notNil := assert.NotNil(t, removeServiceOK)
Expand All @@ -417,11 +416,9 @@ func TestRemoveHAProxy(t *testing.T) {
defer pmmapitests.RemoveNodes(t, nodeID)

removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
Body: mservice.RemoveServiceBody{
ServiceID: serviceID,
ServiceType: pointer.ToString(mservice.RemoveServiceBodyServiceTypeSERVICETYPEHAPROXYSERVICE),
},
Context: pmmapitests.Context,
ServiceID: serviceID,
ServiceType: pointer.ToString(types.ServiceTypeHAProxyService),
Context: pmmapitests.Context,
})
noError := assert.NoError(t, err)
notNil := assert.NotNil(t, removeServiceOK)
Expand All @@ -438,25 +435,6 @@ func TestRemoveHAProxy(t *testing.T) {
assert.Nil(t, listAgents)
})

t.Run("Both params", func(t *testing.T) {
serviceName := pmmapitests.TestString(t, "service-remove-both-params")
nodeName := pmmapitests.TestString(t, "node-remove-both-params")
nodeID, serviceID := addHAProxy(t, serviceName, nodeName)
defer pmmapitests.RemoveNodes(t, nodeID)
defer pmmapitests.RemoveServices(t, serviceID)

removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
Body: mservice.RemoveServiceBody{
ServiceID: serviceID,
ServiceName: serviceName,
ServiceType: pointer.ToString(mservice.RemoveServiceBodyServiceTypeSERVICETYPEHAPROXYSERVICE),
},
Context: pmmapitests.Context,
})
assert.Nil(t, removeServiceOK)
pmmapitests.AssertAPIErrorf(t, err, 400, codes.InvalidArgument, "service_id or service_name expected; not both")
})

t.Run("Wrong type", func(t *testing.T) {
serviceName := pmmapitests.TestString(t, "service-remove-wrong-type")
nodeName := pmmapitests.TestString(t, "node-remove-wrong-type")
Expand All @@ -465,19 +443,16 @@ func TestRemoveHAProxy(t *testing.T) {
defer pmmapitests.RemoveServices(t, serviceID)

removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
Body: mservice.RemoveServiceBody{
ServiceID: serviceID,
ServiceType: pointer.ToString(mservice.RemoveServiceBodyServiceTypeSERVICETYPEPOSTGRESQLSERVICE),
},
Context: pmmapitests.Context,
ServiceID: serviceID,
ServiceType: pointer.ToString(types.ServiceTypePostgreSQLService),
Context: pmmapitests.Context,
})
assert.Nil(t, removeServiceOK)
pmmapitests.AssertAPIErrorf(t, err, 400, codes.InvalidArgument, "wrong service type")
})

t.Run("No params", func(t *testing.T) {
removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
Body: mservice.RemoveServiceBody{},
Context: pmmapitests.Context,
})
assert.Nil(t, removeServiceOK)
Expand Down
45 changes: 10 additions & 35 deletions api-tests/management/mongodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
inventoryClient "github.com/percona/pmm/api/inventory/v1/json/client"
agents "github.com/percona/pmm/api/inventory/v1/json/client/agents_service"
services "github.com/percona/pmm/api/inventory/v1/json/client/services_service"
"github.com/percona/pmm/api/inventory/v1/types"
"github.com/percona/pmm/api/management/v1/json/client"
mservice "github.com/percona/pmm/api/management/v1/json/client/management_service"
)
Expand Down Expand Up @@ -878,11 +879,9 @@ func TestRemoveMongoDB(t *testing.T) {
defer RemovePMMAgentWithSubAgents(t, pmmAgentID)

removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
Body: mservice.RemoveServiceBody{
ServiceName: serviceName,
ServiceType: pointer.ToString(mservice.RemoveServiceBodyServiceTypeSERVICETYPEMONGODBSERVICE),
},
Context: pmmapitests.Context,
ServiceID: serviceName,
ServiceType: pointer.ToString(types.ServiceTypeMongoDBService),
Context: pmmapitests.Context,
})
noError := assert.NoError(t, err)
notNil := assert.NotNil(t, removeServiceOK)
Expand All @@ -907,11 +906,9 @@ func TestRemoveMongoDB(t *testing.T) {
defer RemovePMMAgentWithSubAgents(t, pmmAgentID)

removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
Body: mservice.RemoveServiceBody{
ServiceID: serviceID,
ServiceType: pointer.ToString(mservice.RemoveServiceBodyServiceTypeSERVICETYPEMONGODBSERVICE),
},
Context: pmmapitests.Context,
ServiceID: serviceID,
ServiceType: pointer.ToString(types.ServiceTypeMongoDBService),
Context: pmmapitests.Context,
})
noError := assert.NoError(t, err)
notNil := assert.NotNil(t, removeServiceOK)
Expand All @@ -928,26 +925,6 @@ func TestRemoveMongoDB(t *testing.T) {
assert.Nil(t, listAgents)
})

t.Run("Both params", func(t *testing.T) {
serviceName := pmmapitests.TestString(t, "service-remove-both-params")
nodeName := pmmapitests.TestString(t, "node-remove-both-params")
nodeID, pmmAgentID, serviceID := addMongoDB(t, serviceName, nodeName, false)
defer pmmapitests.RemoveNodes(t, nodeID)
defer pmmapitests.RemoveServices(t, serviceID)
defer RemovePMMAgentWithSubAgents(t, pmmAgentID)

removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
Body: mservice.RemoveServiceBody{
ServiceID: serviceID,
ServiceName: serviceName,
ServiceType: pointer.ToString(mservice.RemoveServiceBodyServiceTypeSERVICETYPEMYSQLSERVICE),
},
Context: pmmapitests.Context,
})
assert.Nil(t, removeServiceOK)
pmmapitests.AssertAPIErrorf(t, err, 400, codes.InvalidArgument, "service_id or service_name expected; not both")
})

t.Run("Wrong type", func(t *testing.T) {
serviceName := pmmapitests.TestString(t, "service-remove-wrong-type")
nodeName := pmmapitests.TestString(t, "node-remove-wrong-type")
Expand All @@ -957,11 +934,9 @@ func TestRemoveMongoDB(t *testing.T) {
defer RemovePMMAgentWithSubAgents(t, pmmAgentID)

removeServiceOK, err := client.Default.ManagementService.RemoveService(&mservice.RemoveServiceParams{
Body: mservice.RemoveServiceBody{
ServiceID: serviceID,
ServiceType: pointer.ToString(mservice.RemoveServiceBodyServiceTypeSERVICETYPEPOSTGRESQLSERVICE),
},
Context: pmmapitests.Context,
ServiceID: serviceID,
ServiceType: pointer.ToString(types.ServiceTypePostgreSQLService),
Context: pmmapitests.Context,
})
assert.Nil(t, removeServiceOK)
pmmapitests.AssertAPIErrorf(t, err, 400, codes.InvalidArgument, "wrong service type")
Expand Down
Loading

0 comments on commit afe6a51

Please sign in to comment.