Skip to content

Commit

Permalink
Allow setting folder_id to empty string on google_project (#1425)
Browse files Browse the repository at this point in the history
* Allow setting folder_id to empty string on google_project

* Simplify logic

* Update Changelog
  • Loading branch information
rosbo authored May 3, 2018
1 parent 662d38d commit 135f5c4
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ FEATURES:
IMPROVEMENTS:
* compute: Autogenerate `google_vpn_gateway` [GH-1409]
* compute: add `enable_flow_logs` field to subnetwork [GH-1385]
* project: Don't fail if `folder_id` and `org_id` are set but one is empty for `google_project` [GH-1425]

## 1.11.0 (May 01, 2018)

Expand Down
41 changes: 25 additions & 16 deletions google/resource_google_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,15 @@ func resourceGoogleProject() *schema.Resource {
Required: true,
},
"org_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"folder_id"},
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"folder_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"org_id"},
StateFunc: parseFolderId,
Type: schema.TypeString,
Optional: true,
Computed: true,
StateFunc: parseFolderId,
},
"policy_data": &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -105,7 +103,9 @@ func resourceGoogleProjectCreate(d *schema.ResourceData, meta interface{}) error
Name: d.Get("name").(string),
}

getParentResourceId(d, project)
if err := getParentResourceId(d, project); err != nil {
return err
}

if _, ok := d.GetOk("labels"); ok {
project.Labels = expandLabels(d)
Expand Down Expand Up @@ -215,20 +215,27 @@ func prefixedProject(pid string) string {
}

func getParentResourceId(d *schema.ResourceData, p *cloudresourcemanager.Project) error {
if v, ok := d.GetOk("org_id"); ok {
org_id := v.(string)
orgId := d.Get("org_id").(string)
folderId := d.Get("folder_id").(string)

if orgId != "" && folderId != "" {
return fmt.Errorf("'org_id' and 'folder_id' cannot be both set.")
}

if orgId != "" {
p.Parent = &cloudresourcemanager.ResourceId{
Id: org_id,
Id: orgId,
Type: "organization",
}
}

if v, ok := d.GetOk("folder_id"); ok {
if folderId != "" {
p.Parent = &cloudresourcemanager.ResourceId{
Id: parseFolderId(v),
Id: parseFolderId(folderId),
Type: "folder",
}
}

return nil
}

Expand Down Expand Up @@ -271,7 +278,9 @@ func resourceGoogleProjectUpdate(d *schema.ResourceData, meta interface{}) error

// Project parent has changed
if d.HasChange("org_id") || d.HasChange("folder_id") {
getParentResourceId(d, p)
if err := getParentResourceId(d, p); err != nil {
return err
}

// Do update on project
p, err = config.clientResourceManager.Projects.Update(p.ProjectId, p).Do()
Expand Down
35 changes: 35 additions & 0 deletions google/resource_google_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,23 @@ func TestAccProject_deleteDefaultNetwork(t *testing.T) {
})
}

func TestAccProject_parentFolder(t *testing.T) {
t.Parallel()

org := getTestOrgFromEnv(t)
pid := "terraform-" + acctest.RandString(10)
folderDisplayName := "tf-test-" + acctest.RandString(10)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccProject_parentFolder(pid, pname, folderDisplayName, org),
},
},
})
}

func testAccCheckGoogleProjectExists(r, pid string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[r]
Expand Down Expand Up @@ -314,6 +331,24 @@ resource "google_project" "acceptance" {
}`, pid, name, org, billing)
}

func testAccProject_parentFolder(pid, projectName, folderName, org string) string {
return fmt.Sprintf(`
resource "google_project" "acceptance" {
project_id = "%s"
name = "%s"
# ensures we can set both org_id and folder_id as long as only one is not empty.
org_id = ""
folder_id = "${google_folder.folder1.id}"
}
resource "google_folder" "folder1" {
display_name = "%s"
parent = "organizations/%s"
}
`, pid, projectName, folderName, org)
}

func skipIfEnvNotSet(t *testing.T, envs ...string) {
for _, k := range envs {
if os.Getenv(k) == "" {
Expand Down

0 comments on commit 135f5c4

Please sign in to comment.