Skip to content

Add support for global application in Apphub #12017

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

Merged
Merged
21 changes: 20 additions & 1 deletion mmv1/products/apphub/Application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,30 @@ async:
path: 'error'
message: 'message'
custom_code:
constants: 'templates/terraform/constants/apphub_application.go.tmpl'
custom_diff:
- 'apphubApplicationCustomizeDiff'
examples:
- name: 'application_basic'
config_path: 'templates/terraform/examples/apphub_application_basic.tf.tmpl'
primary_resource_id: 'example'
vars:
application_id: 'example-application'
location: 'us-east1'
scope_type: 'REGIONAL'
test_vars_overrides:
'location': '"us-east1"'
'scope_type': '"REGIONAL"'
- name: 'application_global_basic'
config_path: 'templates/terraform/examples/apphub_application_basic.tf.tmpl'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is config_path actually necessary here? This is usually just used in for private overrides. I'm assuming other examples of using this were just accidentally copied over from the private repo

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have updated basic and full examples to remove config_path. But for *_global_basic , we were intending to use the same config as basic with different values.

primary_resource_id: 'example'
vars:
application_id: 'example-application'
location: 'global'
scope_type: 'GLOBAL'
test_vars_overrides:
'location': '"global"'
'scope_type': '"GLOBAL"'
- name: 'application_full'
config_path: 'templates/terraform/examples/apphub_application_full.tf.tmpl'
primary_resource_id: 'example2'
Expand Down Expand Up @@ -171,10 +189,11 @@ properties:
properties:
- name: 'type'
type: Enum
description: "Required. Scope Type. \n Possible values:\nREGIONAL"
description: "Required. Scope Type. \n Possible values:\nREGIONAL\nGLOBAL"
required: true
enum_values:
- 'REGIONAL'
- 'GLOBAL'
- name: 'uid'
type: String
description: 'Output only. A universally unique identifier (in UUID4 format) for
Expand Down
17 changes: 17 additions & 0 deletions mmv1/templates/terraform/constants/apphub_application.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
func apphubApplicationCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, meta interface{}) error {
if diff.HasChange("location") || diff.HasChange("scope.0.type") {
location := diff.Get("location")
scope_type := diff.Get("scope.0.type")

if scope_type == "GLOBAL" {
if location != "global" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would the API give an appropriate error in this case? If so, is there a particular pressure to duplicate this validation in the Terraform provider?

Adding additional validation at this level can lead to instances where the API requirements change but the Terraform providers lag behind.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API will give an appropriate error in such case. But we wanted terraform plan to perform validation and catch this error.

return fmt.Errorf("Error validating location %s with %s scope type", location, scope_type)
}
} else {
if location == "global" {
return fmt.Errorf("Error validating location %s with %s scope type", location, scope_type)
}
}
}
return nil
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should cover this logic somewhere in a test. If we are going to keep it (see my previous comment), let's add an invalid config in https://github.com/GoogleCloudPlatform/magic-modules/blob/main/mmv1/third_party/terraform/services/apphub/resource_apphub_application_test.go and use ExpectError

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
resource "google_apphub_application" "{{$.PrimaryResourceId}}" {
location = "us-east1"
location = "{{index $.Vars "location"}}"
application_id = "{{index $.Vars "application_id"}}"
scope {
type = "REGIONAL"
type = "{{index $.Vars "scope_type"}}"
}
}