Skip to content

Commit

Permalink
Added user implementation and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jbristowe committed Nov 7, 2020
1 parent d6ef7a4 commit 717675c
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 4 deletions.
43 changes: 39 additions & 4 deletions octopusdeploy/resource_user.go
Expand Up @@ -26,11 +26,11 @@ func resourceUser() *schema.Resource {
Type: schema.TypeString,
},
constIdentities: {
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Type: schema.TypeList,
Elem: &schema.Resource{
Schema: getIdentitiesSchema(),
},
Type: schema.TypeList,
},
constIsActive: {
Optional: true,
Expand Down Expand Up @@ -64,6 +64,15 @@ func resourceUser() *schema.Resource {
}
}

func getIdentitiesSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
constIdentityProviderName: {
Optional: true,
Type: schema.TypeString,
},
}
}

func buildUser(d *schema.ResourceData) *octopusdeploy.User {
var username string
if v, ok := d.GetOk(constUsername); ok {
Expand All @@ -85,6 +94,10 @@ func buildUser(d *schema.ResourceData) *octopusdeploy.User {
user.EmailAddress = v.(string)
}

if v, ok := d.GetOk(constIdentities); ok {
user.Identities = expandIdentities(v.([]interface{}))
}

if v, ok := d.GetOk(constIsActive); ok {
user.IsActive = v.(bool)
}
Expand Down Expand Up @@ -164,3 +177,25 @@ func resourceUserDelete(ctx context.Context, d *schema.ResourceData, m interface

return nil
}

func expandClaims(claims []interface{}) map[string]octopusdeploy.IdentityClaim {
expandedClaims := make(map[string]octopusdeploy.IdentityClaim, len(claims))
// for _, value := range claims {
// identityClaim := octopusdeploy.IdentityClaim{}
// expandedClaims["email"] = identityClaim
// }
return expandedClaims
}

func expandIdentities(identities []interface{}) []octopusdeploy.Identity {
expandedIdentities := make([]octopusdeploy.Identity, 0, len(identities))
for _, identity := range identities {
rawIdentity := identity.(map[string]interface{})
i := octopusdeploy.Identity{
IdentityProviderName: rawIdentity["IdentityProviderName"].(string),
Claims: expandClaims(rawIdentity["Claims"].([]interface{})),
}
expandedIdentities = append(expandedIdentities, i)
}
return expandedIdentities
}
89 changes: 89 additions & 0 deletions octopusdeploy/resource_user_test.go
@@ -0,0 +1,89 @@
package octopusdeploy

import (
"fmt"
"testing"

"github.com/OctopusDeploy/go-octopusdeploy/octopusdeploy"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestUserBasic(t *testing.T) {
localName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
prefix := constOctopusDeployUser + "." + localName

displayName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
password := acctest.RandStringFromCharSet(20, acctest.CharSetAlpha)
username := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)

resource.Test(t, resource.TestCase{
CheckDestroy: testUserDestroy,
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testUserBasic(localName, displayName, password, username),
Check: resource.ComposeTestCheckFunc(
testUserExists(prefix),
resource.TestCheckResourceAttr(prefix, constDisplayName, displayName),
resource.TestCheckResourceAttr(prefix, constPassword, password),
resource.TestCheckResourceAttr(prefix, constUsername, username),
),
},
},
})
}

func testUserBasic(localName string, displayName string, password string, username string) string {
return fmt.Sprintf(`resource "%s" "%s" {
display_name = "%s"
password = "%s"
username = "%s"
identities = [
{
identity_provider_name = "Octopus ID"
claims = [
{
email = {
value = "bob.smith@example.com"
is_identifying_claim = true
}
dn = {
value = "Bob Smith"
is_identifying_claim = false
}
}
]
}
]
}`, constOctopusDeployUser, localName, displayName, password, username)
}

func testUserExists(prefix string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*octopusdeploy.Client)
userID := s.RootModule().Resources[prefix].Primary.ID
if _, err := client.Users.GetByID(userID); err != nil {
return err
}

return nil
}
}

func testUserDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*octopusdeploy.Client)
for _, rs := range s.RootModule().Resources {
userID := rs.Primary.ID
user, err := client.Users.GetByID(userID)
if err == nil {
if user != nil {
return fmt.Errorf("user (%s) still exists", rs.Primary.ID)
}
}
}

return nil
}

0 comments on commit 717675c

Please sign in to comment.