magodo / terraform-provider-restful

Terraform provider to manage RESTful resources
https://registry.terraform.io/providers/magodo/restful
Mozilla Public License 2.0
15 stars 5 forks source link

`restful_resource` resource & data source: Extend the `output_attrs` to support arrays #70

Closed magodo closed 11 months ago

magodo commented 11 months ago

Fix #68

Test

/tmp/restful via πŸ’  default
πŸ’€  cat main.tf
terraform {
  required_providers {
    restful = {
      source = "magodo/restful"
    }
  }
}

provider "restful" {
  base_url = "http://localhost:3000"
}

resource "restful_resource" "test" {
  path = "/posts"
  body = jsonencode({
    author = "magodo"
    title  = "foobar"
  })
  read_path = "$(path)/$(body.id)"
}

data "restful_resource" "all" {
  id         = "/posts"
  depends_on = [restful_resource.test]
}

data "restful_resource" "filter" {
  id = "/posts"
  output_attrs = [
    "#.id",
    "#.title",
  ]

  depends_on = [restful_resource.test]
}

/tmp/restful via πŸ’  default
πŸ’€  tf show
# data.restful_resource.all:
data "restful_resource" "all" {
    id     = "/posts"
    output = jsonencode(
        [
            {
                author = "typicode"
                id     = 1
                title  = "json-server"
            },
            {
                author = "magodo"
                id     = 2
                title  = "foobar"
            },
        ]
    )
}

# data.restful_resource.filter:
data "restful_resource" "filter" {
    id           = "/posts"
    output       = jsonencode(
        [
            {
                id    = 1
                title = "json-server"
            },
            {
                id    = 2
                title = "foobar"
            },
        ]
    )
    output_attrs = [
        "#.id",
        "#.title",
    ]
}

# restful_resource.test:
resource "restful_resource" "test" {
    body      = jsonencode(
        {
            author = "magodo"
            title  = "foobar"
        }
    )
    id        = "/posts/2"
    output    = jsonencode(
        {
            author = "magodo"
            id     = 2
            title  = "foobar"
        }
    )
    path      = "/posts"
    read_path = "$(path)/$(body.id)"
}