Mastercard / terraform-provider-restapi

A terraform provider to manage objects in a RESTful API
Other
820 stars 217 forks source link

Add more examples to the documentation #125

Closed vmarlier closed 3 years ago

vmarlier commented 3 years ago

Is it possible to add more examples to the documentation ? Currently if I'm not mistaken the only examples are the usage of the Data Source (GET) and the usage of a POST request.

For example I was trying to use this provider for executing a PUT request, I succeeded but not in the right way I think. I read in an issue that to execute a PUT query you had to first go through a Data Source to find the target and then execute your PUT query. But I don't know how to use the data source with the restapi_object.

So please give use more examples.

DRuggeri commented 3 years ago

Hello there The examples in the directory are indeed 'canonical' use cases, but we're more than happy to add examples that you find which are working. I definitely encourage discussing what you got working and creating a pull request with the example.

vmarlier commented 3 years ago

Hello,

Here is the code I used to execute a PUT request, it's very ugly but I don't understand how to do it other way.. So I have no idea on how really and properly use restapi_object to execute a PUT request ^^

Once we worked on this PUT request example, I would be please to do a PR so it can be added to the examples :)

provider "restapi" {
  uri                  = "https://api.scaleway.com"
  write_returns_object = true
  debug                = true

  headers = { "X-Auth-Token" = var.scw_secret_key, "Content-Type" = "application/json" }

  create_method  = "PUT"
  update_method  = "PUT"
  destroy_method = "PUT"
}

resource "restapi_object" "k8s_sg_allow_smtp" {
  depends_on   = [time_sleep.wait_10_seconds_after_cluster_creation]
  path         = "/instance/v1/zones/fr-par-1/security_groups/{id}"
  id_attribute = split("/", data.scaleway_instance_security_group.k8s_sg.id)[1]
  object_id    = split("/", data.scaleway_instance_security_group.k8s_sg.id)[1]
  data         = "{ \"name\": \"kubernetes ${module.kapsule.k8s_cluster_id}\",\"enable_default_security\": false, \"project\": \"${local.project_id}\"}"
}
DRuggeri commented 3 years ago

(Sorry for the slow reply time!)

Actually, this use case looks like it's just toggling a value, right? I'm surprised this does not result in an error. Usually the provider is used for full CRUD operations (Create, Read, Update, Delete), but I think this example is using it more as a cURL-like call to the item being managed.

Is that accurate? While I can't say that's "wrong" per se, it does seem to be a novel use of the provider. I wonder what would happen during destroy, though? I suspect it would error out...

vmarlier commented 3 years ago

Better late than never :)

Yes exactly, I'm just updating a single value. Indeed my first need was to make a curl-like request with a terraform provider to avoid using some "local-exec" coupled with bash code.

Actually I had many errors at the beginning I just fixed them by setting all the methods as PUT method and add {id} to the path. But now that you're say it (used for full CRUD) I have a better understanding of the provider.

I have no problem on destroy and didn't do anything special, I expected to dependency error like "cannot destroy the resource" but none showed up..

DRuggeri commented 3 years ago

Thanks, @vmarlier - that makes sense. Would you mind opening a PR to include this example? I'm happy to annotate it with some additional details.

vmarlier commented 3 years ago

Yes no problem, I'll do it soon.

EDIT: Done https://github.com/Mastercard/terraform-provider-restapi/pull/128

DRuggeri commented 3 years ago

Many thanks! This has been merged