mirror of
https://github.com/edgelesssys/constellation.git
synced 2024-10-01 01:36:09 -04:00
qemu-metadata-api: allow building without cgo dependencies for linting
This commit is contained in:
parent
15d51c3a3f
commit
78085cba68
@ -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 = [
|
||||
|
@ -1,3 +1,5 @@
|
||||
//go:build cgo
|
||||
|
||||
/*
|
||||
Copyright (c) Edgeless Systems GmbH
|
||||
|
||||
|
13
hack/qemu-metadata-api/main_cross.go
Normal file
13
hack/qemu-metadata-api/main_cross.go
Normal 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")
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
//go:build cgo
|
||||
|
||||
/*
|
||||
Copyright (c) Edgeless Systems GmbH
|
||||
|
||||
|
@ -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"],
|
||||
|
@ -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
|
||||
}
|
||||
|
56
hack/qemu-metadata-api/virtwrapper/virtwrapper_cgo.go
Normal file
56
hack/qemu-metadata-api/virtwrapper/virtwrapper_cgo.go
Normal 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
|
||||
}
|
33
hack/qemu-metadata-api/virtwrapper/virtwrapper_cross.go
Normal file
33
hack/qemu-metadata-api/virtwrapper/virtwrapper_cross.go
Normal 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() {}
|
Loading…
Reference in New Issue
Block a user