qemu-metadata-api: allow building without cgo dependencies for linting

This commit is contained in:
Malte Poll 2023-05-17 17:34:29 +02:00 committed by Malte Poll
parent 15d51c3a3f
commit 78085cba68
8 changed files with 119 additions and 36 deletions

View File

@ -5,7 +5,10 @@ load("//bazel/go:platform.bzl", "platform_binary")
go_library(
name = "qemu-metadata-api_lib",
srcs = ["main.go"],
srcs = [
"main.go",
"main_cross.go",
],
importpath = "github.com/edgelesssys/constellation/v2/hack/qemu-metadata-api",
visibility = ["//visibility:private"],
deps = [

View File

@ -1,3 +1,5 @@
//go:build cgo
/*
Copyright (c) Edgeless Systems GmbH

View File

@ -0,0 +1,13 @@
//go:build !cgo
/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
package main
func main() {
panic("CGO disabled but started qemu-metadata-api")
}

View File

@ -1,3 +1,5 @@
//go:build cgo
/*
Copyright (c) Edgeless Systems GmbH

View File

@ -2,7 +2,11 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "virtwrapper",
srcs = ["virtwrapper.go"],
srcs = [
"virtwrapper.go",
"virtwrapper_cgo.go",
"virtwrapper_cross.go",
],
importpath = "github.com/edgelesssys/constellation/v2/hack/qemu-metadata-api/virtwrapper",
visibility = ["//visibility:public"],
deps = ["@org_libvirt_go_libvirt//:libvirt"],

View File

@ -6,38 +6,8 @@ SPDX-License-Identifier: AGPL-3.0-only
package virtwrapper
import "libvirt.org/go/libvirt"
// Connect wraps a libvirt connection.
type Connect struct {
Conn *libvirt.Connect
}
// LookupNetworkByName looks up a network by name.
func (c *Connect) LookupNetworkByName(name string) (*Network, error) {
net, err := c.Conn.LookupNetworkByName(name)
if err != nil {
return nil, err
}
return &Network{Net: net}, nil
}
// Network wraps a libvirt network.
type Network struct {
Net virNetwork
}
// GetDHCPLeases returns the underlying DHCP leases.
func (n *Network) GetDHCPLeases() ([]libvirt.NetworkDHCPLease, error) {
return n.Net.GetDHCPLeases()
}
// Free the network resource.
func (n *Network) Free() {
_ = n.Net.Free()
}
type virNetwork interface {
GetDHCPLeases() ([]libvirt.NetworkDHCPLease, error)
Free() error
// NetworkDHCPLease abstracts a libvirt DHCP lease.
type NetworkDHCPLease struct {
IPaddr string
Hostname string
}

View File

@ -0,0 +1,56 @@
//go:build cgo
/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
package virtwrapper
import "libvirt.org/go/libvirt"
// Connect wraps a libvirt connection.
type Connect struct {
Conn *libvirt.Connect
}
// LookupNetworkByName looks up a network by name.
func (c *Connect) LookupNetworkByName(name string) (*Network, error) {
net, err := c.Conn.LookupNetworkByName(name)
if err != nil {
return nil, err
}
return &Network{Net: net}, nil
}
// Network wraps a libvirt network.
type Network struct {
Net virNetwork
}
// GetDHCPLeases returns the underlying DHCP leases.
func (n *Network) GetDHCPLeases() ([]NetworkDHCPLease, error) {
leases, err := n.Net.GetDHCPLeases()
if err != nil {
return nil, err
}
ret := make([]NetworkDHCPLease, len(leases))
for i, l := range leases {
ret[i] = NetworkDHCPLease{
IPaddr: l.IPaddr,
Hostname: l.Hostname,
}
}
return ret, nil
}
// Free the network resource.
func (n *Network) Free() {
_ = n.Net.Free()
}
type virNetwork interface {
GetDHCPLeases() ([]libvirt.NetworkDHCPLease, error)
Free() error
}

View File

@ -0,0 +1,33 @@
//go:build !cgo
/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
package virtwrapper
import "errors"
// Connect wraps a libvirt connection.
type Connect struct{}
// LookupNetworkByName looks up a network by name.
// This function errors if CGO is disabled.
func (c *Connect) LookupNetworkByName(_ string) (*Network, error) {
return nil, errors.New("using virtwrapper requires building with CGO")
}
// Network wraps a libvirt network.
type Network struct{}
// GetDHCPLeases returns the underlying DHCP leases.
// This function errors if CGO is disabled.
func (n *Network) GetDHCPLeases() ([]NetworkDHCPLease, error) {
return nil, errors.New("using virtwrapper requires building with CGO")
}
// Free the network resource.
// This function does nothing if CGO is disabled.
func (n *Network) Free() {}