| 1 |
//go:build acceptance || metric || resourcetypes |
| 2 |
// +build acceptance metric resourcetypes |
| 3 |
|
| 4 |
package v1 |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/gophercloud/gophercloud/acceptance/tools" |
| 10 |
"github.com/gophercloud/utils/acceptance/clients" |
| 11 |
"github.com/gophercloud/utils/gnocchi/metric/v1/resourcetypes" |
| 12 |
) |
| 13 |
|
| 14 |
func TestResourceTypesList(t *testing.T) { |
| 15 |
client, err := clients.NewGnocchiV1Client() |
| 16 |
if err != nil { |
| 17 |
t.Fatalf("Unable to create a Gnocchi client: %v", err) |
| 18 |
} |
| 19 |
|
| 20 |
allPages, err := resourcetypes.List(client).AllPages() |
| 21 |
if err != nil { |
| 22 |
t.Fatalf("Unable to list resource types: %v", err) |
| 23 |
} |
| 24 |
|
| 25 |
allResourceTypes, err := resourcetypes.ExtractResourceTypes(allPages) |
| 26 |
if err != nil { |
| 27 |
t.Fatalf("Unable to extract resource types: %v", err) |
| 28 |
} |
| 29 |
|
| 30 |
for _, resourceType := range allResourceTypes { |
| 31 |
tools.PrintResource(t, resourceType) |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
func TestResourceTypesCRUD(t *testing.T) { |
| 36 |
client, err := clients.NewGnocchiV1Client() |
| 37 |
if err != nil { |
| 38 |
t.Fatalf("Unable to create a Gnocchi client: %v", err) |
| 39 |
} |
| 40 |
|
| 41 |
resourceType, err := CreateResourceType(t, client) |
| 42 |
if err != nil { |
| 43 |
t.Fatalf("Unable to create a Gnocchi resource type: %v", err) |
| 44 |
} |
| 45 |
defer DeleteResourceType(t, client, resourceType.Name) |
| 46 |
|
| 47 |
tools.PrintResource(t, resourceType) |
| 48 |
|
| 49 |
// Populate attributes that will be deleted. |
| 50 |
attributesToDelete := []string{} |
| 51 |
for attributeName := range resourceType.Attributes { |
| 52 |
attributesToDelete = append(attributesToDelete, attributeName) |
| 53 |
} |
| 54 |
|
| 55 |
// New string attribute parameters. |
| 56 |
newStringAttributeOpts := resourcetypes.AttributeOpts{ |
| 57 |
Details: map[string]interface{}{ |
| 58 |
"required": false, |
| 59 |
"min_length": 32, |
| 60 |
"max_length": 64, |
| 61 |
}, |
| 62 |
Type: "string", |
| 63 |
} |
| 64 |
newStringAttributeOptsName := tools.RandomString("TESTACCT-ATTRIBUTE-", 8) |
| 65 |
|
| 66 |
// New datetime attribute parameters. |
| 67 |
newDatetimeAttributeOpts := resourcetypes.AttributeOpts{ |
| 68 |
Details: map[string]interface{}{ |
| 69 |
"required": true, |
| 70 |
"options": map[string]interface{}{ |
| 71 |
"fill": "2018-07-28T00:01:01Z", |
| 72 |
}, |
| 73 |
}, |
| 74 |
Type: "datetime", |
| 75 |
} |
| 76 |
newDatetimeAttributeOptsName := tools.RandomString("TESTACCT-ATTRIBUTE-", 8) |
| 77 |
|
| 78 |
// Initial options for the Update request with the new resource type attributes. |
| 79 |
updateOpts := resourcetypes.UpdateOpts{ |
| 80 |
Attributes: []resourcetypes.AttributeUpdateOpts{ |
| 81 |
{ |
| 82 |
Name: newStringAttributeOptsName, |
| 83 |
Operation: resourcetypes.AttributeAdd, |
| 84 |
Value: &newStringAttributeOpts, |
| 85 |
}, |
| 86 |
{ |
| 87 |
Name: newDatetimeAttributeOptsName, |
| 88 |
Operation: resourcetypes.AttributeAdd, |
| 89 |
Value: &newDatetimeAttributeOpts, |
| 90 |
}, |
| 91 |
}, |
| 92 |
} |
| 93 |
|
| 94 |
// Add options to delete attributes. |
| 95 |
for _, attributeToDelete := range attributesToDelete { |
| 96 |
updateOpts.Attributes = append(updateOpts.Attributes, resourcetypes.AttributeUpdateOpts{ |
| 97 |
Name: attributeToDelete, |
| 98 |
Operation: resourcetypes.AttributeRemove, |
| 99 |
}) |
| 100 |
} |
| 101 |
|
| 102 |
t.Logf("Attempting to update a Gnocchi resource type \"%s\".", resourceType.Name) |
| 103 |
newResourceType, err := resourcetypes.Update(client, resourceType.Name, updateOpts).Extract() |
| 104 |
if err != nil { |
| 105 |
t.Fatalf("Unable to update a Gnocchi resource type: %v", err) |
| 106 |
} |
| 107 |
t.Logf("Successfully updated the Gnocchi resource type \"%s\".", resourceType.Name) |
| 108 |
|
| 109 |
tools.PrintResource(t, newResourceType) |
| 110 |
} |