|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | +package resourcemanager_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest" |
| 11 | + "github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar" |
| 12 | +) |
| 13 | + |
| 14 | +func TestAccDataSourceGoogleProjectAncestry_basic(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + |
| 17 | + // Common resource configuration |
| 18 | + staticPrefix := "tf-test-" |
| 19 | + randomSuffix := "-" + acctest.RandString(t, 10) |
| 20 | + orgID := envvar.GetTestOrgFromEnv(t) |
| 21 | + |
| 22 | + // Configuration of resources |
| 23 | + folderThanos := staticPrefix + "thanos" + randomSuffix |
| 24 | + folderLoki := staticPrefix + "loki" + randomSuffix |
| 25 | + folderUltron := staticPrefix + "ultron" + randomSuffix |
| 26 | + projectThor := staticPrefix + "thor" + randomSuffix |
| 27 | + projectIronMan := staticPrefix + "ironman" + randomSuffix |
| 28 | + projectCap := staticPrefix + "cap" + randomSuffix |
| 29 | + projectHulk := staticPrefix + "hulk" + randomSuffix |
| 30 | + |
| 31 | + // Configuration map used in test deployment |
| 32 | + context := map[string]interface{}{ |
| 33 | + "org_id": orgID, |
| 34 | + "folder_thanos": folderThanos, |
| 35 | + "folder_loki": folderLoki, |
| 36 | + "folder_ultron": folderUltron, |
| 37 | + "project_thor": projectThor, |
| 38 | + "project_ironman": projectIronMan, |
| 39 | + "project_cap": projectCap, |
| 40 | + "project_hulk": projectHulk, |
| 41 | + } |
| 42 | + |
| 43 | + acctest.VcrTest(t, resource.TestCase{ |
| 44 | + PreCheck: func() { acctest.AccTestPreCheck(t) }, |
| 45 | + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), |
| 46 | + Steps: []resource.TestStep{ |
| 47 | + { |
| 48 | + Config: testAccCheckGoogleProjectAncestryConfig(context), |
| 49 | + Check: resource.ComposeTestCheckFunc( |
| 50 | + // Project thor under organization |
| 51 | + resource.TestCheckResourceAttr("data.google_project_ancestry.thor", "ancestors.#", "2"), |
| 52 | + resource.TestCheckResourceAttr("data.google_project_ancestry.thor", "ancestors.0.type", "project"), |
| 53 | + resource.TestCheckResourceAttr("data.google_project_ancestry.thor", "ancestors.1.type", "organization"), |
| 54 | + |
| 55 | + // Project ironman under organization and thanos |
| 56 | + resource.TestCheckResourceAttr("data.google_project_ancestry.ironman", "ancestors.#", "3"), |
| 57 | + resource.TestCheckResourceAttr("data.google_project_ancestry.ironman", "ancestors.0.type", "project"), |
| 58 | + resource.TestCheckResourceAttr("data.google_project_ancestry.ironman", "ancestors.1.type", "folder"), |
| 59 | + resource.TestCheckResourceAttr("data.google_project_ancestry.ironman", "ancestors.2.type", "organization"), |
| 60 | + |
| 61 | + // Project cap under organization, thanos and loki |
| 62 | + resource.TestCheckResourceAttr("data.google_project_ancestry.cap", "ancestors.#", "4"), |
| 63 | + resource.TestCheckResourceAttr("data.google_project_ancestry.cap", "ancestors.0.type", "project"), |
| 64 | + resource.TestCheckResourceAttr("data.google_project_ancestry.cap", "ancestors.1.type", "folder"), |
| 65 | + resource.TestCheckResourceAttr("data.google_project_ancestry.cap", "ancestors.2.type", "folder"), |
| 66 | + resource.TestCheckResourceAttr("data.google_project_ancestry.cap", "ancestors.3.type", "organization"), |
| 67 | + |
| 68 | + // Project hulk under organization, thanos, loki and ultron |
| 69 | + resource.TestCheckResourceAttr("data.google_project_ancestry.hulk", "ancestors.#", "5"), |
| 70 | + resource.TestCheckResourceAttr("data.google_project_ancestry.hulk", "ancestors.0.type", "project"), |
| 71 | + resource.TestCheckResourceAttr("data.google_project_ancestry.hulk", "ancestors.1.type", "folder"), |
| 72 | + resource.TestCheckResourceAttr("data.google_project_ancestry.hulk", "ancestors.2.type", "folder"), |
| 73 | + resource.TestCheckResourceAttr("data.google_project_ancestry.hulk", "ancestors.3.type", "folder"), |
| 74 | + resource.TestCheckResourceAttr("data.google_project_ancestry.hulk", "ancestors.4.type", "organization"), |
| 75 | + ), |
| 76 | + }, |
| 77 | + }, |
| 78 | + }) |
| 79 | +} |
| 80 | + |
| 81 | +func testAccCheckGoogleProjectAncestryConfig(context map[string]interface{}) string { |
| 82 | + return fmt.Sprintf(` |
| 83 | +locals { |
| 84 | + org_id = "%s" |
| 85 | + folder_thanos = "%s" |
| 86 | + folder_loki = "%s" |
| 87 | + folder_ultron = "%s" |
| 88 | + project_thor = "%s" |
| 89 | + project_ironman = "%s" |
| 90 | + project_cap = "%s" |
| 91 | + project_hulk = "%s" |
| 92 | +} |
| 93 | +
|
| 94 | +resource "google_folder" "thanos" { |
| 95 | + deletion_protection = false |
| 96 | + display_name = local.folder_thanos |
| 97 | + parent = "organizations/${local.org_id}" |
| 98 | +} |
| 99 | +
|
| 100 | +resource "google_folder" "loki" { |
| 101 | + deletion_protection = false |
| 102 | + display_name = local.folder_loki |
| 103 | + parent = google_folder.thanos.name |
| 104 | +} |
| 105 | +
|
| 106 | +resource "google_folder" "ultron" { |
| 107 | + deletion_protection = false |
| 108 | + display_name = local.folder_ultron |
| 109 | + parent = google_folder.loki.name |
| 110 | +} |
| 111 | +
|
| 112 | +resource "google_project" "thor" { |
| 113 | + deletion_policy = "DELETE" |
| 114 | + name = local.project_thor |
| 115 | + org_id = local.org_id |
| 116 | + project_id = local.project_thor |
| 117 | +} |
| 118 | +
|
| 119 | +resource "google_project" "ironman" { |
| 120 | + deletion_policy = "DELETE" |
| 121 | + folder_id = google_folder.thanos.id |
| 122 | + name = local.project_ironman |
| 123 | + project_id = local.project_ironman |
| 124 | +} |
| 125 | +
|
| 126 | +resource "google_project" "cap" { |
| 127 | + deletion_policy = "DELETE" |
| 128 | + folder_id = google_folder.loki.id |
| 129 | + name = local.project_cap |
| 130 | + project_id = local.project_cap |
| 131 | +} |
| 132 | +
|
| 133 | +resource "google_project" "hulk" { |
| 134 | + deletion_policy = "DELETE" |
| 135 | + folder_id = google_folder.ultron.id |
| 136 | + name = local.project_hulk |
| 137 | + project_id = local.project_hulk |
| 138 | +} |
| 139 | +
|
| 140 | +data "google_project_ancestry" "thor" { |
| 141 | + project = google_project.thor.project_id |
| 142 | +} |
| 143 | +
|
| 144 | +data "google_project_ancestry" "ironman" { |
| 145 | + project = google_project.ironman.project_id |
| 146 | +} |
| 147 | +
|
| 148 | +data "google_project_ancestry" "cap" { |
| 149 | + project = google_project.cap.project_id |
| 150 | +} |
| 151 | +
|
| 152 | +data "google_project_ancestry" "hulk" { |
| 153 | + project = google_project.hulk.project_id |
| 154 | +} |
| 155 | +`, |
| 156 | + context["org_id"].(string), |
| 157 | + context["folder_thanos"].(string), |
| 158 | + context["folder_loki"].(string), |
| 159 | + context["folder_ultron"].(string), |
| 160 | + context["project_thor"].(string), |
| 161 | + context["project_ironman"].(string), |
| 162 | + context["project_cap"].(string), |
| 163 | + context["project_hulk"].(string), |
| 164 | + ) |
| 165 | +} |
0 commit comments