-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from markylaing/tags-hardware-info
#96 build build.properties go-path go1.17.11.linux-amd64.tar.gz goversion pr_props Adds a new interface `Tag` (implemented by `tag` in `tags.go`) for interacting with MAAS tags. * Adds a method `Tags() ([]Tag, error)` to the `Controller` for retrieving a slice of MAAS tags. * Adds `Tags` to `MachineArgs` to allow filtering machines by tag. * Adds a method `HardwareInfo() map[string]string` to the `Machine` interface and implements it. Hardware info is added to `machine` when deserializing the API response. I have tried to follow conventions that I have found elsewhere in the codebase but let me know if I have missed something, thanks 😄
- Loading branch information
Showing
6 changed files
with
299 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Copyright 2022 Canonical Ltd. | ||
// Licensed under the LGPLv3, see LICENCE file for details. | ||
|
||
package gomaasapi | ||
|
||
import ( | ||
"github.com/juju/errors" | ||
"github.com/juju/schema" | ||
"github.com/juju/version" | ||
) | ||
|
||
type tag struct { | ||
resourceURI string | ||
|
||
name string | ||
comment string | ||
definition string | ||
kernelOpts string | ||
} | ||
|
||
func (tag tag) Name() string { | ||
return tag.name | ||
} | ||
|
||
func (tag tag) Comment() string { | ||
return tag.comment | ||
} | ||
|
||
func (tag tag) Definition() string { | ||
return tag.definition | ||
} | ||
|
||
func (tag tag) KernelOpts() string { | ||
return tag.kernelOpts | ||
} | ||
|
||
func readTags(controllerVersion version.Number, source interface{}) ([]*tag, error) { | ||
readFunc, err := getTagDeserializationFunc(controllerVersion) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
|
||
checker := schema.List(schema.StringMap(schema.Any())) | ||
coerced, err := checker.Coerce(source, nil) | ||
if err != nil { | ||
return nil, WrapWithDeserializationError(err, "machine base schema check failed") | ||
} | ||
|
||
// OK to do a direct cast here because we just coerced the interface. | ||
valid := coerced.([]interface{}) | ||
return readTagList(valid, readFunc) | ||
} | ||
|
||
func readTagList(sourceList []interface{}, readFunc tagDeserializationFunc) ([]*tag, error) { | ||
result := make([]*tag, 0, len(sourceList)) | ||
for i, value := range sourceList { | ||
source, ok := value.(map[string]interface{}) | ||
if !ok { | ||
return nil, NewDeserializationError("unexpected value for tag %d, %T", i, value) | ||
} | ||
|
||
machine, err := readFunc(source) | ||
if err != nil { | ||
return nil, errors.Annotatef(err, "tag %d", i) | ||
} | ||
|
||
result = append(result, machine) | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func getTagDeserializationFunc(controllerVersion version.Number) (tagDeserializationFunc, error) { | ||
var deserialisationVersion version.Number | ||
for v := range tagDeserializationFuncs { | ||
if v.Compare(deserialisationVersion) > 0 && v.Compare(controllerVersion) <= 0 { | ||
deserialisationVersion = v | ||
} | ||
} | ||
|
||
if deserialisationVersion == version.Zero { | ||
return nil, NewUnsupportedVersionError("no tag read func for version %s", controllerVersion) | ||
} | ||
|
||
return tagDeserializationFuncs[deserialisationVersion], nil | ||
} | ||
|
||
type tagDeserializationFunc func(map[string]interface{}) (*tag, error) | ||
|
||
var tagDeserializationFuncs = map[version.Number]tagDeserializationFunc{ | ||
twoDotOh: tag_2_0, | ||
} | ||
|
||
func tag_2_0(source map[string]interface{}) (*tag, error) { | ||
fields := schema.Fields{ | ||
"resource_uri": schema.String(), | ||
"name": schema.String(), | ||
"comment": schema.String(), | ||
"definition": schema.String(), | ||
"kernel_opts": schema.String(), | ||
} | ||
|
||
defaults := schema.Defaults{ | ||
"comment": "", | ||
"definition": "", | ||
"kernel_opts": "", | ||
} | ||
|
||
checker := schema.FieldMap(fields, defaults) | ||
|
||
coerced, err := checker.Coerce(source, nil) | ||
if err != nil { | ||
return nil, WrapWithDeserializationError(err, "tag 2.0 schema check failed") | ||
} | ||
|
||
valid := coerced.(map[string]interface{}) | ||
|
||
return &tag{ | ||
resourceURI: valid["resource_uri"].(string), | ||
name: valid["name"].(string), | ||
comment: valid["comment"].(string), | ||
definition: valid["definition"].(string), | ||
kernelOpts: valid["kernel_opts"].(string), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package gomaasapi | ||
|
||
import ( | ||
"github.com/juju/testing" | ||
jc "github.com/juju/testing/checkers" | ||
gc "gopkg.in/check.v1" | ||
) | ||
|
||
type tagSuite struct { | ||
testing.LoggingCleanupSuite | ||
} | ||
|
||
var _ = gc.Suite(&tagSuite{}) | ||
|
||
func (*tagSuite) TestReadTags(c *gc.C) { | ||
tags, err := readTags(twoDotOh, parseJSON(c, tagsResponse)) | ||
c.Assert(err, jc.ErrorIsNil) | ||
c.Assert(tags, gc.HasLen, 3) | ||
|
||
tag := tags[0] | ||
|
||
c.Check(tag.Name(), gc.Equals, "virtual") | ||
c.Check(tag.Comment(), gc.Equals, "virtual machines") | ||
c.Check(tag.Definition(), gc.Equals, "tag for machines that are virtual") | ||
c.Check(tag.KernelOpts(), gc.Equals, "nvme_core") | ||
} | ||
|
||
var tagsResponse = `[ | ||
{ | ||
"resource_uri": "/2.0/tags/virtual", | ||
"name": "virtual", | ||
"comment": "virtual machines", | ||
"definition": "tag for machines that are virtual", | ||
"kernel_opts": "nvme_core" | ||
}, | ||
{ | ||
"resource_uri": "/2.0/tags/physical", | ||
"name": "physical", | ||
"comment": "physical machines", | ||
"definition": "tag for machines that are physical", | ||
"kernel_opts": "" | ||
}, | ||
{ | ||
"resource_uri": "/2.0/tags/r-pi", | ||
"name": "r-pi", | ||
"comment": "raspberry pis'", | ||
"definition": "tag for machines that are raspberry pis", | ||
"kernel_opts": "" | ||
} | ||
]` |