From d4c1c87a31f8ae984e8a6a3202b183c655acf321 Mon Sep 17 00:00:00 2001 From: Mark Laing Date: Thu, 26 May 2022 13:38:13 +0100 Subject: [PATCH] machine: Adds and implements HardwareInfo on Machine interface. Adds methods for retrieval of hardware info from the MAAS API response for a machine. Signed-off-by: Mark Laing --- interfaces.go | 1 + machine.go | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/interfaces.go b/interfaces.go index 2bfb0ed..12c1afb 100644 --- a/interfaces.go +++ b/interfaces.go @@ -217,6 +217,7 @@ type Machine interface { Architecture() string Memory() int CPUCount() int + HardwareInfo() map[string]string IPAddresses() []string PowerState() string diff --git a/machine.go b/machine.go index d0b3bef..efaec8b 100644 --- a/machine.go +++ b/machine.go @@ -29,6 +29,7 @@ type machine struct { architecture string memory int cpuCount int + hardwareInfo map[string]string ipAddresses []string powerState string @@ -56,6 +57,7 @@ func (m *machine) updateFrom(other *machine) { m.architecture = other.architecture m.memory = other.memory m.cpuCount = other.cpuCount + m.hardwareInfo = other.hardwareInfo m.ipAddresses = other.ipAddresses m.powerState = other.powerState m.statusName = other.statusName @@ -109,6 +111,16 @@ func (m *machine) CPUCount() int { return m.cpuCount } +// HardwareInfo implements Machine. +func (m *machine) HardwareInfo() map[string]string { + info := make(map[string]string, len(m.hardwareInfo)) + for k, v := range m.hardwareInfo { + info[k] = v + } + + return info +} + // PowerState implements Machine. func (m *machine) PowerState() string { return m.powerState @@ -513,6 +525,7 @@ func machine_2_0(source map[string]interface{}) (*machine, error) { "architecture": schema.OneOf(schema.Nil(""), schema.String()), "memory": schema.ForceInt(), "cpu_count": schema.ForceInt(), + "hardware_info": schema.StringMap(schema.String()), "ip_addresses": schema.List(schema.String()), "power_state": schema.String(), @@ -574,6 +587,13 @@ func machine_2_0(source map[string]interface{}) (*machine, error) { if err != nil { return nil, errors.Trace(err) } + + validHardwareInfo := valid["hardware_info"].(map[string]interface{}) + hardwareInfo := make(map[string]string, len(validHardwareInfo)) + for k, v := range validHardwareInfo { + hardwareInfo[k] = v.(string) + } + architecture, _ := valid["architecture"].(string) statusMessage, _ := valid["status_message"].(string) result := &machine{ @@ -590,6 +610,7 @@ func machine_2_0(source map[string]interface{}) (*machine, error) { architecture: architecture, memory: valid["memory"].(int), cpuCount: valid["cpu_count"].(int), + hardwareInfo: hardwareInfo, ipAddresses: convertToStringSlice(valid["ip_addresses"]), powerState: valid["power_state"].(string),