|
17 | 17 | package networkservices_test |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "fmt" |
20 | 21 | "testing" |
21 | 22 |
|
22 | 23 | "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
23 | 24 | "github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest" |
| 25 | + "github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar" |
24 | 26 | ) |
25 | 27 |
|
26 | 28 | func TestAccNetworkServicesLbRouteExtension_update(t *testing.T) { |
@@ -883,3 +885,260 @@ resource "google_compute_region_backend_service" "callouts_backend_2" { |
883 | 885 | } |
884 | 886 | `, context) |
885 | 887 | } |
| 888 | + |
| 889 | +func TestAccNetworkServicesLbRouteExtension_crossRegionInternalPluginExtension(t *testing.T) { |
| 890 | + t.Parallel() |
| 891 | + |
| 892 | + context := map[string]interface{}{ |
| 893 | + "random_suffix": acctest.RandString(t, 10), |
| 894 | + "test_project_id": envvar.GetTestProjectFromEnv(), |
| 895 | + } |
| 896 | + |
| 897 | + acctest.VcrTest(t, resource.TestCase{ |
| 898 | + PreCheck: func() { acctest.AccTestPreCheck(t) }, |
| 899 | + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), |
| 900 | + CheckDestroy: testAccCheckNetworkServicesLbRouteExtensionDestroyProducer(t), |
| 901 | + Steps: []resource.TestStep{ |
| 902 | + { |
| 903 | + Config: testAccNetworkServicesWasmPlugin_artifactRegistryRepositorySetup(context), |
| 904 | + Check: resource.ComposeTestCheckFunc( |
| 905 | + // Upload the compiled plugin code to Artifact Registry |
| 906 | + testAccCheckNetworkServicesWasmPlugin_uploadCompiledCode( |
| 907 | + t, |
| 908 | + "google_artifact_registry_repository.test_repository", |
| 909 | + "my-wasm-plugin", |
| 910 | + "v1", |
| 911 | + "test-fixtures/compiled-package/plugin.wasm", |
| 912 | + "plugin.wasm", |
| 913 | + ), |
| 914 | + ), |
| 915 | + }, |
| 916 | + { |
| 917 | + ResourceName: "google_artifact_registry_repository.test_repository", |
| 918 | + ImportState: true, |
| 919 | + ImportStateVerify: true, |
| 920 | + ImportStateVerifyIgnore: []string{"labels", "name", "terraform_labels"}, |
| 921 | + }, |
| 922 | + { |
| 923 | + Config: testAccNetworkServicesLbRouteExtension_crossRegionInternalPluginExtension(context), |
| 924 | + }, |
| 925 | + { |
| 926 | + ResourceName: "google_network_services_lb_route_extension.default", |
| 927 | + ImportState: true, |
| 928 | + ImportStateVerify: true, |
| 929 | + ImportStateVerifyIgnore: []string{"location", "name", "labels", "terraform_labels"}, |
| 930 | + }, |
| 931 | + }, |
| 932 | + }) |
| 933 | +} |
| 934 | + |
| 935 | +func testAccNetworkServicesLbRouteExtension_crossRegionInternalPluginExtension(context map[string]interface{}) string { |
| 936 | + return fmt.Sprint(testAccNetworkServicesWasmPlugin_artifactRegistryRepositorySetup(context), acctest.Nprintf(` |
| 937 | +# VPC network |
| 938 | +resource "google_compute_network" "gilb_network" { |
| 939 | + name = "tf-test-l7-ilb-network%{random_suffix}" |
| 940 | + auto_create_subnetworks = false |
| 941 | +} |
| 942 | +
|
| 943 | +# proxy-only subnet |
| 944 | +resource "google_compute_subnetwork" "proxy_subnet" { |
| 945 | + name = "tf-test-l7-ilb-proxy-subnet%{random_suffix}" |
| 946 | + ip_cidr_range = "10.0.0.0/24" |
| 947 | + region = "us-west1" |
| 948 | + purpose = "GLOBAL_MANAGED_PROXY" |
| 949 | + role = "ACTIVE" |
| 950 | + network = google_compute_network.gilb_network.id |
| 951 | +} |
| 952 | +
|
| 953 | +# backend subnet |
| 954 | +resource "google_compute_subnetwork" "gilb_subnet" { |
| 955 | + name = "tf-test-l7-gilb-subnet%{random_suffix}" |
| 956 | + ip_cidr_range = "10.0.1.0/24" |
| 957 | + region = "us-west1" |
| 958 | + network = google_compute_network.gilb_network.id |
| 959 | +} |
| 960 | +
|
| 961 | +# forwarding rule |
| 962 | +resource "google_compute_global_forwarding_rule" "default" { |
| 963 | + name = "tf-test-l7-gilb-forwarding-rule%{random_suffix}" |
| 964 | + depends_on = [google_compute_subnetwork.proxy_subnet] |
| 965 | + ip_protocol = "TCP" |
| 966 | + load_balancing_scheme = "INTERNAL_MANAGED" |
| 967 | + port_range = "80" |
| 968 | + target = google_compute_target_http_proxy.default.id |
| 969 | + network = google_compute_network.gilb_network.id |
| 970 | + subnetwork = google_compute_subnetwork.gilb_subnet.id |
| 971 | +} |
| 972 | +
|
| 973 | +# HTTP target proxy |
| 974 | +resource "google_compute_target_http_proxy" "default" { |
| 975 | + name = "tf-test-l7-gilb-target-http-proxy%{random_suffix}" |
| 976 | + url_map = google_compute_url_map.default.id |
| 977 | +} |
| 978 | +
|
| 979 | +# URL map |
| 980 | +resource "google_compute_url_map" "default" { |
| 981 | + name = "tf-test-l7-gilb-url-map%{random_suffix}" |
| 982 | + default_service = google_compute_backend_service.default.id |
| 983 | +} |
| 984 | +
|
| 985 | +# backend service |
| 986 | +resource "google_compute_backend_service" "default" { |
| 987 | + name = "tf-test-l7-gilb-backend-subnet%{random_suffix}" |
| 988 | + protocol = "HTTP" |
| 989 | + load_balancing_scheme = "INTERNAL_MANAGED" |
| 990 | + timeout_sec = 10 |
| 991 | + health_checks = [google_compute_health_check.default.id] |
| 992 | + backend { |
| 993 | + group = google_compute_instance_group_manager.mig.instance_group |
| 994 | + balancing_mode = "UTILIZATION" |
| 995 | + capacity_scaler = 1.0 |
| 996 | + } |
| 997 | +} |
| 998 | +
|
| 999 | +# instance template |
| 1000 | +resource "google_compute_instance_template" "instance_template" { |
| 1001 | + name = "tf-test-l7-gilb-mig-template%{random_suffix}" |
| 1002 | + machine_type = "e2-small" |
| 1003 | + tags = ["http-server"] |
| 1004 | +
|
| 1005 | + network_interface { |
| 1006 | + network = google_compute_network.gilb_network.id |
| 1007 | + subnetwork = google_compute_subnetwork.gilb_subnet.id |
| 1008 | + access_config { |
| 1009 | + # add external ip to fetch packages |
| 1010 | + } |
| 1011 | + } |
| 1012 | + disk { |
| 1013 | + source_image = "debian-cloud/debian-12" |
| 1014 | + auto_delete = true |
| 1015 | + boot = true |
| 1016 | + } |
| 1017 | +
|
| 1018 | + # install nginx and serve a simple web page |
| 1019 | + metadata = { |
| 1020 | + startup-script = <<-EOF1 |
| 1021 | + #! /bin/bash |
| 1022 | + set -euo pipefail |
| 1023 | +
|
| 1024 | + export DEBIAN_FRONTEND=noninteractive |
| 1025 | + apt-get update |
| 1026 | + apt-get install -y nginx-light jq |
| 1027 | +
|
| 1028 | + NAME=$(curl -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/hostname") |
| 1029 | + IP=$(curl -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/ip") |
| 1030 | + METADATA=$(curl -f -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/attributes/?recursive=True" | jq 'del(.["startup-script"])') |
| 1031 | +
|
| 1032 | + cat <<EOF > /var/www/html/index.html |
| 1033 | + <pre> |
| 1034 | + Name: $NAME |
| 1035 | + IP: $IP |
| 1036 | + Metadata: $METADATA |
| 1037 | + </pre> |
| 1038 | + EOF |
| 1039 | + EOF1 |
| 1040 | + } |
| 1041 | + lifecycle { |
| 1042 | + create_before_destroy = true |
| 1043 | + } |
| 1044 | +} |
| 1045 | +
|
| 1046 | +# health check |
| 1047 | +resource "google_compute_health_check" "default" { |
| 1048 | + name = "tf-test-l7-gilb-hc%{random_suffix}" |
| 1049 | + http_health_check { |
| 1050 | + port_specification = "USE_SERVING_PORT" |
| 1051 | + } |
| 1052 | +} |
| 1053 | +
|
| 1054 | +# MIG |
| 1055 | +resource "google_compute_instance_group_manager" "mig" { |
| 1056 | + name = "tf-test-l7-gilb-mig1%{random_suffix}" |
| 1057 | + zone = "us-west1-b" |
| 1058 | + version { |
| 1059 | + instance_template = google_compute_instance_template.instance_template.id |
| 1060 | + name = "primary" |
| 1061 | + } |
| 1062 | + base_instance_name = "vm" |
| 1063 | + target_size = 2 |
| 1064 | +} |
| 1065 | +
|
| 1066 | +# allow all access from IAP and health check ranges |
| 1067 | +resource "google_compute_firewall" "fw-iap" { |
| 1068 | + name = "tf-test-l7-gilb-fw-allow-iap-hc%{random_suffix}" |
| 1069 | + direction = "INGRESS" |
| 1070 | + network = google_compute_network.gilb_network.id |
| 1071 | + source_ranges = ["130.211.0.0/22", "35.191.0.0/16", "35.235.240.0/20"] |
| 1072 | + allow { |
| 1073 | + protocol = "tcp" |
| 1074 | + } |
| 1075 | +} |
| 1076 | +
|
| 1077 | +# allow http from proxy subnet to backends |
| 1078 | +resource "google_compute_firewall" "fw-gilb-to-backends" { |
| 1079 | + name = "tf-test-l7-gilb-fw-allow-ilb-to-backends%{random_suffix}" |
| 1080 | + direction = "INGRESS" |
| 1081 | + network = google_compute_network.gilb_network.id |
| 1082 | + source_ranges = ["10.0.0.0/24"] |
| 1083 | + target_tags = ["http-server"] |
| 1084 | + allow { |
| 1085 | + protocol = "tcp" |
| 1086 | + ports = ["80", "443", "8080"] |
| 1087 | + } |
| 1088 | +} |
| 1089 | +
|
| 1090 | +resource "google_network_services_lb_route_extension" "default" { |
| 1091 | + name = "tf-test-l7-ilb-route-ext%{random_suffix}" |
| 1092 | + description = "my route extension" |
| 1093 | + location = "global" |
| 1094 | + load_balancing_scheme = "INTERNAL_MANAGED" |
| 1095 | + forwarding_rules = [google_compute_global_forwarding_rule.default.self_link] |
| 1096 | +
|
| 1097 | + extension_chains { |
| 1098 | + name = "chain1" |
| 1099 | +
|
| 1100 | + match_condition { |
| 1101 | + cel_expression = "request.path.startsWith('/extensions')" |
| 1102 | + } |
| 1103 | +
|
| 1104 | + extensions { |
| 1105 | + name = "ext11" |
| 1106 | + service = google_network_services_wasm_plugin.wasm_plugin.id |
| 1107 | + fail_open = false |
| 1108 | +
|
| 1109 | + forward_headers = ["custom-header"] |
| 1110 | + } |
| 1111 | + } |
| 1112 | +
|
| 1113 | + labels = { |
| 1114 | + foo = "bar" |
| 1115 | + } |
| 1116 | +} |
| 1117 | +
|
| 1118 | +resource "google_network_services_wasm_plugin" "wasm_plugin" { |
| 1119 | + name = "tf-test-my-wasm-plugin%{random_suffix}" |
| 1120 | + description = "my wasm plugin" |
| 1121 | +
|
| 1122 | + main_version_id = "v1" |
| 1123 | +
|
| 1124 | + labels = { |
| 1125 | + test_label = "test_value" |
| 1126 | + } |
| 1127 | + log_config { |
| 1128 | + enable = true |
| 1129 | + sample_rate = 1 |
| 1130 | + min_log_level = "WARN" |
| 1131 | + } |
| 1132 | +
|
| 1133 | + versions { |
| 1134 | + version_name = "v1" |
| 1135 | + description = "v1 version of my wasm plugin" |
| 1136 | + image_uri = "projects/%{test_project_id}/locations/us-central1/repositories/tf-test-repository-standard%{random_suffix}/genericArtifacts/my-wasm-plugin:v1" |
| 1137 | +
|
| 1138 | + labels = { |
| 1139 | + test_label = "test_value" |
| 1140 | + } |
| 1141 | + } |
| 1142 | +} |
| 1143 | +`, context)) |
| 1144 | +} |
0 commit comments