Add methods to get attributes more easily in Go API (#2299)
* Add methods to get attributes more easily in Go API * Add type check
This commit is contained in:
1 parent
ab3fe75537
commit
a67085f663
14 files changed
+480
-24
No files matched your search
@@ -1,6 +1,37 @@
|
||||
// Code generated by "make api"; DO NOT EDIT.
|
||||
package hosts
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
type StaticHostAttributes struct {
|
||||
Address string `json:"address,omitempty"`
|
||||
}
|
||||
|
||||
func AttributesMapToStaticHostAttributes(in map[string]interface{}) (*StaticHostAttributes, error) {
|
||||
if in == nil {
|
||||
return nil, fmt.Errorf("nil input map")
|
||||
}
|
||||
var out StaticHostAttributes
|
||||
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
||||
Result: &out,
|
||||
TagName: "json",
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating mapstructure decoder: %w", err)
|
||||
}
|
||||
if err := dec.Decode(in); err != nil {
|
||||
return nil, fmt.Errorf("error decoding: %w", err)
|
||||
}
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
func (pt *Host) GetStaticHostAttributes() (*StaticHostAttributes, error) {
|
||||
if pt.Type != "static" {
|
||||
return nil, fmt.Errorf("asked to fetch %s-type attributes but host is of type %s", "static", pt.Type)
|
||||
}
|
||||
return AttributesMapToStaticHostAttributes(pt.Attributes)
|
||||
}
|
||||
Reference in new issue
Block a user