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

Dynamic category name support #76

Merged
merged 4 commits into from
Sep 4, 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
4 changes: 3 additions & 1 deletion client/v3/v3_structs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package v3

import "time"
import (
"time"
)

// Reference ...
type Reference struct {
Expand Down
48 changes: 42 additions & 6 deletions nutanix/categories.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,57 @@
package nutanix

import "github.com/hashicorp/terraform/helper/schema"
import (
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
)

func categoriesSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeMap,
Type: schema.TypeSet,
Optional: true,
Computed: true,
Set: func(v interface{}) int {
category := v.(map[string]interface{})
return hashcode.String(category["name"].(string) + category["value"].(string))
},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"value": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
},
}
}

func expandCategories(categories map[string]interface{}) map[string]string {
output := make(map[string]string, len(categories))
func expandCategories(categoriesSet interface{}) map[string]string {
categories := categoriesSet.(*schema.Set).List()
output := make(map[string]string)

for i, v := range categories {
output[i] = v.(string)
for _, v := range categories {
category := v.(map[string]interface{})
output[category["name"].(string)] = category["value"].(string)
}

return output
}

func flattenCategories(categories map[string]string) []interface{} {
c := make([]interface{}, 0)

for name, value := range categories {
c = append(c, map[string]interface{}{
"name": name,
"value": value,
})
}

return c
}
14 changes: 3 additions & 11 deletions nutanix/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ func getMetadataAttributes(d *schema.ResourceData, metadata *v3.Metadata, kind s
metadata.Kind = utils.StringPtr(kind)

if v, ok := d.GetOk("categories"); ok {
catl := v.(map[string]interface{})
metadata.Categories = expandCategories(catl)
metadata.Categories = expandCategories(v)
} else {
metadata.Categories = nil
}
Expand Down Expand Up @@ -47,7 +46,7 @@ func getMetadataAttributes(d *schema.ResourceData, metadata *v3.Metadata, kind s
return nil
}

func setRSEntityMetadata(v *v3.Metadata) (map[string]interface{}, map[string]interface{}) {
func setRSEntityMetadata(v *v3.Metadata) (map[string]interface{}, []interface{}) {
metadata := make(map[string]interface{})
metadata["last_update_time"] = utils.TimeValue(v.LastUpdateTime).String()
metadata["uuid"] = utils.StringValue(v.UUID)
Expand All @@ -56,14 +55,7 @@ func setRSEntityMetadata(v *v3.Metadata) (map[string]interface{}, map[string]int
metadata["spec_hash"] = utils.StringValue(v.SpecHash)
metadata["name"] = utils.StringValue(v.Name)

c := make(map[string]interface{}, len(v.Categories))
if v.Categories != nil {
for name, values := range v.Categories {
c[name] = values
}
}

return metadata, c
return metadata, flattenCategories(v.Categories)
}

func flattenReferenceValues(r *v3.Reference) map[string]interface{} {
Expand Down
3 changes: 1 addition & 2 deletions nutanix/resource_nutanix_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,7 @@ func resourceNutanixImageUpdate(d *schema.ResourceData, meta interface{}) error
}

if d.HasChange("categories") {
catl := d.Get("categories").(map[string]interface{})
metadata.Categories = expandCategories(catl)
metadata.Categories = expandCategories(d.Get("categories"))
}

if d.HasChange("owner_reference") {
Expand Down
93 changes: 77 additions & 16 deletions nutanix/resource_nutanix_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,18 @@ func TestAccNutanixImage_WithCategories(t *testing.T) {
Config: testAccNutanixImageConfigWithCategories(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckNutanixImageExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "categories.%", "2"),
resource.TestCheckResourceAttr(resourceName, "categories.os_type", "ubuntu"),
resource.TestCheckResourceAttr(resourceName, "categories.os_version", "current"),
resource.TestCheckResourceAttr(resourceName, "categories.#", "2"),
//resource.TestCheckResourceAttr(resourceName, "categories.os_type", "ubuntu"),
//resource.TestCheckResourceAttr(resourceName, "categories.os_version", "current"),
),
},
{
Config: testAccNutanixImageConfigWithCategoriesUpdated(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckNutanixImageExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "categories.%", "2"),
resource.TestCheckResourceAttr(resourceName, "categories.os_type", "ubuntu"),
resource.TestCheckResourceAttr(resourceName, "categories.os_version", "18.04"),
resource.TestCheckResourceAttr(resourceName, "categories.#", "2"),
//resource.TestCheckResourceAttr(resourceName, "categories.os_type", "ubuntu"),
//resource.TestCheckResourceAttr(resourceName, "categories.os_version", "18.04"),
),
},
{
Expand Down Expand Up @@ -277,14 +277,41 @@ resource "nutanix_image" "acctest-testLocal" {

func testAccNutanixImageConfigWithCategories(r int) string {
return fmt.Sprintf(`
resource "nutanix_category_key" "os_version"{
name = "os_version"
description = "testacc-os-version"
}

resource "nutanix_category_value" "os_version_value"{
name = nutanix_category_key.os_version.id
description = "testacc-os-current"
value = "os_current"
}

resource "nutanix_category_key" "os_type"{
name = "os_type"
description = "testacc-os-type"
}

resource "nutanix_category_value" "ubuntu"{
name = nutanix_category_key.os_type.id
description = "testacc-ubuntu"
value = "ubuntu"
}

resource "nutanix_image" "acctest-test-categories" {
name = "Ubuntu-%d"
description = "Ubuntu"

categories = {
os_type = "ubuntu"
os_version = "current"
}
categories {
name = nutanix_category_key.os_type.id
value = nutanix_category_value.ubuntu.id
}

categories {
name = nutanix_category_key.os_version.id
value = nutanix_category_value.os_version_value.id
}

source_uri = "http://archive.ubuntu.com/ubuntu/dists/bionic/main/installer-amd64/current/images/netboot/mini.iso"

Expand All @@ -294,16 +321,50 @@ resource "nutanix_image" "acctest-test-categories" {

func testAccNutanixImageConfigWithCategoriesUpdated(r int) string {
return fmt.Sprintf(`
resource "nutanix_category_key" "os_version"{
name = "os_version"
description = "testacc-os-version"
}

resource "nutanix_category_value" "os_version_value"{
name = nutanix_category_key.os_version.id
description = "testacc-os-current"
value = "os_current"
}

resource "nutanix_category_value" "os_version_value_updated"{
name = nutanix_category_key.os_version.id
description = "testacc-ubuntu18"
value = "18.08"
}

resource "nutanix_category_key" "os_type"{
name = "os_type"
description = "testacc-os-type"
}

resource "nutanix_category_value" "ubuntu"{
name = nutanix_category_key.os_type.id
description = "testacc-ubuntu"
value = "ubuntu"
}


resource "nutanix_image" "acctest-test-categories" {
name = "Ubuntu-%d"
description = "Ubuntu"
name = "Ubuntu-%d"
description = "Ubuntu"

categories = {
os_type = "ubuntu"
os_version = "18.04"
categories {
name = nutanix_category_key.os_type.id
value = nutanix_category_value.ubuntu.id
}

categories {
name = nutanix_category_key.os_version.id
value = nutanix_category_value.os_version_value_updated.id
}

source_uri = "http://archive.ubuntu.com/ubuntu/dists/bionic/main/installer-amd64/current/images/netboot/mini.iso"
source_uri = "http://archive.ubuntu.com/ubuntu/dists/bionic/main/installer-amd64/current/images/netboot/mini.iso"

}
`, r)
Expand Down
3 changes: 1 addition & 2 deletions nutanix/resource_nutanix_network_security_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,8 +707,7 @@ func resourceNutanixNetworkSecurityRuleUpdate(d *schema.ResourceData, meta inter
}

if d.HasChange("categories") {
catl := d.Get("categories").(map[string]interface{})
metadata.Categories = expandCategories(catl)
metadata.Categories = expandCategories(d.Get("categories"))
}

if d.HasChange("owner_reference") {
Expand Down
3 changes: 1 addition & 2 deletions nutanix/resource_nutanix_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,8 +544,7 @@ func resourceNutanixSubnetUpdate(d *schema.ResourceData, meta interface{}) error
}

if d.HasChange("categories") {
catl := d.Get("categories").(map[string]interface{})
metadata.Categories = expandCategories(catl)
metadata.Categories = expandCategories(d.Get("categories"))
}
if d.HasChange("owner_reference") {
or := d.Get("owner_reference").(map[string]interface{})
Expand Down
49 changes: 41 additions & 8 deletions nutanix/resource_nutanix_subnet_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nutanix

import (
"encoding/json"
"fmt"
"regexp"
"strings"
Expand Down Expand Up @@ -91,16 +92,23 @@ func TestAccNutanixSubnet_WithCategory(t *testing.T) {
Config: testAccNutanixSubnetConfigWithCategory(r),
Check: resource.ComposeTestCheckFunc(
testAccCheckNutanixSubnetExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "categories.%", "1"),
resource.TestCheckResourceAttr(resourceName, "categories.Environment", "Production"),
testAccCheckNutanixCategories(resourceName),
resource.TestCheckResourceAttr(resourceName, "categories.#", "1"),
resource.TestCheckResourceAttrSet(resourceName, "categories.2228745532.name"),
resource.TestCheckResourceAttrSet(resourceName, "categories.2228745532.value"),
resource.TestCheckResourceAttr(resourceName, "categories.2228745532.name", "Environment"),
resource.TestCheckResourceAttr(resourceName, "categories.2228745532.value", "Production"),
),
},
{
Config: testAccNutanixSubnetConfigWithCategoryUpdate(r),
Check: resource.ComposeTestCheckFunc(
testAccCheckNutanixSubnetExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "categories.%", "1"),
resource.TestCheckResourceAttr(resourceName, "categories.Environment", "Staging"),
resource.TestCheckResourceAttr(resourceName, "categories.#", "1"),
resource.TestCheckResourceAttrSet(resourceName, "categories.2940305446.name"),
resource.TestCheckResourceAttrSet(resourceName, "categories.2940305446.value"),
resource.TestCheckResourceAttr(resourceName, "categories.2940305446.name", "Environment"),
resource.TestCheckResourceAttr(resourceName, "categories.2940305446.value", "Staging"),
),
},
{
Expand Down Expand Up @@ -163,10 +171,33 @@ func testAccCheckNutanixSubnetExists(n string) resource.TestCheckFunc {
if !ok {
return fmt.Errorf("not found: %s", n)
}

pretty, _ := json.MarshalIndent(rs, "", " ")
fmt.Print("\n\n[DEBUG] State of Subnet", string(pretty))

if rs.Primary.ID == "" {
return fmt.Errorf("no ID is set")
}

return nil
}
}

func testAccCheckNutanixCategories(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("no ID is set")
}

if val, ok := rs.Primary.Attributes["categories.2228745532.name"]; !ok || val == "" {
return fmt.Errorf("%s: manual Attribute '%s' expected to be set", n, "categories.2228745532.name")
}

return nil
}
}
Expand Down Expand Up @@ -296,8 +327,9 @@ resource "nutanix_subnet" "acctest-managed-categories" {
dhcp_domain_name_server_list = ["8.8.8.8", "4.2.2.2"]
dhcp_domain_search_list = ["terraform.nutanix.com", "terraform.unit.test.com"]

categories = {
Environment = "Production"
categories {
name = "Environment"
value = "Production"
}
}
`, r)
Expand Down Expand Up @@ -337,8 +369,9 @@ resource "nutanix_subnet" "acctest-managed-categories" {
dhcp_domain_name_server_list = ["8.8.8.8", "4.2.2.2"]
dhcp_domain_search_list = ["terraform.nutanix.com", "terraform.unit.test.com"]

categories = {
Environment = "Staging"
categories {
name = "Environment"
value = "Staging"
}
}
`, r)
Expand Down
3 changes: 1 addition & 2 deletions nutanix/resource_nutanix_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1014,8 +1014,7 @@ func resourceNutanixVirtualMachineUpdate(d *schema.ResourceData, meta interface{
}

if d.HasChange("categories") {
catl := d.Get("categories").(map[string]interface{})
metadata.Categories = expandCategories(catl)
metadata.Categories = expandCategories(d.Get("categories"))
hotPlugChange = false
}
metadata.OwnerReference = response.Metadata.OwnerReference
Expand Down
Loading