Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implements GetGuestIDList svc #5

Merged
merged 1 commit into from
May 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
go install google.golang.org/grpc/cmd/[email protected]

lint: format
PATH=${HOME}/go/bin:${PATH} golangci-lint run --skip-dirs-use-default --skip-dirs=thirdparty
Expand Down
11 changes: 11 additions & 0 deletions internal/server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions internal/server/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion internal/virt/guest/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
16 changes: 16 additions & 0 deletions internal/virt/guest/guest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions internal/virt/guest/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions internal/virt/guest/manager/mocks/Manageable.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions pkg/libvirt/libvirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand All @@ -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)
}