From 9aff3e84250faefbbf1519ef2a1d0fd864b247ef Mon Sep 17 00:00:00 2001 From: Esha Goel Date: Wed, 6 Mar 2024 10:52:36 +0000 Subject: [PATCH] Fix tests --- mmv1/products/apphub/Service.yaml | 33 ++++++++++--------- .../examples/apphub_service_basic.tf.erb | 24 +++++++------- .../examples/apphub_service_full.tf.erb | 30 ++++++++--------- .../data_source_apphub_discovered_service.go | 33 ++++++++++++------- 4 files changed, 66 insertions(+), 54 deletions(-) diff --git a/mmv1/products/apphub/Service.yaml b/mmv1/products/apphub/Service.yaml index 20e08f3f1f9f..3502e383e170 100644 --- a/mmv1/products/apphub/Service.yaml +++ b/mmv1/products/apphub/Service.yaml @@ -1,3 +1,16 @@ +# Copyright 2024 Google Inc. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + --- !ruby/object:Api::Resource base_url: projects/{{project}}/locations/{{location}}/applications/{{applications_id}}/services create_url: projects/{{project}}/locations/{{location}}/applications/{{applications_id}}/services?serviceId={{service_id}} @@ -6,7 +19,8 @@ id_format: projects/{{project}}/locations/{{location}}/applications/{{applicatio import_format: - projects/{{project}}/locations/{{location}}/applications/{{applications_id}}/services/{{service_id}} name: Service -description: Description +description: 'Service is a network/api interface that exposes some functionality to clients for consumption over the network. + Service typically has one or more Workloads behind it. It registers identified service to the Application.' autogen_async: true custom_code: !ruby/object:Provider::Terraform::CustomCode constants: 'templates/terraform/constants/apphub_discovered_resource.go.erb' @@ -15,20 +29,9 @@ examples: name: "service_basic" primary_resource_id: "example" config_path: "templates/terraform/examples/apphub_service_basic.tf.erb" - skip_test: true vars: - location: "us-east1" application_id: "example-application-1" service_project_attachment_id: "project-1" - test_env_vars: - org_id: :ORG_ID - - !ruby/object:Provider::Terraform::Examples - name: "service_basic" - primary_resource_id: "example" - config_path: "templates/terraform/examples/apphub_service_basic.tf.erb" - skip_docs: true - vars: - application_id: "example-application-1" ilb_network: "l7-ilb-network" ilb_subnet: "l7-ilb-subnet" forwarding_rule: "l7-ilb-forwarding-rule" @@ -38,9 +41,9 @@ examples: name: "service_full" primary_resource_id: "example" config_path: "templates/terraform/examples/apphub_service_full.tf.erb" - skip_docs: true vars: application_id: "example-application-1" + service_project_attachment_id: "project-1" display_name: "Example Service Full" description: "Register service for testing" business_name: "Alice" @@ -105,7 +108,6 @@ properties: description: 'Criticality type. ' required: true values: - - :TYPE_UNSPECIFIED - :MISSION_CRITICAL - :HIGH - :MEDIUM @@ -119,7 +121,6 @@ properties: description: 'Environment type. ' required: true values: - - :TYPE_UNSPECIFIED - :PRODUCTION - :STAGING - :TEST @@ -148,6 +149,7 @@ properties: - !ruby/object:Api::Type::String name: email description: 'Required. Email address of the contacts. ' + required: true description: 'Optional. Operator team that ensures runtime and operations. ' - !ruby/object:Api::Type::Array name: businessOwners @@ -159,6 +161,7 @@ properties: - !ruby/object:Api::Type::String name: email description: 'Required. Email address of the contacts. ' + required: true description: 'Optional. Business team that ensures user needs are met and value is delivered ' name: attributes diff --git a/mmv1/templates/terraform/examples/apphub_service_basic.tf.erb b/mmv1/templates/terraform/examples/apphub_service_basic.tf.erb index 64d2580cb651..9d9b6574f9a6 100644 --- a/mmv1/templates/terraform/examples/apphub_service_basic.tf.erb +++ b/mmv1/templates/terraform/examples/apphub_service_basic.tf.erb @@ -6,14 +6,14 @@ resource "google_apphub_application" "application" { } } -# resource "google_project" "service_project" { -# project_id ="<%= ctx[:vars]['service_project_attachment_id'] %>" -# name = "Service Project" -# org_id = "<%= ctx[:test_env_vars]['org_id'] %>" -# } +resource "google_project" "service_project" { + project_id ="<%= ctx[:vars]['service_project_attachment_id'] %>" + name = "Service Project" + org_id = "<%= ctx[:test_env_vars]['org_id'] %>" +} resource "google_apphub_service_project_attachment" "service_project_attachment" { - service_project_attachment_id = "serviceproject-demo-3" + service_project_attachment_id = google_project.service_project.project_id } # discovered service block @@ -29,7 +29,7 @@ data "google_apphub_discovered_service" "catalog-service" { resource "google_apphub_service" "<%= ctx[:primary_resource_id] %>" { location = "us-east1" applications_id = google_apphub_application.application.application_id - service_id = "l7-ilb-backend-subnet" + service_id = google_compute_forwarding_rule.forwarding_rule.name discovered_service = data.google_apphub_discovered_service.catalog-service.discovered_service[0].name } @@ -39,7 +39,7 @@ resource "google_apphub_service" "<%= ctx[:primary_resource_id] %>" { # VPC network resource "google_compute_network" "ilb_network" { name = "<%= ctx[:vars]['ilb_network'] %>" - project = "serviceproject-demo-3" + project = google_project.service_project.project_id auto_create_subnetworks = false } @@ -47,7 +47,7 @@ resource "google_compute_network" "ilb_network" { # backend subnet resource "google_compute_subnetwork" "ilb_subnet" { name = "<%= ctx[:vars]['ilb_subnet'] %>" - project = "serviceproject-demo-3" + project = google_project.service_project.project_id ip_cidr_range = "10.0.1.0/24" region = "us-east1" network = google_compute_network.ilb_network.id @@ -56,7 +56,7 @@ resource "google_compute_subnetwork" "ilb_subnet" { # forwarding rule resource "google_compute_forwarding_rule" "forwarding_rule" { name ="<%= ctx[:vars]['forwarding_rule'] %>" - project = "serviceproject-demo-3" + project = google_project.service_project.project_id region = "us-east1" ip_version = "IPV4" load_balancing_scheme = "INTERNAL" @@ -71,7 +71,7 @@ resource "google_compute_forwarding_rule" "forwarding_rule" { # backend service resource "google_compute_region_backend_service" "backend" { name = "<%= ctx[:vars]['backend_service'] %>" - project = "serviceproject-demo-3" + project = google_project.service_project.project_id region = "us-east1" health_checks = [google_compute_health_check.default.id] } @@ -79,7 +79,7 @@ resource "google_compute_region_backend_service" "backend" { # health check resource "google_compute_health_check" "default" { name = "<%= ctx[:vars]['health_check'] %>" - project = "serviceproject-demo-3" + project = google_project.service_project.project_id check_interval_sec = 1 timeout_sec = 1 diff --git a/mmv1/templates/terraform/examples/apphub_service_full.tf.erb b/mmv1/templates/terraform/examples/apphub_service_full.tf.erb index aa5d3f40f449..75865ceb29af 100644 --- a/mmv1/templates/terraform/examples/apphub_service_full.tf.erb +++ b/mmv1/templates/terraform/examples/apphub_service_full.tf.erb @@ -6,16 +6,15 @@ resource "google_apphub_application" "application" { } } -# resource "google_project" "service_project" { -# project_id ="<%= ctx[:vars]['service_project_attachment_id'] %>" -# name = "Service Project" -# org_id = "<%= ctx[:test_env_vars]['org_id'] %>" -# } +resource "google_project" "service_project" { + project_id ="<%= ctx[:vars]['service_project_attachment_id'] %>" + name = "Service Project" + org_id = "<%= ctx[:test_env_vars]['org_id'] %>" +} -# resource "google_apphub_service_project_attachment" "service_project_attachment" { -# service_project_attachment_id = "serviceproject-demo-2" -# depends_on = [google_project_service.apphub] -# } +resource "google_apphub_service_project_attachment" "service_project_attachment" { + service_project_attachment_id = google_project.service_project.project_id +} # discovered service block data "google_apphub_discovered_service" "catalog-service" { @@ -24,12 +23,13 @@ data "google_apphub_discovered_service" "catalog-service" { # ServiceReference | Application Hub | Google Cloud # Using this reference means that this resource will not be provisioned until the forwarding rule is fully created service_uri = "//compute.googleapis.com/${google_compute_forwarding_rule.forwarding_rule.id}" + depends_on = [google_apphub_service_project_attachment.service_project_attachment] } resource "google_apphub_service" "<%= ctx[:primary_resource_id] %>" { location = "us-east1" applications_id = google_apphub_application.application.application_id - service_id = "l7-ilb-forwarding-rule" + service_id = google_compute_forwarding_rule.forwarding_rule.name discovered_service = data.google_apphub_discovered_service.catalog-service.discovered_service[0].name display_name = "<%= ctx[:vars]['display_name'] %>" description = "<%= ctx[:vars]['description'] %>" @@ -61,7 +61,7 @@ resource "google_apphub_service" "<%= ctx[:primary_resource_id] %>" { # VPC network resource "google_compute_network" "ilb_network" { name = "<%= ctx[:vars]['ilb_network'] %>" - project = "serviceproject-demo-2" + project = google_project.service_project.project_id auto_create_subnetworks = false } @@ -69,7 +69,7 @@ resource "google_compute_network" "ilb_network" { # backend subnet resource "google_compute_subnetwork" "ilb_subnet" { name = "<%= ctx[:vars]['ilb_subnet'] %>" - project = "serviceproject-demo-2" + project = google_project.service_project.project_id ip_cidr_range = "10.0.1.0/24" region = "us-east1" network = google_compute_network.ilb_network.id @@ -78,7 +78,7 @@ resource "google_compute_subnetwork" "ilb_subnet" { # forwarding rule resource "google_compute_forwarding_rule" "forwarding_rule" { name ="<%= ctx[:vars]['forwarding_rule'] %>" - project = "serviceproject-demo-2" + project = google_project.service_project.project_id region = "us-east1" ip_version = "IPV4" load_balancing_scheme = "INTERNAL" @@ -93,7 +93,7 @@ resource "google_compute_forwarding_rule" "forwarding_rule" { # backend service resource "google_compute_region_backend_service" "backend" { name = "<%= ctx[:vars]['backend_service'] %>" - project = "serviceproject-demo-2" + project = google_project.service_project.project_id region = "us-east1" health_checks = [google_compute_health_check.default.id] } @@ -101,7 +101,7 @@ resource "google_compute_region_backend_service" "backend" { # health check resource "google_compute_health_check" "default" { name = "<%= ctx[:vars]['health_check'] %>" - project = "serviceproject-demo-2" + project = google_project.service_project.project_id check_interval_sec = 1 timeout_sec = 1 diff --git a/mmv1/third_party/terraform/services/apphub/data_source_apphub_discovered_service.go b/mmv1/third_party/terraform/services/apphub/data_source_apphub_discovered_service.go index c49616cb470f..c03887b2406d 100644 --- a/mmv1/third_party/terraform/services/apphub/data_source_apphub_discovered_service.go +++ b/mmv1/third_party/terraform/services/apphub/data_source_apphub_discovered_service.go @@ -2,7 +2,6 @@ package apphub import ( "fmt" - "time" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-provider-google/google/tpgresource" @@ -92,25 +91,35 @@ func dataSourceApphubDiscoveredServiceRead(d *schema.ResourceData, meta interfac billingProject = bp } - res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ - Config: config, - Method: "GET", - Project: billingProject, - RawURL: url, - UserAgent: userAgent, + var res map[string]interface{} + + err = transport_tpg.Retry(transport_tpg.RetryOptions{ + RetryFunc: func() (rerr error) { + res, rerr = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "GET", + Project: billingProject, + RawURL: url, + UserAgent: userAgent, + }) + return rerr + }, + Timeout: d.Timeout(schema.TimeoutRead), }) - fmt.Println(res, err, "Test print") + if err != nil { - return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("ApphubDiscoveredService %q", d.Id())) + return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("ApphubDiscoveredService %q", d.Id()), url) } if err := d.Set("discovered_service", flattenApphubDiscoveredService(res["discoveredService"], d, config)); err != nil { return fmt.Errorf("Error setting discovered service: %s", err) } - // ds := d.Get("discovered_service").([]map[string]interface{}) - // d.SetId(ds[0]["name"].(string)) - d.SetId(time.Now().UTC().String()) + id, err := tpgresource.ReplaceVars(d, config, "{{service_uri}}") + if err != nil { + return err + } + d.SetId(id) return nil