Skip to content

Commit 9490a4b

Browse files
Merge pull request openshift#7267 from r4f4/gcp-proj-list-to-get
OCPBUGS-15238: GCP: ic: improve project validation
2 parents 099a0d8 + f111af3 commit 9490a4b

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

pkg/asset/installconfig/gcp/client.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type API interface {
3939
GetEnabledServices(ctx context.Context, project string) ([]string, error)
4040
GetCredentials() *googleoauth.Credentials
4141
GetProjectPermissions(ctx context.Context, project string, permissions []string) (sets.Set[string], error)
42+
GetProjectByID(ctx context.Context, project string) (*cloudresourcemanager.Project, error)
4243
ValidateServiceAccountHasPermissions(ctx context.Context, project string, permissions []string) (bool, error)
4344
}
4445

@@ -262,6 +263,19 @@ func (c *Client) GetProjects(ctx context.Context) (map[string]string, error) {
262263
return projects, nil
263264
}
264265

266+
// GetProjectByID retrieves the project specified by its ID.
267+
func (c *Client) GetProjectByID(ctx context.Context, project string) (*cloudresourcemanager.Project, error) {
268+
ctx, cancel := context.WithTimeout(ctx, 1*time.Minute)
269+
defer cancel()
270+
271+
svc, err := c.getCloudResourceService(ctx)
272+
if err != nil {
273+
return nil, err
274+
}
275+
276+
return svc.Projects.Get(project).Context(ctx).Do()
277+
}
278+
265279
// GetRegions gets the regions that are valid for the project. An error is returned when unsuccessful
266280
func (c *Client) GetRegions(ctx context.Context, project string) ([]string, error) {
267281
svc, err := c.getComputeService(ctx)

pkg/asset/installconfig/gcp/mock/gcpclient_generated.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/asset/installconfig/gcp/validation.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,13 @@ func validateProject(client API, ic *types.InstallConfig, fieldPath *field.Path)
209209
allErrs := field.ErrorList{}
210210

211211
if ic.GCP.ProjectID != "" {
212-
projects, err := client.GetProjects(context.TODO())
212+
_, err := client.GetProjectByID(context.TODO(), ic.GCP.ProjectID)
213213
if err != nil {
214+
if IsNotFound(err) {
215+
return append(allErrs, field.Invalid(fieldPath.Child("project"), ic.GCP.ProjectID, "invalid project ID"))
216+
}
214217
return append(allErrs, field.InternalError(fieldPath.Child("project"), err))
215218
}
216-
if _, found := projects[ic.GCP.ProjectID]; !found {
217-
return append(allErrs, field.Invalid(fieldPath.Child("project"), ic.GCP.ProjectID, "invalid project ID"))
218-
}
219219
}
220220

221221
return allErrs
@@ -225,13 +225,13 @@ func validateNetworkProject(client API, ic *types.InstallConfig, fieldPath *fiel
225225
allErrs := field.ErrorList{}
226226

227227
if ic.GCP.NetworkProjectID != "" {
228-
projects, err := client.GetProjects(context.TODO())
228+
_, err := client.GetProjectByID(context.TODO(), ic.GCP.NetworkProjectID)
229229
if err != nil {
230+
if IsNotFound(err) {
231+
return append(allErrs, field.Invalid(fieldPath.Child("networkProjectID"), ic.GCP.NetworkProjectID, "invalid project ID"))
232+
}
230233
return append(allErrs, field.InternalError(fieldPath.Child("networkProjectID"), err))
231234
}
232-
if _, found := projects[ic.GCP.NetworkProjectID]; !found {
233-
return append(allErrs, field.Invalid(fieldPath.Child("networkProjectID"), ic.GCP.NetworkProjectID, "invalid project ID"))
234-
}
235235
}
236236

237237
return allErrs

pkg/asset/installconfig/gcp/validation_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/golang/mock/gomock"
1212
"github.com/stretchr/testify/assert"
1313
googleoauth "golang.org/x/oauth2/google"
14+
"google.golang.org/api/cloudresourcemanager/v1"
1415
compute "google.golang.org/api/compute/v1"
1516
dns "google.golang.org/api/dns/v1"
1617
"google.golang.org/api/googleapi"
@@ -279,8 +280,15 @@ func TestGCPInstallConfigValidation(t *testing.T) {
279280
defer mockCtrl.Finish()
280281

281282
gcpClient := mock.NewMockAPI(mockCtrl)
283+
284+
errNotFound := &googleapi.Error{Code: http.StatusNotFound}
285+
282286
// Should get the list of projects.
283287
gcpClient.EXPECT().GetProjects(gomock.Any()).Return(map[string]string{"valid-project": "valid-project"}, nil).AnyTimes()
288+
gcpClient.EXPECT().GetProjectByID(gomock.Any(), "valid-project").Return(&cloudresourcemanager.Project{}, nil).AnyTimes()
289+
gcpClient.EXPECT().GetProjectByID(gomock.Any(), "invalid-project").Return(nil, errNotFound).AnyTimes()
290+
gcpClient.EXPECT().GetProjectByID(gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("error")).AnyTimes()
291+
284292
// Should get the list of zones.
285293
gcpClient.EXPECT().GetZones(gomock.Any(), gomock.Any(), gomock.Any()).Return([]*compute.Zone{{Name: validZone}}, nil).AnyTimes()
286294

0 commit comments

Comments
 (0)