File: resourcetypes.go

package info (click to toggle)
golang-github-gophercloud-utils 0.0~git20200508.b0167b9-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 784 kB
  • sloc: sh: 20; makefile: 7
file content (58 lines) | stat: -rw-r--r-- 1,891 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package v1

import (
	"testing"

	"github.com/gophercloud/gophercloud"
	"github.com/gophercloud/gophercloud/acceptance/tools"
	"github.com/gophercloud/utils/gnocchi/metric/v1/resourcetypes"
)

// CreateResourceType creates Gnocchi resource type. An error will be returned if the
// resource type could not be created.
func CreateResourceType(t *testing.T, client *gophercloud.ServiceClient) (*resourcetypes.ResourceType, error) {
	resourceTypeName := tools.RandomString("TESTACCT-", 8)
	attributeStringName := tools.RandomString("TESTACCT-ATTRIBUTE-", 8)
	attributeUUIDName := tools.RandomString("TESTACCT-ATTRIBUTE-", 8)

	createOpts := resourcetypes.CreateOpts{
		Name: resourceTypeName,
		Attributes: map[string]resourcetypes.AttributeOpts{
			attributeStringName: resourcetypes.AttributeOpts{
				Type: "string",
				Details: map[string]interface{}{
					"max_length": 128,
					"required":   false,
				},
			},
			attributeUUIDName: resourcetypes.AttributeOpts{
				Type: "uuid",
				Details: map[string]interface{}{
					"required": true,
				},
			},
		},
	}
	t.Logf("Attempting to create a Gnocchi resource type")

	resourceType, err := resourcetypes.Create(client, createOpts).Extract()
	if err != nil {
		return nil, err
	}

	t.Logf("Successfully created the Gnocchi resource type.")
	return resourceType, nil
}

// DeleteResourceType deletes a Gnocchi resource type with the specified name.
// A fatal error will occur if the delete was not successful.
func DeleteResourceType(t *testing.T, client *gophercloud.ServiceClient, resourceTypeName string) {
	t.Logf("Attempting to delete the Gnocchi resource type: %s", resourceTypeName)

	err := resourcetypes.Delete(client, resourceTypeName).ExtractErr()
	if err != nil {
		t.Fatalf("Unable to delete the Gnocchi resource type %s: %v", resourceTypeName, err)
	}

	t.Logf("Deleted the Gnocchi resource type: %s", resourceTypeName)
}