Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eshagoel06 committed Mar 6, 2024
1 parent bf6a98f commit 9aff3e8
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 54 deletions.
33 changes: 18 additions & 15 deletions mmv1/products/apphub/Service.yaml
Original file line number Diff line number Diff line change
@@ -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}}
Expand All @@ -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'
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -105,7 +108,6 @@ properties:
description: 'Criticality type. '
required: true
values:
- :TYPE_UNSPECIFIED
- :MISSION_CRITICAL
- :HIGH
- :MEDIUM
Expand All @@ -119,7 +121,6 @@ properties:
description: 'Environment type. '
required: true
values:
- :TYPE_UNSPECIFIED
- :PRODUCTION
- :STAGING
- :TEST
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
24 changes: 12 additions & 12 deletions mmv1/templates/terraform/examples/apphub_service_basic.tf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -39,15 +39,15 @@ 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
}


# 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
Expand All @@ -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"
Expand All @@ -71,15 +71,15 @@ 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]
}

# 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

Expand Down
30 changes: 15 additions & 15 deletions mmv1/templates/terraform/examples/apphub_service_full.tf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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'] %>"
Expand Down Expand Up @@ -61,15 +61,15 @@ 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
}


# 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
Expand All @@ -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"
Expand All @@ -93,15 +93,15 @@ 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]
}

# 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 9aff3e8

Please sign in to comment.