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

Fix failed tests related to google_service_networking_connection #8904

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
2 changes: 0 additions & 2 deletions mmv1/products/cloudbuild/BitbucketServerConfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ examples:
config_id: 'bbs-config'
network_name: 'vpc-network'
global_address_name: 'private-ip-alloc'
test_vars_overrides:
network_name: 'acctest.BootstrapSharedTestNetwork(t, "peered-network")'
custom_code: !ruby/object:Provider::Terraform::CustomCode
encoder: templates/terraform/encoders/cloudbuild_bitbucketserver_config.go.erb
post_create: templates/terraform/post_create/cloudbuild_bitbucketserver_config.go.erb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ examples:
profile: 'my-profileid'
global_address_name: 'private-ip-alloc'
network_name: 'vpc-network'
skip_test: true
parameters:
- !ruby/object:Api::Type::String
name: 'connectionProfileId'
Expand Down
1 change: 1 addition & 0 deletions mmv1/products/tpu/Node.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ examples:
vars:
node_name: 'test-tpu'
global_address_name: 'my-global-address'
network_name: 'tpu-node-network'
custom_code: !ruby/object:Provider::Terraform::CustomCode
constants: 'templates/terraform/constants/tpu_node.erb'
custom_diff: [
Expand Down
2 changes: 0 additions & 2 deletions mmv1/products/vertexai/IndexEndpoint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ examples:
vars:
address_name: "address-name"
network_name: "network-name"
test_vars_overrides:
network_name: 'acctest.BootstrapSharedTestNetwork(t, "vertex-ai-index-endpoint")'
- !ruby/object:Provider::Terraform::Examples
name: "vertex_ai_index_endpoint_with_public_endpoint"
primary_resource_id: "index_endpoint"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ resource "google_project_service" "servicenetworking" {
service = "servicenetworking.googleapis.com"
disable_on_destroy = false
}
data "google_compute_network" "vpc_network" {

resource "google_compute_network" "vpc_network" {
name = "<%= ctx[:vars]['network_name'] %>"
depends_on = [google_project_service.servicenetworking]
}
Expand All @@ -15,11 +15,11 @@ resource "google_compute_global_address" "private_ip_alloc" {
purpose = "VPC_PEERING"
address_type = "INTERNAL"
prefix_length = 16
network = data.google_compute_network.vpc_network.id
network = google_compute_network.vpc_network.id
}

resource "google_service_networking_connection" "default" {
network = data.google_compute_network.vpc_network.id
network = google_compute_network.vpc_network.id
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = [google_compute_global_address.private_ip_alloc.name]
depends_on = [google_project_service.servicenetworking]
Expand All @@ -36,7 +36,7 @@ resource "google_cloudbuild_bitbucket_server_config" "<%= ctx[:primary_resource_
}
username = "test"
api_key = "<api-key>"
peered_network = replace(data.google_compute_network.vpc_network.id, data.google_project.project.name, data.google_project.project.number)
peered_network = replace(google_compute_network.vpc_network.id, data.google_project.project.name, data.google_project.project.number)
ssl_ca = "-----BEGIN CERTIFICATE-----\n-----END CERTIFICATE-----\n-----BEGIN CERTIFICATE-----\n-----END CERTIFICATE-----\n"
depends_on = [google_service_networking_connection.default]
}
8 changes: 4 additions & 4 deletions mmv1/templates/terraform/examples/tpu_node_full.tf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@ resource "google_tpu_node" "<%= ctx[:primary_resource_id] %>" {
}
}

data "google_compute_network" "network" {
name = "default"
resource "google_compute_network" "network" {
name = "<%= ctx[:vars]['network_name'] %>"
}

resource "google_compute_global_address" "service_range" {
name = "<%= ctx[:vars]['global_address_name'] %>"
purpose = "VPC_PEERING"
address_type = "INTERNAL"
prefix_length = 16
network = data.google_compute_network.network.id
network = google_compute_network.network.id
}

resource "google_service_networking_connection" "private_service_connection" {
network = data.google_compute_network.network.id
network = google_compute_network.network.id
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = [google_compute_global_address.service_range.name]
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ resource "google_vertex_ai_endpoint" "<%= ctx[:primary_resource_id] %>" {
labels = {
label-one = "value-one"
}
network = "projects/${data.google_project.project.number}/global/networks/${data.google_compute_network.vertex_network.name}"
network = "projects/${data.google_project.project.number}/global/networks/${google_compute_network.vertex_network.name}"
encryption_spec {
kms_key_name = "<%= ctx[:vars]['kms_key_name'] %>"
}
Expand All @@ -17,7 +17,7 @@ resource "google_vertex_ai_endpoint" "<%= ctx[:primary_resource_id] %>" {
}

resource "google_service_networking_connection" "vertex_vpc_connection" {
network = data.google_compute_network.vertex_network.id
network = google_compute_network.vertex_network.id
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = [google_compute_global_address.vertex_range.name]
}
Expand All @@ -27,10 +27,10 @@ resource "google_compute_global_address" "vertex_range" {
purpose = "VPC_PEERING"
address_type = "INTERNAL"
prefix_length = 24
network = data.google_compute_network.vertex_network.id
network = google_compute_network.vertex_network.id
}

data "google_compute_network" "vertex_network" {
resource "google_compute_network" "vertex_network" {
name = "<%= ctx[:vars]['network_name'] %>"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ resource "google_vertex_ai_index_endpoint" "<%= ctx[:primary_resource_id] %>" {
labels = {
label-one = "value-one"
}
network = "projects/${data.google_project.project.number}/global/networks/${data.google_compute_network.vertex_network.name}"
network = "projects/${data.google_project.project.number}/global/networks/${google_compute_network.vertex_network.name}"
depends_on = [
google_service_networking_connection.vertex_vpc_connection
]
}

resource "google_service_networking_connection" "vertex_vpc_connection" {
network = data.google_compute_network.vertex_network.id
network = google_compute_network.vertex_network.id
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = [google_compute_global_address.vertex_range.name]
}
Expand All @@ -22,10 +22,10 @@ resource "google_compute_global_address" "vertex_range" {
purpose = "VPC_PEERING"
address_type = "INTERNAL"
prefix_length = 24
network = data.google_compute_network.vertex_network.id
network = google_compute_network.vertex_network.id
}

data "google_compute_network" "vertex_network" {
resource "google_compute_network" "vertex_network" {
name = "<%= ctx[:vars]['network_name'] %>"
}

Expand Down
131 changes: 131 additions & 0 deletions mmv1/third_party/terraform/acctest/bootstrap_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
tpgcompute "github.com/hashicorp/terraform-provider-google/google/services/compute"
"github.com/hashicorp/terraform-provider-google/google/services/privateca"
"github.com/hashicorp/terraform-provider-google/google/services/resourcemanager"
tpgservicenetworking "github.com/hashicorp/terraform-provider-google/google/services/servicenetworking"
"github.com/hashicorp/terraform-provider-google/google/services/sql"
"github.com/hashicorp/terraform-provider-google/google/tpgiamresource"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
Expand All @@ -23,6 +24,7 @@ import (
cloudresourcemanager "google.golang.org/api/cloudresourcemanager/v1"
iam "google.golang.org/api/iam/v1"
"google.golang.org/api/iamcredentials/v1"
"google.golang.org/api/servicenetworking/v1"
"google.golang.org/api/serviceusage/v1"
sqladmin "google.golang.org/api/sqladmin/v1beta4"
)
Expand Down Expand Up @@ -354,6 +356,135 @@ func BootstrapSharedTestNetwork(t *testing.T, testId string) string {
return network.Name
}

const SharedTestGlobalAddressPrefix = "tf-bootstrap-addr-"

func BootstrapSharedTestGlobalAddress(t *testing.T, testId, networkId string) string {
project := envvar.GetTestProjectFromEnv()
addressName := SharedTestGlobalAddressPrefix + testId

config := BootstrapConfig(t)
if config == nil {
return ""
}

log.Printf("[DEBUG] Getting shared test global address %q", addressName)
_, err := config.NewComputeClient(config.UserAgent).GlobalAddresses.Get(project, addressName).Do()
if err != nil && transport_tpg.IsGoogleApiErrorWithCode(err, 404) {
log.Printf("[DEBUG] Global address %q not found, bootstrapping", addressName)
url := fmt.Sprintf("%sprojects/%s/global/addresses", config.ComputeBasePath, project)
netObj := map[string]interface{}{
"name": addressName,
"address_type": "INTERNAL",
"purpose": "VPC_PEERING",
"prefix_length": 16,
"network": networkId,
}

res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "POST",
Project: project,
RawURL: url,
UserAgent: config.UserAgent,
Body: netObj,
Timeout: 4 * time.Minute,
})
if err != nil {
t.Fatalf("Error bootstrapping shared test global address %q: %s", addressName, err)
}

log.Printf("[DEBUG] Waiting for global address creation to finish")
err = tpgcompute.ComputeOperationWaitTime(config, res, project, "Error bootstrapping shared test global address", config.UserAgent, 4*time.Minute)
if err != nil {
t.Fatalf("Error bootstrapping shared test global address %q: %s", addressName, err)
}
}

address, err := config.NewComputeClient(config.UserAgent).GlobalAddresses.Get(project, addressName).Do()
if err != nil {
t.Errorf("Error getting shared test global address %q: %s", addressName, err)
}
if address == nil {
t.Fatalf("Error getting shared test global address %q: is nil", addressName)
}
return address.Name
}

// BootstrapSharedServiceNetworkingConnection will create a shared network
// if it hasn't been created in the test project, a global address
// if it hasn't been created in the test project, and a service networking connection
// if it hasn't been created in the test project.
//
// BootstrapSharedServiceNetworkingConnection returns a persistent compute network name
// for a test or set of tests.
//
// To delete a service networking conneciton, all of the service instances that use that connection
// must be deleted first. After the service instances are deleted, some service producers delay the deletion
// utnil a waiting period has passed. For example, after four days that you delete a SQL instance,
// the service networking connection can be deleted.
// That is the reason to use the shared service networking connection for thest resources.
// https://cloud.google.com/vpc/docs/configure-private-services-access#removing-connection
//
// testId specifies the test for which a shared network and a gobal address are used/initialized.
func BootstrapSharedServiceNetworkingConnection(t *testing.T, testId string) string {
parentService := "services/servicenetworking.googleapis.com"
project := envvar.GetTestProjectFromEnv()
projectNumber := envvar.GetTestProjectNumberFromEnv()

config := BootstrapConfig(t)
if config == nil {
return ""
}

networkName := BootstrapSharedTestNetwork(t, testId)
networkId := fmt.Sprintf("projects/%v/global/networks/%v", projectNumber, networkName)
globalAddressName := BootstrapSharedTestGlobalAddress(t, testId, networkId)

readCall := config.NewServiceNetworkingClient(config.UserAgent).Services.Connections.List(parentService).Network(networkId)
if config.UserProjectOverride {
readCall.Header().Add("X-Goog-User-Project", project)
}
response, err := readCall.Do()
if err != nil {
t.Errorf("Error getting shared test service networking connection: %s", err)
}

var connection *servicenetworking.Connection
for _, c := range response.Connections {
if c.Network == networkId {
connection = c
break
}
}

if connection == nil {
log.Printf("[DEBUG] Service networking connection not found, bootstrapping")

connection := &servicenetworking.Connection{
Network: networkId,
ReservedPeeringRanges: []string{globalAddressName},
}

createCall := config.NewServiceNetworkingClient(config.UserAgent).Services.Connections.Create(parentService, connection)
if config.UserProjectOverride {
createCall.Header().Add("X-Goog-User-Project", project)
}
op, err := createCall.Do()
if err != nil {
t.Fatalf("Error bootstrapping shared test service networking connection: %s", err)
}

log.Printf("[DEBUG] Waiting for service networking connection creation to finish")
if err := tpgservicenetworking.ServiceNetworkingOperationWaitTime(config, op, "Create Service Networking Connection", config.UserAgent, project, 4*time.Minute); err != nil {
t.Fatalf("Error bootstrapping shared test service networking connection: %s", err)
}
}

log.Printf("[DEBUG] Getting shared test service networking connection")

return networkName
}

var SharedServicePerimeterProjectPrefix = "tf-bootstrap-sp-"

func BootstrapServicePerimeterProjects(t *testing.T, desiredProjects int) []*cloudresourcemanager.Project {
Expand Down
Loading
Loading