mulesoft-anypoint / terraform-provider-anypoint

The Anypoint Platform Terraform Provider
https://registry.terraform.io/providers/mulesoft-anypoint/anypoint/latest
MIT License
12 stars 6 forks source link

API Manager Instance Data object malformed api instance id #37

Closed mulesoft-mike closed 9 months ago

mulesoft-mike commented 9 months ago

Problem

The api instance id is malformed when using the data anypoint_apim_instance which means you cannot programatically manage policies for API Instances that already exist.

Symptoms

When you attempt to manage an already existing API Manager Instance the id value is malformed.

resource "anypoint_apim_policy_client_id_enforcement" "policy01" {
  depends_on = [
    data.anypoint_apim_instance.uri-test
  ]

  org_id = local.org_id
  env_id = local.env_id
  apim_id = data.anypoint_apim_instance.uri-test.id
  disabled = false
  asset_version = "1.3.2"
  configuration_data {
    credentials_origin_has_http_basic_authentication_header = "customExpression"
    client_id_expression = "#[attributes.headers['client']]"
    client_secret_expression = "#[attributes.headers['test']]"
  }
}

data "anypoint_apim_instance" "uri-test" {
  id     = local.api_id
  org_id = local.org_id
  env_id = local.env_id
}

The output of the state file shows that the id is not correctly set:

{
  "version": 4,
  "terraform_version": "1.5.7",
  "serial": 10,
  "lineage": "8efdd37a-96e0-7241-8233-1782b3f03be0",
  "outputs": {},
  "resources": [
    {
      "mode": "data",
      "type": "anypoint_apim_instance",
      "name": "uri-test",
      "provider": "provider[\"registry.terraform.io/mulesoft-anypoint/anypoint\"]",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "asset_group_id": "xxxxx",
            "asset_id": "uri-test",
            "asset_version": "1.0.1",
            "audit": {
              "created": "2022-01-06T16:17:09.940Z"
            },
            "id": "�",

Investigation

The OAS specification and the API endpoint defines and returns an integer: https://github.com/salesforce/anypoint-automation-client-generator/blob/master/spec/apim.yml#L523 but the id is set as string here: https://github.com/salesforce/terraform-provider-anypoint/blob/dev/anypoint/data_source_apim_instance.go#L448

Should the api id always be an integer in the client to successfully set this value in the terraform state file?

soufi commented 9 months ago

Discussed. this has to do with the terraform import not being properly executed.