Skip to content

Commit

Permalink
allow {{project_id_or_project}} in assetName (#246)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored and nat-henderson committed Nov 12, 2019
1 parent d32ae05 commit d8f1980
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
4 changes: 2 additions & 2 deletions google/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func GetProjectCaiObject(d TerraformResourceData, config *Config) (Asset, error) {
// NOTE: asset.name should use the project number, but we use project_id b/c
// the number is computed server-side.
name, err := assetName(d, config, "//cloudresourcemanager.googleapis.com/projects/{{project}}")
name, err := assetName(d, config, "//cloudresourcemanager.googleapis.com/projects/{{project_id_or_project}}")
if err != nil {
return Asset{}, err
}
Expand Down Expand Up @@ -76,7 +76,7 @@ func getParentResourceId(d TerraformResourceData, p *cloudresourcemanager.Projec
}

func GetProjectBillingInfoCaiObject(d TerraformResourceData, config *Config) (Asset, error) {
name, err := assetName(d, config, "//cloudbilling.googleapis.com/projects/{{project}}/billingInfo")
name, err := assetName(d, config, "//cloudbilling.googleapis.com/projects/{{project_id_or_project}}/billingInfo")
if err != nil {
return Asset{}, err
}
Expand Down
23 changes: 21 additions & 2 deletions google/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ func replaceVars(d TerraformResourceData, config *Config, linkTmpl string) (stri
}

// This function replaces references to Terraform properties (in the form of {{var}}) with their value in Terraform
// It also replaces {{project}}, {{region}}, and {{zone}} with their appropriate values
// It also replaces {{project}}, {{project_id_or_project}}, {{region}}, and {{zone}} with their appropriate values
// This function supports URL-encoding the result by prepending '%' to the field name e.g. {{%var}}
func buildReplacementFunc(re *regexp.Regexp, d TerraformResourceData, config *Config, linkTmpl string) (func(string) string, error) {
var project, region, zone string
var project, projectID, region, zone string
var err error

if strings.Contains(linkTmpl, "{{project}}") {
Expand All @@ -150,6 +150,19 @@ func buildReplacementFunc(re *regexp.Regexp, d TerraformResourceData, config *Co
}
}

if strings.Contains(linkTmpl, "{{project_id_or_project}}") {
v, ok := d.GetOkExists("project_id")
if ok {
projectID, _ = v.(string)
}
if projectID == "" {
project, err = getProject(d, config)
}
if err != nil {
return nil, err
}
}

if strings.Contains(linkTmpl, "{{region}}") {
region, err = getRegion(d, config)
if err != nil {
Expand All @@ -169,6 +182,12 @@ func buildReplacementFunc(re *regexp.Regexp, d TerraformResourceData, config *Co
if m == "project" {
return project
}
if m == "project_id_or_project" {
if projectID != "" {
return projectID
}
return project
}
if m == "region" {
return region
}
Expand Down

0 comments on commit d8f1980

Please sign in to comment.