diff --git a/mmv1/templates/terraform/custom_flatten/vertex_ai_reasoning_engine_env.go.tmpl b/mmv1/templates/terraform/custom_flatten/vertex_ai_reasoning_engine_env.go.tmpl index 3f134b02871d..55cb312b2295 100644 --- a/mmv1/templates/terraform/custom_flatten/vertex_ai_reasoning_engine_env.go.tmpl +++ b/mmv1/templates/terraform/custom_flatten/vertex_ai_reasoning_engine_env.go.tmpl @@ -13,6 +13,24 @@ func flatten{{$.GetPrefix}}{{$.TitlelizeProperty}}(v interface{}, d *schema.Reso if v == nil { return nil } + hasTelemetryConfig := false + if envRaw, ok := d.GetOk("spec.0.deployment_spec.0.env"); ok && envRaw != nil { + var envList []interface{} + if set, ok := envRaw.(*schema.Set); ok { + envList = set.List() + } else if list, ok := envRaw.([]interface{}); ok { + envList = list + } + for _, e := range envList { + if m, ok := e.(map[string]interface{}); ok { + if m["name"] == "GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY" { + hasTelemetryConfig = true + break + } + } + } + } + l := v.([]interface{}) transformed := make([]interface{}, 0, len(l)) for _, raw := range l { @@ -20,7 +38,7 @@ func flatten{{$.GetPrefix}}{{$.TitlelizeProperty}}(v interface{}, d *schema.Reso continue } original := raw.(map[string]interface{}) - if original["name"] == "GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY" { + if original["name"] == "GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY" && !hasTelemetryConfig { continue } transformed = append(transformed, map[string]interface{}{ diff --git a/mmv1/third_party/terraform/services/vertexai/flatten_vertex_ai_reasoning_engine_env_test.go b/mmv1/third_party/terraform/services/vertexai/flatten_vertex_ai_reasoning_engine_env_test.go new file mode 100644 index 000000000000..75776644191f --- /dev/null +++ b/mmv1/third_party/terraform/services/vertexai/flatten_vertex_ai_reasoning_engine_env_test.go @@ -0,0 +1,138 @@ +package vertexai + +import ( + "reflect" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func TestFlattenVertexAIReasoningEngineSpecDeploymentSpecEnv(t *testing.T) { + resourceSchema := map[string]*schema.Schema{ + "spec": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "deployment_spec": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "env": { + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + "value": { + Type: schema.TypeString, + Required: true, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + } + + apiResponseEnv := []interface{}{ + map[string]interface{}{ + "name": "GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY", + "value": "true", + }, + map[string]interface{}{ + "name": "CUSTOM_VAR", + "value": "custom_val", + }, + } + + // Case 1: Telemetry env var NOT configured in ResourceData + t.Run("Telemetry not in config - filtered out", func(t *testing.T) { + d := schema.TestResourceDataRaw(t, resourceSchema, map[string]interface{}{ + "spec": []interface{}{ + map[string]interface{}{ + "deployment_spec": []interface{}{ + map[string]interface{}{ + "env": []interface{}{ + map[string]interface{}{ + "name": "CUSTOM_VAR", + "value": "custom_val", + }, + }, + }, + }, + }, + }, + }) + + res := flattenVertexAIReasoningEngineSpecDeploymentSpecEnv(apiResponseEnv, d, nil) + resList, ok := res.([]interface{}) + if !ok { + t.Fatalf("Expected []interface{}, got %T", res) + } + + expected := []interface{}{ + map[string]interface{}{ + "name": "CUSTOM_VAR", + "value": "custom_val", + }, + } + + if !reflect.DeepEqual(resList, expected) { + t.Errorf("Expected %v, got %v", expected, resList) + } + }) + + // Case 2: Telemetry env var IS configured in ResourceData + t.Run("Telemetry in config - preserved in state", func(t *testing.T) { + d := schema.TestResourceDataRaw(t, resourceSchema, map[string]interface{}{ + "spec": []interface{}{ + map[string]interface{}{ + "deployment_spec": []interface{}{ + map[string]interface{}{ + "env": []interface{}{ + map[string]interface{}{ + "name": "GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY", + "value": "true", + }, + map[string]interface{}{ + "name": "CUSTOM_VAR", + "value": "custom_val", + }, + }, + }, + }, + }, + }, + }) + + res := flattenVertexAIReasoningEngineSpecDeploymentSpecEnv(apiResponseEnv, d, nil) + resList, ok := res.([]interface{}) + if !ok { + t.Fatalf("Expected []interface{}, got %T", res) + } + + expected := []interface{}{ + map[string]interface{}{ + "name": "GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY", + "value": "true", + }, + map[string]interface{}{ + "name": "CUSTOM_VAR", + "value": "custom_val", + }, + } + + if !reflect.DeepEqual(resList, expected) { + t.Errorf("Expected %v, got %v", expected, resList) + } + }) +}