hashicorp / terraform-provider-azurerm

Terraform provider for Azure Resource Manager
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Mozilla Public License 2.0
4.59k stars 4.62k forks source link

Representation examples not working for Azure API operation requests or responses #15810

Closed matt-zappitello closed 2 years ago

matt-zappitello commented 2 years ago

Community Note

Terraform (and AzureRM Provider) Version

Terraform v1.1.7 on windows_amd64

Affected Resource(s)

Terraform Configuration Files

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 2.98.0"
    }
  }

  required_version = ">= 1.1.7"
}

provider "azurerm" {
  features {
    api_management {
      purge_soft_delete_on_destroy = true
    }
  }
}

resource "azurerm_resource_group" "matt_test" {
  name     = "matt_test"
  location = "westus2"
}

resource "azurerm_api_management" "matts-apim" {
  name                = "matts-apim"
  location            = azurerm_resource_group.matt_test.location
  resource_group_name = azurerm_resource_group.matt_test.name
  publisher_name      = "My Publisher"
  publisher_email     = "my.email@email.com"
  sku_name            = "Consumption_0"
}

resource "azurerm_api_management_api" "carto-replacement-api" {
  name                = "carto-replacement-api"
  resource_group_name = azurerm_resource_group.matt_test.name
  api_management_name = azurerm_api_management.matts-apim.name
  revision            = "1"
  display_name        = "CARTO Replacement"
  path                = "carto"
  protocols           = ["https"]
}

resource "azurerm_api_management_api_operation" "bbox" {
  operation_id        = "bbox"
  api_name            = azurerm_api_management_api.carto-replacement-api.name
  api_management_name = azurerm_api_management_api.carto-replacement-api.api_management_name
  resource_group_name = azurerm_api_management_api.carto-replacement-api.resource_group_name
  display_name        = "Bounding Box"
  method              = "POST"
  url_template        = "/api/v1/data/bbox"
  description         = "Returns datasets from a box specified by opposite corners"

  request {
    description = ""

    header {
      name     = "Content-Type"
      required = "false"
      type     = "string"
      values   = ["application/json"]
    }

    header {
      name     = "Authorization"
      required = "true"
      type     = "string"
    }

    representation {
      content_type = "application/json"
      example {
        name  = "response example"
        value = "{\"coordinates\":[[-121.2,42.2],[-122.4,43.4]],\"datasets\":[\"10m_terrain\"]}"
      }
    }
  }

  response {
    status_code = 200
  }

  response {
    status_code = 201

    representation {
      content_type = "application/json"
      example {
        name  = "Response example"
        value = <<JSON
                {"job_ids":["0bd62539-7975-4872-ba8c-75e756296d76"]}
                JSON
      }
    }
  }
}

resource "azurerm_api_management_api_operation_policy" "bbox_inbound_policy" {
  api_name            = azurerm_api_management_api_operation.bbox.api_name
  api_management_name = azurerm_api_management_api_operation.bbox.api_management_name
  resource_group_name = azurerm_api_management_api_operation.bbox.resource_group_name
  operation_id        = azurerm_api_management_api_operation.bbox.operation_id

  xml_content = <<XML
<policies>
    <inbound>
        <base />
        <mock-response status-code="201" content-type="application/json" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies> 
    XML
}

Expected Behaviour

An API with a "Bounding Box" operation should have been created. The Request should have one representation added with a Sample including the JSON as specified in the above terraform. The 201 Response should also have a representation added with the Sample field populated.

Actual Behaviour

The API is created and both the Request and 201 Response have a representation added; however the Sample field for both representations is empty.

Steps to Reproduce

  1. terraform apply

Important Factoids

I can do this exact thing manually so I suspect there is either a bug in the representation.example code being applied or I specified it incorrectly, in which case the documentation is incomplete and the examples insufficient for me to follow.

matt-zappitello commented 2 years ago

I'm not sure if this helps, but when I manually added the samples in the portal, then applied my Terraform config again, it gave me the following output:

terraform apply azurerm_resource_group.matt_test: Refreshing state... [id=/subscriptions/20c74213-6aef-436f-bd6f-0399983b5594/resourceGroups/matt_test] azurerm_api_management.matts-apim: Refreshing state... [id=/subscriptions/20c74213-6aef-436f-bd6f-0399983b5594/resourceGroups/matt_test/providers/Microsoft.ApiManagement/service/matts-apim] azurerm_api_management_api.carto-replacement-api: Refreshing state... [id=/subscriptions/20c74213-6aef-436f-bd6f-0399983b5594/resourceGroups/matt_test/providers/Microsoft.ApiManagement/service/matts-apim/apis/carto-replacement-api] azurerm_api_management_api_operation.bbox: Refreshing state... [id=/subscriptions/20c74213-6aef-436f-bd6f-0399983b5594/resourceGroups/matt_test/providers/Microsoft.ApiManagement/service/matts-apim/apis/carto-replacement-api/operations/bbox] β•· β”‚ Error: Plugin did not respond β”‚ β”‚ with azurerm_api_management_api_operation.bbox, β”‚ on main.tf line 56, in resource "azurerm_api_management_api_operation" "bbox": β”‚ 56: resource "azurerm_api_management_api_operation" "bbox" { β”‚ β”‚ The plugin encountered an error, and failed to respond to the plugin.(*GRPCProvider).ReadResource call. The plugin β”‚ logs may contain more details. β•΅

Stack trace from the terraform-provider-azurerm_v2.98.0_x5.exe plugin:

panic: interface conversion: interface {} is map[string]interface {}, not string

goroutine 145 [running]: github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/schemaz.FlattenApiManagementOperationParameterExampleContract(0xc001c5b590) /opt/teamcity-agent/work/5d79fe75d4460a2f/src/github.com/terraform-providers/terraform-provider-azurerm/internal/services/apimanagement/schemaz/api_management.go:426 +0x3fc github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/schemaz.FlattenApiManagementOperationRepresentation(0xc0021a6210) /opt/teamcity-agent/work/5d79fe75d4460a2f/src/github.com/terraform-providers/terraform-provider-azurerm/internal/services/apimanagement/schemaz/api_management.go:200 +0x1db github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement.flattenApiManagementOperationResponseContract(0xc0021a6180) /opt/teamcity-agent/work/5d79fe75d4460a2f/src/github.com/terraform-providers/terraform-provider-azurerm/internal/services/apimanagement/api_management_api_operation_resource.go:359 +0x245 github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement.resourceApiManagementApiOperationRead(0xc001c4c300, {0x57563c0, 0xc000f16700}) /opt/teamcity-agent/work/5d79fe75d4460a2f/src/github.com/terraform-providers/terraform-provider-azurerm/internal/services/apimanagement/api_management_api_operation_resource.go:213 +0x6b6 github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(Resource).read(0x60521fc, {0x6783898, 0xc001fdee40}, 0x24, {0x57563c0, 0xc000f16700}) /opt/teamcity-agent/work/5d79fe75d4460a2f/src/github.com/terraform-providers/terraform-provider-azurerm/vendor/github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema/resource.go:346 +0x178 github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(Resource).RefreshWithoutUpgrade(0xc000008d20, {0x6783898, 0xc001fdee40}, 0xc000eef1e0, {0x57563c0, 0xc000f16700}) /opt/teamcity-agent/work/5d79fe75d4460a2f/src/github.com/terraform-providers/terraform-provider-azurerm/vendor/github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema/resource.go:635 +0x35b github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(GRPCProviderServer).ReadResource(0xc0015025e8, {0x6783898, 0xc001fdee40}, 0xc001fdeec0) /opt/teamcity-agent/work/5d79fe75d4460a2f/src/github.com/terraform-providers/terraform-provider-azurerm/vendor/github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema/grpc_provider.go:576 +0x534 github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server.(server).ReadResource(0xc000310f00, {0x6783940, 0xc002f8db00}, 0xc00257d3e0) /opt/teamcity-agent/work/5d79fe75d4460a2f/src/github.com/terraform-providers/terraform-provider-azurerm/vendor/github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server/server.go:553 +0x3b0 github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5._Provider_ReadResource_Handler({0x5cab3a0, 0xc000310f00}, {0x6783940, 0xc002f8db00}, 0xc00257d380, 0x0) /opt/teamcity-agent/work/5d79fe75d4460a2f/src/github.com/terraform-providers/terraform-provider-azurerm/vendor/github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5/tfplugin5_grpc.pb.go:344 +0x170 google.golang.org/grpc.(Server).processUnaryRPC(0xc000328fc0, {0x67d4508, 0xc00066c000}, 0xc002faf0e0, 0xc001132f90, 0xae323d0, 0x0) /opt/teamcity-agent/work/5d79fe75d4460a2f/src/github.com/terraform-providers/terraform-provider-azurerm/vendor/google.golang.org/grpc/server.go:1292 +0xc6f google.golang.org/grpc.(Server).handleStream(0xc000328fc0, {0x67d4508, 0xc00066c000}, 0xc002faf0e0, 0x0) /opt/teamcity-agent/work/5d79fe75d4460a2f/src/github.com/terraform-providers/terraform-provider-azurerm/vendor/google.golang.org/grpc/server.go:1617 +0xa2a google.golang.org/grpc.(Server).serveStreams.func1.2() /opt/teamcity-agent/work/5d79fe75d4460a2f/src/github.com/terraform-providers/terraform-provider-azurerm/vendor/google.golang.org/grpc/server.go:940 +0x98 created by google.golang.org/grpc.(Server).serveStreams.func1 /opt/teamcity-agent/work/5d79fe75d4460a2f/src/github.com/terraform-providers/terraform-provider-azurerm/vendor/google.golang.org/grpc/server.go:938 +0x294

Error: The terraform-provider-azurerm_v2.98.0_x5.exe plugin crashed!

This is always indicative of a bug within the plugin. It would be immensely helpful if you could report the crash with the plugin's maintainers so that it can be fixed. The output above should help diagnose the issue.

mbfrahry commented 2 years ago

Hey @matt-zappitello. It looks like this was fixed and merged in v3.1.0 of the provider with #14848. Feel free to reopen this if you're still experiencing this issue when upgrading

github-actions[bot] commented 2 years 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.