Skip to content

Commit

Permalink
refactor module attribution work (#4016) (#7376)
Browse files Browse the repository at this point in the history
* set provider meta in RUD methods

* make generating useragent string its own function

* update config.client, create new service, pass around/generate userAgent as needed

* no longer creates no client

* fix errors in beta, pass config.userAgent for tests

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Sep 28, 2020
1 parent 8abb9f9 commit 08c9e64
Show file tree
Hide file tree
Showing 569 changed files with 4,639 additions and 5,755 deletions.
3 changes: 3 additions & 0 deletions .changelog/4016.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:none

```
19 changes: 11 additions & 8 deletions google/access_context_manager_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import (
)

type AccessContextManagerOperationWaiter struct {
Config *Config
Config *Config
UserAgent string
CommonOperationWaiter
}

Expand All @@ -30,16 +31,18 @@ func (w *AccessContextManagerOperationWaiter) QueryOp() (interface{}, error) {
}
// Returns the proper get.
url := fmt.Sprintf("https://accesscontextmanager.googleapis.com/v1/%s", w.CommonOperationWaiter.Op.Name)
return sendRequest(w.Config, "GET", "", url, nil)

return sendRequest(w.Config, "GET", "", url, w.UserAgent, nil)
}

func createAccessContextManagerWaiter(config *Config, op map[string]interface{}, activity string) (*AccessContextManagerOperationWaiter, error) {
func createAccessContextManagerWaiter(config *Config, op map[string]interface{}, activity, userAgent string) (*AccessContextManagerOperationWaiter, error) {
if val, ok := op["name"]; !ok || val == "" {
// This was a synchronous call - there is no operation to wait for.
return nil, nil
}
w := &AccessContextManagerOperationWaiter{
Config: config,
Config: config,
UserAgent: userAgent,
}
if err := w.CommonOperationWaiter.SetOp(op); err != nil {
return nil, err
Expand All @@ -48,8 +51,8 @@ func createAccessContextManagerWaiter(config *Config, op map[string]interface{},
}

// nolint: deadcode,unused
func accessContextManagerOperationWaitTimeWithResponse(config *Config, op map[string]interface{}, response *map[string]interface{}, activity string, timeout time.Duration) error {
w, err := createAccessContextManagerWaiter(config, op, activity)
func accessContextManagerOperationWaitTimeWithResponse(config *Config, op map[string]interface{}, response *map[string]interface{}, activity, userAgent string, timeout time.Duration) error {
w, err := createAccessContextManagerWaiter(config, op, activity, userAgent)
if err != nil || w == nil {
// If w is nil, the op was synchronous.
return err
Expand All @@ -60,8 +63,8 @@ func accessContextManagerOperationWaitTimeWithResponse(config *Config, op map[st
return json.Unmarshal([]byte(w.CommonOperationWaiter.Op.Response), response)
}

func accessContextManagerOperationWaitTime(config *Config, op map[string]interface{}, activity string, timeout time.Duration) error {
w, err := createAccessContextManagerWaiter(config, op, activity)
func accessContextManagerOperationWaitTime(config *Config, op map[string]interface{}, activity, userAgent string, timeout time.Duration) error {
w, err := createAccessContextManagerWaiter(config, op, activity, userAgent)
if err != nil || w == nil {
// If w is nil, the op was synchronous.
return err
Expand Down
23 changes: 13 additions & 10 deletions google/active_directory_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import (
)

type ActiveDirectoryOperationWaiter struct {
Config *Config
Project string
Config *Config
UserAgent string
Project string
CommonOperationWaiter
}

Expand All @@ -31,17 +32,19 @@ func (w *ActiveDirectoryOperationWaiter) QueryOp() (interface{}, error) {
}
// Returns the proper get.
url := fmt.Sprintf("https://managedidentities.googleapis.com/v1/%s", w.CommonOperationWaiter.Op.Name)
return sendRequest(w.Config, "GET", w.Project, url, nil)

return sendRequest(w.Config, "GET", w.Project, url, w.UserAgent, nil)
}

func createActiveDirectoryWaiter(config *Config, op map[string]interface{}, project, activity string) (*ActiveDirectoryOperationWaiter, error) {
func createActiveDirectoryWaiter(config *Config, op map[string]interface{}, project, activity, userAgent string) (*ActiveDirectoryOperationWaiter, error) {
if val, ok := op["name"]; !ok || val == "" {
// This was a synchronous call - there is no operation to wait for.
return nil, nil
}
w := &ActiveDirectoryOperationWaiter{
Config: config,
Project: project,
Config: config,
UserAgent: userAgent,
Project: project,
}
if err := w.CommonOperationWaiter.SetOp(op); err != nil {
return nil, err
Expand All @@ -50,8 +53,8 @@ func createActiveDirectoryWaiter(config *Config, op map[string]interface{}, proj
}

// nolint: deadcode,unused
func activeDirectoryOperationWaitTimeWithResponse(config *Config, op map[string]interface{}, response *map[string]interface{}, project, activity string, timeout time.Duration) error {
w, err := createActiveDirectoryWaiter(config, op, project, activity)
func activeDirectoryOperationWaitTimeWithResponse(config *Config, op map[string]interface{}, response *map[string]interface{}, project, activity, userAgent string, timeout time.Duration) error {
w, err := createActiveDirectoryWaiter(config, op, project, activity, userAgent)
if err != nil || w == nil {
// If w is nil, the op was synchronous.
return err
Expand All @@ -62,8 +65,8 @@ func activeDirectoryOperationWaitTimeWithResponse(config *Config, op map[string]
return json.Unmarshal([]byte(w.CommonOperationWaiter.Op.Response), response)
}

func activeDirectoryOperationWaitTime(config *Config, op map[string]interface{}, project, activity string, timeout time.Duration) error {
w, err := createActiveDirectoryWaiter(config, op, project, activity)
func activeDirectoryOperationWaitTime(config *Config, op map[string]interface{}, project, activity, userAgent string, timeout time.Duration) error {
w, err := createActiveDirectoryWaiter(config, op, project, activity, userAgent)
if err != nil || w == nil {
// If w is nil, the op was synchronous.
return err
Expand Down
4 changes: 2 additions & 2 deletions google/appengine_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (w *AppEngineOperationWaiter) QueryOp() (interface{}, error) {
return w.Service.Apps.Operations.Get(w.AppId, matches[1]).Do()
}

func appEngineOperationWaitTimeWithResponse(config *Config, res interface{}, response *map[string]interface{}, appId, activity string, timeout time.Duration) error {
func appEngineOperationWaitTimeWithResponse(config *Config, res interface{}, response *map[string]interface{}, appId, activity, userAgent string, timeout time.Duration) error {
op := &appengine.Operation{}
err := Convert(res, op)
if err != nil {
Expand All @@ -51,7 +51,7 @@ func appEngineOperationWaitTimeWithResponse(config *Config, res interface{}, res
return json.Unmarshal([]byte(w.CommonOperationWaiter.Op.Response), response)
}

func appEngineOperationWaitTime(config *Config, res interface{}, appId, activity string, timeout time.Duration) error {
func appEngineOperationWaitTime(config *Config, res interface{}, appId, activity, userAgent string, timeout time.Duration) error {
op := &appengine.Operation{}
err := Convert(res, op)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions google/bootstrap_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,13 @@ func BootstrapSharedTestNetwork(t *testing.T, testId string) string {
"autoCreateSubnetworks": false,
}

res, err := sendRequestWithTimeout(config, "POST", project, url, netObj, 4*time.Minute)
res, err := sendRequestWithTimeout(config, "POST", project, url, config.userAgent, netObj, 4*time.Minute)
if err != nil {
t.Fatalf("Error bootstrapping shared test network %q: %s", networkName, err)
}

log.Printf("[DEBUG] Waiting for network creation to finish")
err = computeOperationWaitTime(config, res, project, "Error bootstrapping shared test network", 4*time.Minute)
err = computeOperationWaitTime(config, res, project, "Error bootstrapping shared test network", config.userAgent, 4*time.Minute)
if err != nil {
t.Fatalf("Error bootstrapping shared test network %q: %s", networkName, err)
}
Expand Down Expand Up @@ -300,7 +300,7 @@ func BootstrapServicePerimeterProjects(t *testing.T, desiredProjects int) []*clo
t.Fatalf("Error bootstrapping shared test project: %s", err)
}

err = resourceManagerOperationWaitTime(config, opAsMap, "creating project", 4)
err = resourceManagerOperationWaitTime(config, opAsMap, "creating project", config.userAgent, 4)
if err != nil {
t.Fatalf("Error bootstrapping shared test project: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion google/composer_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (w *ComposerOperationWaiter) QueryOp() (interface{}, error) {
return w.Service.Operations.Get(w.Op.Name).Do()
}

func composerOperationWaitTime(config *Config, op *composer.Operation, project, activity string, timeout time.Duration) error {
func composerOperationWaitTime(config *Config, op *composer.Operation, project, activity, userAgent string, timeout time.Duration) error {
w := &ComposerOperationWaiter{
Service: config.clientComposer.Projects.Locations,
}
Expand Down
2 changes: 1 addition & 1 deletion google/compute_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (w *ComputeOperationWaiter) TargetStates() []string {
return []string{"DONE"}
}

func computeOperationWaitTime(config *Config, res interface{}, project, activity string, timeout time.Duration) error {
func computeOperationWaitTime(config *Config, res interface{}, project, activity, userAgent string, timeout time.Duration) error {
op := &compute.Operation{}
err := Convert(res, op)
if err != nil {
Expand Down
8 changes: 3 additions & 5 deletions google/data_source_dns_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,12 @@ func flattenDigests(dnsKeyDigests []*dns.DnsKeyDigest) []map[string]interface{}
}

func dataSourceDNSKeysRead(d *schema.ResourceData, meta interface{}) error {
var m providerMeta

err := d.GetProviderMeta(&m)
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
config := meta.(*Config)
config.clientDns.UserAgent = fmt.Sprintf("%s %s", config.clientDns.UserAgent, m.ModuleName)
config.clientDns.UserAgent = userAgent

fv, err := parseProjectFieldValue("managedZones", d.Get("managed_zone").(string), "project", d, config, false)
if err != nil {
Expand Down
8 changes: 3 additions & 5 deletions google/data_source_dns_managed_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,12 @@ func dataSourceDnsManagedZone() *schema.Resource {
}

func dataSourceDnsManagedZoneRead(d *schema.ResourceData, meta interface{}) error {
var m providerMeta

err := d.GetProviderMeta(&m)
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
config := meta.(*Config)
config.clientDns.UserAgent = fmt.Sprintf("%s %s", config.clientDns.UserAgent, m.ModuleName)
config.clientDns.UserAgent = userAgent

project, err := getProject(d, config)
if err != nil {
Expand Down
8 changes: 3 additions & 5 deletions google/data_source_google_active_folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ func dataSourceGoogleActiveFolder() *schema.Resource {
}

func dataSourceGoogleActiveFolderRead(d *schema.ResourceData, meta interface{}) error {
var m providerMeta

err := d.GetProviderMeta(&m)
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
config := meta.(*Config)
config.clientResourceManagerV2Beta1.UserAgent = fmt.Sprintf("%s %s", config.clientResourceManagerV2Beta1.UserAgent, m.ModuleName)
config.clientResourceManagerV2Beta1.UserAgent = userAgent

parent := d.Get("parent").(string)
displayName := d.Get("display_name").(string)
Expand Down
8 changes: 3 additions & 5 deletions google/data_source_google_bigquery_default_service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@ func dataSourceGoogleBigqueryDefaultServiceAccount() *schema.Resource {
}

func dataSourceGoogleBigqueryDefaultServiceAccountRead(d *schema.ResourceData, meta interface{}) error {
var m providerMeta

err := d.GetProviderMeta(&m)
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
config := meta.(*Config)
config.clientBigQuery.UserAgent = fmt.Sprintf("%s %s", config.clientBigQuery.UserAgent, m.ModuleName)
config.clientBigQuery.UserAgent = userAgent

project, err := getProject(d, config)
if err != nil {
Expand Down
8 changes: 3 additions & 5 deletions google/data_source_google_billing_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,12 @@ func dataSourceGoogleBillingAccount() *schema.Resource {
}

func dataSourceBillingAccountRead(d *schema.ResourceData, meta interface{}) error {
var m providerMeta

err := d.GetProviderMeta(&m)
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
config := meta.(*Config)
config.clientBilling.UserAgent = fmt.Sprintf("%s %s", config.clientBilling.UserAgent, m.ModuleName)
config.clientBilling.UserAgent = userAgent

open, openOk := d.GetOkExists("open")

Expand Down
9 changes: 3 additions & 6 deletions google/data_source_google_client_openid_userinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@ func dataSourceGoogleClientOpenIDUserinfo() *schema.Resource {
}

func dataSourceGoogleClientOpenIDUserinfoRead(d *schema.ResourceData, meta interface{}) error {
var m providerMeta

err := d.GetProviderMeta(&m)
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
config := meta.(*Config)
config.userAgent = fmt.Sprintf("%s %s", config.userAgent, m.ModuleName)

email, err := GetCurrentUserEmail(config)
email, err := GetCurrentUserEmail(config, userAgent)
if err != nil {
return err
}
Expand Down
9 changes: 3 additions & 6 deletions google/data_source_google_composer_image_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,11 @@ func dataSourceGoogleComposerImageVersions() *schema.Resource {
}

func dataSourceGoogleComposerImageVersionsRead(d *schema.ResourceData, meta interface{}) error {
var m providerMeta

err := d.GetProviderMeta(&m)
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
config := meta.(*Config)
config.userAgent = fmt.Sprintf("%s %s", config.userAgent, m.ModuleName)

project, err := getProject(d, config)
if err != nil {
Expand All @@ -67,7 +64,7 @@ func dataSourceGoogleComposerImageVersionsRead(d *schema.ResourceData, meta inte
return err
}

versions, err := paginatedListRequest(project, url, config, flattenGoogleComposerImageVersions)
versions, err := paginatedListRequest(project, url, userAgent, config, flattenGoogleComposerImageVersions)
if err != nil {
return fmt.Errorf("Error listing Composer image versions: %s", err)
}
Expand Down
8 changes: 3 additions & 5 deletions google/data_source_google_compute_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,12 @@ func dataSourceGoogleComputeAddress() *schema.Resource {
}

func dataSourceGoogleComputeAddressRead(d *schema.ResourceData, meta interface{}) error {
var m providerMeta

err := d.GetProviderMeta(&m)
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
config := meta.(*Config)
config.clientCompute.UserAgent = fmt.Sprintf("%s %s", config.clientCompute.UserAgent, m.ModuleName)
config.clientCompute.UserAgent = userAgent

project, err := getProject(d, config)
if err != nil {
Expand Down
8 changes: 3 additions & 5 deletions google/data_source_google_compute_default_service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ func dataSourceGoogleComputeDefaultServiceAccount() *schema.Resource {
}

func dataSourceGoogleComputeDefaultServiceAccountRead(d *schema.ResourceData, meta interface{}) error {
var m providerMeta

err := d.GetProviderMeta(&m)
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
config := meta.(*Config)
config.clientCompute.UserAgent = fmt.Sprintf("%s %s", config.clientCompute.UserAgent, m.ModuleName)
config.clientCompute.UserAgent = userAgent

project, err := getProject(d, config)
if err != nil {
Expand Down
8 changes: 3 additions & 5 deletions google/data_source_google_compute_forwarding_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,12 @@ func dataSourceGoogleComputeForwardingRule() *schema.Resource {
}

func dataSourceGoogleComputeForwardingRuleRead(d *schema.ResourceData, meta interface{}) error {
var m providerMeta

err := d.GetProviderMeta(&m)
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
config := meta.(*Config)
config.clientCompute.UserAgent = fmt.Sprintf("%s %s", config.clientCompute.UserAgent, m.ModuleName)
config.clientCompute.UserAgent = userAgent

region, err := getRegion(d, config)
if err != nil {
Expand Down
8 changes: 3 additions & 5 deletions google/data_source_google_compute_global_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,12 @@ func dataSourceGoogleComputeGlobalAddress() *schema.Resource {
}

func dataSourceGoogleComputeGlobalAddressRead(d *schema.ResourceData, meta interface{}) error {
var m providerMeta

err := d.GetProviderMeta(&m)
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
config := meta.(*Config)
config.clientCompute.UserAgent = fmt.Sprintf("%s %s", config.clientCompute.UserAgent, m.ModuleName)
config.clientCompute.UserAgent = userAgent

project, err := getProject(d, config)
if err != nil {
Expand Down
Loading

0 comments on commit 08c9e64

Please sign in to comment.