Skip to content

Commit

Permalink
Convert custom_check_destroy, custom_create, custom_delete, custom_im…
Browse files Browse the repository at this point in the history
…port templates (GoogleCloudPlatform#10460)
  • Loading branch information
zli82016 authored and pengq-google committed May 21, 2024
1 parent 905982c commit 6419927
Show file tree
Hide file tree
Showing 134 changed files with 3,082 additions and 38 deletions.
12 changes: 12 additions & 0 deletions mmv1/api/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -937,3 +937,15 @@ func (r Resource) IgnoreReadPropertiesToString(e resource.Examples) string {
func (r *Resource) SetCompiler(t string) {
r.Compiler = fmt.Sprintf("%s-codegen", strings.ToLower(t))
}

// Returns the id format of an object, or self_link_uri if none is explicitly defined
// We prefer the long name of a resource as the id so that users can reference
// resources in a standard way, and most APIs accept short name, long name or self_link
// def id_format(object)
func (r Resource) GetIdFormat() string {
idFormat := r.IdFormat
if idFormat == "" {
idFormat = r.SelfLinkUri()
}
return idFormat
}
2 changes: 1 addition & 1 deletion mmv1/products/identityplatform/Config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ examples:
- "client.0.api_key"
- "client.0.firebase_subdomain"
custom_code: !ruby/object:Provider::Terraform::CustomCode
custom_create: 'templates/terraform/custom_create/identity_platform_config.go'
custom_create: 'templates/terraform/custom_create/identity_platform_config.go.erb'
properties:
- !ruby/object:Api::Type::String
name: 'name'
Expand Down
2 changes: 2 additions & 0 deletions mmv1/provider/template_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ var TemplateFunctions = template.FuncMap{
"replace": strings.Replace,
"camelize": google.Camelize,
"underscore": google.Underscore,
"plural": google.Plural,
"contains": strings.Contains,
"join": strings.Join,
}

var GA_VERSION = "ga"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
*/ -}}
var accessApprovalCloudProductMapping = map[string]string{
"appengine.googleapis.com": "App Engine",
"bigquery.googleapis.com": "BigQuery",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
*/ -}}
func upstreamPoliciesDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
o, n := d.GetChange("virtual_repository_config.0.upstream_policies")
oldPolicies, ok := o.([]any)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
*/ -}}
// suppress changes on sample_rate if log_config is set to disabled.
func suppressWhenDisabled(k, old, new string, d *schema.ResourceData) bool {
_, n := d.GetChange("log_config.0.enable")
Expand Down
2 changes: 1 addition & 1 deletion mmv1/templates/terraform/constants/go/billing_budget.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
*/ -}}

// Check to see if a specified value in the config exists and suppress diffs if so. Otherwise run EmptyOrDefaultStringSuppress.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
*/ -}}
func DefaultBinaryAuthorizationPolicy(project string) map[string]interface{} {
return map[string]interface{}{
"name": fmt.Sprintf("projects/%s/policy", project),
Expand Down
2 changes: 1 addition & 1 deletion mmv1/templates/terraform/constants/go/cert_manager.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
*/ -}}

func certManagerDefaultScopeDiffSuppress(_, old, new string, diff *schema.ResourceData) bool {
if old == "" && new == "DEFAULT" || old == "DEFAULT" && new == "" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
*/ -}}

// Hash based on key, which is either project_id_or_num or network_url.
func computeServiceAttachmentConsumerAcceptListsHash(v interface{}) int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
*/ -}}
func resourceDatastreamStreamCustomDiffFunc(diff tpgresource.TerraformResourceDiff) error {
if diff.HasChange("desired_state") {
old, new := diff.GetChange("desired_state")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
*/ -}}
// This customizeDiff allows updating the dictionary, regex, and large_custom_dictionary fields, but
// it recreates the resource if changing between these fields. e.g., updating the regex field should
// be allowed, while changing from regex to dictionary should trigger the recreation of the resource.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
func enableRTDB(config *transport_tpg.Config, d *schema.ResourceData, project string, billingProject string, userAgent string) error {
url, err := tpgresource.ReplaceVars(d, config, "{{FirebaseDatabaseBasePath}}projects/{{project}}/locations/{{region}}/instances/{{instance_id}}:reenable")
url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}FirebaseDatabaseBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}region{{"}}"}}/instances/{{"{{"}}instance_id{{"}}"}}:reenable")
if err != nil {
return err
}
Expand All @@ -20,7 +20,7 @@ func enableRTDB(config *transport_tpg.Config, d *schema.ResourceData, project st
}

func disableRTDB(config *transport_tpg.Config, d *schema.ResourceData, project string, billingProject string, userAgent string) error {
url, err := tpgresource.ReplaceVars(d, config, "{{FirebaseDatabaseBasePath}}projects/{{project}}/locations/{{region}}/instances/{{instance_id}}:disable")
url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}FirebaseDatabaseBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}region{{"}}"}}/instances/{{"{{"}}instance_id{{"}}"}}:disable")
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
func getExistingFirebaseProjectId(config *transport_tpg.Config, d *schema.ResourceData, billingProject string, userAgent string) (string, error) {
url, err := tpgresource.ReplaceVars(d, config, "{{FirebaseBasePath}}projects/{{project}}")
url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}FirebaseBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}")
if err != nil {
return "", err
}
Expand All @@ -12,7 +12,7 @@ func getExistingFirebaseProjectId(config *transport_tpg.Config, d *schema.Resour
UserAgent: userAgent,
})
if err == nil {
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}")
id, err := tpgresource.ReplaceVars(d, config, "projects/{{"{{"}}project{{"}}"}}")
if err != nil {
return "", fmt.Errorf("Error constructing id: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion mmv1/templates/terraform/constants/go/firewall.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
*/ -}}
func resourceComputeFirewallRuleHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
*/ -}}

// API does not return a value for REDUCE_NONE
func crossSeriesReducerDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ func NetAppVolumeReplicationWaitForMirror(d *schema.ResourceData, meta interface
return err
}

url, err := tpgresource.ReplaceVars(d, config, "{{NetappBasePath}}projects/{{project}}/locations/{{location}}/volumes/{{volume_name}}/replications/{{name}}")
url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}NetappBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}location{{"}}"}}/volumes/{{"{{"}}volume_name{{"}}"}}/replications/{{"{{"}}name{{"}}"}}")
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
*/ -}}

type NetworkEndpointsNetworkEndpoint struct {
IPAddress string
Expand Down Expand Up @@ -64,7 +64,7 @@ func networkEndpointsPaginatedRead(d *schema.ResourceData, config *transport_tpg
func networkEndpointsPaginatedMutate(d *schema.ResourceData, endpoints []interface{}, config *transport_tpg.Config, userAgent, url, project, billingProject string, chunkSize int, returnLastPage bool) ([]interface{}, error) {
// Pull out what this mutation is doing - either attachNetworkEndpoints or detachNetworkEndpoints
verb := url[len(url)-len("attachNetworkEndpoints"):]
id, err := tpgresource.ReplaceVars(d, config, "{{project}}/{{zone}}/{{network_endpoint_group}}/endpoints")
id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}project{{"}}"}}/{{"{{"}}zone{{"}}"}}/{{"{{"}}network_endpoint_group{{"}}"}}/endpoints")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
*/ -}}
// Checks if there is another gateway under the same location.
func gatewaysSameLocation(d *schema.ResourceData, config *transport_tpg.Config, billingProject, userAgent string) ([]interface{}, error) {
log.Print("[DEBUG] Looking for gateways under the same location.")
var gateways []interface{}

gatewaysUrl, err := tpgresource.ReplaceVars(d, config, "{{NetworkServicesBasePath}}projects/{{project}}/locations/{{location}}/gateways")
gatewaysUrl, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}NetworkServicesBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}location{{"}}"}}/gateways")
if err != nil {
return gateways, err
}
Expand Down Expand Up @@ -77,7 +77,7 @@ func isLastSWGGateway(gateways []interface{}, network string) bool {
func deleteSWGAutoGenRouter(d *schema.ResourceData, config *transport_tpg.Config, billingProject, userAgent string) error {
log.Printf("[DEBUG] Searching the network id by name %q.", d.Get("network"))

networkPath := fmt.Sprintf("{{ComputeBasePath}}%s", d.Get("network"))
networkPath := fmt.Sprintf("{{"{{"}}ComputeBasePath{{"}}"}}%s", d.Get("network"))
networkUrl, err := tpgresource.ReplaceVars(d, config, networkPath)
if err != nil {
return err
Expand All @@ -98,7 +98,7 @@ func deleteSWGAutoGenRouter(d *schema.ResourceData, config *transport_tpg.Config
routerId := fmt.Sprintf("swg-autogen-router-%s", resp["id"])
log.Printf("[DEBUG] Deleting the auto generated router %q.", routerId)

routerPath := fmt.Sprintf("{{ComputeBasePath}}projects/{{project}}/regions/{{location}}/routers/%s", routerId)
routerPath := fmt.Sprintf("{{"{{"}}ComputeBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/regions/{{"{{"}}location{{"}}"}}/routers/%s", routerId)
routerUrl, err := tpgresource.ReplaceVars(d, config, routerPath)
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
*/ -}}
// Fields in "backends" that are not allowed for non-managed backend services
// (loadBalancingScheme) - the API returns an error if they are set at all
// in the request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
*/ -}}
func regionSslPolicyCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, v interface{}) error {
profile := diff.Get("profile")
customFeaturesCount := diff.Get("custom_features.#")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func rrdatasDnsDiffSuppress(k, old, new string, d *schema.ResourceData) bool {

// suppress on a list when 1) its items have dups that need to be ignored
// and 2) string comparison on the items may need a special parse function
// example of usage can be found ../../../third_party/terraform/services/dns/resource_dns_record_set_test.go.erb
// example of usage can be found ../../../third_party/terraform/services/dns/resource_dns_record_set_test.go.tmpl
func RrdatasListDiffSuppress(oldList, newList []string, fun func(x string) string, _ *schema.ResourceData) bool {
// compare two lists of unordered records
diff := make(map[string]bool, len(oldList))
Expand Down
2 changes: 1 addition & 1 deletion mmv1/templates/terraform/constants/go/router.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
*/ -}}
// customizeDiff func for additional checks on google_compute_router properties:
func resourceComputeRouterCustomDiff(_ context.Context, diff *schema.ResourceDiff, meta interface{}) error {

Expand Down
2 changes: 1 addition & 1 deletion mmv1/templates/terraform/constants/go/router_nat.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
*/ -}}
func resourceNameSetFromSelfLinkSet(v interface{}) *schema.Set {
if v == nil {
return schema.NewSet(schema.HashString, nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
*/ -}}
func resourceSourceRepoRepositoryPubSubConfigsHash(v interface{}) int {
if v == nil {
return 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
*/ -}}
// customizeDiff func for additional checks on google_spanner_database properties:
func resourceSpannerDBDdlCustomDiffFunc(diff tpgresource.TerraformResourceDiff) error {
old, new := diff.GetChange("ddl")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
*/ -}}
func deleteSpannerBackups(d *schema.ResourceData, config *transport_tpg.Config, res map[string]interface{}, userAgent string, billingProject string) error {
var v interface{}
var ok bool
Expand All @@ -30,7 +30,7 @@ func deleteSpannerBackups(d *schema.ResourceData, config *transport_tpg.Config,

log.Printf("[DEBUG] Found backups for resource %q: %#v)", d.Id(), item)

path := "{{SpannerBasePath}}" + backupName
path := "{{"{{"}}SpannerBasePath{{"}}"}}" + backupName

url, err := tpgresource.ReplaceVars(d, config, path)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion mmv1/templates/terraform/constants/go/ssl_policy.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
*/ -}}
func sslPolicyCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, v interface{}) error {
profile := diff.Get("profile")
customFeaturesCount := diff.Get("custom_features.#")
Expand Down
2 changes: 1 addition & 1 deletion mmv1/templates/terraform/constants/go/subscription.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
*/ -}}

func comparePubsubSubscriptionExpirationPolicy(_, old, new string, _ *schema.ResourceData) bool {
trimmedNew := strings.TrimLeft(new, "0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
*/ -}}

//Use it to delete TagTemplate Field
func deleteTagTemplateField(d *schema.ResourceData, config *transport_tpg.Config, name, billingProject, userAgent string) (error) {

url_delete, err := tpgresource.ReplaceVars(d, config, "{{DataCatalogBasePath}}{{name}}/fields/"+name+"?force={{force_delete}}")
url_delete, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}DataCatalogBasePath{{"}}"}}{{"{{"}}name{{"}}"}}/fields/"+name+"?force={{"{{"}}force_delete{{"}}"}}")
if err != nil {
return err
}
Expand All @@ -39,7 +39,7 @@ func deleteTagTemplateField(d *schema.ResourceData, config *transport_tpg.Config
//Use it to create TagTemplate Field
func createTagTemplateField(d *schema.ResourceData, config *transport_tpg.Config, body map[string]interface{}, name, billingProject, userAgent string) (error) {

url_create, err := tpgresource.ReplaceVars(d, config, "{{DataCatalogBasePath}}{{name}}/fields")
url_create, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}DataCatalogBasePath{{"}}"}}{{"{{"}}name{{"}}"}}/fields")
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
config := acctest.GoogleProviderConfig(t)

url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{"{{"}}ApigeeBasePath{{"}}"}}organizations/{{"{{"}}org{{"}}"}}")
if err != nil {
return err
}

billingProject := ""

if config.BillingProject != "" {
billingProject = config.BillingProject
}

res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "GET",
Project: billingProject,
RawURL: url,
UserAgent: config.UserAgent,
})

if err != nil {
// If the Apigee org doesn't exist, then a 403 can also be returned.
if transport_tpg.IsGoogleApiErrorWithCode(err, 403) || transport_tpg.IsGoogleApiErrorWithCode(err, 404) {
return nil
} else {
return err
}
}

v, ok := res["addonsConfig"]

if ok || v != nil {
return fmt.Errorf("ApigeeAddonConfig still exists at %s", url)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
log.Printf("[DEBUG] Ignoring destroy during test")
Loading

0 comments on commit 6419927

Please sign in to comment.