-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add virtual machines data sources #2463
Add virtual machines data sources #2463
Conversation
It currently only returns the ID of the Virtual Machine and only allows searching by exact name.
hey @inkel Thanks for this PR :) Out of interest what's the use-case you're looking to solve with this Data Source? We've previously discussed adding a Data Source for a Virtual Machine but ultimately found that most of the information you'd want to obtain from it can be found via another Data Source (e.g. the IP Addresses of the VM can be obtained using the One thing of particular note here is that the Thanks! |
Hi @tombuildsstuff! Thanks for taking the time for this. I wasn't aware of the newest resources you will be adding, but for the current state of the provider, my use case is the following: I'm working as a contractor for a company that already has infrastructure created outside of Terraform. My task was to add some monitoring metrics for that infrastructure, and we've chosen to use Terraform to simplify the process; migrating all of the infrastructure to Terraform is in the roadmap but further in the future, this task is the selling point for IaaC. Given we are going to add the same metrics to each of our different virtual machines, we created a Terraform module, let's name it This is where these two data sources I'm proposing come at hand: without them, we have to manually pass the VM IDs to the module, and as you might recall, Azure IDs aren't just a simply ID but almost an URI, so we ended up with a variable "vm_server_id" {
default = "/subscriptions/${var.subscription_id}/resourceGroups/${var.resource_group_name}/providers/Microsoft.Compute/virtualMachines/${var.vm_server_name}"
} In the case of only one VM, this is somewhat easy to maintain, though still error-prone to typos. But when we have a cluster of machines, all named using a fixed prefix and a variable numeric suffix, things got more complicated, as we had to declare one variable for each resource. So far we managed to get this going by manually having to Now, if we were to have this data sources in our tooling, we could make things much resilient and easy, as we could have something like: # main.tf
data "azurerm_virtual_machine" "vm_server" {
resource_group_name = "${var.resource_group_name}"
name = "vm_server"
}
module "vm_server_alerts" {
source = "modules/vm_server_alerts"
resource_group_name = "${var.resource_group_name}"
vm_id = "${data.azurerm_virtual_machine.vm_server.id}"
}
data "azurerm_virtual_machines" "vm_cluster" {
resource_group_name = "${var.resource_group_name}"
name_prefix = "vm_cluster_server_"
}
module "vm_cluster_alerts" {
source = "modules/vm_cluster_alerts"
resource_group_name = "${var.resource_group_name}"
vm_ids = "${data.azurerm_virtual_machines.vm_cluster.ids}"
vm_names = "${data.azurerm_virtual_machines.vm_cluster.names}"
} I hope this is clear to understand. Feel free to ask me any further questions! |
hey @inkel Taking a little time to think this through some more - I could definitely see this being useful, but only for the ID of the Virtual Machine (and not the other fields). Based on the scenario you've described above - I believe it should be possible to achieve this either using one data source either for a single machine:
or for multiple virtual machines:
This'd allow the Virtual Machine ID to be used with the As such I believe by using Count with the Data Source that the Thanks! |
I like this idea! Let me try it locally and see how does thing look, but I'm positive it will work. Also, it will remove the need of adding an additional data resource (and I'll get to avoid writing more tests 😛) I'm more than ok with only returning the VM ID.
I'm not sure I'm following you on this comment, care to explain it again to me? Thanks! |
Yup, the
|
There's been a feature request recently to enable the
Yep basically as you've mentioned above - ensure the Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey @inkel
Thanks for this PR :)
I've taken a look through and left some minor comments inline, but this is mostly looking good - if we can fix up the comments (and remove the Virtual Machines Data Source) we should be able to run the tests and get this merged 👍
Thanks!
name = "${azurerm_virtual_machine.test.name}" | ||
} | ||
|
||
`, rInt, location) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'd be better to use a Linux VM here (since they tend to boot faster)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just copied the same definition from azurerm/data_source_arm_virtual_network_test.go
, but sure, I can do that 👌
Co-Authored-By: inkel <inkel.ar@gmail.com>
This reverts commit 2c343a2.
As suggested by @tombuildsstuff the test VM was changed to a Linux one, which tends to boot faster. I've also properly formattd the test using `terraform fmt`.
I've should've known better, but the GitHub suggestion feature, awesome as it is, only applied to one line, so when I applied your suggestion to rename the function it broke the build 😞 Anyway, that's fixed now, alongside with all your other suggestions, so this should be ready for another review. Thank you @tombuildsstuff for your time and feedback! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey @inkel
Thanks for pushing those changes - this now LGTM 👍 - I'll kick off the tests shortly
Thanks!
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 hashibot-feedback@hashicorp.com. Thanks! |
This pull request adds two new Virtual Machine data sources:
azurerm_virtual_machine
to retrieve information about a single Virtual Machine.azurerm_virtual_machines
to retrieve information about many Virtual Machines.Usage
azurerm_virtual_machine
azurerm_virtual_machines
TODO
azurerm_virtual_machines
azurerm_virtual_machine
(e.g.location
)azurerm_virtual_machines
(e.g.location
)