Skip to content

Commit 0102203

Browse files
Fix issue with composer environment creation and add a test (#7187) (#13644)
Signed-off-by: Modular Magician <[email protected]>
1 parent 5c958f3 commit 0102203

File tree

3 files changed

+189
-3
lines changed

3 files changed

+189
-3
lines changed

.changelog/7187.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
composer: fixed an issue with cleaning up environments created in an error state
3+
```

google/resource_composer_environment.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -1094,8 +1094,6 @@ func resourceComposerEnvironmentPostCreateUpdate(updateEnv *composer.Environment
10941094
return nil
10951095
}
10961096

1097-
d.Partial(true)
1098-
10991097
if updateEnv.Config != nil && updateEnv.Config.SoftwareConfig != nil && len(updateEnv.Config.SoftwareConfig.PypiPackages) > 0 {
11001098
log.Printf("[DEBUG] Running post-create update for Environment %q", d.Id())
11011099
err := resourceComposerEnvironmentPatchField("config.softwareConfig.pypiPackages", userAgent, updateEnv, d, cfg)
@@ -1105,7 +1103,7 @@ func resourceComposerEnvironmentPostCreateUpdate(updateEnv *composer.Environment
11051103

11061104
log.Printf("[DEBUG] Finish update to Environment %q post create for update only fields", d.Id())
11071105
}
1108-
d.Partial(false)
1106+
11091107
return resourceComposerEnvironmentRead(d, cfg)
11101108
}
11111109

google/resource_composer_environment_test.go

+185
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"log"
9+
"regexp"
910
"strings"
1011
"time"
1112

@@ -891,6 +892,35 @@ func TestAccComposerEnvironment_withUpdateOnCreate(t *testing.T) {
891892
})
892893
}
893894

895+
func TestAccComposerEnvironment_fixPyPiPackages(t *testing.T) {
896+
t.Parallel()
897+
898+
envName := fmt.Sprintf("%s-%d", testComposerEnvironmentPrefix, randInt(t))
899+
network := fmt.Sprintf("%s-%d", testComposerNetworkPrefix, randInt(t))
900+
subnetwork := network + "-1"
901+
serviceAccount := fmt.Sprintf("tf-test-%d", randInt(t))
902+
903+
vcrTest(t, resource.TestCase{
904+
PreCheck: func() { testAccPreCheck(t) },
905+
Providers: testAccProviders,
906+
CheckDestroy: testAccComposerEnvironmentDestroyProducer(t),
907+
Steps: []resource.TestStep{
908+
{
909+
Config: testAccComposerEnvironment_fixPyPiPackages(envName, network, subnetwork, serviceAccount),
910+
ExpectError: regexp.MustCompile("Failed to install pypi packages"),
911+
},
912+
{
913+
Config: testAccComposerEnvironment_fixPyPiPackagesUpdate(envName, network, subnetwork, serviceAccount),
914+
},
915+
{
916+
ResourceName: "google_composer_environment.test",
917+
ImportState: true,
918+
ImportStateVerify: true,
919+
},
920+
},
921+
})
922+
}
923+
894924
func testAccComposerEnvironmentDestroyProducer(t *testing.T) func(s *terraform.State) error {
895925
return func(s *terraform.State) error {
896926
config := googleProviderConfig(t)
@@ -2061,6 +2091,161 @@ resource "google_compute_subnetwork" "test" {
20612091
`, name, network, subnetwork)
20622092
}
20632093

2094+
func testAccComposerEnvironment_fixPyPiPackages(environment, network, subnetwork, serviceAccount string) string {
2095+
return fmt.Sprintf(`
2096+
resource "google_composer_environment" "test" {
2097+
name = "%s"
2098+
region = "us-central1"
2099+
config {
2100+
2101+
software_config {
2102+
image_version = "composer-2-airflow-2"
2103+
2104+
pypi_packages = {
2105+
"google-cloud-bigquery" = "==1"
2106+
}
2107+
}
2108+
2109+
private_environment_config {
2110+
enable_private_endpoint = true
2111+
master_ipv4_cidr_block = "10.10.0.0/28"
2112+
}
2113+
2114+
workloads_config {
2115+
scheduler {
2116+
cpu = 0.5
2117+
memory_gb = 1.875
2118+
storage_gb = 1
2119+
count = 1
2120+
}
2121+
web_server {
2122+
cpu = 0.5
2123+
memory_gb = 1.875
2124+
storage_gb = 1
2125+
}
2126+
worker {
2127+
cpu = 0.5
2128+
memory_gb = 1.875
2129+
storage_gb = 1
2130+
min_count = 1
2131+
max_count = 3
2132+
}
2133+
}
2134+
2135+
environment_size = "ENVIRONMENT_SIZE_SMALL"
2136+
2137+
node_config {
2138+
network = google_compute_network.test.id
2139+
subnetwork = google_compute_subnetwork.test.id
2140+
service_account = google_service_account.test.name
2141+
}
2142+
}
2143+
}
2144+
2145+
// use a separate network to avoid conflicts with other tests running in parallel
2146+
// that use the default network/subnet
2147+
resource "google_compute_network" "test" {
2148+
name = "%s"
2149+
auto_create_subnetworks = false
2150+
}
2151+
2152+
resource "google_compute_subnetwork" "test" {
2153+
name = "%s"
2154+
ip_cidr_range = "10.2.0.0/16"
2155+
region = "us-central1"
2156+
network = google_compute_network.test.self_link
2157+
}
2158+
2159+
resource "google_service_account" "test" {
2160+
account_id = "%s"
2161+
display_name = "Test Service Account for Composer Environment"
2162+
}
2163+
2164+
data "google_project" "project" {}
2165+
2166+
resource "google_project_iam_member" "composer-worker" {
2167+
project = data.google_project.project.project_id
2168+
role = "roles/composer.worker"
2169+
member = "serviceAccount:${google_service_account.test.email}"
2170+
}`, environment, network, subnetwork, serviceAccount)
2171+
}
2172+
2173+
func testAccComposerEnvironment_fixPyPiPackagesUpdate(environment, network, subnetwork, serviceAccount string) string {
2174+
return fmt.Sprintf(`
2175+
resource "google_composer_environment" "test" {
2176+
name = "%s"
2177+
region = "us-central1"
2178+
config {
2179+
2180+
software_config {
2181+
image_version = "composer-2-airflow-2"
2182+
}
2183+
2184+
private_environment_config {
2185+
enable_private_endpoint = true
2186+
master_ipv4_cidr_block = "10.10.0.0/28"
2187+
}
2188+
2189+
workloads_config {
2190+
scheduler {
2191+
cpu = 0.5
2192+
memory_gb = 1.875
2193+
storage_gb = 1
2194+
count = 1
2195+
}
2196+
web_server {
2197+
cpu = 0.5
2198+
memory_gb = 1.875
2199+
storage_gb = 1
2200+
}
2201+
worker {
2202+
cpu = 0.5
2203+
memory_gb = 1.875
2204+
storage_gb = 1
2205+
min_count = 1
2206+
max_count = 3
2207+
}
2208+
}
2209+
2210+
environment_size = "ENVIRONMENT_SIZE_SMALL"
2211+
2212+
node_config {
2213+
network = google_compute_network.test.id
2214+
subnetwork = google_compute_subnetwork.test.id
2215+
service_account = google_service_account.test.name
2216+
}
2217+
}
2218+
}
2219+
2220+
// use a separate network to avoid conflicts with other tests running in parallel
2221+
// that use the default network/subnet
2222+
resource "google_compute_network" "test" {
2223+
name = "%s"
2224+
auto_create_subnetworks = false
2225+
}
2226+
2227+
resource "google_compute_subnetwork" "test" {
2228+
name = "%s"
2229+
ip_cidr_range = "10.2.0.0/16"
2230+
region = "us-central1"
2231+
network = google_compute_network.test.self_link
2232+
}
2233+
2234+
resource "google_service_account" "test" {
2235+
account_id = "%s"
2236+
display_name = "Test Service Account for Composer Environment"
2237+
}
2238+
2239+
data "google_project" "project" {}
2240+
2241+
resource "google_project_iam_member" "composer-worker" {
2242+
project = data.google_project.project.project_id
2243+
role = "roles/composer.worker"
2244+
member = "serviceAccount:${google_service_account.test.email}"
2245+
}
2246+
`, environment, network, subnetwork, serviceAccount)
2247+
}
2248+
20642249
/**
20652250
* CLEAN UP HELPER FUNCTIONS
20662251
* Because the environments are flaky and bucket deletion rates can be

0 commit comments

Comments
 (0)