Skip to content

Commit acc18ed

Browse files
Split some functions and tests into differnt files (#7684) (#5469)
Signed-off-by: Modular Magician <[email protected]>
1 parent 4e81650 commit acc18ed

24 files changed

+807
-785
lines changed

.changelog/7684.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:none
2+
3+
```
File renamed without changes.
File renamed without changes.

google-beta/config.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"log"
99
"net/http"
10+
"os"
1011
"regexp"
1112
"strconv"
1213
"strings"
@@ -2056,3 +2057,31 @@ func ConfigureBasePaths(c *Config) {
20562057
c.BigtableAdminBasePath = DefaultBasePaths[BigtableAdminBasePathKey]
20572058
c.TagsLocationBasePath = DefaultBasePaths[TagsLocationBasePathKey]
20582059
}
2060+
2061+
func GetCurrentUserEmail(config *Config, userAgent string) (string, error) {
2062+
// When environment variables UserProjectOverride and BillingProject are set for the provider,
2063+
// the header X-Goog-User-Project is set for the API requests.
2064+
// But it causes an error when calling GetCurrentUserEmail. Set the project to be "NO_BILLING_PROJECT_OVERRIDE".
2065+
// And then it triggers the header X-Goog-User-Project to be set to empty string.
2066+
2067+
// See https://github.com/golang/oauth2/issues/306 for a recommendation to do this from a Go maintainer
2068+
// URL retrieved from https://accounts.google.com/.well-known/openid-configuration
2069+
res, err := SendRequest(config, "GET", "NO_BILLING_PROJECT_OVERRIDE", "https://openidconnect.googleapis.com/v1/userinfo", userAgent, nil)
2070+
2071+
if err != nil {
2072+
return "", fmt.Errorf("error retrieving userinfo for your provider credentials. have you enabled the 'https://www.googleapis.com/auth/userinfo.email' scope? error: %s", err)
2073+
}
2074+
if res["email"] == nil {
2075+
return "", fmt.Errorf("error retrieving email from userinfo. email was nil in the response.")
2076+
}
2077+
return res["email"].(string), nil
2078+
}
2079+
2080+
func MultiEnvSearch(ks []string) string {
2081+
for _, k := range ks {
2082+
if v := os.Getenv(k); v != "" {
2083+
return v
2084+
}
2085+
}
2086+
return ""
2087+
}

google-beta/config_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ import (
1111
"golang.org/x/oauth2/google"
1212
)
1313

14-
const testFakeCredentialsPath = "./test-fixtures/fake_account.json"
15-
const testOauthScope = "https://www.googleapis.com/auth/compute"
16-
1714
func TestHandleSDKDefaults_ImpersonateServiceAccount(t *testing.T) {
1815
cases := map[string]struct {
1916
ConfigValue string

google-beta/config_test_utils.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import (
55
"strings"
66
)
77

8+
const testFakeCredentialsPath = "./test-fixtures/fake_account.json"
9+
const testOauthScope = "https://www.googleapis.com/auth/compute"
10+
811
// NewTestConfig create a config using the http test server.
912
func NewTestConfig(server *httptest.Server) *Config {
1013
cfg := &Config{}

google-beta/gcp_sweeper.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package google
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
// List of prefixes used for test resource names
9+
var testResourcePrefixes = []string{
10+
// tf-test and tf_test are automatically prepended to resource ids in examples that
11+
// include a "-" or "_" respectively, and they are the preferred prefix for our test resources to use
12+
"tf-test",
13+
"tf_test",
14+
"tfgen",
15+
"gke-us-central1-tf", // composer-created disks which are abandoned by design (https://cloud.google.com/composer/pricing)
16+
"gcs-bucket-tf-test-", // https://github.com/hashicorp/terraform-provider-google/issues/8909
17+
"df-", // https://github.com/hashicorp/terraform-provider-google/issues/8909
18+
"resourcegroup-", // https://github.com/hashicorp/terraform-provider-google/issues/8924
19+
"cluster-", // https://github.com/hashicorp/terraform-provider-google/issues/8924
20+
"k8s-fw-", // firewall rules are getting created and not cleaned up by k8 resources using this prefix
21+
}
22+
23+
// SharedConfigForRegion returns a common config setup needed for the sweeper
24+
// functions for a given region
25+
func SharedConfigForRegion(region string) (*Config, error) {
26+
project := GetTestProjectFromEnv()
27+
if project == "" {
28+
return nil, fmt.Errorf("set project using any of these env variables %v", ProjectEnvVars)
29+
}
30+
31+
if v := MultiEnvSearch(CredsEnvVars); v == "" {
32+
return nil, fmt.Errorf("set credentials using any of these env variables %v", CredsEnvVars)
33+
}
34+
35+
conf := &Config{
36+
Credentials: GetTestCredsFromEnv(),
37+
Region: region,
38+
Project: project,
39+
}
40+
41+
ConfigureBasePaths(conf)
42+
43+
return conf, nil
44+
}
45+
46+
func IsSweepableTestResource(resourceName string) bool {
47+
for _, p := range testResourcePrefixes {
48+
if strings.HasPrefix(resourceName, p) {
49+
return true
50+
}
51+
}
52+
return false
53+
}

google-beta/gcp_sweeper_test.go

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,11 @@
11
package google
22

33
import (
4-
"fmt"
5-
"strings"
64
"testing"
75

86
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
97
)
108

11-
// List of prefixes used for test resource names
12-
var testResourcePrefixes = []string{
13-
// tf-test and tf_test are automatically prepended to resource ids in examples that
14-
// include a "-" or "_" respectively, and they are the preferred prefix for our test resources to use
15-
"tf-test",
16-
"tf_test",
17-
"tfgen",
18-
"gke-us-central1-tf", // composer-created disks which are abandoned by design (https://cloud.google.com/composer/pricing)
19-
"gcs-bucket-tf-test-", // https://github.com/hashicorp/terraform-provider-google/issues/8909
20-
"df-", // https://github.com/hashicorp/terraform-provider-google/issues/8909
21-
"resourcegroup-", // https://github.com/hashicorp/terraform-provider-google/issues/8924
22-
"cluster-", // https://github.com/hashicorp/terraform-provider-google/issues/8924
23-
"k8s-fw-", // firewall rules are getting created and not cleaned up by k8 resources using this prefix
24-
}
25-
269
func TestMain(m *testing.M) {
2710
resource.TestMain(m)
2811
}
29-
30-
// SharedConfigForRegion returns a common config setup needed for the sweeper
31-
// functions for a given region
32-
func SharedConfigForRegion(region string) (*Config, error) {
33-
project := GetTestProjectFromEnv()
34-
if project == "" {
35-
return nil, fmt.Errorf("set project using any of these env variables %v", ProjectEnvVars)
36-
}
37-
38-
if v := MultiEnvSearch(CredsEnvVars); v == "" {
39-
return nil, fmt.Errorf("set credentials using any of these env variables %v", CredsEnvVars)
40-
}
41-
42-
conf := &Config{
43-
Credentials: GetTestCredsFromEnv(),
44-
Region: region,
45-
Project: project,
46-
}
47-
48-
ConfigureBasePaths(conf)
49-
50-
return conf, nil
51-
}
52-
53-
func IsSweepableTestResource(resourceName string) bool {
54-
for _, p := range testResourcePrefixes {
55-
if strings.HasPrefix(resourceName, p) {
56-
return true
57-
}
58-
}
59-
return false
60-
}

google-beta/iam.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,15 @@ func IamWithDeprecationMessage(message string) func(s *IamSettings) {
466466
func IamWithGAResourceDeprecation() func(s *IamSettings) {
467467
return IamWithDeprecationMessage("")
468468
}
469+
470+
// Util to deref and print auditConfigs
471+
func debugPrintAuditConfigs(bs []*cloudresourcemanager.AuditConfig) string {
472+
v, _ := json.MarshalIndent(bs, "", "\t")
473+
return string(v)
474+
}
475+
476+
// Util to deref and print bindings
477+
func debugPrintBindings(bs []*cloudresourcemanager.Binding) string {
478+
v, _ := json.MarshalIndent(bs, "", "\t")
479+
return string(v)
480+
}

google-beta/iam_test.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package google
22

33
import (
4-
"encoding/json"
54
"reflect"
65
"testing"
76

@@ -1324,15 +1323,3 @@ func TestIamListFromIamAuditConfigsMap(t *testing.T) {
13241323
}
13251324
}
13261325
}
1327-
1328-
// Util to deref and print auditConfigs
1329-
func debugPrintAuditConfigs(bs []*cloudresourcemanager.AuditConfig) string {
1330-
v, _ := json.MarshalIndent(bs, "", "\t")
1331-
return string(v)
1332-
}
1333-
1334-
// Util to deref and print bindings
1335-
func debugPrintBindings(bs []*cloudresourcemanager.Binding) string {
1336-
v, _ := json.MarshalIndent(bs, "", "\t")
1337-
return string(v)
1338-
}

0 commit comments

Comments
 (0)