From 2d0fb0f172cdee33b2f3c9bd4c5e7d74180594b0 Mon Sep 17 00:00:00 2001 From: Nathan Mckinley Date: Tue, 30 Jun 2020 15:02:18 -0700 Subject: [PATCH 1/3] Guess at config_id value so that updates don't break downstream resources. --- .../resources/resource_endpoints_service.go | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/third_party/terraform/resources/resource_endpoints_service.go b/third_party/terraform/resources/resource_endpoints_service.go index 9d309c55e8f3..5e0a769a5bd9 100644 --- a/third_party/terraform/resources/resource_endpoints_service.go +++ b/third_party/terraform/resources/resource_endpoints_service.go @@ -4,7 +4,11 @@ import ( "encoding/base64" "encoding/json" "errors" + "fmt" "log" + "regexp" + "strconv" + "strings" "time" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" @@ -141,9 +145,34 @@ func resourceEndpointsService() *schema.Resource { }, }, }, + CustomizeDiff: predictServiceId, } } +func predictServiceId(d *schema.ResourceDiff, meta interface{}) error { + if !d.HasChange("openapi_config") && !d.HasChange("grpc_config") && !d.HasChange("protoc_output_base64") { + return nil + } + loc, _ := time.LoadLocation("America/Los_Angeles") + baseDate := time.Now().In(loc).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 { + // If we do not match the expected format, we will guess + // wrong and that is worse than not guessing. + return nil + } + if strings.HasPrefix(oldConfigId, baseDate) { + n, err := strconv.Atoi(strings.Split(oldConfigId, "r")[1]) + if err != nil { + return err + } + d.SetNew("config_id", fmt.Sprintf("%sr%d", baseDate, n+1)) + } else { + d.SetNew("config_id", baseDate+"r0") + } + return nil +} + func getEndpointServiceOpenAPIConfigSource(configText string) *servicemanagement.ConfigSource { // We need to provide a ConfigSource object to the API whenever submitting a // new config. A ConfigSource contains a ConfigFile which contains the b64 From 8e6f225067fbb42499af89f46d902bbf074abb27 Mon Sep 17 00:00:00 2001 From: Nathan Mckinley Date: Tue, 30 Jun 2020 15:23:58 -0700 Subject: [PATCH 2/3] Add a test which ensures that the config id update is correct. --- .../tests/resource_endpoints_service_test.go | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/third_party/terraform/tests/resource_endpoints_service_test.go b/third_party/terraform/tests/resource_endpoints_service_test.go index feabb7ddab73..21e2e20125da 100644 --- a/third_party/terraform/tests/resource_endpoints_service_test.go +++ b/third_party/terraform/tests/resource_endpoints_service_test.go @@ -21,7 +21,15 @@ func TestAccEndpointsService_basic(t *testing.T) { Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccEndpointsService_basic(serviceId, getTestProjectFromEnv()), + Config: testAccEndpointsService_basic(serviceId, getTestProjectFromEnv(), "1"), + Check: testAccCheckEndpointExistsByName(t, serviceId), + }, + { + Config: testAccEndpointsService_basic(serviceId, getTestProjectFromEnv(), "2"), + Check: testAccCheckEndpointExistsByName(t, serviceId), + }, + { + Config: testAccEndpointsService_basic(serviceId, getTestProjectFromEnv(), "3"), Check: testAccCheckEndpointExistsByName(t, serviceId), }, }, @@ -97,7 +105,7 @@ func TestEndpointsService_grpcMigrateState(t *testing.T) { } } -func testAccEndpointsService_basic(serviceId, project string) string { +func testAccEndpointsService_basic(serviceId, project, rev string) string { return fmt.Sprintf(` resource "google_endpoints_service" "endpoints_service" { service_name = "%[1]s.endpoints.%[2]s.cloud.goog" @@ -106,7 +114,7 @@ resource "google_endpoints_service" "endpoints_service" { swagger: "2.0" info: description: "A simple Google Cloud Endpoints API example." - title: "Endpoints Example" + title: "Endpoints Example, rev. %[3]s" version: "1.0.0" host: "%[1]s.endpoints.%[2]s.cloud.goog" basePath: "/" @@ -145,7 +153,13 @@ definitions: EOF } -`, serviceId, project) + +resource "null_resource" "some" { + triggers = { + config_id = google_endpoints_service.endpoints_service.config_id + } +} +`, serviceId, project, rev) } func testAccEndpointsService_grpc(serviceId, project string) string { From e5c5b9ac3b50f25577db6b3ee91f106b55788095 Mon Sep 17 00:00:00 2001 From: Nathan Mckinley Date: Tue, 30 Jun 2020 15:35:50 -0700 Subject: [PATCH 3/3] null provider not available - use random instead. --- .../terraform/tests/resource_endpoints_service_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/third_party/terraform/tests/resource_endpoints_service_test.go b/third_party/terraform/tests/resource_endpoints_service_test.go index 21e2e20125da..aa7d12413214 100644 --- a/third_party/terraform/tests/resource_endpoints_service_test.go +++ b/third_party/terraform/tests/resource_endpoints_service_test.go @@ -154,10 +154,11 @@ EOF } -resource "null_resource" "some" { - triggers = { +resource "random_id" "foo" { + keepers = { config_id = google_endpoints_service.endpoints_service.config_id } + byte_length = 8 } `, serviceId, project, rev) }