@@ -22,13 +22,13 @@ import (
22
22
)
23
23
24
24
type TagIface interface {
25
- AddClusterTag (resourceType ResourceType , resourceID string , csCluster * infrav1.CloudStackCluster , addCreatedByCAPCTag bool ) error
26
- DeleteClusterTag (resourceType ResourceType , resourceID string , csCluster * infrav1.CloudStackCluster ) error
27
- DeleteCreatedByCAPCTag (resourceType ResourceType , resourceID string ) error
28
- DoClusterTagsAllowDisposal (resourceType ResourceType , resourceID string ) (bool , error )
29
- AddTags (resourceType ResourceType , resourceID string , tags map [string ]string ) error
30
- GetTags (resourceType ResourceType , resourceID string ) (map [string ]string , error )
31
- DeleteTags (resourceType ResourceType , resourceID string , tagsToDelete map [string ]string ) error
25
+ AddClusterTag (ResourceType , string , * infrav1.CloudStackCluster , bool ) error
26
+ DeleteClusterTag (ResourceType , string , * infrav1.CloudStackCluster ) error
27
+ DeleteCreatedByCAPCTag (ResourceType , string ) error
28
+ DoClusterTagsAllowDisposal (ResourceType , string ) (bool , error )
29
+ AddTags (ResourceType , string , map [string ]string ) error
30
+ GetTags (ResourceType , string ) (map [string ]string , error )
31
+ DeleteTags (ResourceType , string , map [string ]string ) error
32
32
}
33
33
34
34
type ResourceType string
@@ -40,6 +40,8 @@ const (
40
40
ResourceTypeIPAddress ResourceType = "PublicIpAddress"
41
41
)
42
42
43
+ // AddClusterTag adds cluster-related tags to a resource. One tag indicates that the resource is used by a given
44
+ // cluster. The other tag, if applied, indicates that CAPC created the resource and may dispose of it later.
43
45
func (c * client ) AddClusterTag (resourceType ResourceType , resourceID string , csCluster * infrav1.CloudStackCluster , addCreatedByCAPCTag bool ) error {
44
46
clusterTagName := generateClusterTagName (csCluster )
45
47
newTags := map [string ]string {}
@@ -64,6 +66,7 @@ func (c *client) AddClusterTag(resourceType ResourceType, resourceID string, csC
64
66
return nil
65
67
}
66
68
69
+ // DeleteClusterTag deletes the tag that associates the resource with a given cluster.
67
70
func (c * client ) DeleteClusterTag (resourceType ResourceType , resourceID string , csCluster * infrav1.CloudStackCluster ) error {
68
71
tags , err := c .GetTags (resourceType , resourceID )
69
72
if err != nil {
@@ -78,6 +81,8 @@ func (c *client) DeleteClusterTag(resourceType ResourceType, resourceID string,
78
81
return nil
79
82
}
80
83
84
+ // DeleteCreatedByCAPCTag deletes the tag that indicates that the resource was created by CAPC. This is useful when a
85
+ // resource is disassociated instead of deleted. That way the tag won't cause confusion if the resource is reused later.
81
86
func (c * client ) DeleteCreatedByCAPCTag (resourceType ResourceType , resourceID string ) error {
82
87
tags , err := c .GetTags (resourceType , resourceID )
83
88
if err != nil {
@@ -91,6 +96,8 @@ func (c *client) DeleteCreatedByCAPCTag(resourceType ResourceType, resourceID st
91
96
return nil
92
97
}
93
98
99
+ // DoClusterTagsAllowDisposal checks to see if the resource is in a state that makes it eligible for disposal. CAPC can
100
+ // dispose of a resource if the tags show it was created by CAPC and isn't being used by any clusters.
94
101
func (c * client ) DoClusterTagsAllowDisposal (resourceType ResourceType , resourceID string ) (bool , error ) {
95
102
tags , err := c .GetTags (resourceType , resourceID )
96
103
if err != nil {
@@ -107,12 +114,14 @@ func (c *client) DoClusterTagsAllowDisposal(resourceType ResourceType, resourceI
107
114
return clusterTagCount == 0 && tags [createdByCAPCTagName ] != "" , nil
108
115
}
109
116
117
+ // AddTags adds arbitrary tags to a resource.
110
118
func (c * client ) AddTags (resourceType ResourceType , resourceID string , tags map [string ]string ) error {
111
119
p := c .cs .Resourcetags .NewCreateTagsParams ([]string {resourceID }, string (resourceType ), tags )
112
120
_ , err := c .cs .Resourcetags .CreateTags (p )
113
121
return err
114
122
}
115
123
124
+ // GetTags gets all of a resource's tags.
116
125
func (c * client ) GetTags (resourceType ResourceType , resourceID string ) (map [string ]string , error ) {
117
126
p := c .cs .Resourcetags .NewListTagsParams ()
118
127
p .SetResourceid (resourceID )
@@ -128,6 +137,8 @@ func (c *client) GetTags(resourceType ResourceType, resourceID string) (map[stri
128
137
return tags , nil
129
138
}
130
139
140
+ // DeleteTags deletes the given tags from a resource. If the tags don't exist, or if the values don't match, it will
141
+ // result in an error.
131
142
func (c * client ) DeleteTags (resourceType ResourceType , resourceID string , tagsToDelete map [string ]string ) error {
132
143
p := c .cs .Resourcetags .NewDeleteTagsParams ([]string {resourceID }, string (resourceType ))
133
144
p .SetTags (tagsToDelete )
0 commit comments