Skip to content

Commit

Permalink
Fixed issue where ConfigId was erroneously pre-computed when other fi…
Browse files Browse the repository at this point in the history
…elds were unknown at plan time (#9708) (#16946)

* Add case of valueUnknown in configId pre-computing

* add openapi parameters test
[upstream:37b21aabe9e09ada0614411ed558487cae0785fc]

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Jan 9, 2024
1 parent 150705d commit eb3388c
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/9708.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
servicemanagement: fixed issue in `google_endpoints_service` where an inconsistent plan would be created when certain fields had computed values
```
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ func predictServiceId(_ context.Context, d *schema.ResourceDiff, meta interface{
if !d.HasChange("openapi_config") && !d.HasChange("grpc_config") && !d.HasChange("protoc_output_base64") {
return nil
}
if !d.NewValueKnown("openapi_config") || !d.NewValueKnown("grpc_config") || !d.NewValueKnown("protoc_output_base64") {
d.SetNewComputed("config_id")
return nil
}
baseDate := time.Now().Format("2006-01-02")
oldConfigId := d.Get("config_id").(string)
if match, err := regexp.MatchString(`\d\d\d\d-\d\d-\d\dr\d*`, oldConfigId); !match || err != nil {
Expand Down
146 changes: 146 additions & 0 deletions google/services/servicemanagement/resource_endpoints_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,52 @@ func TestAccEndpointsService_grpc(t *testing.T) {
})
}

func TestAccEndpointsService_grpcNotPreComputeConfigIdByGrpcConfig(t *testing.T) {
t.Parallel()
prj := envvar.GetTestProjectFromEnv()
parent := fmt.Sprintf("projects/%s", prj)
serviceId := "tf-test" + acctest.RandString(t, 10)

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckEndpointServiceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccEndpointsService_grpcNotPreComputeConfigIdByGrpcConfig(serviceId, envvar.GetTestProjectFromEnv(), parent, "1"),
Check: testAccCheckEndpointExistsByName(t, serviceId),
},
{
Config: testAccEndpointsService_grpcNotPreComputeConfigIdByGrpcConfig(serviceId, envvar.GetTestProjectFromEnv(), parent, "2"),
Check: testAccCheckEndpointExistsByName(t, serviceId),
},
},
})
}

func TestAccEndpointsService_openapiNotPreComputeConfigId(t *testing.T) {
t.Parallel()
prj := envvar.GetTestProjectFromEnv()
parent := fmt.Sprintf("projects/%s", prj)
serviceId := "tf-test" + acctest.RandString(t, 10)

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckEndpointServiceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccEndpointsService_openapiNotPreComputeConfigId(serviceId, envvar.GetTestProjectFromEnv(), parent, "1"),
Check: testAccCheckEndpointExistsByName(t, serviceId),
},
{
Config: testAccEndpointsService_openapiNotPreComputeConfigId(serviceId, envvar.GetTestProjectFromEnv(), parent, "2"),
Check: testAccCheckEndpointExistsByName(t, serviceId),
},
},
})
}

func testAccEndpointsService_basic(serviceId, project, rev string) string {
return fmt.Sprintf(`
resource "google_endpoints_service" "endpoints_service" {
Expand Down Expand Up @@ -131,6 +177,106 @@ EOF
`, serviceId, project)
}

func testAccEndpointsService_grpcNotPreComputeConfigIdByGrpcConfig(serviceId, project, parent, description string) string {
return fmt.Sprintf(`
resource "google_tags_tag_key" "key1" {
parent = "%[3]s"
short_name = "endpoints-%[1]s-1"
description = "%[4]s"
}
resource "google_tags_tag_key" "key2" {
parent = "%[3]s"
short_name = "endpoints-%[1]s-2"
lifecycle {
replace_triggered_by = [google_tags_tag_key.key1.description]
}
}
resource "google_endpoints_service" "endpoints_service" {
service_name = "%[1]s.endpoints.%[2]s.cloud.goog"
project = "%[2]s"
grpc_config = <<EOF
type: google.api.Service
config_version: 3
name: %[1]s.endpoints.%[2]s.cloud.goog
title: Test ${google_tags_tag_key.key2.namespaced_name}
usage:
rules:
- selector: endpoints.examples.bookstore.Bookstore.ListShelves
allow_unregistered_calls: true
EOF
protoc_output_base64 = filebase64("test-fixtures/test_api_descriptor.pb")
}
`, serviceId, project, parent, description)
}

func testAccEndpointsService_openapiNotPreComputeConfigId(serviceId, project, parent, description string) string {
return fmt.Sprintf(`
resource "google_tags_tag_key" "key1" {
parent = "%[3]s"
short_name = "endpoints-%[1]s-1"
description = "%[4]s"
}
resource "google_tags_tag_key" "key2" {
parent = "%[3]s"
short_name = "endpoints-%[1]s-2"
lifecycle {
replace_triggered_by = [google_tags_tag_key.key1.description]
}
}
resource "google_endpoints_service" "endpoints_service" {
service_name = "%[1]s.endpoints.%[2]s.cloud.goog"
project = "%[2]s"
openapi_config = <<EOF
swagger: "2.0"
info:
description: "${google_tags_tag_key.key2.namespaced_name}"
title: "Endpoints Example, rev. 1"
version: "1.0.0"
host: "%[1]s.endpoints.%[2]s.cloud.goog"
basePath: "/"
consumes:
- "application/json"
produces:
- "application/json"
schemes:
- "https"
paths:
"/echo":
post:
description: "Echo back a given message."
operationId: "echo"
produces:
- "application/json"
responses:
200:
description: "Echo"
schema:
$ref: "#/definitions/echoMessage"
parameters:
- description: "Message to echo"
in: body
name: message
required: true
schema:
$ref: "#/definitions/echoMessage"
security:
- api_key: []
definitions:
echoMessage:
properties:
message:
type: "string"
EOF
}
`, serviceId, project, parent, description)
}

func testAccCheckEndpointExistsByName(t *testing.T, serviceId string) resource.TestCheckFunc {
return func(s *terraform.State) error {
config := acctest.GoogleProviderConfig(t)
Expand Down

0 comments on commit eb3388c

Please sign in to comment.