diff --git a/.github/workflows/teamcity-services-diff-check.yml b/.github/workflows/teamcity-services-diff-check.yml new file mode 100644 index 000000000000..c6c7fe67aeb5 --- /dev/null +++ b/.github/workflows/teamcity-services-diff-check.yml @@ -0,0 +1,71 @@ +name: TeamCity Services Diff Check +permissions: read-all + +on: + workflow_dispatch: + pull_request: + types: [opened, edited] + paths: + - '.github/workflows/teamcity-services-diff-check.yml' + - 'mmv1/third_party/terraform/.teamcity/components/inputs/services_ga.kt' + - 'mmv1/third_party/terraform/.teamcity/components/inputs/services_beta.kt' +jobs: + terraform-provider-google: + uses: ./.github/workflows/build-downstream.yml + with: + repo: 'terraform-provider-google' + + terraform-provider-google-beta: + uses: ./.github/workflows/build-downstream.yml + with: + repo: 'terraform-provider-google-beta' + + teamcity-services-diff-check: + needs: [terraform-provider-google, terraform-provider-google-beta] + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v3 + + - name: Download built artifacts + uses: actions/download-artifact@v2 + with: + name: artifact-terraform-provider-google + path: artifacts + + - name: Unzip the artifacts and delete the zip + run: | + unzip -o artifacts/output.zip -d ./provider + rm artifacts/output.zip + + - name: Download built artifacts + uses: actions/download-artifact@v2 + with: + name: artifact-terraform-provider-google-beta + path: artifacts + + - name: Unzip the artifacts and delete the zip + run: | + unzip -o artifacts/output.zip -d ./provider + rm artifacts/output.zip + + - name: Setup Go + uses: actions/setup-go@v3 + with: + go-version: '^1.20' + + - name: Cache Go modules and build cache + uses: actions/cache@v3 + with: + path: | + ~/go/pkg/mod + ~/.cache/go-build + key: ${{ runner.os }}-test-terraform-provider-google-${{hashFiles('go.sum','google-*/transport/**','google-*/tpgresource/**','google-*/acctest/**','google-*/envvar/**','google-*/sweeper/**','google-*/verify/**') }} + restore-keys: | + ${{ runner.os }}-test-terraform-provider-google-${{ hashFiles('go.sum') }} + ${{ runner.os }}-test-terraform-provider-google- + + - name: Diff Check + run: | + cd tools/teamcity-diff-check + go run main.go + go run main.go -provider=google-beta -service_file=services_beta.kt \ No newline at end of file diff --git a/mmv1/third_party/terraform/.teamcity/components/inputs/services_beta.kt b/mmv1/third_party/terraform/.teamcity/components/inputs/services_beta.kt index 27550dabce13..64adb208d724 100644 --- a/mmv1/third_party/terraform/.teamcity/components/inputs/services_beta.kt +++ b/mmv1/third_party/terraform/.teamcity/components/inputs/services_beta.kt @@ -13,6 +13,11 @@ var ServicesListBeta = mapOf( "displayName" to "Accessapproval", "path" to "./google-beta/services/accessapproval" ), + "supersql" to mapOf( + "name" to "accessapproval", + "displayName" to "Accessapproval", + "path" to "./google-beta/services/accessapproval" + ), "accesscontextmanager" to mapOf( "name" to "accesscontextmanager", "displayName" to "Accesscontextmanager", diff --git a/mmv1/third_party/terraform/.teamcity/components/inputs/services_ga.kt b/mmv1/third_party/terraform/.teamcity/components/inputs/services_ga.kt index 51f98ed08a05..42f295f13a24 100644 --- a/mmv1/third_party/terraform/.teamcity/components/inputs/services_ga.kt +++ b/mmv1/third_party/terraform/.teamcity/components/inputs/services_ga.kt @@ -13,6 +13,11 @@ var ServicesListGa = mapOf( "displayName" to "Accessapproval", "path" to "./google/services/accessapproval" ), + "supersql" to mapOf( + "name" to "accessapproval", + "displayName" to "Accessapproval", + "path" to "./google/services/accessapproval" + ), "accesscontextmanager" to mapOf( "name" to "accesscontextmanager", "displayName" to "Accesscontextmanager", diff --git a/tools/teamcity-diff-check/main.go b/tools/teamcity-diff-check/main.go new file mode 100644 index 000000000000..0fae8dabcc99 --- /dev/null +++ b/tools/teamcity-diff-check/main.go @@ -0,0 +1,111 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 +/* + * Copyright (c) HashiCorp, Inc. + * SPDX-License-Identifier: MPL-2.0 + */ + +// This file is controlled by MMv1, any changes made here will be overwritten + +package main + +import ( + "bufio" + "flag" + "fmt" + "io" + "os" + "os/exec" + "regexp" +) + +var serviceFile = flag.String("service_file", "services_ga.kt", "kotlin service file to be parsed") +var provider = flag.String("provider", "google", "Specify which provider to run diff_check on") + +func serviceDifference(gS, tS []string) []string { + g := make(map[string]struct{}, len(gS)) + for _, s := range gS { + g[s] = struct{}{} + } + + var diff []string + for _, s := range tS { + if _, found := g[s]; !found { + diff = append(diff, s) + } + } + + return diff +} + +func main() { + flag.Parse() + + servicesPath := fmt.Sprintf("../../provider/%s/services/", *provider) + cmd := exec.Command("go", "list", "./...") + cmd.Dir = servicesPath + stdout, err := cmd.Output() + // if err != nil { + // fmt.Println(string(stdout)) + // fmt.Println(err.Error()) + // return + // } + + // Regex pattern captures "services" from `go list ../../provider/{{*provider}}/services/..` + pattern := regexp.MustCompile(`github\.com\/hashicorp\/terraform-provider-(google|google-beta)\/(google|google-beta)\/services\/(?P\w+)`) + + template := []byte("$service") + dst := []byte{} + + googleServices := []string{} + + // For each match of the regex in the content. + for _, submatches := range pattern.FindAllSubmatchIndex(stdout, -1) { + service := pattern.Expand(dst, template, stdout, submatches) + googleServices = append(googleServices, string(service)) + } + + //////////////////////////////////////////////////////////////////////////////// + + f, err := os.Open(fmt.Sprintf("../../mmv1/third_party/terraform/.teamcity/components/inputs/%s", *serviceFile)) + if err != nil { + panic(err) + } + + // Get the file size + stat, err := f.Stat() + if err != nil { + fmt.Println(err) + return + } + + // Read the file into a byte slice + bs := make([]byte, stat.Size()) + _, err = bufio.NewReader(f).Read(bs) + if err != nil && err != io.EOF { + fmt.Println(err) + return + } + + // Regex pattern captures "services" from *serviceFile. + pattern = regexp.MustCompile(`(?m)"(?P\w+)"\sto\s+mapOf`) + + template = []byte("$service") + + dst = []byte{} + teamcityServices := []string{} + + // For each match of the regex in the content. + for _, submatches := range pattern.FindAllSubmatchIndex(bs, -1) { + service := pattern.Expand(dst, template, bs, submatches) + teamcityServices = append(teamcityServices, string(service)) + } + + if diff := serviceDifference(googleServices, teamcityServices); len(diff) != 0 { + fmt.Fprintf(os.Stderr, "error: diff in %s\n", *serviceFile) + fmt.Fprintf(os.Stderr, "Missing Services: %s\n", diff) + os.Exit(1) + } + + fmt.Printf("No Diff in %s provider\n", *provider) +}