File: resources.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 (134 lines) | stat: -rw-r--r-- 4,198 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package v1

import (
	"testing"
	"time"

	"github.com/gophercloud/gophercloud"
	"github.com/gophercloud/gophercloud/acceptance/tools"
	"github.com/gophercloud/utils/gnocchi/metric/v1/resources"
	"github.com/hashicorp/go-uuid"
)

// CreateGenericResource will create a Gnocchi resource with a generic type.
// An error will be returned if the resource could not be created.
func CreateGenericResource(t *testing.T, client *gophercloud.ServiceClient) (*resources.Resource, error) {
	id, err := uuid.GenerateUUID()
	if err != nil {
		return nil, err
	}

	randomDay := tools.RandomInt(1, 100)
	now := time.Now().UTC().AddDate(0, 0, -randomDay)
	metricName := tools.RandomString("TESTACCT-", 8)
	createOpts := resources.CreateOpts{
		ID:        id,
		StartedAt: &now,
		Metrics: map[string]interface{}{
			metricName: map[string]string{
				"archive_policy_name": "medium",
			},
		},
	}
	resourceType := "generic"
	t.Logf("Attempting to create a generic Gnocchi resource")

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

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

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

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

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

// CreateResourcesToBatchMeasures will create Gnocchi resources with metrics to test batch measures requests and
// return a map with references of resource IDs and metric names.
// An error will be returned if resources or metrics could not be created.
func CreateResourcesToBatchMeasures(t *testing.T, client *gophercloud.ServiceClient) (map[string][]string, error) {
	// Prepare metric names.
	firstMetricName := tools.RandomString("TESTACCT-", 8)
	secondMetricName := tools.RandomString("TESTACCT-", 8)
	thirdMetricName := tools.RandomString("TESTACCT-", 8)

	// Prepare the first resource.
	firstResourceID, err := uuid.GenerateUUID()
	if err != nil {
		return nil, err
	}
	firstRandomDay := tools.RandomInt(1, 100)
	firstStartTimestamp := time.Now().UTC().AddDate(0, 0, -firstRandomDay)
	firstResourceCreateOpts := resources.CreateOpts{
		ID:        firstResourceID,
		StartedAt: &firstStartTimestamp,
		Metrics: map[string]interface{}{
			firstMetricName: map[string]string{
				"archive_policy_name": "medium",
			},
			secondMetricName: map[string]string{
				"archive_policy_name": "low",
			},
		},
	}
	firstResourceType := "generic"

	t.Logf("Attempting to create a generic Gnocchi resource")
	firstResource, err := resources.Create(client, firstResourceType, firstResourceCreateOpts).Extract()
	if err != nil {
		return nil, err
	}

	t.Logf("Successfully created the generic Gnocchi resource.")
	tools.PrintResource(t, firstResource)

	// Prepare the second resource.
	secondResourceID, err := uuid.GenerateUUID()
	if err != nil {
		return nil, err
	}
	secondRandomDay := tools.RandomInt(1, 100)
	secondStartTimestamp := time.Now().UTC().AddDate(0, 0, -secondRandomDay)
	secondResourceCreateOpts := resources.CreateOpts{
		ID:        secondResourceID,
		StartedAt: &secondStartTimestamp,
		Metrics: map[string]interface{}{
			thirdMetricName: map[string]string{
				"archive_policy_name": "low",
			},
		},
	}
	secondResourceType := "generic"

	t.Logf("Attempting to create a generic Gnocchi resource")
	secondResource, err := resources.Create(client, secondResourceType, secondResourceCreateOpts).Extract()
	if err != nil {
		return nil, err
	}

	t.Logf("Successfully created the generic Gnocchi resource.")
	tools.PrintResource(t, secondResource)

	resourcesReferenceMap := map[string][]string{
		firstResource.ID: []string{
			firstMetricName,
			secondMetricName,
		},
		secondResource.ID: []string{
			thirdMetricName,
		},
	}
	return resourcesReferenceMap, nil
}