@@ -39,8 +39,8 @@ type TagIface interface {
39
39
type ResourceType string
40
40
41
41
const (
42
- clusterTagNamePrefix = "CAPC_cluster_"
43
- createdByCAPCTagName = "created_by_CAPC"
42
+ ClusterTagNamePrefix = "CAPC_cluster_"
43
+ CreatedByCAPCTagName = "created_by_CAPC"
44
44
ResourceTypeNetwork ResourceType = "Network"
45
45
ResourceTypeIPAddress ResourceType = "PublicIpAddress"
46
46
)
@@ -54,28 +54,47 @@ func ignoreAlreadyPresentErrors(err error, rType ResourceType, rID string) error
54
54
return nil
55
55
}
56
56
57
+ func (c * client ) IsCapcManaged (resourceType ResourceType , resourceID string ) (bool , error ) {
58
+ tags , err := c .GetTags (resourceType , resourceID )
59
+ if err != nil {
60
+ return false , errors .Wrapf (err ,
61
+ "error encountered while checking if %s with ID: %s is tagged as CAPC managed" , resourceType , resourceID )
62
+ }
63
+ _ , CreatedByCAPC := tags [CreatedByCAPCTagName ]
64
+ return CreatedByCAPC , nil
65
+ }
66
+
57
67
// AddClusterTag adds cluster tag to a resource. This tag indicates the resource is used by a given the cluster.
58
68
func (c * client ) AddClusterTag (rType ResourceType , rID string , csCluster * infrav1.CloudStackCluster ) error {
59
- clusterTagName := generateClusterTagName (csCluster )
60
- return c .AddTags (rType , rID , map [string ]string {clusterTagName : "1" })
69
+ if managedByCAPC , err := c .IsCapcManaged (rType , rID ); err != nil {
70
+ return err
71
+ } else if managedByCAPC {
72
+ ClusterTagName := generateClusterTagName (csCluster )
73
+ return c .AddTags (rType , rID , map [string ]string {ClusterTagName : "1" })
74
+ }
75
+ return nil
61
76
}
62
77
63
78
// DeleteClusterTag deletes the tag that associates the resource with a given cluster.
64
79
func (c * client ) DeleteClusterTag (rType ResourceType , rID string , csCluster * infrav1.CloudStackCluster ) error {
65
- clusterTagName := generateClusterTagName (csCluster )
66
- return c .DeleteTags (rType , rID , map [string ]string {clusterTagName : "1" })
80
+ if managedByCAPC , err := c .IsCapcManaged (rType , rID ); err != nil {
81
+ return err
82
+ } else if managedByCAPC {
83
+ ClusterTagName := generateClusterTagName (csCluster )
84
+ return c .DeleteTags (rType , rID , map [string ]string {ClusterTagName : "1" })
85
+ }
86
+ return nil
67
87
}
68
88
69
- // AddCreatedByCAPCTag deletes the tag that indicates that the resource was created by CAPC. This is useful when a
70
- // resource is disassociated instead of deleted. That way the tag won't cause confusion if the resource is reused later .
89
+ // AddCreatedByCAPCTag adds the tag that indicates that the resource was created by CAPC.
90
+ // This is useful when a resource is disassociated but not deleted .
71
91
func (c * client ) AddCreatedByCAPCTag (rType ResourceType , rID string ) error {
72
- return c .AddTags (rType , rID , map [string ]string {createdByCAPCTagName : "1" })
92
+ return c .AddTags (rType , rID , map [string ]string {CreatedByCAPCTagName : "1" })
73
93
}
74
94
75
- // DeleteCreatedByCAPCTag deletes the tag that indicates that the resource was created by CAPC. This is useful when a
76
- // resource is disassociated instead of deleted. That way the tag won't cause confusion if the resource is reused later.
95
+ // DeleteCreatedByCAPCTag deletes the tag that indicates that the resource was created by CAPC.
77
96
func (c * client ) DeleteCreatedByCAPCTag (rType ResourceType , rID string ) error {
78
- return c .DeleteTags (rType , rID , map [string ]string {createdByCAPCTagName : "1" })
97
+ return c .DeleteTags (rType , rID , map [string ]string {CreatedByCAPCTagName : "1" })
79
98
}
80
99
81
100
// DoClusterTagsAllowDisposal checks to see if the resource is in a state that makes it eligible for disposal. CAPC can
@@ -88,12 +107,12 @@ func (c *client) DoClusterTagsAllowDisposal(resourceType ResourceType, resourceI
88
107
89
108
var clusterTagCount int
90
109
for tagName := range tags {
91
- if strings .HasPrefix (tagName , clusterTagNamePrefix ) {
110
+ if strings .HasPrefix (tagName , ClusterTagNamePrefix ) {
92
111
clusterTagCount ++
93
112
}
94
113
}
95
114
96
- return clusterTagCount == 0 && tags [createdByCAPCTagName ] != "" , nil
115
+ return clusterTagCount == 0 && tags [CreatedByCAPCTagName ] != "" , nil
97
116
}
98
117
99
118
// AddTags adds arbitrary tags to a resource.
@@ -140,5 +159,5 @@ func (c *client) DeleteTags(resourceType ResourceType, resourceID string, tagsTo
140
159
}
141
160
142
161
func generateClusterTagName (csCluster * infrav1.CloudStackCluster ) string {
143
- return clusterTagNamePrefix + string (csCluster .UID )
162
+ return ClusterTagNamePrefix + string (csCluster .UID )
144
163
}
0 commit comments