-
Notifications
You must be signed in to change notification settings - Fork 13
/
tag_helpers.go
67 lines (54 loc) · 1.66 KB
/
tag_helpers.go
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
package ddcloud
import (
"fmt"
"github.com/DimensionDataResearch/go-dd-cloud-compute/compute"
"github.com/hashicorp/terraform/helper/schema"
"log"
)
// Create a set containing the names of all tag keys defined in CloudControl for the current user's organisation.
func getDefinedTagKeys(apiClient *compute.Client) (definedTagKeys *schema.Set, err error) {
definedTagKeys = &schema.Set{F: schema.HashString}
page := compute.DefaultPaging()
page.PageSize = 20
for {
tagKeys, err := apiClient.ListTagKeys(page)
if err != nil {
return nil, err
}
if tagKeys.IsEmpty() {
break
}
for _, tagKey := range tagKeys.Items {
definedTagKeys.Add(tagKey.Name)
}
page.Next()
}
return definedTagKeys, nil
}
// Given a set of tags, ensure that the corresponding tag keys are defined.
//
// The current user must have permissions to manage tags for their organisation.
func ensureTagKeysAreDefined(apiClient *compute.Client, configuredTags []compute.Tag) error {
definedTagKeys, err := getDefinedTagKeys(apiClient)
if err != nil {
return err
}
for _, configuredTag := range configuredTags {
if !definedTagKeys.Contains(configuredTag.Name) {
log.Printf("No tag key named '%s' is defined. Since the provider is configured to auto-create missing tag keys, this tag key will now be created.",
configuredTag.Name,
)
var tagKeyID string
tagKeyID, err = apiClient.CreateTagKey(configuredTag.Name,
fmt.Sprintf("Tag '%s'", configuredTag.Name),
false, // isValueRequired
false, // displayOnReports
)
if err != nil {
return err
}
log.Printf("Created new tag key '%s' with ID '%s'.", configuredTag.Name, tagKeyID)
}
}
return nil
}