hashicorp / terraform-provider-google

Terraform Provider for Google Cloud Platform
https://registry.terraform.io/providers/hashicorp/google/latest/docs
Mozilla Public License 2.0
2.32k stars 1.73k forks source link

404 on creating gateway configs (OpenAPI) #16533

Closed assertnotnull closed 11 months ago

assertnotnull commented 11 months ago

Community Note

Terraform Version

v1.6.3

Affected Resource(s)

google_api_gateway_api_config

Terraform Configuration Files

locals {
  project_name = "replaceme"
  services = [
    "servicemanagement.googleapis.com",
    "servicecontrol.googleapis.com",
    "apigateway.googleapis.com",
  ]
  function_bucket = "mybucket"
  location        = "northamerica-northeast1"
  apispec = templatefile("${path.module}/../gateway-forwarder/src/api/api.yaml", {    
    syncTenantUrl = "https://${local.location}-${local.project_name}.cloudfunctions.net/forward-gateway-event-to-topic"
  })
}

provider "google" {
  project = local.project_name
  region  = "us-east1"
}

provider "google-beta" {
  project = local.project_name
  region  = "us-east1"
}

resource "google_api_gateway_api" "gateway" {
  provider = google-beta
  api_id   = "testgateway"
}

resource "google_service_account" "gateway" {
  account_id   = "test-api-gateway"
  display_name = "gateway test account"
  description  = "test account for api gateway"
}

resource "google_project_iam_member" "gatway_perms" {
  project = local.project_name
  role    = "roles/run.invoker"
  member  = "serviceAccount:${google_service_account.gateway.email}"
}

resource "google_project_service" "enable_services" {
  count   = length(local.services)
  service = local.services[count.index]
}

resource "google_api_gateway_api_config" "api_config" {
  provider      = google-beta
  api           = google_api_gateway_api.gateway.id
  api_config_id = "testconfig"
  openapi_documents {
    document {
      path     = "api.yaml"
      contents = base64encode(local.apispec)
    }
  }
  lifecycle {
    create_before_destroy = true
  }
}

resource "google_api_gateway_gateway" "forwarder" {
  provider   = google-beta
  gateway_id = google_api_gateway_api.gateway.api_id
  api_config = google_api_gateway_api_config.api_config.id
}

resource "google_pubsub_topic" "update_resident" {
  name = "update-resident"
}

resource "google_pubsub_topic" "update_project" {
  name = "update-project"
}

resource "google_storage_bucket_object" "forwarder" {
  bucket = local.function_bucket
  source = "${path.module}/../gateway-forwarder/forwarder.zip"
  name   = "forwarder.zip"
}

resource "google_cloudfunctions2_function" "forwarder" {
  name     = "forward-gateway-event-to-topic"
  location = local.location
  build_config {
    runtime     = "nodejs20"
    entry_point = "handleRequest"
    source {
      storage_source {
        bucket = local.function_bucket
        object = "forwarder.zip"
      }
    }
  }

  service_config {
    all_traffic_on_latest_revision   = true
    available_cpu                    = "0.1666"
    available_memory                 = "256M"
    ingress_settings                 = "ALLOW_ALL"
    max_instance_count               = 100
    max_instance_request_concurrency = 1
    min_instance_count               = 0
    timeout_seconds                  = 60
  }
}

Debug Output

https://gist.github.com/assertnotnull/f10ac82312ac62893ac3e33996c8ea9e

Expected Behavior

Create the configs

Actual Behavior

An 404 error:

β”‚ Error: Error creating ApiConfig: googleapi: got HTTP response code 404 with body: <!DOCTYPE html>
β”‚ <html lang=en>
β”‚   <meta charset=utf-8>
β”‚   <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
β”‚   <title>Error 404 (Not Found)!!1</title>
β”‚   <style>
β”‚     *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
β”‚   </style>
β”‚   <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
β”‚   <p><b>404.</b> <ins>That’s an error.</ins>
β”‚   <p>The requested URL <code>/v1beta/projects/projectId/locations/global/apis/projects/projectId/locations/global/apis/testgateway/configs?alt=json&amp;apiConfigId=testconfig</code> was not found on this server.  <ins>That’s all we know.</ins>
β”‚ 
β”‚ 
β”‚   with google_api_gateway_api_config.api_config,
β”‚   on main.tf line 50, in resource "google_api_gateway_api_config" "api_config":
β”‚   50: resource "google_api_gateway_api_config" "api_config" {

Steps to Reproduce

  1. terraform apply

Notes

The URL seems wrong when looking at the doc https://cloud.google.com/api-gateway/docs/reference/rest/v1beta/projects.locations.apis.configs/create

edwardmedia commented 11 months ago

@assertnotnull what do you see if you provide api_id instead?

resource "google_api_gateway_api_config" "api_config" {
  provider      = google-beta
  api           = google_api_gateway_api.gateway.api_id <------- replace id with api_id?
  api_config_id = "testconfig"
  openapi_documents {
    document {
      path     = "api.yaml"
      contents = base64encode(local.apispec)
    }
  }
  lifecycle {
    create_before_destroy = true
  }
}
assertnotnull commented 11 months ago

It works thanks a lot!

github-actions[bot] commented 10 months ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.