From 160dcbcb63977c2e56e4a342905588829b120e3f Mon Sep 17 00:00:00 2001 From: Salome Papiashvili Date: Fri, 15 Dec 2023 19:57:16 +0100 Subject: [PATCH] Composer internal ip (#9507) --- .../resource_composer_environment.go.erb | 24 ++++++- .../resource_composer_environment_test.go.erb | 69 +++++++++++++++++++ .../docs/r/composer_environment.html.markdown | 5 ++ 3 files changed, 96 insertions(+), 2 deletions(-) diff --git a/mmv1/third_party/terraform/services/composer/resource_composer_environment.go.erb b/mmv1/third_party/terraform/services/composer/resource_composer_environment.go.erb index ef756d19ba7e..437bb9d4806b 100644 --- a/mmv1/third_party/terraform/services/composer/resource_composer_environment.go.erb +++ b/mmv1/third_party/terraform/services/composer/resource_composer_environment.go.erb @@ -348,6 +348,15 @@ func ResourceComposerEnvironment() *schema.Resource { }, }, }, +<% unless version == "ga" -%> + "composer_internal_ipv4_cidr_block": { + Type: schema.TypeString, + Computed: true, + Optional: true, + ForceNew: true, + Description: `IPv4 cidr range that will be used by Composer internal components.`, + }, +<% end -%> }, }, }, @@ -1586,8 +1595,10 @@ func flattenComposerEnvironmentConfigPrivateEnvironmentConfig(envCfg *composer.P if envCfg.NetworkingConfig != nil{ transformed["connection_type"] = envCfg.NetworkingConfig.ConnectionType } - transformed["enable_private_endpoint"] = envCfg.PrivateClusterConfig.EnablePrivateEndpoint - transformed["master_ipv4_cidr_block"] = envCfg.PrivateClusterConfig.MasterIpv4CidrBlock + if envCfg.PrivateClusterConfig != nil{ + transformed["enable_private_endpoint"] = envCfg.PrivateClusterConfig.EnablePrivateEndpoint + transformed["master_ipv4_cidr_block"] = envCfg.PrivateClusterConfig.MasterIpv4CidrBlock + } transformed["cloud_sql_ipv4_cidr_block"] = envCfg.CloudSqlIpv4CidrBlock transformed["web_server_ipv4_cidr_block"] = envCfg.WebServerIpv4CidrBlock transformed["cloud_composer_network_ipv4_cidr_block"] = envCfg.CloudComposerNetworkIpv4CidrBlock @@ -1615,6 +1626,9 @@ func flattenComposerEnvironmentConfigNodeConfig(nodeCfg *composer.NodeConfig) in transformed["enable_ip_masq_agent"] = nodeCfg.EnableIpMasqAgent transformed["tags"] = flattenComposerEnvironmentConfigNodeConfigTags(nodeCfg.Tags) transformed["ip_allocation_policy"] = flattenComposerEnvironmentConfigNodeConfigIPAllocationPolicy(nodeCfg.IpAllocationPolicy) +<% unless version == "ga" -%> + transformed["composer_internal_ipv4_cidr_block"] = nodeCfg.ComposerInternalIpv4CidrBlock +<% end -%> return []interface{}{transformed} } @@ -2157,6 +2171,12 @@ func expandComposerEnvironmentConfigNodeConfig(v interface{}, d *schema.Resource } transformed.Tags = transformedTags +<% unless version == "ga" -%> + if transformedComposerInternalIpv4CidrBlock, ok := original["composer_internal_ipv4_cidr_block"]; ok { + transformed.ComposerInternalIpv4CidrBlock = transformedComposerInternalIpv4CidrBlock.(string) + } +<% end -%> + return transformed, nil } diff --git a/mmv1/third_party/terraform/services/composer/resource_composer_environment_test.go.erb b/mmv1/third_party/terraform/services/composer/resource_composer_environment_test.go.erb index 4c299f2e21c8..46eb7a6d269c 100644 --- a/mmv1/third_party/terraform/services/composer/resource_composer_environment_test.go.erb +++ b/mmv1/third_party/terraform/services/composer/resource_composer_environment_test.go.erb @@ -1116,6 +1116,42 @@ func TestAccComposerEnvironment_customBucket(t *testing.T) { }) } +<% unless version == "ga" -%> +// Checks Composer 3 environment creation with new fields. +func TestAccComposerEnvironmentComposer3_basic(t *testing.T) { + t.Parallel() + + envName := fmt.Sprintf("%s-%d", testComposerEnvironmentPrefix, acctest.RandInt(t)) + network := fmt.Sprintf("%s-%d", testComposerNetworkPrefix, acctest.RandInt(t)) + subnetwork := network + "-1" + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + CheckDestroy: testAccComposerEnvironmentDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccComposerEnvironmentComposer3_basic(envName, network, subnetwork), + }, + { + ResourceName: "google_composer_environment.test", + ImportState: true, + ImportStateVerify: true, + }, + // This is a terrible clean-up step in order to get destroy to succeed, + // due to dangling firewall rules left by the Composer Environment blocking network deletion. + // TODO: Remove this check if firewall rules bug gets fixed by Composer. + { + PlanOnly: true, + ExpectNonEmptyPlan: false, + Config: testAccComposerEnvironmentComposer3_basic(envName, network, subnetwork), + Check: testAccCheckClearComposerEnvironmentFirewalls(t, network), + }, + }, + }) +} +<% end -%> + func testAccComposerEnvironment_customBucket(bucketName, envName, network, subnetwork string) string { return fmt.Sprintf(` resource "google_storage_bucket" "test" { @@ -2677,6 +2713,39 @@ resource "google_project_iam_member" "composer-worker" { `, environment, network, subnetwork, serviceAccount) } +<% unless version == "ga" -%> +func testAccComposerEnvironmentComposer3_basic(name, network, subnetwork string) string { + return fmt.Sprintf(` +resource "google_composer_environment" "test" { + name = "%s" + region = "us-central1" + config { + node_config { + composer_internal_ipv4_cidr_block = "100.64.128.0/20" + } + software_config { + image_version = "composer-3-airflow-2" + } + } +} + +// 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 +} +`, name, network, subnetwork) +} +<% end -%> + // WARNING: This is not actually a check and is a terrible clean-up step because Composer Environments // have a bug that hasn't been fixed. Composer will add firewalls to non-default networks for environments // but will not remove them when the Environment is deleted. diff --git a/mmv1/third_party/terraform/website/docs/r/composer_environment.html.markdown b/mmv1/third_party/terraform/website/docs/r/composer_environment.html.markdown index c9efd3c5abe4..e31ba2a158cd 100644 --- a/mmv1/third_party/terraform/website/docs/r/composer_environment.html.markdown +++ b/mmv1/third_party/terraform/website/docs/r/composer_environment.html.markdown @@ -769,6 +769,11 @@ The `node_config` block supports: packets from node IP addresses instead of Pod IP addresses See the [documentation](https://cloud.google.com/composer/docs/enable-ip-masquerade-agent). +* `composer_internal_ipv4_cidr_block` - + (Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html), Cloud Composer 3 only) + At least /20 IPv4 cidr range that will be used by Composer internal components. + Cannot be updated. + The `software_config` block supports: * `airflow_config_overrides` -