Skip to content
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

Data Source for categories #51

Merged
merged 2 commits into from
Apr 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions nutanix/data_source_nutanix_category_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package nutanix

import (
"fmt"
"log"
"strings"

"github.com/hashicorp/terraform/helper/schema"
v3 "github.com/terraform-providers/terraform-provider-nutanix/client/v3"
"github.com/terraform-providers/terraform-provider-nutanix/utils"
)

func dataSourceNutanixCategoryKey() *schema.Resource {
return &schema.Resource{
Read: dataSourceNutanixCategoryKeyRead,

Schema: map[string]*schema.Schema{
"system_defined": {
Type: schema.TypeBool,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"api_version": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"values": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

func dataSourceNutanixCategoryKeyRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Reading CategoryKey: %s", d.Get("name").(string))

// Get client connection
conn := meta.(*Client).API

// Make request to the API
resp, err := conn.V3.GetCategoryKey(d.Get("name").(string))

if err != nil {
if strings.Contains(fmt.Sprint(err), "ENTITY_NOT_FOUND") {
d.SetId("")
}
return err
}

d.Set("api_version", utils.StringValue(resp.APIVersion))
d.Set("name", utils.StringValue(resp.Name))
d.Set("description", utils.StringValue(resp.Description))
d.Set("system_defined", utils.BoolValue(resp.SystemDefined))

d.SetId(utils.StringValue(resp.Name))

list, err := conn.V3.ListCategoryValues(d.Get("name").(string), &v3.CategoryListMetadata{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only concern here is pagination, not really an issue here, but I think there is a limit on how much categories can be created, isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was thinking about it as well, I already have a workaround on this but I think I'll put it in a next enhancement phase.

Kind: utils.StringPtr("category"),
Length: utils.Int64Ptr(500),
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this is kinda hard to read, I know it seems easier to send the struct as an argument instead of creating a variable, but it gives unnecessary length to the call


if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

related to the comments above, we can move this if to the call above, instead of leaving it here alone

return err
}
values := make([]string, len(list.Entities))

for k, v := range list.Entities {
values[k] = utils.StringValue(v.Value)
}

return d.Set("values", values)
}
71 changes: 71 additions & 0 deletions nutanix/data_source_nutanix_category_key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package nutanix

import (
"testing"

"github.com/hashicorp/terraform/helper/resource"
)

func TestAccNutanixCategoryKeyDataSource_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCategoryKeyDataSourceConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(
"data.nutanix_category_key.test_key", "id"),
resource.TestCheckResourceAttr(
"data.nutanix_category_key.test_key", "values.#", "0"),
),
},
},
})
}

func TestAccNutanixCategoryKeyDataSource_withValues(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCategoryKeyDataSourceConfigWithValues,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(
"data.nutanix_category_key.test_key_value", "id"),
resource.TestCheckResourceAttr(
"data.nutanix_category_key.test_key_value", "values.#", "1"),
),
},
},
})
}

const testAccCategoryKeyDataSourceConfig = `
resource "nutanix_category_key" "test_key"{
name = "data_source_category_key_test"
description = "Data Source CategoryKey Test"
}


data "nutanix_category_key" "test_key" {
name = "${nutanix_category_key.test_update.name}"
}`

const testAccCategoryKeyDataSourceConfigWithValues = `
resource "nutanix_category_key" "test_key_value"{
name = "data_source_category_key_test_values"
description = "Data Source CategoryKey Test with Values"
}

resource "nutanix_category_value" "test_value"{
name = "${nutanix_category_key.test_key_value.name}"
value = "test_category_value_data_source"
description = "Data Source CategoryValue Test with Values"
}


data "nutanix_category_key" "test_key_value" {
name = "${nutanix_category_key.test_key_value.name}"
}`
1 change: 1 addition & 0 deletions nutanix/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func Provider() terraform.ResourceProvider {
"nutanix_cluster": dataSourceNutanixCluster(),
"nutanix_clusters": dataSourceNutanixClusters(),
"nutanix_virtual_machine": dataSourceNutanixVirtualMachine(),
"nutanix_category_key": dataSourceNutanixCategoryKey(),
// "nutanix_network_security_rule": dataSourceNutanixNetworkSecurityRule(),
// "nutanix_volume_group": dataSourceNutanixVolumeGroup(),
// "nutanix_volume_groups": dataSourceNutanixVolumeGroups(),
Expand Down
48 changes: 48 additions & 0 deletions website/docs/d/category_key.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
layout: "nutanix"
page_title: "NUTANIX: nutanix_category_key"
sidebar_current: "docs-nutanix-datasource-category-key"
description: |-
Describe a Nutanix Category Key and its values (if it has them).
---

# nutanix_category_key

Describe a Nutanix Category Key and its values (if it has them).

## Example Usage

```hcl
resource "nutanix_category_key" "test_key_value"{
name = "data_source_category_key_test_values"
description = "Data Source CategoryKey Test with Values"
}

resource "nutanix_category_value" "test_value"{
name = "${nutanix_category_key.test_key_value.name}"
value = "test_category_value_data_source"
description = "Data Source CategoryValue Test with Values"
}


data "nutanix_category_key" "test_key_value" {
name = "${nutanix_category_key.test_key_value.name}"
}
```

## Argument Reference

The following arguments are supported:

* `name`: - (Required) The name for the category key.

## Attributes Reference

The following attributes are exported:

* `system_defined`: - Specifying whether its a system defined category.
* `description`: - A description for category key.
* `api_version` - The version of the API.
* `values`: - A list of the values from this category key (if it has them).

See detailed information in [Nutanix Image](https://nutanix.github.io/Automation/experimental/swagger-redoc-sandbox/#tag/category/paths/~1categories~1{name}/get).