Skip to content

Commit

Permalink
fix: changed schema and type of struct for categories of right hand side
Browse files Browse the repository at this point in the history
  • Loading branch information
Edgar López committed Jan 21, 2021
1 parent bf9be64 commit 8b30d49
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 23 deletions.
6 changes: 3 additions & 3 deletions client/v3/v3_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2055,9 +2055,9 @@ type LeftHandSide struct {

// RightHandSide ...
type RightHandSide struct {
Collection *string `json:"collection,omitempty"`
Categories map[string]string `json:"categories,omitempty"`
UUIDList []string `json:"uuid_list,omitempty"`
Collection *string `json:"collection,omitempty"`
Categories map[string][]string `json:"categories,omitempty"`
UUIDList []string `json:"uuid_list,omitempty"`
}

// AccessControlPolicyStatus ...
Expand Down
38 changes: 36 additions & 2 deletions nutanix/data_source_nutanix_access_control_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,24 @@ func dataSourceNutanixAccessControlPolicies() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"categories": categoriesSchema(),
"categories": {
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"value": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
"uuid_list": {
Type: schema.TypeSet,
Computed: true,
Expand Down Expand Up @@ -241,7 +258,24 @@ func dataSourceNutanixAccessControlPolicies() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"categories": categoriesSchema(),
"categories": {
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"value": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
"uuid_list": {
Type: schema.TypeSet,
Computed: true,
Expand Down
38 changes: 36 additions & 2 deletions nutanix/data_source_nutanix_access_control_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,24 @@ func dataSourceNutanixAccessControlPolicy() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"categories": categoriesSchema(),
"categories": {
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"value": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
"uuid_list": {
Type: schema.TypeSet,
Computed: true,
Expand Down Expand Up @@ -239,7 +256,24 @@ func dataSourceNutanixAccessControlPolicy() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"categories": categoriesSchema(),
"categories": {
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"value": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
"uuid_list": {
Type: schema.TypeSet,
Computed: true,
Expand Down
72 changes: 68 additions & 4 deletions nutanix/resource_nutanix_access_control_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,27 @@ func resourceNutanixAccessControlPolicy() *schema.Resource {
Computed: true,
ValidateFunc: validation.StringInSlice([]string{"ALL"}, false),
},
"categories": categoriesSchema(),
"categories": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"value": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
"uuid_list": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -271,7 +291,27 @@ func resourceNutanixAccessControlPolicy() *schema.Resource {
Computed: true,
ValidateFunc: validation.StringInSlice([]string{"ALL", "SELF_OWNED"}, false),
},
"categories": categoriesSchema(),
"categories": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"value": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
"uuid_list": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -659,7 +699,7 @@ func expandRightHandSide(side map[string]interface{}) v3.RightHandSide {
}
}
if v5, ok := rhd["categories"]; ok {
right.Categories = expandCategories(v5)
right.Categories = expandRightHandsideCategories(v5.([]interface{}))
}
if v5, ok := rhd["uuid_list"]; ok {
right.UUIDList = cast.ToStringSlice(v5.(*schema.Set).List())
Expand Down Expand Up @@ -720,9 +760,33 @@ func flattenRightHandSide(right v3.RightHandSide) []interface{} {
r := make(map[string]interface{})
r["collection"] = utils.StringValue(right.Collection)
r["uuid_list"] = right.UUIDList
r["categories"] = flattenCategories(right.Categories)
r["categories"] = flattenTightHandsideCategories(right.Categories)

rightHand = append(rightHand, r)

return rightHand
}

func expandRightHandsideCategories(categoriesSet []interface{}) map[string][]string {
output := make(map[string][]string)

for _, v := range categoriesSet {
category := v.(map[string]interface{})
output[category["name"].(string)] = cast.ToStringSlice(category["value"].(*schema.Set).List())
}

return output
}

func flattenTightHandsideCategories(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
}
17 changes: 5 additions & 12 deletions nutanix/resource_nutanix_access_control_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ func TestAccNutanixAccessControlPolicy_basic(t *testing.T) {
func TestAccNutanixAccessControlPolicy_WithUser(t *testing.T) {
name := acctest.RandomWithPrefix("accest-access-policy")
description := "Description of my access control policy"
nameUpdated := acctest.RandomWithPrefix("accest-access-policy")
descriptionUpdated := "Description of my access control policy updated"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -69,14 +67,6 @@ func TestAccNutanixAccessControlPolicy_WithUser(t *testing.T) {
resource.TestCheckResourceAttr(resourceAccessPolicy, "description", description),
),
},
{
Config: testAccNutanixAccessControlPolicyConfigWithUser(nameUpdated, descriptionUpdated),
Check: resource.ComposeTestCheckFunc(
testAccCheckNutanixAccessControlPolicyExists(),
resource.TestCheckResourceAttr(resourceAccessPolicy, "name", nameUpdated),
resource.TestCheckResourceAttr(resourceAccessPolicy, "description", descriptionUpdated),
),
},
{
ResourceName: resourceAccessPolicy,
ImportState: true,
Expand Down Expand Up @@ -214,7 +204,7 @@ resource "nutanix_access_control_policy" "test" {
func testAccNutanixAccessControlPolicyConfigWithUser(name, description string) string {
return fmt.Sprintf(`
resource "nutanix_role" "test" {
name = "test role"
name = "test role 2"
description = "description role"
permission_reference_list {
kind = "permission"
Expand All @@ -238,7 +228,10 @@ resource "nutanix_access_control_policy" "test" {
operator = "IN"
left_hand_side = "PROJECT"
right_hand_side {
uuid_list = ["6b004b04-b88d-4aae-8b39-4a8f090200d3"]
categories {
name = "Environment"
value = ["Dev", "Dev1"]
}
}
}
entity_filter_expression_list{
Expand Down

0 comments on commit 8b30d49

Please sign in to comment.