-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: create shared project number/id mapper
- Loading branch information
Showing
5 changed files
with
95 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package projects | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
resourcemanager "cloud.google.com/go/resourcemanager/apiv3" | ||
"cloud.google.com/go/resourcemanager/apiv3/resourcemanagerpb" | ||
) | ||
|
||
type ProjectMapper struct { | ||
client *resourcemanager.ProjectsClient | ||
} | ||
|
||
func NewProjectMapper(client *resourcemanager.ProjectsClient) *ProjectMapper { | ||
return &ProjectMapper{ | ||
client: client, | ||
} | ||
} | ||
|
||
func (m *ProjectMapper) ReplaceProjectNumberWithID(ctx context.Context, projectID string) (string, error) { | ||
if _, err := strconv.ParseInt(projectID, 10, 64); err != nil { | ||
// Not a project number, no need to map | ||
return projectID, nil | ||
} | ||
|
||
req := &resourcemanagerpb.GetProjectRequest{ | ||
Name: "projects/" + projectID, | ||
} | ||
project, err := m.client.GetProject(ctx, req) | ||
if err != nil { | ||
return "", fmt.Errorf("error getting project %q: %w", req.Name, err) | ||
} | ||
return project.ProjectId, nil | ||
} | ||
|
||
func (m *ProjectMapper) LookupProjectNumber(ctx context.Context, projectID string) (int64, error) { | ||
// Check if the project number is already a valid integer | ||
// If not, we need to look it up | ||
projectNumber, err := strconv.ParseInt(projectID, 10, 64) | ||
if err != nil { | ||
req := &resourcemanagerpb.GetProjectRequest{ | ||
Name: "projects/" + projectID, | ||
} | ||
project, err := m.client.GetProject(ctx, req) | ||
if err != nil { | ||
return 0, fmt.Errorf("error getting project %q: %w", req.Name, err) | ||
} | ||
n, err := strconv.ParseInt(strings.TrimPrefix(project.Name, "projects/"), 10, 64) | ||
if err != nil { | ||
return 0, fmt.Errorf("error parsing project number for %q: %w", project.Name, err) | ||
} | ||
projectNumber = n | ||
} | ||
return projectNumber, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters