Skip to content

Commit

Permalink
Attribute refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jtopjian committed Jun 23, 2018
1 parent add8a4f commit 89b3bbb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 28 deletions.
31 changes: 12 additions & 19 deletions gnocchi/metric/v1/resourcetypes/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type GetResult struct {
// ResourceType represents custom Gnocchi resource type.
type ResourceType struct {
// Attributes is a collection of keys and values of different resource types.
Attributes []Attribute `json:"-"`
Attributes map[string]Attribute `json:"-"`

// Name is a human-readable resource type identifier.
Name string `json:"name"`
Expand All @@ -38,14 +38,11 @@ type ResourceType struct {

// Attribute represents single attribute of a Gnocchi resource type.
type Attribute struct {
// Name a human-readable attribute identifier.
Name string `json:"-"`

// Type is an attribute type.
Type string `json:"type"`

// Extra represents different attribute fields.
Extra map[string]interface{}
// Details represents different attribute fields.
Details map[string]interface{} `json:"-"`
}

// UnmarshalJSON helps to unmarshal ResourceType fields into needed values.
Expand All @@ -66,33 +63,29 @@ func (r *ResourceType) UnmarshalJSON(b []byte) error {
}

// Populate attributes from the JSON map structure.
attributes := make([]Attribute, len(s.Attributes))
idx := 0
attributes := make(map[string]Attribute)
for attributeName, attributeValues := range s.Attributes {
attributes[idx] = Attribute{
Name: attributeName,
}
attribute := new(Attribute)
attribute.Details = make(map[string]interface{})

var attributeValuesMap map[string]interface{}
var ok bool

if attributeValuesMap, ok = attributeValues.(map[string]interface{}); !ok {
attributeValuesMap, ok := attributeValues.(map[string]interface{})
if !ok {
// Got some strange resource type attribute representation, skip it.
continue
}

// Populate extra and type attribute values.
attributes[idx].Extra = make(map[string]interface{})
for k, v := range attributeValuesMap {
if k == "type" {
if attributeType, ok := v.(string); ok {
attributes[idx].Type = attributeType
attribute.Type = attributeType
}
} else {
attributes[idx].Extra[k] = v
attribute.Details[k] = v
}
}
idx++

attributes[attributeName] = *attribute
}

r.Attributes = attributes
Expand Down
16 changes: 7 additions & 9 deletions gnocchi/metric/v1/resourcetypes/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,17 @@ const ResourceTypeListResult = `[
var ResourceType1 = resourcetypes.ResourceType{
Name: "generic",
State: "active",
Attributes: []resourcetypes.Attribute{},
Attributes: map[string]resourcetypes.Attribute{},
}

// ResourceType2 is an expected representation of a first resource from the ResourceTypeListResult.
var ResourceType2 = resourcetypes.ResourceType{
Name: "identity_project",
State: "active",
Attributes: []resourcetypes.Attribute{
{
Name: "parent_id",
Attributes: map[string]resourcetypes.Attribute{
"parent_id": resourcetypes.Attribute{
Type: "uuid",
Extra: map[string]interface{}{
Details: map[string]interface{}{
"required": false,
},
},
Expand All @@ -59,11 +58,10 @@ var ResourceType2 = resourcetypes.ResourceType{
var ResourceType3 = resourcetypes.ResourceType{
Name: "compute_instance",
State: "active",
Attributes: []resourcetypes.Attribute{
{
Name: "host",
Attributes: map[string]resourcetypes.Attribute{
"host": resourcetypes.Attribute{
Type: "string",
Extra: map[string]interface{}{
Details: map[string]interface{}{
"max_length": float64(128),
"min_length": float64(0),
"required": true,
Expand Down

0 comments on commit 89b3bbb

Please sign in to comment.