Skip to content

Commit aaa064a

Browse files
Guess at config_id value so that updates don't break downstream resources. (#3723) (#2248)
Signed-off-by: Modular Magician <[email protected]>
1 parent bc70938 commit aaa064a

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

.changelog/3723.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
`google_endpoints_service` now allows dependent resources to plan based on the `config_id` value.
3+
```

google-beta/resource_endpoints_service.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import (
44
"encoding/base64"
55
"encoding/json"
66
"errors"
7+
"fmt"
78
"log"
9+
"regexp"
10+
"strconv"
11+
"strings"
812
"time"
913

1014
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
@@ -141,9 +145,34 @@ func resourceEndpointsService() *schema.Resource {
141145
},
142146
},
143147
},
148+
CustomizeDiff: predictServiceId,
144149
}
145150
}
146151

152+
func predictServiceId(d *schema.ResourceDiff, meta interface{}) error {
153+
if !d.HasChange("openapi_config") && !d.HasChange("grpc_config") && !d.HasChange("protoc_output_base64") {
154+
return nil
155+
}
156+
loc, _ := time.LoadLocation("America/Los_Angeles")
157+
baseDate := time.Now().In(loc).Format("2006-01-02")
158+
oldConfigId := d.Get("config_id").(string)
159+
if match, err := regexp.MatchString(`\d\d\d\d-\d\d-\d\dr\d*`, oldConfigId); !match || err != nil {
160+
// If we do not match the expected format, we will guess
161+
// wrong and that is worse than not guessing.
162+
return nil
163+
}
164+
if strings.HasPrefix(oldConfigId, baseDate) {
165+
n, err := strconv.Atoi(strings.Split(oldConfigId, "r")[1])
166+
if err != nil {
167+
return err
168+
}
169+
d.SetNew("config_id", fmt.Sprintf("%sr%d", baseDate, n+1))
170+
} else {
171+
d.SetNew("config_id", baseDate+"r0")
172+
}
173+
return nil
174+
}
175+
147176
func getEndpointServiceOpenAPIConfigSource(configText string) *servicemanagement.ConfigSource {
148177
// We need to provide a ConfigSource object to the API whenever submitting a
149178
// new config. A ConfigSource contains a ConfigFile which contains the b64

google-beta/resource_endpoints_service_test.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@ func TestAccEndpointsService_basic(t *testing.T) {
2121
Providers: testAccProviders,
2222
Steps: []resource.TestStep{
2323
{
24-
Config: testAccEndpointsService_basic(serviceId, getTestProjectFromEnv()),
24+
Config: testAccEndpointsService_basic(serviceId, getTestProjectFromEnv(), "1"),
25+
Check: testAccCheckEndpointExistsByName(t, serviceId),
26+
},
27+
{
28+
Config: testAccEndpointsService_basic(serviceId, getTestProjectFromEnv(), "2"),
29+
Check: testAccCheckEndpointExistsByName(t, serviceId),
30+
},
31+
{
32+
Config: testAccEndpointsService_basic(serviceId, getTestProjectFromEnv(), "3"),
2533
Check: testAccCheckEndpointExistsByName(t, serviceId),
2634
},
2735
},
@@ -97,7 +105,7 @@ func TestEndpointsService_grpcMigrateState(t *testing.T) {
97105
}
98106
}
99107

100-
func testAccEndpointsService_basic(serviceId, project string) string {
108+
func testAccEndpointsService_basic(serviceId, project, rev string) string {
101109
return fmt.Sprintf(`
102110
resource "google_endpoints_service" "endpoints_service" {
103111
service_name = "%[1]s.endpoints.%[2]s.cloud.goog"
@@ -106,7 +114,7 @@ resource "google_endpoints_service" "endpoints_service" {
106114
swagger: "2.0"
107115
info:
108116
description: "A simple Google Cloud Endpoints API example."
109-
title: "Endpoints Example"
117+
title: "Endpoints Example, rev. %[3]s"
110118
version: "1.0.0"
111119
host: "%[1]s.endpoints.%[2]s.cloud.goog"
112120
basePath: "/"
@@ -145,7 +153,14 @@ definitions:
145153
EOF
146154
147155
}
148-
`, serviceId, project)
156+
157+
resource "random_id" "foo" {
158+
keepers = {
159+
config_id = google_endpoints_service.endpoints_service.config_id
160+
}
161+
byte_length = 8
162+
}
163+
`, serviceId, project, rev)
149164
}
150165

151166
func testAccEndpointsService_grpc(serviceId, project string) string {

0 commit comments

Comments
 (0)