Skip to content

Commit

Permalink
Add support for role importing
Browse files Browse the repository at this point in the history
  • Loading branch information
YoanaStoyanova committed Jan 19, 2023
1 parent e5ec802 commit 05aae6a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
3 changes: 3 additions & 0 deletions vsphere/resource_vsphere_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func resourceVsphereRole() *schema.Resource {
Update: resourceRoleUpdate,
Delete: resourceRoleDelete,
Schema: sch,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}

Expand Down
84 changes: 83 additions & 1 deletion vsphere/resource_vsphere_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const Privilege2 = "Alarm.Create"
const Privilege3 = "Datacenter.Create"
const Privilege4 = "Datacenter.Move"

func TestAccResourceVsphereRole_basic(t *testing.T) {
func TestAccResourceVsphereRole_createRole(t *testing.T) {
roleName := "terraform_role" + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
resource.Test(t, resource.TestCase{
PreCheck: func() {
Expand All @@ -28,6 +28,66 @@ func TestAccResourceVsphereRole_basic(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccResourceVsphereRoleConfigBasic(roleName),
Check: resource.ComposeTestCheckFunc(
testAccResourceVsphereRoleCheckExists(true),
resource.TestCheckResourceAttr("vsphere_role."+RoleResource, "name", roleName),
resource.TestCheckResourceAttr("vsphere_role."+RoleResource, "role_privileges.0", Privilege1),
resource.TestCheckResourceAttr("vsphere_role."+RoleResource, "role_privileges.1", Privilege2),
),
},
{
ResourceName: "vsphere_role." + RoleResource,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccResourceVsphereRole_addPrivilege(t *testing.T) {
roleName := "terraform_role" + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccResourceVsphereRoleCheckExists(false),
Steps: []resource.TestStep{
{
Config: testAccResourceVsphereRoleConfigBasic(roleName),
Check: resource.ComposeTestCheckFunc(
testAccResourceVsphereRoleCheckExists(true),
resource.TestCheckResourceAttr("vsphere_role."+RoleResource, "name", roleName),
resource.TestCheckResourceAttr("vsphere_role."+RoleResource, "role_privileges.0", Privilege1),
resource.TestCheckResourceAttr("vsphere_role."+RoleResource, "role_privileges.1", Privilege2),
),
},
{
Config: testAccResourceVsphereRoleConfigAdditionalPrivileges(roleName),
Check: resource.ComposeTestCheckFunc(
testAccResourceVsphereRoleCheckExists(true),
resource.TestCheckResourceAttr("vsphere_role."+RoleResource, "name", roleName),
resource.TestCheckResourceAttr("vsphere_role."+RoleResource, "role_privileges.0", Privilege1),
resource.TestCheckResourceAttr("vsphere_role."+RoleResource, "role_privileges.1", Privilege2),
resource.TestCheckResourceAttr("vsphere_role."+RoleResource, "role_privileges.2", Privilege3),
resource.TestCheckResourceAttr("vsphere_role."+RoleResource, "role_privileges.3", Privilege4),
),
},
},
})
}

func TestAccResourceVsphereRole_removePrivilege(t *testing.T) {
roleName := "terraform_role" + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccResourceVsphereRoleCheckExists(false),
Steps: []resource.TestStep{
{
Config: testAccResourceVsphereRoleConfigAdditionalPrivileges(roleName),
Check: resource.ComposeTestCheckFunc(
testAccResourceVsphereRoleCheckExists(true),
resource.TestCheckResourceAttr("vsphere_role."+RoleResource, "name", roleName),
Expand All @@ -37,6 +97,15 @@ func TestAccResourceVsphereRole_basic(t *testing.T) {
resource.TestCheckResourceAttr("vsphere_role."+RoleResource, "role_privileges.3", Privilege4),
),
},
{
Config: testAccResourceVsphereRoleConfigAdditionalPrivileges(roleName),
Check: resource.ComposeTestCheckFunc(
testAccResourceVsphereRoleCheckExists(true),
resource.TestCheckResourceAttr("vsphere_role."+RoleResource, "name", roleName),
resource.TestCheckResourceAttr("vsphere_role."+RoleResource, "role_privileges.0", Privilege1),
resource.TestCheckResourceAttr("vsphere_role."+RoleResource, "role_privileges.1", Privilege2),
),
},
},
})
}
Expand All @@ -62,6 +131,19 @@ func testAccResourceVsphereRoleConfigBasic(roleName string) string {
return fmt.Sprintf(`
resource "vsphere_role" "%s" {
name = "%s"
role_privileges = ["%s", "%s"]
}
`, RoleResource,
roleName,
Privilege1,
Privilege2,
)
}

func testAccResourceVsphereRoleConfigAdditionalPrivileges(roleName string) string {
return fmt.Sprintf(`
resource "vsphere_role" "%s" {
name = "%s"
role_privileges = ["%s", "%s","%s","%s"]
}
`, RoleResource,
Expand Down

0 comments on commit 05aae6a

Please sign in to comment.