terraform-provider-vsphere icon indicating copy to clipboard operation
terraform-provider-vsphere copied to clipboard

Add support to attach tags to any compatible resource

Open bloudraak opened this issue 3 years ago • 5 comments

Description

Assume for a minute you have resources in vCenter that supports tags, and you'd like to attach tags to them. Examples include existing datastores, hosts, content items, and whatnot. This would benefit folks since some resources in vSphere are created outside of Terraform.

My use cases include tagging hosts, data stores, and content libraries with "packer", and then using PowerCLI to generate a configuration for Packer, in order to create templates and whatnot. After Packer is done, I'd then move the template to the appropriate content libraries (also identified using tags), and tag the template, and so forth accordingly.

Potential Terraform Configuration

data "vsphere_datacenter" "main" {
  name = var.datacenter
}

data "vsphere_datastore" "main" {
  name          = var.datastore
  datacenter_id = data.vsphere_datacenter.main.id
}

resource "vsphere_tag_category" "automation" {
  name        = "Automation"
  description = "Used by automation to determine which resources to use when building images"
  cardinality = "SINGLE"

  associable_types = [
    "VirtualMachine",
    "Folder",
    "Datacenter",
    "HostSystem",
  ]
}

resource "vsphere_tag" "packer" {
  name        = "Packer"
  category_id = vsphere_tag_category.automation.id
  description = "Indicates that Packer should use this resource when provisining stuff"
}

resource "vsphere_datastore_tags_attachment" "packer" {
   datastore = data.vsphere_datastore.main.id
   tags     = [vsphere_tag.packer.id]
}

You may end up wither with multiple resources, for example, vsphere_xxx_tags_attachment where xxx is the resource type in vSphere that supports tagging. An alternate approach (based on the vSphere SDK), would be to have a generic vsphere_tags_attachment, which takes a managed object id of the resource that supports tagging.

Yet another consideration would be whether several tags should be applied at once (as in my example), or whether it is just one tag per assignment, as follows:

resource "vsphere_datastore_tag_attachment" "packer" {
   datastore = data.vsphere_datastore.main.id
   tag     = vsphere_tag.packer.id
}

References

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

bloudraak avatar Oct 26 '21 14:10 bloudraak

Related: #1452

Suggest closing #1452 and consolidating enhancement requests to this issue #1498.

Ryan

tenthirtyam avatar Oct 26 '21 21:10 tenthirtyam

resource/virtual_host : Added support add tags to hosts. (GH-1499]

tenthirtyam avatar Feb 11 '22 23:02 tenthirtyam

Status of Support for Tags on Provider Resources

Based on Associable Object Types As of 2022-03-11

Resource Status
r/vsphere_compute_cluster
r/vsphere_content_library
r/vsphere_content_library_item
r/vsphere_datacenter
r/vsphere_datastore_cluster
r/vsphere_distributed_port_group
r/vsphere_distributed_virtual_switch
r/vsphere_folder
r/vsphere_nas_datastore
r/vsphere_resource_pool
r/vsphere_host
r/vsphere_vapp_container
r/vsphere_vapp_entity
r/vsphere_virtual_machine
r/vsphere_vmfs_datastore

cc @appilon @tejavar

tenthirtyam avatar Mar 11 '22 20:03 tenthirtyam

Hey, Troy (@tlindsay42 ! 👋 - I was recently looking at pulling this over to the content library after I completed the analytics and added tagging to a couple of other resources. I'll keep this issue updated whilst I have time to take a look.

Ryan Johnson Staff II Solutions Architect | VMware, Inc.

tenthirtyam avatar Apr 19 '22 20:04 tenthirtyam