Skip to content

Commit

Permalink
Merge pull request #87 from magodo/dynamic
Browse files Browse the repository at this point in the history
resources - Change `body`, `delete_body`, `output`  from string tpye to dynamic type
  • Loading branch information
magodo authored May 22, 2024
2 parents 0c98b21 + bb3546c commit 773524f
Show file tree
Hide file tree
Showing 28 changed files with 2,202 additions and 308 deletions.
2 changes: 1 addition & 1 deletion docs/data-sources/resource.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ data "restful_resource" "test" {

### Read-Only

- `output` (String) The response body after reading the resource.
- `output` (Dynamic) The response body after reading the resource.

<a id="nestedatt--precheck"></a>
### Nested Schema for `precheck`
Expand Down
6 changes: 3 additions & 3 deletions docs/resources/operation.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ resource "restful_operation" "register_rp" {

### Optional

- `body` (String) The payload for the `Create`/`Update` call.
- `delete_body` (String) The payload for the `Delete` call.
- `body` (Dynamic) The payload for the `Create`/`Update` call.
- `delete_body` (Dynamic) The payload for the `Delete` call.
- `delete_method` (String) The method for the `Delete` call. Possible values are `POST`, `PUT`, `PATCH` and `DELETE`. If this is not specified, no `Delete` call will occur.
- `delete_path` (String) The path for the `Delete` call, relative to the `base_url` of the provider. The `path` is used instead if `delete_path` is absent.
- `header` (Map of String) The header parameters that are applied to each request. This overrides the `header` set in the provider block.
Expand All @@ -57,7 +57,7 @@ resource "restful_operation" "register_rp" {
### Read-Only

- `id` (String) The ID of the operation. Same as the `path`.
- `output` (String) The response body.
- `output` (Dynamic) The response body.

<a id="nestedatt--poll"></a>
### Nested Schema for `poll`
Expand Down
8 changes: 4 additions & 4 deletions docs/resources/resource.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ resource "restful_resource" "rg" {
pending = ["202", "200"]
}
}
body = jsonencode({
body = {
location = "westus"
tags = {
foo = "bar"
}
})
}
}
```

Expand All @@ -40,7 +40,7 @@ resource "restful_resource" "rg" {

### Required

- `body` (String) The properties of the resource.
- `body` (Dynamic) The properties of the resource.
- `path` (String) The path used to create the resource, relative to the `base_url` of the provider.

### Optional
Expand Down Expand Up @@ -74,7 +74,7 @@ resource "restful_resource" "rg" {
### Read-Only

- `id` (String) The ID of the Resource.
- `output` (String) The response body after reading the resource.
- `output` (Dynamic) The response body after reading the resource.

<a id="nestedatt--poll_create"></a>
### Nested Schema for `poll_create`
Expand Down
4 changes: 2 additions & 2 deletions examples/resources/restful_resource/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ resource "restful_resource" "rg" {
pending = ["202", "200"]
}
}
body = jsonencode({
body = {
location = "westus"
tags = {
foo = "bar"
}
})
}
}
16 changes: 8 additions & 8 deletions examples/usecases/azure/route_table/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ resource "restful_resource" "rg" {
pending = ["202", "200"]
}
}
body = jsonencode({
body = {
location = "westus"
tags = {
foo = "bar"
}
})
}
}

locals {
Expand All @@ -80,9 +80,9 @@ resource "restful_resource" "table" {
query = {
api-version = ["2022-07-01"]
}
body = jsonencode({
body = {
location = "westus"
})
}
poll_create = local.poll
poll_delete = local.poll
}
Expand All @@ -101,12 +101,12 @@ resource "restful_resource" "route1" {
poll_update = local.poll
poll_delete = local.poll

body = jsonencode({
body = {
properties = {
nextHopType = "VnetLocal"
addressPrefix = "10.1.0.0/16"
}
})
}
}

resource "restful_resource" "route2" {
Expand All @@ -123,10 +123,10 @@ resource "restful_resource" "route2" {
poll_update = local.poll
poll_delete = local.poll

body = jsonencode({
body = {
properties = {
nextHopType = "VnetLocal"
addressPrefix = "10.2.0.0/16"
}
})
}
}
8 changes: 4 additions & 4 deletions examples/usecases/azure/virtual_network/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ resource "restful_resource" "rg" {
pending = ["202", "200"]
}
}
body = jsonencode({
body = {
location = "westus"
tags = {
foo = "bar"
}
})
}
}

locals {
Expand All @@ -77,7 +77,7 @@ resource "restful_resource" "vnet" {
poll_create = local.vnet_poll
poll_update = local.vnet_poll
poll_delete = local.vnet_poll
body = jsonencode({
body = {
location = "westus"
properties = {
addressSpace = {
Expand All @@ -92,5 +92,5 @@ resource "restful_resource" "vnet" {
}
]
}
})
}
}
8 changes: 4 additions & 4 deletions examples/usecases/feedly/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ resource "restful_resource" "collection_go" {
update_method = "POST"
read_path = "$(path)/$(body.0.id)"
read_selector = "0"
body = jsonencode({
body = {
label = "Go"
})
}
}

resource "restful_resource" "feeds" {
Expand All @@ -47,7 +47,7 @@ resource "restful_resource" "feeds" {
create_selector = "#[feedId == \"${each.value}\"]"
read_path = "feeds/$(body.id)"
delete_path = "${restful_resource.collection_go.id}/feeds/$(body.id)"
body = jsonencode({
body = {
id = each.value
})
}
}
8 changes: 4 additions & 4 deletions examples/usecases/msgraph/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ provider "restful" {
resource "restful_resource" "group" {
path = "/groups"
read_path = "$(path)/$(body.id)"
body = jsonencode({
body = {
description = "Self help community for library"
displayName = "Library Assist"
groupTypes = [
Expand All @@ -45,21 +45,21 @@ resource "restful_resource" "group" {
mailEnabled = true
mailNickname = "library"
securityEnabled = false
})
}
}

resource "restful_resource" "user" {
path = "/users"
read_path = "$(path)/$(body.id)"
body = jsonencode({
body = {
accountEnabled = true
mailNickname = "AdeleV"
displayName = "J.Doe"
userPrincipalName = "[email protected]"
passwordProfile = {
password = "SecretP@sswd99!"
}
})
}
write_only_attrs = [
"mailNickname",
"accountEnabled",
Expand Down
12 changes: 6 additions & 6 deletions examples/usecases/spotify/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ data "restful_resource" "me" {
}

resource "restful_resource" "playlist" {
path = "/users/${jsondecode(data.restful_resource.me.output).id}/playlists"
path = "/users/${data.restful_resource.me.output.id}/playlists"
read_path = "/playlists/$(body.id)"
delete_path = "/playlists/$(body.id)/followers"
body = jsonencode({
body = {
name = "World Cup (by Terraform)"
})
}
}

locals {
Expand All @@ -55,7 +55,7 @@ data "restful_resource" "track" {
resource "restful_operation" "add_tracks_to_playlist" {
path = "${restful_resource.playlist.id}/tracks"
method = "PUT"
body = jsonencode({
uris = [for d in data.restful_resource.track : jsondecode(d.output).tracks.items[0].uri]
})
body = {
uris = [for d in data.restful_resource.track : d.output.tracks.items[0].uri]
}
}
32 changes: 16 additions & 16 deletions examples/usecases/thingsboard/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ data "restful_resource" "user" {
resource "restful_resource" "customer" {
path = "/customer"
read_path = "$(path)/$(body.id.id)"
body = jsonencode({
body = {
title = "Example Company"
tenantId = {
id = jsondecode(data.restful_resource.user.output).tenantId.id
id = data.restful_resource.user.output.tenantId.id
entityType = "TENANT"
}
country = "US"
Expand All @@ -64,15 +64,15 @@ resource "restful_resource" "customer" {
zip = "10004"
phone = "+1(415)777-7777"
email = "[email protected]"
})
}
}

resource "restful_resource" "device_profile" {
path = "/deviceProfile"
read_path = "$(path)/$(body.id.id)"
body = jsonencode({
body = {
tenantId = {
id = jsondecode(data.restful_resource.user.output).tenantId.id
id = data.restful_resource.user.output.tenantId.id
entityType = "TENANT"
}
name = "My Profile"
Expand All @@ -99,28 +99,28 @@ resource "restful_resource" "device_profile" {
firmwareId = null
softwareId = null
default = false
})
}
}

resource "restful_resource" "device" {
path = "/device"
read_path = "$(path)/$(body.id.id)"
body = jsonencode({
body = {
tenantId = {
id = jsondecode(data.restful_resource.user.output).tenantId.id
id = data.restful_resource.user.output.tenantId.id
entityType = "TENANT"
}
customerId = {
id = jsondecode(restful_resource.customer.output).id.id
id = restful_resource.customer.output.id.id
entityType = "CUSTOMER"
}
name = "My Device"
label = "Room 123 Sensor"
deviceProfileId : {
id = jsondecode(restful_resource.device_profile.output).id.id
id = restful_resource.device_profile.output.id.id
entityType = "DEVICE_PROFILE"
}
})
}
}

data "restful_resource" "device_credential" {
Expand All @@ -137,7 +137,7 @@ locals {
resolveMultiple = false
singleEntity = {
entityType = "DEVICE"
id = jsondecode(restful_resource.device.output).id.id
id = restful_resource.device.output.id.id
}
type = "singleEntity"
}
Expand Down Expand Up @@ -208,9 +208,9 @@ locals {
resource "restful_resource" "dashboard" {
path = "/dashboard"
read_path = "$(path)/$(body.id.id)"
body = jsonencode({
body = {
tenantId = {
id = jsondecode(data.restful_resource.user.output).tenantId.id
id = data.restful_resource.user.output.tenantId.id
entityType = "TENANT"
}
title = "My Dashboard"
Expand Down Expand Up @@ -256,10 +256,10 @@ resource "restful_resource" "dashboard" {
(local.my_device_widget.id) = local.my_device_widget
}
}
})
}
}

output "device_token" {
value = jsondecode(data.restful_resource.device_credential.output).credentialsId
value = data.restful_resource.device_credential.output.credentialsId
sensitive = true
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.22.1
require (
github.com/evanphx/json-patch v0.5.2
github.com/go-resty/resty/v2 v2.10.0
github.com/hashicorp/terraform-plugin-framework v1.7.0
github.com/hashicorp/terraform-plugin-framework v1.7.1-0.20240326130300-484f311c99cf
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0
github.com/hashicorp/terraform-plugin-go v0.22.1
github.com/hashicorp/terraform-plugin-sdk/v2 v2.33.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ github.com/hashicorp/terraform-exec v0.20.0 h1:DIZnPsqzPGuUnq6cH8jWcPunBfY+C+M8J
github.com/hashicorp/terraform-exec v0.20.0/go.mod h1:ckKGkJWbsNqFKV1itgMnE0hY9IYf1HoiekpuN0eWoDw=
github.com/hashicorp/terraform-json v0.21.0 h1:9NQxbLNqPbEMze+S6+YluEdXgJmhQykRyRNd+zTI05U=
github.com/hashicorp/terraform-json v0.21.0/go.mod h1:qdeBs11ovMzo5puhrRibdD6d2Dq6TyE/28JiU4tIQxk=
github.com/hashicorp/terraform-plugin-framework v1.7.0 h1:wOULbVmfONnJo9iq7/q+iBOBJul5vRovaYJIu2cY/Pw=
github.com/hashicorp/terraform-plugin-framework v1.7.0/go.mod h1:jY9Id+3KbZ17OMpulgnWLSfwxNVYSoYBQFTgsx044CI=
github.com/hashicorp/terraform-plugin-framework v1.7.1-0.20240326130300-484f311c99cf h1:pUx5HaXbPjLAhIO/vxisMrqDlalIUyQAxMDun0TKLBM=
github.com/hashicorp/terraform-plugin-framework v1.7.1-0.20240326130300-484f311c99cf/go.mod h1:jY9Id+3KbZ17OMpulgnWLSfwxNVYSoYBQFTgsx044CI=
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0 h1:HOjBuMbOEzl7snOdOoUfE2Jgeto6JOjLVQ39Ls2nksc=
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0/go.mod h1:jfHGE/gzjxYz6XoUwi/aYiiKrJDeutQNUtGQXkaHklg=
github.com/hashicorp/terraform-plugin-go v0.22.1 h1:iTS7WHNVrn7uhe3cojtvWWn83cm2Z6ryIUDTRO0EV7w=
Expand Down
Loading

0 comments on commit 773524f

Please sign in to comment.