Skip to content

Commit

Permalink
machine: Adds and implements HardwareInfo on Machine interface.
Browse files Browse the repository at this point in the history
Adds methods for retrieval of hardware info from the MAAS API response
for a machine.

Signed-off-by: Mark Laing <mark.laing@canonical.com>
  • Loading branch information
markylaing committed May 26, 2022
1 parent e51edec commit d4c1c87
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ type Machine interface {
Architecture() string
Memory() int
CPUCount() int
HardwareInfo() map[string]string

IPAddresses() []string
PowerState() string
Expand Down
21 changes: 21 additions & 0 deletions machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type machine struct {
architecture string
memory int
cpuCount int
hardwareInfo map[string]string

ipAddresses []string
powerState string
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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{
Expand All @@ -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),
Expand Down

0 comments on commit d4c1c87

Please sign in to comment.