-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #167 from terraform-providers/f-tag-category-data-…
…source New data source: vsphere_tag_category
- Loading branch information
Showing
8 changed files
with
256 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package vsphere | ||
|
||
import "github.com/hashicorp/terraform/helper/schema" | ||
|
||
func dataSourceVSphereTagCategory() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceVSphereTagCategoryRead, | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Description: "The display name of the category.", | ||
Required: true, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The description of the category.", | ||
}, | ||
"cardinality": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The associated cardinality of the category. Can be one of SINGLE (object can only be assigned one tag in this category) or MULTIPLE (object can be assigned multiple tags in this category).", | ||
}, | ||
"associable_types": { | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Description: "Object types to which this category's tags can be attached.", | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceVSphereTagCategoryRead(d *schema.ResourceData, meta interface{}) error { | ||
client, err := meta.(*VSphereClient).TagsClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
id, err := tagCategoryByName(client, d.Get("name").(string)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(id) | ||
return resourceVSphereTagCategoryRead(d, meta) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package vsphere | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceVSphereTagCategory(t *testing.T) { | ||
var tp *testing.T | ||
testAccDataSourceVSphereTagCategoryCases := []struct { | ||
name string | ||
testCase resource.TestCase | ||
}{ | ||
{ | ||
"basic", | ||
resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheck(tp) | ||
}, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceVSphereTagCategoryConfig(), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
"data.vsphere_tag_category.terraform-test-category-data", | ||
"name", | ||
testAccDataSourceVSphereTagCategoryConfigName, | ||
), | ||
resource.TestCheckResourceAttr( | ||
"data.vsphere_tag_category.terraform-test-category-data", | ||
"description", | ||
testAccDataSourceVSphereTagCategoryConfigDescription, | ||
), | ||
resource.TestCheckResourceAttr( | ||
"data.vsphere_tag_category.terraform-test-category-data", | ||
"cardinality", | ||
testAccDataSourceVSphereTagCategoryConfigCardinality, | ||
), | ||
resource.TestCheckResourceAttr( | ||
"data.vsphere_tag_category.terraform-test-category-data", | ||
"associable_types.#", | ||
"1", | ||
), | ||
resource.TestCheckResourceAttr( | ||
"data.vsphere_tag_category.terraform-test-category-data", | ||
"associable_types.3125094965", | ||
testAccDataSourceVSphereTagCategoryConfigAssociableType, | ||
), | ||
resource.TestCheckResourceAttrPair( | ||
"data.vsphere_tag_category.terraform-test-category-data", "id", | ||
"vsphere_tag_category.terraform-test-category", "id", | ||
), | ||
), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testAccDataSourceVSphereTagCategoryCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
tp = t | ||
resource.Test(t, tc.testCase) | ||
}) | ||
} | ||
} | ||
|
||
const testAccDataSourceVSphereTagCategoryConfigName = "terraform-test-category" | ||
const testAccDataSourceVSphereTagCategoryConfigDescription = "Managed by Terraform" | ||
const testAccDataSourceVSphereTagCategoryConfigCardinality = vSphereTagCategoryCardinalitySingle | ||
const testAccDataSourceVSphereTagCategoryConfigAssociableType = vSphereTagCategoryAssociableTypeVirtualMachine | ||
|
||
func testAccDataSourceVSphereTagCategoryConfig() string { | ||
return fmt.Sprintf(` | ||
variable "tag_category_name" { | ||
default = "%s" | ||
} | ||
variable "tag_category_description" { | ||
default = "%s" | ||
} | ||
variable "tag_category_cardinality" { | ||
default = "%s" | ||
} | ||
variable "tag_category_associable_types" { | ||
default = [ | ||
"%s", | ||
] | ||
} | ||
resource "vsphere_tag_category" "terraform-test-category" { | ||
name = "${var.tag_category_name}" | ||
description = "${var.tag_category_description}" | ||
cardinality = "${var.tag_category_cardinality}" | ||
associable_types = [ | ||
"${var.tag_category_associable_types}", | ||
] | ||
} | ||
data "vsphere_tag_category" "terraform-test-category-data" { | ||
name = "${vsphere_tag_category.terraform-test-category.name}" | ||
} | ||
`, | ||
testAccDataSourceVSphereTagCategoryConfigName, | ||
testAccDataSourceVSphereTagCategoryConfigDescription, | ||
testAccDataSourceVSphereTagCategoryConfigCardinality, | ||
testAccDataSourceVSphereTagCategoryConfigAssociableType, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
layout: "vsphere" | ||
page_title: "VMware vSphere: vsphere_tag_category" | ||
sidebar_current: "docs-vsphere-data-source-tag-category" | ||
description: |- | ||
Provides a vSphere tag category data source. This can be used to reference tag categories not managed in Terraform. | ||
--- | ||
|
||
# vsphere\_tag\_category | ||
|
||
The `vsphere_tag_category` data source can be used to reference tag categories | ||
that are not managed by Terraform. Its attributes are exactly the same as the | ||
[`vsphere_tag_category` resource][resource-tag-category], and, like importing, | ||
the data source takes a name to search on. The `id` and other attributes are | ||
then populated with the data found by the search. | ||
|
||
[resource-tag-category]: /docs/providers/vsphere/r/tag_category.html | ||
|
||
~> **NOTE:** Tagging support is unsupported on direct ESXi connections and | ||
requires vCenter 6.0 or higher. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "vsphere_tag_category" "category" { | ||
name = "terraform-test-category" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `name` - (String, required) The name of the tag category. | ||
|
||
## Attribute Reference | ||
|
||
In addition to the `id` being exported, all of the fields that are available in | ||
the [`vsphere_tag_category` resource][resource-tag-category] are also | ||
populated. See that page for further details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters