Skip to content

Commit

Permalink
feat: added role name in datasource roles
Browse files Browse the repository at this point in the history
  • Loading branch information
Edgar López committed Nov 23, 2020
1 parent 0b76155 commit 71103b6
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 10 deletions.
52 changes: 45 additions & 7 deletions nutanix/data_source_nutanix_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ func dataSourceNutanixRole() *schema.Resource {
Schema: map[string]*schema.Schema{
"role_id": {
Type: schema.TypeString,
Required: true,
Optional: true,
},
"role_name": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"role_id"},
},
"api_version": {
Type: schema.TypeString,
Expand Down Expand Up @@ -138,18 +143,24 @@ func dataSourceNutanixRoleRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*Client).API

accessID, iok := d.GetOk("role_id")
roleName, rnOk := d.GetOk("role_name")

if !iok {
return fmt.Errorf("please provide `role_id`")
if !iok || rnOk {
return fmt.Errorf("please provide `role_id` or `role_name`")
}

var reqErr error
var err error
var resp *v3.Role

resp, reqErr = conn.V3.GetRole(accessID.(string))
if iok {
resp, err = conn.V3.GetRole(accessID.(string))
}
if rnOk {
resp, err = findRoleByName(conn, roleName.(string))
}

if reqErr != nil {
return reqErr
if err != nil {
return err
}

m, c := setRSEntityMetadata(resp.Metadata)
Expand Down Expand Up @@ -191,3 +202,30 @@ func dataSourceNutanixRoleRead(d *schema.ResourceData, meta interface{}) error {

return nil
}

func findRoleByName(conn *v3.Client, name string) (*v3.Role, error) {
filter := fmt.Sprintf("name==%s", name)
resp, err := conn.V3.ListAllRole(filter)
if err != nil {
return nil, err
}

entities := resp.Entities

found := make([]*v3.Role, 0)
for _, v := range entities {
if *v.Spec.Name == name {
found = append(found, v)
}
}

if len(found) > 1 {
return nil, fmt.Errorf("your query returned more than one result. Please use role_id argument instead")
}

if len(found) == 0 {
return nil, fmt.Errorf("role with the given name, not found")
}

return found[0], nil
}
45 changes: 42 additions & 3 deletions nutanix/data_source_nutanix_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccNutanixRoleDataSource_basic(t *testing.T) {
func TestAccNutanixRoleDataSourceByID_basic(t *testing.T) {
name := acctest.RandomWithPrefix("accest-access-role")
description := "Description of my role"

Expand All @@ -18,7 +18,7 @@ func TestAccNutanixRoleDataSource_basic(t *testing.T) {
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccRoleDataSourceConfig(name, description),
Config: testAccRoleDataSourceConfigByID(name, description),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"data.nutanix_role.test", "name", name),
Expand All @@ -31,7 +31,29 @@ func TestAccNutanixRoleDataSource_basic(t *testing.T) {
})
}

func testAccRoleDataSourceConfig(name, description string) string {
func TestAccNutanixRoleDataSourceByName_basic(t *testing.T) {
name := acctest.RandomWithPrefix("accest-access-role")
description := "Description of my role"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccRoleDataSourceConfigByID(name, description),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"data.nutanix_role.test", "name", name),
resource.TestCheckResourceAttr(
"data.nutanix_role.test", "description", description),
resource.TestCheckResourceAttrSet("data.nutanix_role.test", "name"),
),
},
},
})
}

func testAccRoleDataSourceConfigByID(name, description string) string {
return fmt.Sprintf(`
resource "nutanix_role" "test" {
name = "%[1]s"
Expand All @@ -47,3 +69,20 @@ data "nutanix_role" "test" {
}
`, name, description)
}

func testAccRoleDataSourceConfigByName(name, description string) string {
return fmt.Sprintf(`
resource "nutanix_role" "test" {
name = "%[1]s"
description = "%[2]s"
permission_reference_list {
kind = "permission"
uuid = "2e9988df-47ae-44ae-9114-ada346657b90"
}
}
data "nutanix_role" "test" {
role_name = nutanix_role.test.name
}
`, name, description)
}

0 comments on commit 71103b6

Please sign in to comment.