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

Cannot create multiple versioned APIs with Swagger #4985

Closed
sam-cogan opened this issue Nov 26, 2019 · 3 comments · Fixed by #4993
Closed

Cannot create multiple versioned APIs with Swagger #4985

sam-cogan opened this issue Nov 26, 2019 · 3 comments · Fixed by #4993

Comments

@sam-cogan
Copy link
Contributor

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Terraform (and AzureRM Provider) Version

Terraform: v0.12.13
Azure RM provider : v1.37.0

Affected Resource(s)

  • azurerm_api_management_api

Terraform Configuration Files

resource "azurerm_api_management_api_version_set" "client-api-versionset" {
  count               = length(var.client_services)
  name                = "${var.client_short_name}-${var.environment_name}-${var.client_services[count.index]}"
  resource_group_name = var.apim_resource_group_name
  api_management_name = "${data.azurerm_api_management.apim.name}"
  display_name        = "${var.client_short_name}-${var.environment_name}-${var.client_services[count.index]}"
  versioning_scheme   = "Segment"
}


resource "azurerm_api_management_api" "client-api-v1" {
  count               = length(var.client_services)
  name                = "${var.client_short_name}-${var.environment_name}-${var.client_services[count.index]}-v1"
  resource_group_name = var.apim_resource_group_name
  api_management_name = data.azurerm_api_management.apim.name
  revision            = var.api_version
  display_name        = "${var.client_short_name}-${var.environment_name}-${var.client_services[count.index]}-v1"
  path                = "${var.client_short_name}/${var.environment_name}/${var.client_services[count.index]}"
  protocols           = ["https"]
  service_url         = "https://${var.client_short_name}-${var.environment_name}.client.contoso.com/${var.client_services[count.index]}/dpo"
  version             = "v1"
  version_set_id      = azurerm_api_management_api_version_set.client-api-versionset[count.index].id
  import {
    content_format = "swagger-json"
    content_value  = templatefile("${path.module}/open-api-definition/client-v1-swagger.tmpl", { api_name = "${var.client_short_name}-${var.environment_name}-${var.client_services[count.index]}-v1" })

  }
}


resource "azurerm_api_management_api" "client-api-v2" {
  count               = length(var.client_services)
  name                = "${var.client_short_name}-${var.environment_name}-${var.client_services[count.index]}-v2"
  resource_group_name = var.apim_resource_group_name
  api_management_name = "${data.azurerm_api_management.apim.name}"
  revision            = var.api_version
  display_name        = "${var.client_short_name}-${var.environment_name}-${var.client_services[count.index]}-v2"
  path                = "${var.client_short_name}/${var.environment_name}/${var.client_services[count.index]}"
  protocols           = ["https"]
  service_url         = "https://${var.client_short_name}-${var.environment_name}.client.contoso.com/${var.client_services[count.index]}/dpo"
  version             = "v2"
  version_set_id      = azurerm_api_management_api_version_set.client-api-versionset[count.index].id
  import {
    content_format = "swagger-json"
    content_value  = templatefile("${path.module}/open-api-definition/client-v2-swagger.tmpl", { api_name = "${var.client_short_name}-${var.environment_name}-${var.client_services[count.index]}-v2" })

  }
}

Debug Output

Panic Output

Expected Behavior

An API with two versions should be created

Actual Behavior

The version set and first API (v1) is created fine, the second version (v2) fails with this error:

Error: Error creating/updating API Management API "sct001-test-dpo2-v2" (Resource Group "CLIENT"): apimanagement.APIClient#CreateOrUpdate: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="ValidationError" Message="One or more fields contain incorrect values:" Details=[{"code":"ValidationError","message":"Cannot create API 'sct001-test-dpo2-v2' with the same Path 'sct001/test/dpo2' as API 'sct001-test-dpo2-v1'  unless it's a part of the same version set","target":"path"}]

If I create the APIs without importing a swagger file then it works as expected and I get two empty versioned APIs

Steps to Reproduce

  1. Create two swagger files for versions of the API
  2. Create a version set
  3. Create two apis, using the version set and swagger files
  4. Terraform apply

Important Factoids

References

  • #0000
@sam-cogan
Copy link
Contributor Author

This is related to the recent update of the APIM resource to add versions in #4592

@sam-cogan sam-cogan changed the title Cannot created versioned APIs with Swagger Cannot create multiple versioned APIs with Swagger Nov 26, 2019
@sam-cogan
Copy link
Contributor Author

Having looked at the code I think there problem is down to the way API's are being created when importing the content from a file. It looks like the provider first creates the API using the swagger file, with no version ID attached, and then applies the version ID and version_set to it. This works fine for the first API in a version set, but when you try and create a second using the same path it fails, because it is first creating an un-versioned API with the intent of adding the version after, which is not allowed as the first version already exists.

For this to work properly the version needs to be applied at the time of creation.

@ghost
Copy link

ghost commented Mar 29, 2020

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 feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 hashibot-feedback@hashicorp.com. Thanks!

@ghost ghost locked and limited conversation to collaborators Mar 29, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants