|
| 1 | +package google |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 7 | +) |
| 8 | + |
| 9 | +func TestAccSnapshotDatasource_name(t *testing.T) { |
| 10 | + t.Parallel() |
| 11 | + |
| 12 | + vcrTest(t, resource.TestCase{ |
| 13 | + PreCheck: func() { testAccPreCheck(t) }, |
| 14 | + Providers: testAccProviders, |
| 15 | + Steps: []resource.TestStep{ |
| 16 | + { |
| 17 | + Config: testAccSnapshot_name(getTestProjectFromEnv(), randString(t, 10)), |
| 18 | + Check: resource.ComposeTestCheckFunc( |
| 19 | + checkDataSourceStateMatchesResourceStateWithIgnores( |
| 20 | + "data.google_compute_snapshot.default", |
| 21 | + "google_compute_snapshot.default", |
| 22 | + map[string]struct{}{"zone": {}}, |
| 23 | + ), |
| 24 | + ), |
| 25 | + }, |
| 26 | + }, |
| 27 | + }) |
| 28 | +} |
| 29 | + |
| 30 | +func TestAccSnapshotDatasource_filter(t *testing.T) { |
| 31 | + t.Parallel() |
| 32 | + |
| 33 | + vcrTest(t, resource.TestCase{ |
| 34 | + PreCheck: func() { testAccPreCheck(t) }, |
| 35 | + Providers: testAccProviders, |
| 36 | + Steps: []resource.TestStep{ |
| 37 | + { |
| 38 | + Config: testAccSnapshot_filter(getTestProjectFromEnv(), randString(t, 10)), |
| 39 | + Check: resource.ComposeTestCheckFunc( |
| 40 | + checkDataSourceStateMatchesResourceStateWithIgnores( |
| 41 | + "data.google_compute_snapshot.default", |
| 42 | + "google_compute_snapshot.c", |
| 43 | + map[string]struct{}{"zone": {}}, |
| 44 | + ), |
| 45 | + ), |
| 46 | + }, |
| 47 | + }, |
| 48 | + }) |
| 49 | +} |
| 50 | + |
| 51 | +func TestAccSnapshotDatasource_filterMostRecent(t *testing.T) { |
| 52 | + t.Parallel() |
| 53 | + |
| 54 | + vcrTest(t, resource.TestCase{ |
| 55 | + PreCheck: func() { testAccPreCheck(t) }, |
| 56 | + Providers: testAccProviders, |
| 57 | + Steps: []resource.TestStep{ |
| 58 | + { |
| 59 | + Config: testAccSnapshot_filter_mostRecent(getTestProjectFromEnv(), randString(t, 10)), |
| 60 | + Check: resource.ComposeTestCheckFunc( |
| 61 | + checkDataSourceStateMatchesResourceStateWithIgnores( |
| 62 | + "data.google_compute_snapshot.default", |
| 63 | + "google_compute_snapshot.c", |
| 64 | + map[string]struct{}{"zone": {}}, |
| 65 | + ), |
| 66 | + ), |
| 67 | + }, |
| 68 | + }, |
| 69 | + }) |
| 70 | +} |
| 71 | + |
| 72 | +func testAccSnapshot_name(project, suffix string) string { |
| 73 | + return Nprintf(` |
| 74 | + data "google_compute_image" "tf-test-image" { |
| 75 | + family = "debian-11" |
| 76 | + project = "debian-cloud" |
| 77 | + } |
| 78 | + resource "google_compute_disk" "tf-test-disk" { |
| 79 | + name = "debian-disk-%{suffix}" |
| 80 | + image = data.google_compute_image.tf-test-image.self_link |
| 81 | + size = 10 |
| 82 | + type = "pd-ssd" |
| 83 | + zone = "us-central1-a" |
| 84 | + } |
| 85 | +
|
| 86 | + resource "google_compute_snapshot" "default" { |
| 87 | + name = "tf-test-snapshot-%{suffix}" |
| 88 | + description = "Example snapshot." |
| 89 | + source_disk = google_compute_disk.tf-test-disk.id |
| 90 | + zone = "us-central1-a" |
| 91 | + labels = { |
| 92 | + my_label = "value" |
| 93 | + } |
| 94 | + storage_locations = ["us-central1"] |
| 95 | + } |
| 96 | + data "google_compute_snapshot" "default" { |
| 97 | + project = "%{project}" |
| 98 | + name = google_compute_snapshot.default.name |
| 99 | + } |
| 100 | +
|
| 101 | + `, map[string]interface{}{"project": project, "suffix": suffix}) |
| 102 | +} |
| 103 | + |
| 104 | +func testAccSnapshot_filter(project, suffix string) string { |
| 105 | + return Nprintf(` |
| 106 | + data "google_compute_image" "tf-test-image" { |
| 107 | + family = "debian-11" |
| 108 | + project = "debian-cloud" |
| 109 | + } |
| 110 | + resource "google_compute_disk" "tf-test-disk" { |
| 111 | + name = "debian-disk-%{suffix}" |
| 112 | + image = data.google_compute_image.tf-test-image.self_link |
| 113 | + size = 10 |
| 114 | + type = "pd-ssd" |
| 115 | + zone = "us-central1-a" |
| 116 | + } |
| 117 | + resource "google_compute_snapshot" "a" { |
| 118 | + name = "tf-test-snapshot-a-%{suffix}" |
| 119 | + description = "Example snapshot." |
| 120 | + source_disk = google_compute_disk.tf-test-disk.id |
| 121 | + zone = "us-central1-a" |
| 122 | + labels = { |
| 123 | + my_label = "a" |
| 124 | + } |
| 125 | + storage_locations = ["us-central1"] |
| 126 | + } |
| 127 | + resource "google_compute_snapshot" "b" { |
| 128 | + name = "tf-test-snapshot-b-%{suffix}" |
| 129 | + description = "Example snapshot." |
| 130 | + source_disk = google_compute_disk.tf-test-disk.id |
| 131 | + zone = "us-central1-a" |
| 132 | + labels = { |
| 133 | + my_label = "b" |
| 134 | + } |
| 135 | + storage_locations = ["us-central1"] |
| 136 | + } |
| 137 | + resource "google_compute_snapshot" "c" { |
| 138 | + name = "tf-test-snapshot-c-%{suffix}" |
| 139 | + description = "Example snapshot." |
| 140 | + source_disk = google_compute_disk.tf-test-disk.id |
| 141 | + zone = "us-central1-a" |
| 142 | + labels = { |
| 143 | + my_label = "c" |
| 144 | + } |
| 145 | + storage_locations = ["us-central1"] |
| 146 | + } |
| 147 | + data "google_compute_snapshot" "default" { |
| 148 | + project = "%{project}" |
| 149 | + filter = "name = tf-test-snapshot-c-%{suffix}" |
| 150 | + depends_on = [google_compute_snapshot.c] |
| 151 | + } |
| 152 | +`, map[string]interface{}{"project": project, "suffix": suffix}) |
| 153 | +} |
| 154 | + |
| 155 | +func testAccSnapshot_filter_mostRecent(project, suffix string) string { |
| 156 | + return Nprintf(` |
| 157 | + data "google_compute_image" "tf-test-image" { |
| 158 | + family = "debian-11" |
| 159 | + project = "debian-cloud" |
| 160 | + } |
| 161 | + resource "google_compute_disk" "tf-test-disk" { |
| 162 | + name = "debian-disk-%{suffix}" |
| 163 | + image = data.google_compute_image.tf-test-image.self_link |
| 164 | + size = 10 |
| 165 | + type = "pd-ssd" |
| 166 | + zone = "us-central1-a" |
| 167 | + } |
| 168 | + resource "google_compute_snapshot" "a" { |
| 169 | + name = "tf-test-snapshot-a-%{suffix}" |
| 170 | + description = "Example snapshot." |
| 171 | + source_disk = google_compute_disk.tf-test-disk.id |
| 172 | + zone = "us-central1-a" |
| 173 | + labels = { |
| 174 | + my_label = "a" |
| 175 | + } |
| 176 | + storage_locations = ["us-central1"] |
| 177 | + } |
| 178 | + resource "google_compute_snapshot" "b" { |
| 179 | + name = "tf-test-snapshot-b-%{suffix}" |
| 180 | + description = "Example snapshot." |
| 181 | + source_disk = google_compute_disk.tf-test-disk.id |
| 182 | + zone = "us-central1-a" |
| 183 | + labels = { |
| 184 | + my_label = "b" |
| 185 | + } |
| 186 | + storage_locations = ["us-central1"] |
| 187 | + } |
| 188 | + resource "google_compute_snapshot" "c" { |
| 189 | + name = "tf-test-snapshot-c-%{suffix}" |
| 190 | + description = "Example snapshot." |
| 191 | + source_disk = google_compute_disk.tf-test-disk.id |
| 192 | + zone = "us-central1-a" |
| 193 | + labels = { |
| 194 | + my_label = "c" |
| 195 | + } |
| 196 | + storage_locations = ["us-central1"] |
| 197 | + } |
| 198 | + data "google_compute_snapshot" "default" { |
| 199 | + project = "%{project}" |
| 200 | + most_recent = true |
| 201 | + filter = "name = tf-test-snapshot-c-%{suffix}" |
| 202 | + depends_on = [google_compute_snapshot.c] |
| 203 | + } |
| 204 | +`, map[string]interface{}{"project": project, "suffix": suffix}) |
| 205 | +} |
0 commit comments