@@ -10,15 +10,11 @@ import (
1010
1111 "github.com/googleapis/gax-go/v2/apierror"
1212 "github.com/sirupsen/logrus"
13- tags "google.golang.org/api/cloudresourcemanager/v3"
14- "google.golang.org/api/option"
1513 "k8s.io/apimachinery/pkg/util/sets"
1614
1715 "github.com/openshift/installer/pkg/types/gcp"
1816)
1917
20- //go:generate mockgen -source=./usertags.go -destination=./mock/usertags_mock.go -package=mock
21-
2218const (
2319 // maxUserTagLimit is the maximum userTags that can be configured as defined in openshift/api.
2420 // https://github.com/openshift/api/commit/ae73a19d05c35068af16c9aeff375d0b7c936a8a#diff-07b264a49084976b670fb699badaca1795027d6ea732a99226a5388104f6174fR604-R613
@@ -39,17 +35,11 @@ type processedUserTags struct {
3935 sync.Mutex
4036}
4137
42- // tagManager handles resource tagging.
43- type tagManager struct {
38+ // TagManager handles resource tagging.
39+ type TagManager struct {
4440 client API
4541}
4642
47- // TagManager is the interface that wraps methods for resource tag operations.
48- type TagManager interface {
49- GetProjectTags (ctx context.Context , projectID string ) (sets.Set [string ], error )
50- GetNamespacedTagValue (ctx context.Context , tagNamespacedName string ) (* tags.TagValue , error )
51- }
52-
5343// newProcessedUserTags is for initializing an instance of processedUserTags.
5444func newProcessedUserTags () * processedUserTags {
5545 return & processedUserTags {}
@@ -100,16 +90,16 @@ func (p *processedUserTags) copy() map[string]string {
10090 return t
10191}
10292
103- // NewTagManager creates a tagManager instance.
104- func NewTagManager (client API ) TagManager {
105- return & tagManager {client : client }
93+ // NewTagManager creates a TagManager instance.
94+ func NewTagManager (client API ) * TagManager {
95+ return & TagManager {client : client }
10696}
10797
10898// GetUserTags returns the processed list of user provided tags if already available,
10999// else validates, persists in-memory and returns the processed tags.
110- func GetUserTags (ctx context.Context , mgr TagManager , projectID string , userTags []gcp.UserTag ) (map [string ]string , error ) {
100+ func ( t * TagManager ) GetUserTags (ctx context.Context , projectID string , userTags []gcp.UserTag ) (map [string ]string , error ) {
111101 if ! processedTags .isProcessed () {
112- if err := validateAndPersistUserTags (ctx , mgr , projectID , userTags ); err != nil {
102+ if err := t . validateAndPersistUserTags (ctx , projectID , userTags ); err != nil {
113103 return nil , err
114104 }
115105 }
@@ -123,7 +113,7 @@ func GetUserTags(ctx context.Context, mgr TagManager, projectID string, userTags
123113// with key of the form `tagKeys/{tag_key_id}` and value of the form
124114// `tagValues/{tag_value_id}`. Returns error when fetching a tag fails or when
125115// tag already exists on the project resource.
126- func validateAndPersistUserTags (ctx context.Context , mgr TagManager , project string , userTags []gcp.UserTag ) error {
116+ func ( t * TagManager ) validateAndPersistUserTags (ctx context.Context , project string , userTags []gcp.UserTag ) error {
127117 if len (userTags ) == 0 {
128118 return nil
129119 }
@@ -135,7 +125,7 @@ func validateAndPersistUserTags(ctx context.Context, mgr TagManager, project str
135125 return fmt .Errorf ("more than %d user tags is not allowed, configured count: %d" , maxUserTagLimit , len (userTags ))
136126 }
137127
138- projectTags , err := mgr .GetProjectTags (ctx , project )
128+ projectTags , err := t . client .GetProjectTags (ctx , project )
139129 if err != nil {
140130 return err
141131 }
@@ -148,7 +138,7 @@ func validateAndPersistUserTags(ctx context.Context, mgr TagManager, project str
148138 nonexistentTags := make ([]string , 0 )
149139 for _ , tag := range userTags {
150140 name := fmt .Sprintf ("%s/%s/%s" , tag .ParentID , tag .Key , tag .Value )
151- tagValue , err := mgr .GetNamespacedTagValue (ctx , name )
141+ tagValue , err := t . client .GetNamespacedTagValue (ctx , name )
152142 if err != nil {
153143 // check and return all non-existing tags at once
154144 // for user to fix in one go.
@@ -187,60 +177,3 @@ func findDuplicateTags(userTags []gcp.UserTag, parentTags sets.Set[string]) []st
187177 }
188178 return dupTags
189179}
190-
191- // getCloudResourceServiceForTags returns the client required for querying resource manager resources.
192- func (m * tagManager ) getCloudResourceServiceForTags (ctx context.Context ) (* tags.Service , error ) {
193- svc , err := tags .NewService (ctx , option .WithCredentials (m .client .GetCredentials ()))
194- if err != nil {
195- return nil , fmt .Errorf ("failed to create cloud resource service: %w" , err )
196- }
197- return svc , nil
198- }
199-
200- // GetProjectTags returns the list of effective tags attached to the provided project resource.
201- func (m * tagManager ) GetProjectTags (ctx context.Context , projectID string ) (sets.Set [string ], error ) {
202- const (
203- // projectParentPathFmt is the format string for parent path of a project resource.
204- projectParentPathFmt = "//cloudresourcemanager.googleapis.com/projects/%s"
205- )
206-
207- ctx , cancel := context .WithTimeout (ctx , defaultTimeout )
208- defer cancel ()
209-
210- service , err := m .getCloudResourceServiceForTags (ctx )
211- if err != nil {
212- return nil , fmt .Errorf ("failed to create cloud resource service: %w" , err )
213- }
214-
215- effectiveTags := sets .New [string ]()
216- effectiveTagsService := tags .NewEffectiveTagsService (service )
217- effectiveTagsRequest := effectiveTagsService .List ().Context (ctx ).Parent (fmt .Sprintf (projectParentPathFmt , projectID ))
218- if err := effectiveTagsRequest .Pages (ctx , func (page * tags.ListEffectiveTagsResponse ) error {
219- for _ , effectiveTag := range page .EffectiveTags {
220- effectiveTags .Insert (effectiveTag .NamespacedTagValue )
221- }
222- return nil
223- }); err != nil {
224- return nil , err
225- }
226-
227- return effectiveTags , nil
228- }
229-
230- // GetNamespacedTagValue returns the Tag Value metadata fetched using the tag's NamespacedName.
231- func (m * tagManager ) GetNamespacedTagValue (ctx context.Context , tagNamespacedName string ) (* tags.TagValue , error ) {
232- ctx , cancel := context .WithTimeout (ctx , defaultTimeout )
233- defer cancel ()
234-
235- service , err := m .getCloudResourceServiceForTags (ctx )
236- if err != nil {
237- return nil , fmt .Errorf ("failed to create cloud resource service: %w" , err )
238- }
239-
240- tagValuesService := tags .NewTagValuesService (service )
241-
242- return tagValuesService .GetNamespaced ().
243- Context (ctx ).
244- Name (tagNamespacedName ).
245- Do ()
246- }
0 commit comments