Skip to content

Commit

Permalink
Add billing_project configuration to the provider (#3886) (#7113)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Aug 26, 2020
1 parent 71ebc32 commit cf269e7
Show file tree
Hide file tree
Showing 165 changed files with 5,673 additions and 663 deletions.
3 changes: 3 additions & 0 deletions .changelog/3886.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
provider: added a new field `billing_project` to the provider that's associated as a billing/quota project with most requests when `user_project_override` true
```
1 change: 1 addition & 0 deletions google/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Config struct {
Credentials string
AccessToken string
Project string
BillingProject string
Region string
Zone string
Scopes []string
Expand Down
11 changes: 11 additions & 0 deletions google/field_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,17 @@ func getProjectFromSchema(projectSchemaField string, d TerraformResourceData, co
return "", fmt.Errorf("%s: required field is not set", projectSchemaField)
}

func getBillingProjectFromSchema(billingProjectSchemaField string, d TerraformResourceData, config *Config) (string, error) {
res, ok := d.GetOk(billingProjectSchemaField)
if ok && billingProjectSchemaField != "" {
return res.(string), nil
}
if config.BillingProject != "" {
return config.BillingProject, nil
}
return "", fmt.Errorf("%s: required field is not set", billingProjectSchemaField)
}

type OrganizationFieldValue struct {
OrgId string
Name string
Expand Down
12 changes: 12 additions & 0 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ func Provider() terraform.ResourceProvider {
}, nil),
},

"billing_project": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"GOOGLE_BILLING_PROJECT",
}, nil),
},

"region": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -101,6 +109,9 @@ func Provider() terraform.ResourceProvider {
"user_project_override": {
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"USER_PROJECT_OVERRIDE",
}, nil),
},

"request_timeout": {
Expand Down Expand Up @@ -987,6 +998,7 @@ func providerConfigure(d *schema.ResourceData, p *schema.Provider, terraformVers
Region: d.Get("region").(string),
Zone: d.Get("zone").(string),
UserProjectOverride: d.Get("user_project_override").(bool),
BillingProject: d.Get("billing_project").(string),
terraformVersion: terraformVersion,
}

Expand Down
28 changes: 25 additions & 3 deletions google/resource_access_approval_folder_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ func resourceAccessApprovalFolderSettingsCreate(d *schema.ResourceData, meta int
}

log.Printf("[DEBUG] Creating new FolderSettings: %#v", obj)
billingProject := ""

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}

updateMask := []string{}

if d.HasChange("notification_emails") {
Expand All @@ -154,7 +161,7 @@ func resourceAccessApprovalFolderSettingsCreate(d *schema.ResourceData, meta int
if err != nil {
return err
}
res, err := sendRequestWithTimeout(config, "PATCH", "", url, obj, d.Timeout(schema.TimeoutCreate))
res, err := sendRequestWithTimeout(config, "PATCH", billingProject, url, obj, d.Timeout(schema.TimeoutCreate))
if err != nil {
return fmt.Errorf("Error creating FolderSettings: %s", err)
}
Expand Down Expand Up @@ -182,7 +189,14 @@ func resourceAccessApprovalFolderSettingsRead(d *schema.ResourceData, meta inter
return err
}

res, err := sendRequest(config, "GET", "", url, nil)
billingProject := ""

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}

res, err := sendRequest(config, "GET", billingProject, url, nil)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("AccessApprovalFolderSettings %q", d.Id()))
}
Expand All @@ -206,6 +220,8 @@ func resourceAccessApprovalFolderSettingsRead(d *schema.ResourceData, meta inter
func resourceAccessApprovalFolderSettingsUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

billingProject := ""

obj := make(map[string]interface{})
notificationEmailsProp, err := expandAccessApprovalFolderSettingsNotificationEmails(d.Get("notification_emails"), d, config)
if err != nil {
Expand Down Expand Up @@ -241,7 +257,13 @@ func resourceAccessApprovalFolderSettingsUpdate(d *schema.ResourceData, meta int
if err != nil {
return err
}
res, err := sendRequestWithTimeout(config, "PATCH", "", url, obj, d.Timeout(schema.TimeoutUpdate))

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}

res, err := sendRequestWithTimeout(config, "PATCH", billingProject, url, obj, d.Timeout(schema.TimeoutUpdate))

if err != nil {
return fmt.Errorf("Error updating FolderSettings %q: %s", d.Id(), err)
Expand Down
28 changes: 25 additions & 3 deletions google/resource_access_approval_organization_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ func resourceAccessApprovalOrganizationSettingsCreate(d *schema.ResourceData, me
}

log.Printf("[DEBUG] Creating new OrganizationSettings: %#v", obj)
billingProject := ""

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}

updateMask := []string{}

if d.HasChange("notification_emails") {
Expand All @@ -154,7 +161,7 @@ func resourceAccessApprovalOrganizationSettingsCreate(d *schema.ResourceData, me
if err != nil {
return err
}
res, err := sendRequestWithTimeout(config, "PATCH", "", url, obj, d.Timeout(schema.TimeoutCreate))
res, err := sendRequestWithTimeout(config, "PATCH", billingProject, url, obj, d.Timeout(schema.TimeoutCreate))
if err != nil {
return fmt.Errorf("Error creating OrganizationSettings: %s", err)
}
Expand Down Expand Up @@ -182,7 +189,14 @@ func resourceAccessApprovalOrganizationSettingsRead(d *schema.ResourceData, meta
return err
}

res, err := sendRequest(config, "GET", "", url, nil)
billingProject := ""

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}

res, err := sendRequest(config, "GET", billingProject, url, nil)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("AccessApprovalOrganizationSettings %q", d.Id()))
}
Expand All @@ -206,6 +220,8 @@ func resourceAccessApprovalOrganizationSettingsRead(d *schema.ResourceData, meta
func resourceAccessApprovalOrganizationSettingsUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

billingProject := ""

obj := make(map[string]interface{})
notificationEmailsProp, err := expandAccessApprovalOrganizationSettingsNotificationEmails(d.Get("notification_emails"), d, config)
if err != nil {
Expand Down Expand Up @@ -241,7 +257,13 @@ func resourceAccessApprovalOrganizationSettingsUpdate(d *schema.ResourceData, me
if err != nil {
return err
}
res, err := sendRequestWithTimeout(config, "PATCH", "", url, obj, d.Timeout(schema.TimeoutUpdate))

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}

res, err := sendRequestWithTimeout(config, "PATCH", billingProject, url, obj, d.Timeout(schema.TimeoutUpdate))

if err != nil {
return fmt.Errorf("Error updating OrganizationSettings %q: %s", d.Id(), err)
Expand Down
33 changes: 30 additions & 3 deletions google/resource_access_approval_project_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,19 @@ func resourceAccessApprovalProjectSettingsCreate(d *schema.ResourceData, meta in
}

log.Printf("[DEBUG] Creating new ProjectSettings: %#v", obj)
billingProject := ""

project, err := getProject(d, config)
if err != nil {
return err
}
billingProject = project

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}

updateMask := []string{}

if d.HasChange("notification_emails") {
Expand All @@ -164,7 +173,7 @@ func resourceAccessApprovalProjectSettingsCreate(d *schema.ResourceData, meta in
if err != nil {
return err
}
res, err := sendRequestWithTimeout(config, "PATCH", project, url, obj, d.Timeout(schema.TimeoutCreate))
res, err := sendRequestWithTimeout(config, "PATCH", billingProject, url, obj, d.Timeout(schema.TimeoutCreate))
if err != nil {
return fmt.Errorf("Error creating ProjectSettings: %s", err)
}
Expand Down Expand Up @@ -192,11 +201,20 @@ func resourceAccessApprovalProjectSettingsRead(d *schema.ResourceData, meta inte
return err
}

billingProject := ""

project, err := getProject(d, config)
if err != nil {
return err
}
res, err := sendRequest(config, "GET", project, url, nil)
billingProject = project

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}

res, err := sendRequest(config, "GET", billingProject, url, nil)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("AccessApprovalProjectSettings %q", d.Id()))
}
Expand Down Expand Up @@ -224,10 +242,13 @@ func resourceAccessApprovalProjectSettingsRead(d *schema.ResourceData, meta inte
func resourceAccessApprovalProjectSettingsUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

billingProject := ""

project, err := getProject(d, config)
if err != nil {
return err
}
billingProject = project

obj := make(map[string]interface{})
notificationEmailsProp, err := expandAccessApprovalProjectSettingsNotificationEmails(d.Get("notification_emails"), d, config)
Expand Down Expand Up @@ -264,7 +285,13 @@ func resourceAccessApprovalProjectSettingsUpdate(d *schema.ResourceData, meta in
if err != nil {
return err
}
res, err := sendRequestWithTimeout(config, "PATCH", project, url, obj, d.Timeout(schema.TimeoutUpdate))

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}

res, err := sendRequestWithTimeout(config, "PATCH", billingProject, url, obj, d.Timeout(schema.TimeoutUpdate))

if err != nil {
return fmt.Errorf("Error updating ProjectSettings %q: %s", d.Id(), err)
Expand Down
37 changes: 33 additions & 4 deletions google/resource_access_context_manager_access_level.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,14 @@ 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))
billingProject := ""

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}

res, err := sendRequestWithTimeout(config, "POST", billingProject, url, obj, d.Timeout(schema.TimeoutCreate))
if err != nil {
return fmt.Errorf("Error creating AccessLevel: %s", err)
}
Expand Down Expand Up @@ -380,7 +387,14 @@ func resourceAccessContextManagerAccessLevelRead(d *schema.ResourceData, meta in
return err
}

res, err := sendRequest(config, "GET", "", url, nil)
billingProject := ""

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}

res, err := sendRequest(config, "GET", billingProject, url, nil)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("AccessContextManagerAccessLevel %q", d.Id()))
}
Expand All @@ -407,6 +421,8 @@ func resourceAccessContextManagerAccessLevelRead(d *schema.ResourceData, meta in
func resourceAccessContextManagerAccessLevelUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

billingProject := ""

obj := make(map[string]interface{})
titleProp, err := expandAccessContextManagerAccessLevelTitle(d.Get("title"), d, config)
if err != nil {
Expand Down Expand Up @@ -467,7 +483,13 @@ func resourceAccessContextManagerAccessLevelUpdate(d *schema.ResourceData, meta
if err != nil {
return err
}
res, err := sendRequestWithTimeout(config, "PATCH", "", url, obj, d.Timeout(schema.TimeoutUpdate))

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}

res, err := sendRequestWithTimeout(config, "PATCH", billingProject, url, obj, d.Timeout(schema.TimeoutUpdate))

if err != nil {
return fmt.Errorf("Error updating AccessLevel %q: %s", d.Id(), err)
Expand All @@ -489,6 +511,8 @@ func resourceAccessContextManagerAccessLevelUpdate(d *schema.ResourceData, meta
func resourceAccessContextManagerAccessLevelDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

billingProject := ""

url, err := replaceVars(d, config, "{{AccessContextManagerBasePath}}{{name}}")
if err != nil {
return err
Expand All @@ -497,7 +521,12 @@ 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))
// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}

res, err := sendRequestWithTimeout(config, "DELETE", billingProject, url, obj, d.Timeout(schema.TimeoutDelete))
if err != nil {
return handleNotFoundError(err, d, "AccessLevel")
}
Expand Down
Loading

0 comments on commit cf269e7

Please sign in to comment.