@@ -8,14 +8,16 @@ import (
88
99 "github.com/pkg/errors"
1010 googleoauth "golang.org/x/oauth2/google"
11- "google.golang.org/api/cloudresourcemanager/v1 "
11+ "google.golang.org/api/cloudresourcemanager/v3 "
1212 compute "google.golang.org/api/compute/v1"
1313 dns "google.golang.org/api/dns/v1"
1414 "google.golang.org/api/googleapi"
1515 iam "google.golang.org/api/iam/v1"
1616 "google.golang.org/api/option"
1717 "google.golang.org/api/serviceusage/v1"
1818 "k8s.io/apimachinery/pkg/util/sets"
19+
20+ gcpconsts "github.com/openshift/installer/pkg/constants/gcp"
1921)
2022
2123//go:generate mockgen -source=./client.go -destination=./mock/gcpclient_generated.go -package=mock
@@ -48,6 +50,8 @@ type API interface {
4850 GetProjectPermissions (ctx context.Context , project string , permissions []string ) (sets.Set [string ], error )
4951 GetProjectByID (ctx context.Context , project string ) (* cloudresourcemanager.Project , error )
5052 ValidateServiceAccountHasPermissions (ctx context.Context , project string , permissions []string ) (bool , error )
53+ GetProjectTags (ctx context.Context , projectID string ) (sets.Set [string ], error )
54+ GetNamespacedTagValue (ctx context.Context , tagNamespacedName string ) (* cloudresourcemanager.TagValue , error )
5155}
5256
5357// Client makes calls to the GCP API.
@@ -317,9 +321,9 @@ func (c *Client) GetProjects(ctx context.Context) (map[string]string, error) {
317321 return nil , err
318322 }
319323
320- req := svc .Projects .List ()
324+ req := svc .Projects .Search ()
321325 projects := make (map [string ]string )
322- if err := req .Pages (ctx , func (page * cloudresourcemanager.ListProjectsResponse ) error {
326+ if err := req .Pages (ctx , func (page * cloudresourcemanager.SearchProjectsResponse ) error {
323327 for _ , project := range page .Projects {
324328 projects [project .ProjectId ] = project .Name
325329 }
@@ -340,7 +344,7 @@ func (c *Client) GetProjectByID(ctx context.Context, project string) (*cloudreso
340344 return nil , err
341345 }
342346
343- return svc .Projects .Get (project ).Context (ctx ).Do ()
347+ return svc .Projects .Get (fmt . Sprintf ( gcpconsts . ProjectNameFmt , project ) ).Context (ctx ).Do ()
344348}
345349
346350// GetRegions gets the regions that are valid for the project. An error is returned when unsuccessful
@@ -485,7 +489,7 @@ func (c *Client) getPermissions(ctx context.Context, project string, permissions
485489
486490 projectsService := cloudresourcemanager .NewProjectsService (service )
487491 rb := & cloudresourcemanager.TestIamPermissionsRequest {Permissions : permissions }
488- response , err := projectsService .TestIamPermissions (project , rb ).Context (ctx ).Do ()
492+ response , err := projectsService .TestIamPermissions (fmt . Sprintf ( gcpconsts . ProjectNameFmt , project ) , rb ).Context (ctx ).Do ()
489493 if err != nil {
490494 return nil , errors .Wrapf (err , "failed to get Iam permissions" )
491495 }
@@ -513,3 +517,49 @@ func (c *Client) ValidateServiceAccountHasPermissions(ctx context.Context, proje
513517 }
514518 return validPermissions .Len () == len (permissions ), nil
515519}
520+
521+ // GetProjectTags returns the list of effective tags attached to the provided project resource.
522+ func (c * Client ) GetProjectTags (ctx context.Context , projectID string ) (sets.Set [string ], error ) {
523+ service , err := c .getCloudResourceService (ctx )
524+ if err != nil {
525+ return nil , fmt .Errorf ("failed to create cloud resource service: %w" , err )
526+ }
527+
528+ effectiveTags := sets .New [string ]()
529+ effectiveTagsService := cloudresourcemanager .NewEffectiveTagsService (service )
530+ effectiveTagsRequest := effectiveTagsService .List ().
531+ Context (ctx ).
532+ Parent (fmt .Sprintf (gcpconsts .ProjectParentPathFmt , projectID ))
533+
534+ if err := effectiveTagsRequest .Pages (ctx , func (page * cloudresourcemanager.ListEffectiveTagsResponse ) error {
535+ for _ , effectiveTag := range page .EffectiveTags {
536+ effectiveTags .Insert (effectiveTag .NamespacedTagValue )
537+ }
538+ return nil
539+ }); err != nil {
540+ return nil , fmt .Errorf ("failed to fetch tags attached to %s project: %w" , projectID , err )
541+ }
542+
543+ return effectiveTags , nil
544+ }
545+
546+ // GetNamespacedTagValue returns the Tag Value metadata fetched using the tag's NamespacedName.
547+ func (c * Client ) GetNamespacedTagValue (ctx context.Context , tagNamespacedName string ) (* cloudresourcemanager.TagValue , error ) {
548+ service , err := c .getCloudResourceService (ctx )
549+ if err != nil {
550+ return nil , fmt .Errorf ("failed to create cloud resource service: %w" , err )
551+ }
552+
553+ tagValuesService := cloudresourcemanager .NewTagValuesService (service )
554+
555+ tagValue , err := tagValuesService .GetNamespaced ().
556+ Context (ctx ).
557+ Name (tagNamespacedName ).
558+ Do ()
559+
560+ if err != nil {
561+ return nil , fmt .Errorf ("failed to fetch %s tag value: %w" , tagNamespacedName , err )
562+ }
563+
564+ return tagValue , nil
565+ }
0 commit comments