Skip to content

Commit

Permalink
add support for user project overrides
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
danawillow authored and modular-magician committed Aug 9, 2019
1 parent 6d0c3a0 commit 7f24e12
Show file tree
Hide file tree
Showing 210 changed files with 1,737 additions and 1,135 deletions.
2 changes: 1 addition & 1 deletion google/access_context_manager_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ 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, nil)
}

func accessContextManagerOperationWaitTime(config *Config, op map[string]interface{}, activity string, timeoutMinutes int) error {
Expand Down
15 changes: 8 additions & 7 deletions google/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ import (
// Config is the configuration structure used to instantiate the Google
// provider.
type Config struct {
Credentials string
AccessToken string
Project string
Region string
Zone string
Scopes []string
BatchingConfig *batchingConfig
Credentials string
AccessToken string
Project string
Region string
Zone string
Scopes []string
BatchingConfig *batchingConfig
UserProjectOverride bool

client *http.Client
userAgent string
Expand Down
2 changes: 1 addition & 1 deletion google/data_source_google_client_openid_userinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func dataSourceGoogleClientOpenIDUserinfoRead(d *schema.ResourceData, meta inter

// See https://github.com/golang/oauth2/issues/306 for a recommendation to do this from a Go maintainer
// URL retrieved from https://accounts.google.com/.well-known/openid-configuration
res, err := sendRequest(config, "GET", "https://openidconnect.googleapis.com/v1/userinfo", nil)
res, err := sendRequest(config, "GET", "", "https://openidconnect.googleapis.com/v1/userinfo", nil)
if err != nil {
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)
}
Expand Down
2 changes: 1 addition & 1 deletion google/data_source_google_composer_image_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func dataSourceGoogleComposerImageVersionsRead(d *schema.ResourceData, meta inte
return err
}

versions, err := paginatedListRequest(url, config, flattenGoogleComposerImageVersions)
versions, err := paginatedListRequest(project, url, config, flattenGoogleComposerImageVersions)
if err != nil {
return fmt.Errorf("Error listing Composer image versions: %s", err)
}
Expand Down
11 changes: 8 additions & 3 deletions google/data_source_google_kms_crypto_key_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ func dataSourceGoogleKmsCryptoKeyVersionRead(d *schema.ResourceData, meta interf
}

log.Printf("[DEBUG] Getting attributes for CryptoKeyVersion: %#v", url)
res, err := sendRequest(config, "GET", url, nil)

cryptoKeyId, err := parseKmsCryptoKeyId(d.Get("crypto_key").(string), config)
if err != nil {
return err
}
res, err := sendRequest(config, "GET", cryptoKeyId.KeyRingId.Project, url, nil)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("KmsCryptoKeyVersion %q", d.Id()))
}
Expand All @@ -89,7 +94,7 @@ func dataSourceGoogleKmsCryptoKeyVersionRead(d *schema.ResourceData, meta interf
}

log.Printf("[DEBUG] Getting purpose of CryptoKey: %#v", url)
res, err = sendRequest(config, "GET", url, nil)
res, err = sendRequest(config, "GET", cryptoKeyId.KeyRingId.Project, url, nil)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("KmsCryptoKey %q", d.Id()))
}
Expand All @@ -100,7 +105,7 @@ func dataSourceGoogleKmsCryptoKeyVersionRead(d *schema.ResourceData, meta interf
return err
}
log.Printf("[DEBUG] Getting public key of CryptoKeyVersion: %#v", url)
res, _ = sendRequest(config, "GET", url, nil)
res, _ = sendRequest(config, "GET", cryptoKeyId.KeyRingId.Project, url, nil)

if err := d.Set("public_key", flattenKmsCryptoKeyVersionPublicKey(res, d)); err != nil {
return fmt.Errorf("Error reading CryptoKeyVersion public key: %s", err)
Expand Down
3 changes: 2 additions & 1 deletion google/data_source_google_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package google

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"
)

Expand Down Expand Up @@ -44,7 +45,7 @@ func datasourceGoogleProjectsRead(d *schema.ResourceData, meta interface{}) erro
return err
}

res, err := sendRequest(config, "GET", url, nil)
res, err := sendRequest(config, "GET", "", url, nil)
if err != nil {
return fmt.Errorf("Error retrieving projects: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion google/data_source_google_storage_bucket_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func dataSourceGoogleStorageBucketObjectRead(d *schema.ResourceData, meta interf
// Using REST apis because the storage go client doesn't support folders
url := fmt.Sprintf("https://www.googleapis.com/storage/v1/b/%s/o/%s", bucket, name)

res, err := sendRequest(config, "GET", url, nil)
res, err := sendRequest(config, "GET", "", url, nil)
if err != nil {
return fmt.Errorf("Error retrieving storage bucket object: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion google/data_source_tpu_tensorflow_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func dataSourceTpuTensorFlowVersionsRead(d *schema.ResourceData, meta interface{
return err
}

versionsRaw, err := paginatedListRequest(url, config, flattenTpuTensorflowVersions)
versionsRaw, err := paginatedListRequest(project, url, config, flattenTpuTensorflowVersions)
if err != nil {
return fmt.Errorf("Error listing TPU Tensorflow versions: %s", err)
}
Expand Down
8 changes: 5 additions & 3 deletions google/filestore_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (
)

type FilestoreOperationWaiter struct {
Config *Config
Config *Config
Project string
CommonOperationWaiter
}

Expand All @@ -28,7 +29,7 @@ func (w *FilestoreOperationWaiter) QueryOp() (interface{}, error) {
}
// Returns the proper get.
url := fmt.Sprintf("https://file.googleapis.com/v1/%s", w.CommonOperationWaiter.Op.Name)
return sendRequest(w.Config, "GET", url, nil)
return sendRequest(w.Config, "GET", w.Project, url, nil)
}

func filestoreOperationWaitTime(config *Config, op map[string]interface{}, project, activity string, timeoutMinutes int) error {
Expand All @@ -37,7 +38,8 @@ func filestoreOperationWaitTime(config *Config, op map[string]interface{}, proje
return nil
}
w := &FilestoreOperationWaiter{
Config: config,
Config: config,
Project: project,
}
if err := w.CommonOperationWaiter.SetOp(op); err != nil {
return err
Expand Down
8 changes: 5 additions & 3 deletions google/firestore_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (
)

type FirestoreOperationWaiter struct {
Config *Config
Config *Config
Project string
CommonOperationWaiter
}

Expand All @@ -28,7 +29,7 @@ func (w *FirestoreOperationWaiter) QueryOp() (interface{}, error) {
}
// Returns the proper get.
url := fmt.Sprintf("https://firestore.googleapis.com/v1/%s", w.CommonOperationWaiter.Op.Name)
return sendRequest(w.Config, "GET", url, nil)
return sendRequest(w.Config, "GET", w.Project, url, nil)
}

func firestoreOperationWaitTime(config *Config, op map[string]interface{}, project, activity string, timeoutMinutes int) error {
Expand All @@ -37,7 +38,8 @@ func firestoreOperationWaitTime(config *Config, op map[string]interface{}, proje
return nil
}
w := &FirestoreOperationWaiter{
Config: config,
Config: config,
Project: project,
}
if err := w.CommonOperationWaiter.SetOp(op); err != nil {
return err
Expand Down
14 changes: 12 additions & 2 deletions google/iam_pubsub_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ func PubsubTopicIdParseFunc(d *schema.ResourceData, config *Config) error {
func (u *PubsubTopicIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) {
url := u.qualifyTopicUrl("getIamPolicy")

policy, err := sendRequest(u.Config, "GET", url, nil)
project, err := getProject(u.d, u.Config)
if err != nil {
return nil, err
}

policy, err := sendRequest(u.Config, "GET", project, url, nil)
if err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("Error retrieving IAM policy for %s: {{err}}", u.DescribeResource()), err)
}
Expand All @@ -121,7 +126,12 @@ func (u *PubsubTopicIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanage

url := u.qualifyTopicUrl("setIamPolicy")

_, err = sendRequestWithTimeout(u.Config, "POST", url, obj, u.d.Timeout(schema.TimeoutCreate))
project, err := getProject(u.d, u.Config)
if err != nil {
return err
}

_, err = sendRequestWithTimeout(u.Config, "POST", project, url, obj, u.d.Timeout(schema.TimeoutCreate))
if err != nil {
return errwrap.Wrapf(fmt.Sprintf("Error setting IAM policy for %s: {{err}}", u.DescribeResource()), err)
}
Expand Down
14 changes: 12 additions & 2 deletions google/iam_source_repo_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ func SourceRepoRepositoryIdParseFunc(d *schema.ResourceData, config *Config) err
func (u *SourceRepoRepositoryIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) {
url := u.qualifyRepositoryUrl("getIamPolicy")

policy, err := sendRequest(u.Config, "GET", url, nil)
project, err := getProject(u.d, u.Config)
if err != nil {
return nil, err
}

policy, err := sendRequest(u.Config, "GET", project, url, nil)
if err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("Error retrieving IAM policy for %s: {{err}}", u.DescribeResource()), err)
}
Expand All @@ -121,7 +126,12 @@ func (u *SourceRepoRepositoryIamUpdater) SetResourceIamPolicy(policy *cloudresou

url := u.qualifyRepositoryUrl("setIamPolicy")

_, err = sendRequestWithTimeout(u.Config, "POST", url, obj, u.d.Timeout(schema.TimeoutCreate))
project, err := getProject(u.d, u.Config)
if err != nil {
return err
}

_, err = sendRequestWithTimeout(u.Config, "POST", project, url, obj, u.d.Timeout(schema.TimeoutCreate))
if err != nil {
return errwrap.Wrapf(fmt.Sprintf("Error setting IAM policy for %s: {{err}}", u.DescribeResource()), err)
}
Expand Down
12 changes: 9 additions & 3 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ func Provider() terraform.ResourceProvider {
},
},

"user_project_override": {
Type: schema.TypeBool,
Optional: true,
},

// Generated Products
AccessContextManagerCustomEndpointEntryKey: AccessContextManagerCustomEndpointEntry,
AppEngineCustomEndpointEntryKey: AppEngineCustomEndpointEntry,
Expand Down Expand Up @@ -355,9 +360,10 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config := Config{
Project: d.Get("project").(string),
Region: d.Get("region").(string),
Zone: d.Get("zone").(string),
Project: d.Get("project").(string),
Region: d.Get("region").(string),
Zone: d.Get("zone").(string),
UserProjectOverride: d.Get("user_project_override").(bool),
}

// Add credential source
Expand Down
8 changes: 5 additions & 3 deletions google/redis_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (
)

type RedisOperationWaiter struct {
Config *Config
Config *Config
Project string
CommonOperationWaiter
}

Expand All @@ -28,7 +29,7 @@ func (w *RedisOperationWaiter) QueryOp() (interface{}, error) {
}
// Returns the proper get.
url := fmt.Sprintf("https://redis.googleapis.com/v1/%s", w.CommonOperationWaiter.Op.Name)
return sendRequest(w.Config, "GET", url, nil)
return sendRequest(w.Config, "GET", w.Project, url, nil)
}

func redisOperationWaitTime(config *Config, op map[string]interface{}, project, activity string, timeoutMinutes int) error {
Expand All @@ -37,7 +38,8 @@ func redisOperationWaitTime(config *Config, op map[string]interface{}, project,
return nil
}
w := &RedisOperationWaiter{
Config: config,
Config: config,
Project: project,
}
if err := w.CommonOperationWaiter.SetOp(op); err != nil {
return err
Expand Down
9 changes: 5 additions & 4 deletions google/resource_access_context_manager_access_level.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func resourceAccessContextManagerAccessLevelCreate(d *schema.ResourceData, meta
}

log.Printf("[DEBUG] Creating new AccessLevel: %#v", obj)
res, err := sendRequestWithTimeout(config, "POST", url, obj, d.Timeout(schema.TimeoutCreate))
res, err := sendRequestWithTimeout(config, "POST", "", url, obj, d.Timeout(schema.TimeoutCreate))
if err != nil {
return fmt.Errorf("Error creating AccessLevel: %s", err)
}
Expand Down Expand Up @@ -239,7 +239,7 @@ func resourceAccessContextManagerAccessLevelRead(d *schema.ResourceData, meta in
return err
}

res, err := sendRequest(config, "GET", url, nil)
res, err := sendRequest(config, "GET", "", url, nil)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("AccessContextManagerAccessLevel %q", d.Id()))
}
Expand Down Expand Up @@ -313,7 +313,7 @@ func resourceAccessContextManagerAccessLevelUpdate(d *schema.ResourceData, meta
if err != nil {
return err
}
res, err := sendRequestWithTimeout(config, "PATCH", url, obj, d.Timeout(schema.TimeoutUpdate))
res, err := sendRequestWithTimeout(config, "PATCH", "", url, obj, d.Timeout(schema.TimeoutUpdate))

if err != nil {
return fmt.Errorf("Error updating AccessLevel %q: %s", d.Id(), err)
Expand All @@ -340,7 +340,8 @@ func resourceAccessContextManagerAccessLevelDelete(d *schema.ResourceData, meta

var obj map[string]interface{}
log.Printf("[DEBUG] Deleting AccessLevel %q", d.Id())
res, err := sendRequestWithTimeout(config, "DELETE", url, obj, d.Timeout(schema.TimeoutDelete))

res, err := sendRequestWithTimeout(config, "DELETE", "", url, obj, d.Timeout(schema.TimeoutDelete))
if err != nil {
return handleNotFoundError(err, d, "AccessLevel")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func testAccCheckAccessContextManagerAccessLevelDestroy(s *terraform.State) erro
return err
}

_, err = sendRequest(config, "GET", url, nil)
_, err = sendRequest(config, "GET", "", url, nil)
if err == nil {
return fmt.Errorf("AccessLevel still exists at %s", url)
}
Expand Down
9 changes: 5 additions & 4 deletions google/resource_access_context_manager_access_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func resourceAccessContextManagerAccessPolicyCreate(d *schema.ResourceData, meta
}

log.Printf("[DEBUG] Creating new AccessPolicy: %#v", obj)
res, err := sendRequestWithTimeout(config, "POST", url, obj, d.Timeout(schema.TimeoutCreate))
res, err := sendRequestWithTimeout(config, "POST", "", url, obj, d.Timeout(schema.TimeoutCreate))
if err != nil {
return fmt.Errorf("Error creating AccessPolicy: %s", err)
}
Expand Down Expand Up @@ -135,7 +135,7 @@ func resourceAccessContextManagerAccessPolicyRead(d *schema.ResourceData, meta i
return err
}

res, err := sendRequest(config, "GET", url, nil)
res, err := sendRequest(config, "GET", "", url, nil)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("AccessContextManagerAccessPolicy %q", d.Id()))
}
Expand Down Expand Up @@ -187,7 +187,7 @@ func resourceAccessContextManagerAccessPolicyUpdate(d *schema.ResourceData, meta
if err != nil {
return err
}
res, err := sendRequestWithTimeout(config, "PATCH", url, obj, d.Timeout(schema.TimeoutUpdate))
res, err := sendRequestWithTimeout(config, "PATCH", "", url, obj, d.Timeout(schema.TimeoutUpdate))

if err != nil {
return fmt.Errorf("Error updating AccessPolicy %q: %s", d.Id(), err)
Expand All @@ -214,7 +214,8 @@ func resourceAccessContextManagerAccessPolicyDelete(d *schema.ResourceData, meta

var obj map[string]interface{}
log.Printf("[DEBUG] Deleting AccessPolicy %q", d.Id())
res, err := sendRequestWithTimeout(config, "DELETE", url, obj, d.Timeout(schema.TimeoutDelete))

res, err := sendRequestWithTimeout(config, "DELETE", "", url, obj, d.Timeout(schema.TimeoutDelete))
if err != nil {
return handleNotFoundError(err, d, "AccessPolicy")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func testAccCheckAccessContextManagerAccessPolicyDestroy(s *terraform.State) err
return err
}

_, err = sendRequest(config, "GET", url, nil)
_, err = sendRequest(config, "GET", "", url, nil)
if err == nil {
return fmt.Errorf("AccessPolicy still exists at %s", url)
}
Expand Down
Loading

0 comments on commit 7f24e12

Please sign in to comment.