cloudamqp / terraform-provider-cloudamqp

Terraform Provider for CloudAMQP
https://registry.terraform.io/providers/cloudamqp/cloudamqp
Mozilla Public License 2.0
35 stars 39 forks source link

Unable to find Instance ID #109

Closed koalalorenzo closed 3 years ago

koalalorenzo commented 3 years ago

We are trying to use data.cloudamqp_credentials but I can't figure out where to get the instance ID from as all the other resources are using name and strings (from the docs). I tried to look on the web admin interface but I can't find the number specific.

Code:

data "cloudamqp_credentials" "main" {
  instance_id = "something-someting"
}

Result:

$ tf validate
│ Error: Incorrect attribute value type
│
│   on rabbitmq.tf line 14, in data "cloudamqp_credentials" "main":
│   14:   instance_id = "something-someting"
│
│ Inappropriate value for attribute "instance_id": a number is required.

How can I get the number?

tbroden84 commented 3 years ago

Do you already have a resource for the instance? Then you can reuse the information from that resource.

resource "cloudamqp_instance" "instance" {
  name   = "Terraform test instance"
  plan   = "bunny-3"
  region = "amazon-web-services::us-west-2"
  tags   = ["terraform"]
  rmq_version = "3.9.5"
}

data "cloudamqp_credentials" "main" {
  instance_id = cloudamqp_instance.instance.id
}

Otherwise there is a reference to our API in the documentation on registry.terraform.ip for import

koalalorenzo commented 3 years ago

@tbroden84 OFC I have the instance :D

The issue is that the data is not using any information that is available easily from the other terraform resources. It would be nice to have a name instead of the instance ID that can get only by API 😅 The docs on Terraform are not showing any .id as attributes 😓

In this case, we are using multiple workspaces and the instance resource is created somewhere else. Import is not an option as the resource should not be managed by this project but just referenced to get credentials.

I hope it makes sense.

What I wish would be this:

data "cloudamqp_credentials" "main" {
  instance_name = "bunny-3"
}

Does it make sense? What do you think?

tbroden84 commented 3 years ago

@koalalorenzo

True that, Id attribute is missing from the documentation and I will add them. Never put them there because all resource and data sources always have an Id. But always good to clarify all available attributes.

Unfortunately our underlying API uses the instance ID in order to fetch the correct data. The instance Id (id) is also present in each state-file (usually called terraform.tfstate). Otherwise we need to build a special case in the data source, in order to list all instances and choose the instance with correct name. Not an ideal solution, since the instance Id is both unique and static while name can be changed.

koalalorenzo commented 3 years ago

I see, Keep the issue open as this is quiet confusing :D There is no reference of .id and having a name would be nice 🙏

dentarg commented 3 years ago

True that, Id attribute is missing from the documentation and I will add them

Added in https://github.com/cloudamqp/terraform-provider-cloudamqp/commit/a31d349b61bb180c4753dd595c466c6692da854d#diff-a64ca2b72f6ccc7bafa5eb3d1fc3f805d5a790cf082381287d76ecb25a295786

dentarg commented 3 years ago

We're (me and @tbroden84) are thinking about adding a "account" data source that will fetch details (name, id, ...) of all your instances. Would that help you @koalalorenzo?

tbroden84 commented 3 years ago

@koalalorenzo

We have now tried out to add a "account" data source. This will fetch all instances on an account, based on the API key used in the cloudamqp provider block. When the new data source is populated it's possible to iterate through all instances and then filter out the wanted instance based on the instance name. From there you will have access to the instance id, that can be used to get the correct credentials. See example below

locals {
  instance_name = "<instance_name>"
}

data "cloudamqp_account" "account" {}

data "cloudamqp_credentials" "credentials" {
  instance_id = [for instance in data.cloudamqp_account.account.instances : instance if instance["name"] == local.instance_name][0].id
}

output "username" {
  value = data.cloudamqp_credentials.credentials.username
  sensitive = true
}

output "password" {
  value = data.cloudamqp_credentials.credentials.password
  sensitive = true
}

"[for instance in data.cloudamqp_account.account.instances : instance if instance["name"] == local.instance_name]" The iteration loop will unfortunately give a list back. If you get a match you could take the first item [0] and then the instance identifier attribute (.id). If you get no match the process will fail completely.

Hence "[for instance in data.cloudamqp_account.account.instances : instance if instance["name"] == local.instance_name][0].id"

Could this be something you could use?

Note: In your example you use "bunny-3". Which is the subscription plan and not the instance name, unless you named it that.

dentarg commented 3 years ago

@koalalorenzo can we close this issue now?

tbroden84 commented 3 years ago

@koalalorenzo We have now made a new release v1.11.0 that contains above update. Created a new data source account and updated the documentation with above example.

Will close this issue for now. Please feel free to re-open it if you still having an issue or other suggestion.