diff --git a/.github/workflows/go-lint.yaml b/.github/workflows/go-lint.yaml new file mode 100644 index 00000000..6a6cb1c9 --- /dev/null +++ b/.github/workflows/go-lint.yaml @@ -0,0 +1,49 @@ +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +name: go-lint +on: + pull_request: + branches: + - master + paths: + - ".github/workflows/go-lint.yaml" + - "modules/project_cleanup/function_source/**" +permissions: + contents: read + +concurrency: + group: '$${{ github.workflow }}-$${{ github.head_ref || github.ref }}' + cancel-in-progress: true + +jobs: + golangci: + name: ${{ matrix.folder }} + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + folder: [modules/project_cleanup/function_source] + steps: + - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 + - uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0 + with: + go-version-file: ${{ matrix.folder }}/go.mod + cache-dependency-path: ${{ matrix.folder }}/go.sum + - name: golangci-lint + uses: golangci/golangci-lint-action@3a919529898de77ec3da873e3063ca4b10e7f5cc # v3.7.0 + with: + version: latest + working-directory: ${{ matrix.folder }} + args: --timeout=5m diff --git a/.gitignore b/.gitignore index c8c40d9d..945d049d 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ credentials.json .vscode/ *.pyc .kitchen +go.work.sum examples/**/*.zip examples/**/terraform.tfvars diff --git a/go.work b/go.work new file mode 100644 index 00000000..4857c5df --- /dev/null +++ b/go.work @@ -0,0 +1,6 @@ +go 1.21 + +use ( + ./modules/project_cleanup/function_source + ./test/integration +) diff --git a/modules/project_cleanup/function_source/main.go b/modules/project_cleanup/function_source/main.go index 85b85ee2..bb579f5a 100644 --- a/modules/project_cleanup/function_source/main.go +++ b/modules/project_cleanup/function_source/main.go @@ -34,6 +34,7 @@ import ( cloudresourcemanager3 "google.golang.org/api/cloudresourcemanager/v3" "google.golang.org/api/compute/v1" "google.golang.org/api/googleapi" + "google.golang.org/api/option" "google.golang.org/api/servicemanagement/v1" ) @@ -241,17 +242,17 @@ func getCorrectOrganizationIdOrTerminateExecution() string { return targetOrganizationIdString } -func getServiceManagementServiceOrTerminateExecution(client *http.Client) *servicemanagement.APIService { - service, err := servicemanagement.New(client) +func getServiceManagementServiceOrTerminateExecution(ctx context.Context, client *http.Client) *servicemanagement.APIService { + service, err := servicemanagement.NewService(ctx, option.WithHTTPClient(client)) if err != nil { logger.Fatalf("Failed to get service management API client with error [%s], terminate execution", err.Error()) } return service } -func getResourceManagerServiceOrTerminateExecution(client *http.Client) *cloudresourcemanager.Service { +func getResourceManagerServiceOrTerminateExecution(ctx context.Context, client *http.Client) *cloudresourcemanager.Service { logger.Println("Try to get Cloud Resource Manager") - cloudResourceManagerService, err := cloudresourcemanager.New(client) + cloudResourceManagerService, err := cloudresourcemanager.NewService(ctx, option.WithHTTPClient(client)) if err != nil { logger.Fatalf("Failed to get Cloud Resource Manager with error [%s], terminate execution", err.Error()) } @@ -259,9 +260,9 @@ func getResourceManagerServiceOrTerminateExecution(client *http.Client) *cloudre return cloudResourceManagerService } -func getFolderServiceOrTerminateExecution(client *http.Client) *cloudresourcemanager2.FoldersService { +func getFolderServiceOrTerminateExecution(ctx context.Context, client *http.Client) *cloudresourcemanager2.FoldersService { logger.Println("Try to get Folders Service") - cloudResourceManagerService, err := cloudresourcemanager2.New(client) + cloudResourceManagerService, err := cloudresourcemanager2.NewService(ctx, option.WithHTTPClient(client)) if err != nil { logger.Fatalf("Failed to get Folders Service with error [%s], terminate execution", err.Error()) } @@ -269,9 +270,9 @@ func getFolderServiceOrTerminateExecution(client *http.Client) *cloudresourceman return cloudResourceManagerService.Folders } -func getTagKeysServiceOrTerminateExecution(client *http.Client) *cloudresourcemanager3.TagKeysService { +func getTagKeysServiceOrTerminateExecution(ctx context.Context, client *http.Client) *cloudresourcemanager3.TagKeysService { logger.Println("Try to get TagKeys Service") - cloudResourceManagerService, err := cloudresourcemanager3.New(client) + cloudResourceManagerService, err := cloudresourcemanager3.NewService(ctx, option.WithHTTPClient(client)) if err != nil { logger.Fatalf("Failed to get TagKeys Service with error [%s], terminate execution", err.Error()) } @@ -279,9 +280,9 @@ func getTagKeysServiceOrTerminateExecution(client *http.Client) *cloudresourcema return cloudResourceManagerService.TagKeys } -func getTagValuesServiceOrTerminateExecution(client *http.Client) *cloudresourcemanager3.TagValuesService { +func getTagValuesServiceOrTerminateExecution(ctx context.Context, client *http.Client) *cloudresourcemanager3.TagValuesService { logger.Println("Try to get TagValues Service") - cloudResourceManagerService, err := cloudresourcemanager3.New(client) + cloudResourceManagerService, err := cloudresourcemanager3.NewService(ctx, option.WithHTTPClient(client)) if err != nil { logger.Fatalf("Failed to get TagValues Service with error [%s], terminate execution", err.Error()) } @@ -289,9 +290,9 @@ func getTagValuesServiceOrTerminateExecution(client *http.Client) *cloudresource return cloudResourceManagerService.TagValues } -func getFirewallPoliciesServiceOrTerminateExecution(client *http.Client) *compute.FirewallPoliciesService { +func getFirewallPoliciesServiceOrTerminateExecution(ctx context.Context, client *http.Client) *compute.FirewallPoliciesService { logger.Println("Try to get Firewall Policies Service") - computeService, err := compute.New(client) + computeService, err := compute.NewService(ctx, option.WithHTTPClient(client)) if err != nil { logger.Fatalf("Failed to get Firewall Policies Service with error [%s], terminate execution", err.Error()) } @@ -311,12 +312,12 @@ func initializeGoogleClient(ctx context.Context) *http.Client { func invoke(ctx context.Context) { client := initializeGoogleClient(ctx) - cloudResourceManagerService := getResourceManagerServiceOrTerminateExecution(client) - folderService := getFolderServiceOrTerminateExecution(client) - tagKeyService := getTagKeysServiceOrTerminateExecution(client) - tagValuesService := getTagValuesServiceOrTerminateExecution(client) - firewallPoliciesService := getFirewallPoliciesServiceOrTerminateExecution(client) - endpointService := getServiceManagementServiceOrTerminateExecution(client) + cloudResourceManagerService := getResourceManagerServiceOrTerminateExecution(ctx, client) + folderService := getFolderServiceOrTerminateExecution(ctx, client) + tagKeyService := getTagKeysServiceOrTerminateExecution(ctx, client) + tagValuesService := getTagValuesServiceOrTerminateExecution(ctx, client) + firewallPoliciesService := getFirewallPoliciesServiceOrTerminateExecution(ctx, client) + endpointService := getServiceManagementServiceOrTerminateExecution(ctx, client) removeLien := func(name string) { logger.Printf("Try to remove lien [%s]", name)