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

Guess at config_id value so that updates don't break downstream resources. #6722

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/3723.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
`google_endpoints_service` now allows dependent resources to plan based on the `config_id` value.
```
29 changes: 29 additions & 0 deletions google/resource_endpoints_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"log"
"regexp"
"strconv"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -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
Expand Down
23 changes: 19 additions & 4 deletions google/resource_endpoints_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
},
Expand Down Expand Up @@ -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"
Expand All @@ -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: "/"
Expand Down Expand Up @@ -145,7 +153,14 @@ definitions:
EOF

}
`, serviceId, project)

resource "random_id" "foo" {
keepers = {
config_id = google_endpoints_service.endpoints_service.config_id
}
byte_length = 8
}
`, serviceId, project, rev)
}

func testAccEndpointsService_grpc(serviceId, project string) string {
Expand Down