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

Add support for global application in Apphub #12017

Merged
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
25 changes: 21 additions & 4 deletions mmv1/products/apphub/Application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,30 @@ async:
path: 'error'
message: 'message'
custom_code:
constants: 'templates/terraform/constants/apphub_application.go.tmpl'
custom_diff:
- 'apphubApplicationCustomizeDiff'
examples:
- name: 'application_basic'
- name: 'apphub_application_basic'
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: 'apphub_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'
- name: 'application_full'
config_path: 'templates/terraform/examples/apphub_application_full.tf.tmpl'
location: 'global'
scope_type: 'GLOBAL'
test_vars_overrides:
'location': '"global"'
'scope_type': '"GLOBAL"'
- name: 'apphub_application_full'
primary_resource_id: 'example2'
vars:
application_id: 'example-application'
Expand Down Expand Up @@ -171,10 +187,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"}}"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package apphub_test

import (
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand All @@ -21,7 +22,7 @@ func TestAccApphubApplication_applicationUpdateFull(t *testing.T) {
CheckDestroy: testAccCheckApphubApplicationDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccApphubApplication_applicationFullExample(context),
Config: testAccApphubApplication_apphubApplicationFullExample(context),
},
{
ResourceName: "google_apphub_application.example2",
Expand Down Expand Up @@ -208,3 +209,53 @@ resource "google_apphub_application" "example2" {
}
`, context)
}

func TestAccApphubApplication_invalidConfigFails(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckApphubApplicationDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccApphubApplication_applicationInvalidConfig1(context),
ExpectError: regexp.MustCompile("Error validating location global with REGIONAL scope type"),
},
{
Config: testAccApphubApplication_applicationInvalidConfig2(context),
ExpectError: regexp.MustCompile("Error validating location us-east1 with GLOBAL scope type"),
},
},
})
}

func testAccApphubApplication_applicationInvalidConfig1(context map[string]interface{}) string {
return acctest.Nprintf(`

resource "google_apphub_application" "invalid_example" {
location = "global"
application_id = "tf-test-invalid-example-application%{random_suffix}"
scope {
type = "REGIONAL"
}
}
`, context)
}

func testAccApphubApplication_applicationInvalidConfig2(context map[string]interface{}) string {
return acctest.Nprintf(`

resource "google_apphub_application" "invalid_example" {
location = "us-east1"
application_id = "tf-test-invalid-example-application%{random_suffix}"
scope {
type = "GLOBAL"
}
}
`, context)
}