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

Fixed a problem in which only ConfigId was calculated first when the API parameter was set to Known After Apply #16946

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/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