From 6deb14c1f63fc59a9a72b191300449e06365ab5e Mon Sep 17 00:00:00 2001 From: anrs Date: Sun, 7 May 2023 08:26:58 +0000 Subject: [PATCH] implements GetGuestIDList svc --- Makefile | 2 -- internal/server/service.go | 11 ++++++++ internal/server/service_test.go | 9 +++++++ internal/virt/guest/bot.go | 6 ++++- internal/virt/guest/guest.go | 16 ++++++++++++ internal/virt/guest/manager/manager.go | 6 +++++ .../virt/guest/manager/mocks/Manageable.go | 26 +++++++++++++++++++ pkg/libvirt/libvirt.go | 12 ++++----- 8 files changed, 79 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index dfa3702..e91c319 100644 --- a/Makefile +++ b/Makefile @@ -23,8 +23,6 @@ build-ctl: setup: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest go install github.com/vektra/mockery/v2@latest - go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28 - go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2 lint: format PATH=${HOME}/go/bin:${PATH} golangci-lint run --skip-dirs-use-default --skip-dirs=thirdparty diff --git a/internal/server/service.go b/internal/server/service.go index 740c87b..540e9f8 100644 --- a/internal/server/service.go +++ b/internal/server/service.go @@ -163,6 +163,17 @@ func (svc *Service) GetGuest(ctx virt.Context, id string) (*types.Guest, error) return convGuestResp(vg.Guest), nil } +// GetGuestIDList . +func (svc *Service) GetGuestIDList(ctx virt.Context) ([]string, error) { + ids, err := svc.guest.ListLocalIDs(ctx) + if err != nil { + log.ErrorStack(err) + metrics.IncrError() + return nil, err + } + return ids, err +} + // GetGuestUUID . func (svc *Service) GetGuestUUID(ctx virt.Context, id string) (string, error) { uuid, err := svc.guest.LoadUUID(ctx, id) diff --git a/internal/server/service_test.go b/internal/server/service_test.go index e90dd85..27ddb2d 100644 --- a/internal/server/service_test.go +++ b/internal/server/service_test.go @@ -51,6 +51,15 @@ func TestGetGuest(t *testing.T) { assert.NilErr(t, err) } +func TestGetGuestIDList(t *testing.T) { + localIDs := []string{"ya0", "ya1", "ya2"} + svc := testService(t) + svc.guest.(*managerocks.Manageable).On("ListLocalIDs", mock.Anything, mock.Anything).Return(localIDs, nil).Once() + ids, err := svc.GetGuestIDList(testVirtContext(t)) + assert.NilErr(t, err) + assert.Equal(t, []string{"ya0", "ya1", "ya2"}, ids) +} + func TestGetGuestUUID(t *testing.T) { svc := testService(t) svc.guest.(*managerocks.Manageable).On("LoadUUID", mock.Anything, mock.Anything).Return("uuid", nil) diff --git a/internal/virt/guest/bot.go b/internal/virt/guest/bot.go index 83adb91..4b5b733 100644 --- a/internal/virt/guest/bot.go +++ b/internal/virt/guest/bot.go @@ -64,7 +64,7 @@ type bot struct { } func newVirtGuest(guest *Guest) (Bot, error) { - virt, err := libvirt.Connect("qemu:///system") + virt, err := connectSystemLibvirt() if err != nil { return nil, errors.Trace(err) } @@ -81,6 +81,10 @@ func newVirtGuest(guest *Guest) (Bot, error) { return vg, nil } +func connectSystemLibvirt() (libvirt.Libvirt, error) { + return libvirt.Connect("qemu:///system") +} + func newVolume(volmod *models.Volume) volume.Virt { return volume.New(volmod) } diff --git a/internal/virt/guest/guest.go b/internal/virt/guest/guest.go index b6a0d2e..5fd0d71 100644 --- a/internal/virt/guest/guest.go +++ b/internal/virt/guest/guest.go @@ -36,6 +36,22 @@ func New(ctx virt.Context, g *models.Guest) *Guest { } } +// ListLocalIDs lists all local guest domain names. +func ListLocalIDs(virt.Context) ([]string, error) { + virt, err := connectSystemLibvirt() + if err != nil { + return nil, errors.Trace(err) + } + + defer func() { + if _, ce := virt.Close(); ce != nil { + log.ErrorStack(ce) + } + }() + + return virt.ListDomainsNames() +} + // Load . func (g *Guest) Load() error { host, err := models.LoadHost(g.HostName) diff --git a/internal/virt/guest/manager/manager.go b/internal/virt/guest/manager/manager.go index 0482454..11867fd 100644 --- a/internal/virt/guest/manager/manager.go +++ b/internal/virt/guest/manager/manager.go @@ -67,6 +67,7 @@ type Controllable interface { type Loadable interface { Load(ctx virt.Context, id string) (*guest.Guest, error) LoadUUID(ctx virt.Context, id string) (string, error) + ListLocalIDs(virt.Context) ([]string, error) } var imageMutex sync.Mutex @@ -577,6 +578,11 @@ func (m Manager) doCtrl(ctx virt.Context, id string, op op, fn doCtrlFunc, rollb return m.do(ctx, id, op, do, rollback) } +// ListLocals lists all local guests. +func (m Manager) ListLocalIDs(ctx virt.Context) ([]string, error) { + return guest.ListLocalIDs(ctx) +} + // LoadUUID read a guest's UUID. func (m Manager) LoadUUID(ctx virt.Context, id string) (string, error) { g, err := m.Load(ctx, id) diff --git a/internal/virt/guest/manager/mocks/Manageable.go b/internal/virt/guest/manager/mocks/Manageable.go index f903697..3a0384d 100644 --- a/internal/virt/guest/manager/mocks/Manageable.go +++ b/internal/virt/guest/manager/mocks/Manageable.go @@ -313,6 +313,32 @@ func (_m *Manageable) ListImage(ctx virt.Context, filter string) ([]models.Image return r0, r1 } +// ListLocalIDs provides a mock function with given fields: _a0 +func (_m *Manageable) ListLocalIDs(_a0 virt.Context) ([]string, error) { + ret := _m.Called(_a0) + + var r0 []string + var r1 error + if rf, ok := ret.Get(0).(func(virt.Context) ([]string, error)); ok { + return rf(_a0) + } + if rf, ok := ret.Get(0).(func(virt.Context) []string); ok { + r0 = rf(_a0) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]string) + } + } + + if rf, ok := ret.Get(1).(func(virt.Context) error); ok { + r1 = rf(_a0) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + // ListSnapshot provides a mock function with given fields: ctx, guestID, volID func (_m *Manageable) ListSnapshot(ctx virt.Context, guestID string, volID string) (map[*models.Volume]models.Snapshots, error) { ret := _m.Called(ctx, guestID, volID) diff --git a/pkg/libvirt/libvirt.go b/pkg/libvirt/libvirt.go index bb67298..62170c9 100644 --- a/pkg/libvirt/libvirt.go +++ b/pkg/libvirt/libvirt.go @@ -26,11 +26,6 @@ func Connect(uri string) (l *Libvirtee, err error) { return } -// ListAllDomains lists all domains regardless the state. -func (l *Libvirtee) ListAllDomains() ([]libvirtgo.Domain, error) { - return l.Connect.ListAllDomains(ListAllDomainFlags) -} - // DefineDomain defines a new domain. func (l *Libvirtee) DefineDomain(xml string) (Domain, error) { raw, err := l.Connect.DomainDefineXML(xml) @@ -54,7 +49,7 @@ func (l *Libvirtee) LookupDomain(name string) (Domain, error) { // ListDomainsNames lists all domains' name. func (l *Libvirtee) ListDomainsNames() ([]string, error) { - raw, err := l.Connect.ListAllDomains(ListAllDomainFlags) + raw, err := l.ListAllDomains() if err != nil { return nil, errors.Trace(err) } @@ -68,3 +63,8 @@ func (l *Libvirtee) ListDomainsNames() ([]string, error) { return names, nil } + +// ListAllDomains lists all domains regardless the state. +func (l *Libvirtee) ListAllDomains() ([]libvirtgo.Domain, error) { + return l.Connect.ListAllDomains(ListAllDomainFlags) +}