terraform-provider-restapi icon indicating copy to clipboard operation
terraform-provider-restapi copied to clipboard

[Feature Request] Allow to config headers for read method

Open JeffFang opened this issue 3 years ago • 1 comments
trafficstars

Assumption When use restapi_object resource to interact with REST APIs: Step1: when terraform creates the resource, a POST call(by default) will be made to the uri + path configured. Step2: when terraform tries to refresh the state for the resource, a GET call(by default) will be made to uri + path + {id} to get the resource.

Use case I use a restapi_object resource to create a token(step 1), the POST returns a token, but when tries to read the object back in step2, the API server expect the token to be in header rather than in path because it's long and for security or standard reasons.

Sample Code

provider "restapi" {
  uri                  = "https://xxx.com/tokens"
  write_returns_object = true
  debug                = true

  headers = {
    content-type = "application/json"
  }
}

resource "restapi_object" "token" {
  path         = ""
  id_attribute = "access/token/id"

  data = jsonencode({
    "auth" : {
      "passwordCredentials" : {
        "password" : "${var.password}",
        "username" : "${var.username}",
      }
    }
  })
}

Feature request Like allowing to config read/create/update method per resource, the read headers can be configured in a similar way and allows the id placeholder. Take my code as an example:

resource "restapi_object" "token" {
  path         = ""
  id_attribute = "access/token/id"

  data = jsonencode({
    "auth" : {
      "passwordCredentials" : {
        "password" : "${var.password}",
        "username" : "${var.username}",
      }
    }
  })

  # expected
  read_headers = {
    content-type = "application/json"
    "X-Auth-Token": "{id}"
  }
}

JeffFang avatar Aug 05 '22 22:08 JeffFang

Hello @JeffFang

If this is still actual you can check my workaround for this in #213

RedStalker avatar May 11 '23 11:05 RedStalker