Skip to content

Commit

Permalink
Merge pull request #2613 from dtantsur/inventory-api
Browse files Browse the repository at this point in the history
Migrate baremetal inventory to a common location
  • Loading branch information
EmilienM committed Aug 12, 2023
2 parents 08f27f3 + e44eb43 commit 117d6fc
Show file tree
Hide file tree
Showing 6 changed files with 298 additions and 288 deletions.
126 changes: 126 additions & 0 deletions openstack/baremetal/inventory/testing/fixtures.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package testing

import "github.com/gophercloud/gophercloud/openstack/baremetal/inventory"

const InventorySample = `{
"bmc_address": "192.167.2.134",
"boot": {
"current_boot_mode": "bios",
"pxe_interface": "52:54:00:4e:3d:30"
},
"cpu": {
"architecture": "x86_64",
"count": 2,
"flags": [
"fpu",
"mmx",
"fxsr",
"sse",
"sse2"
],
"frequency": "2100.084"
},
"disks": [
{
"hctl": null,
"model": "",
"name": "/dev/vda",
"rotational": true,
"serial": null,
"size": 13958643712,
"vendor": "0x1af4",
"wwn": null,
"wwn_vendor_extension": null,
"wwn_with_extension": null
}
],
"hostname": "myawesomehost",
"interfaces": [
{
"client_id": null,
"has_carrier": true,
"ipv4_address": "172.24.42.101",
"mac_address": "52:54:00:47:20:4d",
"name": "eth1",
"product": "0x0001",
"vendor": "0x1af4"
},
{
"client_id": null,
"has_carrier": true,
"ipv4_address": "172.24.42.100",
"mac_address": "52:54:00:4e:3d:30",
"name": "eth0",
"product": "0x0001",
"vendor": "0x1af4",
"speed_mbps": 1000
}
],
"memory": {
"physical_mb": 2048,
"total": 2105864192
},
"system_vendor": {
"manufacturer": "Bochs",
"product_name": "Bochs",
"serial_number": "Not Specified",
"firmware": {
"version": "1.2.3.4"
}
}
}`

var Inventory = inventory.InventoryType{
SystemVendor: inventory.SystemVendorType{
Manufacturer: "Bochs",
ProductName: "Bochs",
SerialNumber: "Not Specified",
Firmware: inventory.SystemFirmwareType{
Version: "1.2.3.4",
},
},
BmcAddress: "192.167.2.134",
Boot: inventory.BootInfoType{
CurrentBootMode: "bios",
PXEInterface: "52:54:00:4e:3d:30",
},
CPU: inventory.CPUType{
Count: 2,
Flags: []string{"fpu", "mmx", "fxsr", "sse", "sse2"},
Frequency: "2100.084",
Architecture: "x86_64",
},
Disks: []inventory.RootDiskType{
{
Rotational: true,
Model: "",
Name: "/dev/vda",
Size: 13958643712,
Vendor: "0x1af4",
},
},
Interfaces: []inventory.InterfaceType{
{
Vendor: "0x1af4",
HasCarrier: true,
MACAddress: "52:54:00:47:20:4d",
Name: "eth1",
Product: "0x0001",
IPV4Address: "172.24.42.101",
},
{
IPV4Address: "172.24.42.100",
MACAddress: "52:54:00:4e:3d:30",
Name: "eth0",
Product: "0x0001",
HasCarrier: true,
Vendor: "0x1af4",
SpeedMbps: 1000,
},
},
Memory: inventory.MemoryType{
PhysicalMb: 2048.0,
Total: 2.105864192e+09,
},
Hostname: "myawesomehost",
}
40 changes: 40 additions & 0 deletions openstack/baremetal/inventory/testing/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package testing

import (
"encoding/json"
"strings"
"testing"

"github.com/gophercloud/gophercloud/openstack/baremetal/inventory"
th "github.com/gophercloud/gophercloud/testhelper"
)

func TestIntrospectionNUMA(t *testing.T) {
var output inventory.InventoryType
err := json.Unmarshal([]byte(InventorySample), &output)
if err != nil {
t.Fatalf("Failed to unmarshal inventory: %s", err)
}

th.CheckDeepEquals(t, Inventory, output)
}

func TestLLDPTLVErrors(t *testing.T) {
badInputs := []string{
"[1]",
"[1, 2]",
"[\"foo\", \"bar\"]",
}

for _, input := range badInputs {
var output inventory.LLDPTLVType
err := json.Unmarshal([]byte(input), &output)
if err == nil {
t.Fatalf("No JSON parse error for invalid LLDP TLV %s", input)
}

if !strings.Contains(err.Error(), "LLDP TLV") {
t.Fatalf("Unexpected JSON parse error \"%s\" for invalid LLDP TLV %s", err, input)
}
}
}
106 changes: 106 additions & 0 deletions openstack/baremetal/inventory/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package inventory

import (
"encoding/json"
"fmt"
)

type BootInfoType struct {
CurrentBootMode string `json:"current_boot_mode"`
PXEInterface string `json:"pxe_interface"`
}

type CPUType struct {
Architecture string `json:"architecture"`
Count int `json:"count"`
Flags []string `json:"flags"`
Frequency string `json:"frequency"`
ModelName string `json:"model_name"`
}

type InterfaceType struct {
BIOSDevName string `json:"biosdevname"`
ClientID string `json:"client_id"`
HasCarrier bool `json:"has_carrier"`
IPV4Address string `json:"ipv4_address"`
IPV6Address string `json:"ipv6_address"`
MACAddress string `json:"mac_address"`
Name string `json:"name"`
Product string `json:"product"`
SpeedMbps int `json:"speed_mbps"`
Vendor string `json:"vendor"`
}

type LLDPTLVType struct {
Type int
Value string
}

type MemoryType struct {
PhysicalMb int `json:"physical_mb"`
Total int `json:"total"`
}

type RootDiskType struct {
Hctl string `json:"hctl"`
Model string `json:"model"`
Name string `json:"name"`
ByPath string `json:"by_path"`
Rotational bool `json:"rotational"`
Serial string `json:"serial"`
Size int64 `json:"size"`
Vendor string `json:"vendor"`
Wwn string `json:"wwn"`
WwnVendorExtension string `json:"wwn_vendor_extension"`
WwnWithExtension string `json:"wwn_with_extension"`
}

type SystemFirmwareType struct {
Version string `json:"version"`
BuildDate string `json:"build_date"`
Vendor string `json:"vendor"`
}

type SystemVendorType struct {
Manufacturer string `json:"manufacturer"`
ProductName string `json:"product_name"`
SerialNumber string `json:"serial_number"`
Firmware SystemFirmwareType `json:"firmware"`
}

type InventoryType struct {
BmcAddress string `json:"bmc_address"`
Boot BootInfoType `json:"boot"`
CPU CPUType `json:"cpu"`
Disks []RootDiskType `json:"disks"`
Interfaces []InterfaceType `json:"interfaces"`
Memory MemoryType `json:"memory"`
SystemVendor SystemVendorType `json:"system_vendor"`
Hostname string `json:"hostname"`
}

// UnmarshalJSON interprets an LLDP TLV [key, value] pair as an LLDPTLVType structure
func (r *LLDPTLVType) UnmarshalJSON(data []byte) error {
var list []interface{}
if err := json.Unmarshal(data, &list); err != nil {
return err
}

if len(list) != 2 {
return fmt.Errorf("Invalid LLDP TLV key-value pair")
}

fieldtype, ok := list[0].(float64)
if !ok {
return fmt.Errorf("LLDP TLV key is not number")
}

value, ok := list[1].(string)
if !ok {
return fmt.Errorf("LLDP TLV value is not string")
}

r.Type = int(fieldtype)
r.Value = value
return nil
}

0 comments on commit 117d6fc

Please sign in to comment.