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. #3723

Merged
merged 3 commits into from
Jun 30, 2020
Merged
Changes from 1 commit
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
29 changes: 29 additions & 0 deletions third_party/terraform/resources/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