Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with composer environment creation and add a test #13644

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/7187.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
composer: fixed an issue with cleaning up environments created in an error state
```
4 changes: 1 addition & 3 deletions google/resource_composer_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -1094,8 +1094,6 @@ func resourceComposerEnvironmentPostCreateUpdate(updateEnv *composer.Environment
return nil
}

d.Partial(true)

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

log.Printf("[DEBUG] Finish update to Environment %q post create for update only fields", d.Id())
}
d.Partial(false)

return resourceComposerEnvironmentRead(d, cfg)
}

Expand Down
185 changes: 185 additions & 0 deletions google/resource_composer_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"log"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -891,6 +892,35 @@ func TestAccComposerEnvironment_withUpdateOnCreate(t *testing.T) {
})
}

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

envName := fmt.Sprintf("%s-%d", testComposerEnvironmentPrefix, randInt(t))
network := fmt.Sprintf("%s-%d", testComposerNetworkPrefix, randInt(t))
subnetwork := network + "-1"
serviceAccount := fmt.Sprintf("tf-test-%d", randInt(t))

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccComposerEnvironmentDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComposerEnvironment_fixPyPiPackages(envName, network, subnetwork, serviceAccount),
ExpectError: regexp.MustCompile("Failed to install pypi packages"),
},
{
Config: testAccComposerEnvironment_fixPyPiPackagesUpdate(envName, network, subnetwork, serviceAccount),
},
{
ResourceName: "google_composer_environment.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccComposerEnvironmentDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
config := googleProviderConfig(t)
Expand Down Expand Up @@ -2061,6 +2091,161 @@ resource "google_compute_subnetwork" "test" {
`, name, network, subnetwork)
}

func testAccComposerEnvironment_fixPyPiPackages(environment, network, subnetwork, serviceAccount string) string {
return fmt.Sprintf(`
resource "google_composer_environment" "test" {
name = "%s"
region = "us-central1"
config {

software_config {
image_version = "composer-2-airflow-2"

pypi_packages = {
"google-cloud-bigquery" = "==1"
}
}

private_environment_config {
enable_private_endpoint = true
master_ipv4_cidr_block = "10.10.0.0/28"
}

workloads_config {
scheduler {
cpu = 0.5
memory_gb = 1.875
storage_gb = 1
count = 1
}
web_server {
cpu = 0.5
memory_gb = 1.875
storage_gb = 1
}
worker {
cpu = 0.5
memory_gb = 1.875
storage_gb = 1
min_count = 1
max_count = 3
}
}

environment_size = "ENVIRONMENT_SIZE_SMALL"

node_config {
network = google_compute_network.test.id
subnetwork = google_compute_subnetwork.test.id
service_account = google_service_account.test.name
}
}
}

// use a separate network to avoid conflicts with other tests running in parallel
// that use the default network/subnet
resource "google_compute_network" "test" {
name = "%s"
auto_create_subnetworks = false
}

resource "google_compute_subnetwork" "test" {
name = "%s"
ip_cidr_range = "10.2.0.0/16"
region = "us-central1"
network = google_compute_network.test.self_link
}

resource "google_service_account" "test" {
account_id = "%s"
display_name = "Test Service Account for Composer Environment"
}

data "google_project" "project" {}

resource "google_project_iam_member" "composer-worker" {
project = data.google_project.project.project_id
role = "roles/composer.worker"
member = "serviceAccount:${google_service_account.test.email}"
}`, environment, network, subnetwork, serviceAccount)
}

func testAccComposerEnvironment_fixPyPiPackagesUpdate(environment, network, subnetwork, serviceAccount string) string {
return fmt.Sprintf(`
resource "google_composer_environment" "test" {
name = "%s"
region = "us-central1"
config {

software_config {
image_version = "composer-2-airflow-2"
}

private_environment_config {
enable_private_endpoint = true
master_ipv4_cidr_block = "10.10.0.0/28"
}

workloads_config {
scheduler {
cpu = 0.5
memory_gb = 1.875
storage_gb = 1
count = 1
}
web_server {
cpu = 0.5
memory_gb = 1.875
storage_gb = 1
}
worker {
cpu = 0.5
memory_gb = 1.875
storage_gb = 1
min_count = 1
max_count = 3
}
}

environment_size = "ENVIRONMENT_SIZE_SMALL"

node_config {
network = google_compute_network.test.id
subnetwork = google_compute_subnetwork.test.id
service_account = google_service_account.test.name
}
}
}

// use a separate network to avoid conflicts with other tests running in parallel
// that use the default network/subnet
resource "google_compute_network" "test" {
name = "%s"
auto_create_subnetworks = false
}

resource "google_compute_subnetwork" "test" {
name = "%s"
ip_cidr_range = "10.2.0.0/16"
region = "us-central1"
network = google_compute_network.test.self_link
}

resource "google_service_account" "test" {
account_id = "%s"
display_name = "Test Service Account for Composer Environment"
}

data "google_project" "project" {}

resource "google_project_iam_member" "composer-worker" {
project = data.google_project.project.project_id
role = "roles/composer.worker"
member = "serviceAccount:${google_service_account.test.email}"
}
`, environment, network, subnetwork, serviceAccount)
}

/**
* CLEAN UP HELPER FUNCTIONS
* Because the environments are flaky and bucket deletion rates can be
Expand Down