diff --git a/mmv1/third_party/terraform/data_sources/data_source_google_compute_router_status.go.erb b/mmv1/third_party/terraform/data_sources/data_source_google_compute_router_status.go.erb new file mode 100644 index 000000000000..13a638ffcfda --- /dev/null +++ b/mmv1/third_party/terraform/data_sources/data_source_google_compute_router_status.go.erb @@ -0,0 +1,134 @@ +<% autogen_exception -%> +package google + +import ( + "fmt" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +<% if version == "ga" -%> + "google.golang.org/api/compute/v1" +<% else -%> + "google.golang.org/api/compute/v0.beta" +<% end -%> +) + +func dataSourceGoogleComputeRouterStatus() *schema.Resource { + routeElemSchema := datasourceSchemaFromResourceSchema(resourceComputeRoute().Schema) + + return &schema.Resource{ + Read: dataSourceComputeRouterStatusRead, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Description: "Name of the router to query.", + Required: true, + Computed: false, + }, + "project": { + Type: schema.TypeString, + Description: "Project ID of the target router.", + Optional: true, + Computed: false, + }, + "region": { + Type: schema.TypeString, + Description: "Region of the target router.", + Optional: true, + Computed: true, + }, + "network": { + Type: schema.TypeString, + Description: "URI of the network to which this router belongs.", + Computed: true, + }, + "best_routes": { + Type: schema.TypeList, + Description: "Best routes for this router's network.", + Elem: &schema.Resource{ + Schema: routeElemSchema, + }, + Computed: true, + }, + "best_routes_for_router": { + Type: schema.TypeList, + Description: "Best routes learned by this router.", + Elem: &schema.Resource{ + Schema: routeElemSchema, + }, + Computed: true, + }, + }, + } +} + +func dataSourceComputeRouterStatusRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + userAgent, err := generateUserAgentString(d, config.userAgent) + if err != nil { + return err + } + + project, err := getProject(d, config) + if err != nil { + return err + } + + region, err := getRegion(d, config) + if err != nil { + return err + } + + var name string + if n, ok := d.GetOk("name"); ok { + name = n.(string) + } + + resp, err := config.NewComputeClient(userAgent).Routers.GetRouterStatus(project, region, name).Do() + if err != nil { + return err + } + + status := resp.Result + + if err := d.Set("network", status.Network); err != nil { + return fmt.Errorf("Error setting network: %s", err) + } + + if err := d.Set("best_routes", flattenRoutes(status.BestRoutes)); err != nil { + return fmt.Errorf("Error setting best_routes: %s", err) + } + + if err := d.Set("best_routes_for_router", flattenRoutes(status.BestRoutesForRouter)); err != nil { + return fmt.Errorf("Error setting best_routes_for_router: %s", err) + } + + id, err := replaceVars(d, config, "projects/{{project}}/regions/{{region}}/routers/{{name}}") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + return nil +} + +func flattenRoutes(routes []*compute.Route) []map[string]interface{} { + results := make([]map[string]interface{}, len(routes)) + + for i, route := range routes { + results[i] = map[string]interface{}{ + "dest_range": route.DestRange, + "name": route.Name, + "network": route.Network, + "description": route.Description, + "next_hop_gateway": route.NextHopGateway, + "next_hop_ilb": route.NextHopIlb, + "next_hop_ip": route.NextHopIp, + "next_hop_vpn_tunnel": route.NextHopVpnTunnel, + "priority": route.Priority, + "tags": route.Tags, + "next_hop_network": route.NextHopNetwork, + } + } + + return results +} diff --git a/mmv1/third_party/terraform/tests/data_source_google_compute_router_status_test.go b/mmv1/third_party/terraform/tests/data_source_google_compute_router_status_test.go new file mode 100644 index 000000000000..5dd0a2ae9cc1 --- /dev/null +++ b/mmv1/third_party/terraform/tests/data_source_google_compute_router_status_test.go @@ -0,0 +1,172 @@ +package google + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccDataSourceComputeRouterStatus(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "suffix": randString(t, 10), + "region": "us-central1", + } + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + ExternalProviders: map[string]resource.ExternalProvider{ + "time": {}, + }, + CheckDestroy: testAccCheckComputeRouterDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccDataSourceComputeRouterStatusConfig(context), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair("data.google_compute_router_status.router1", "name", "google_compute_router.router1", "name"), + resource.TestCheckResourceAttrPair("data.google_compute_router_status.router1", "region", "google_compute_router.router1", "region"), + resource.TestCheckResourceAttrSet("data.google_compute_router_status.router1", "network"), + resource.TestCheckResourceAttr("data.google_compute_router_status.router1", "best_routes.#", "2"), + resource.TestCheckResourceAttr("data.google_compute_router_status.router1", "best_routes_for_router.#", "2"), + resource.TestCheckResourceAttrPair("data.google_compute_router_status.router1", "best_routes.0.next_hop_ip", "google_compute_router_peer.router1_peer1", "peer_ip_address"), + resource.TestCheckResourceAttrSet("data.google_compute_router_status.router1", "best_routes.0.next_hop_vpn_tunnel"), + ), + }, + }, + }) +} + +func testAccDataSourceComputeRouterStatusConfig(context map[string]interface{}) string { + return Nprintf(` +resource "google_compute_network" "network1" { + name = "network1-%{suffix}" + routing_mode = "GLOBAL" + auto_create_subnetworks = false +} + +resource "google_compute_network" "network2" { + name = "network2-%{suffix}" + routing_mode = "GLOBAL" + auto_create_subnetworks = false +} + +resource "google_compute_subnetwork" "network2_subnet1" { + name = "ha-vpn-subnet-1-%{suffix}" + ip_cidr_range = "192.168.1.0/24" + region = "%{region}" + network = google_compute_network.network2.id +} + +resource "google_compute_subnetwork" "network2_subnet2" { + name = "ha-vpn-subnet-2-%{suffix}" + ip_cidr_range = "192.168.2.0/24" + region = "us-east1" + network = google_compute_network.network2.id +} + +resource "google_compute_router" "router1" { + name = "tf-test-ha-vpn-router1-%{suffix}" + network = google_compute_network.network1.name + region = "%{region}" + bgp { + asn = 64514 + } +} + +resource "google_compute_router" "router2" { + name = "tf-test-ha-vpn-router2-%{suffix}" + network = google_compute_network.network2.name + region = "%{region}" + bgp { + asn = 64515 + } +} + +resource "google_compute_ha_vpn_gateway" "ha_gateway1" { + region = "%{region}" + name = "tf-test-ha-vpn-1-%{suffix}" + network = google_compute_network.network1.id +} + +resource "google_compute_ha_vpn_gateway" "ha_gateway2" { + region = "%{region}" + name = "tf-test-ha-vpn-2-%{suffix}" + network = google_compute_network.network2.id +} + +resource "google_compute_vpn_tunnel" "tunnel1" { + name = "ha-vpn-tunnel1-%{suffix}" + region = "%{region}" + vpn_gateway = google_compute_ha_vpn_gateway.ha_gateway1.id + peer_gcp_gateway = google_compute_ha_vpn_gateway.ha_gateway2.id + shared_secret = "a secret message" + router = google_compute_router.router1.id + vpn_gateway_interface = 0 +} + +resource "google_compute_vpn_tunnel" "tunnel2" { + name = "ha-vpn-tunnel2-%{suffix}" + region = "%{region}" + vpn_gateway = google_compute_ha_vpn_gateway.ha_gateway2.id + peer_gcp_gateway = google_compute_ha_vpn_gateway.ha_gateway1.id + shared_secret = "a secret message" + router = google_compute_router.router2.id + vpn_gateway_interface = 0 +} + +resource "google_compute_router_interface" "router1_interface1" { + name = "router1-interface1-%{suffix}" + router = google_compute_router.router1.name + region = "%{region}" + ip_range = "169.254.0.1/30" + vpn_tunnel = google_compute_vpn_tunnel.tunnel1.name +} + +resource "google_compute_router_peer" "router1_peer1" { + name = "router1-peer1-%{suffix}" + router = google_compute_router.router1.name + region = "%{region}" + peer_ip_address = "169.254.0.2" + peer_asn = 64515 + advertised_route_priority = 100 + interface = google_compute_router_interface.router1_interface1.name +} + +resource "google_compute_router_interface" "router2_interface1" { + name = "router2-interface1-%{suffix}" + router = google_compute_router.router2.name + region = "%{region}" + ip_range = "169.254.0.2/30" + vpn_tunnel = google_compute_vpn_tunnel.tunnel2.name +} + +resource "google_compute_router_peer" "router2_peer1" { + name = "router2-peer1-%{suffix}" + router = google_compute_router.router2.name + region = "%{region}" + peer_ip_address = "169.254.0.1" + peer_asn = 64514 + advertised_route_priority = 100 + interface = google_compute_router_interface.router2_interface1.name +} + +resource "time_sleep" "wait_60_seconds" { + create_duration = "60s" + + depends_on = [ + google_compute_router_peer.router1_peer1, + google_compute_router_peer.router2_peer1, + ] +} + +data "google_compute_router_status" "router1" { + name = google_compute_router.router1.name + region = google_compute_router.router1.region + + depends_on = [time_sleep.wait_60_seconds] +} +`, context) + +} diff --git a/mmv1/third_party/terraform/utils/provider.go.erb b/mmv1/third_party/terraform/utils/provider.go.erb index a012c93c9bcc..5e670f49a247 100644 --- a/mmv1/third_party/terraform/utils/provider.go.erb +++ b/mmv1/third_party/terraform/utils/provider.go.erb @@ -238,6 +238,7 @@ func Provider() *schema.Provider { "google_compute_region_ssl_certificate": dataSourceGoogleRegionComputeSslCertificate(), "google_compute_resource_policy": dataSourceGoogleComputeResourcePolicy(), "google_compute_router": dataSourceGoogleComputeRouter(), + "google_compute_router_status": dataSourceGoogleComputeRouterStatus(), "google_compute_ssl_certificate": dataSourceGoogleComputeSslCertificate(), "google_compute_ssl_policy": dataSourceGoogleComputeSslPolicy(), "google_compute_subnetwork": dataSourceGoogleComputeSubnetwork(), diff --git a/mmv1/third_party/terraform/website/docs/d/compute_router_status.html.markdown b/mmv1/third_party/terraform/website/docs/d/compute_router_status.html.markdown new file mode 100644 index 000000000000..f948abf1c9b0 --- /dev/null +++ b/mmv1/third_party/terraform/website/docs/d/compute_router_status.html.markdown @@ -0,0 +1,49 @@ +--- +layout: "google" +subcategory: "Compute Engine" +page_title: "Google: google_compute_router" +sidebar_current: "docs-google-datasource-compute-router-status" +description: |- + Get a Cloud Router's Status. +--- + +# google\_compute\_router\_status + +Get a Cloud Router's status within GCE from its name and region. This data source exposes the +routes learned by a Cloud Router via BGP peers. + +For more information see [the official documentation](https://cloud.google.com/network-connectivity/docs/router/how-to/viewing-router-details) +and +[API](https://cloud.google.com/compute/docs/reference/rest/v1/routers/getRouterStatus). + +## Example Usage + +```hcl +data "google_compute_router_status" "my-router" { + name = "myrouter" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) The name of the router. + +* `project` - (Optional) The ID of the project in which the resource + belongs. If it is not provided, the provider project is used. + +* `region` - (Optional) The region this router has been created in. If + unspecified, this defaults to the region configured in the provider. + + +## Attributes Reference + +In addition to the arguments listed above, the following attributes are exported: + +* `network` - The network name or resource link to the parent + network of this subnetwork. + +* `best_routes` - List of best `compute#routes` configurations for this router's network. See [google_compute_route](https://www.terraform.io/docs/providers/google/r/compute_route.html) resource for available attributes. + +* `best_routes_for_router` - List of best `compute#routes` for this specific router. See [google_compute_route](https://www.terraform.io/docs/providers/google/r/compute_route.html) resource for available attributes.