Skip to content

Commit

Permalink
Refactored schema for users
Browse files Browse the repository at this point in the history
  • Loading branch information
jbristowe committed Nov 16, 2020
1 parent 950fcc6 commit 37414f1
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 102 deletions.
61 changes: 61 additions & 0 deletions octopusdeploy/schema_identity.go
@@ -0,0 +1,61 @@
package octopusdeploy

import (
"github.com/OctopusDeploy/go-octopusdeploy/octopusdeploy"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func expandIdentities(identities []interface{}) []octopusdeploy.Identity {
expandedIdentities := make([]octopusdeploy.Identity, 0, len(identities))
for _, identity := range identities {
if identity != nil {
rawIdentity := identity.(map[string]interface{})

identityProviderName := ""
if rawIdentity["provider"] != nil {
identityProviderName = rawIdentity["provider"].(string)
}

i := octopusdeploy.Identity{
IdentityProviderName: identityProviderName,
Claims: expandIdentityClaims(rawIdentity["claim"].(*schema.Set).List()),
}
expandedIdentities = append(expandedIdentities, i)
}
}
return expandedIdentities
}

func flattenIdentities(identities []octopusdeploy.Identity) []interface{} {
if identities == nil {
return nil
}

flattenedIdentities := make([]interface{}, len(identities))
for i, identity := range identities {
rawIdentity := map[string]interface{}{
"provider": identity.IdentityProviderName,
}
if identity.Claims != nil {
rawIdentity["claim"] = flattenIdentityClaims(identity.Claims)
}

flattenedIdentities[i] = rawIdentity
}

return flattenedIdentities
}

func getIdentitySchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"provider": {
Optional: true,
Type: schema.TypeString,
},
"claim": {
Optional: true,
Elem: &schema.Resource{Schema: getIdentityClaimSchema()},
Type: schema.TypeSet,
},
}
}
55 changes: 55 additions & 0 deletions octopusdeploy/schema_identity_claim.go
@@ -0,0 +1,55 @@
package octopusdeploy

import (
"github.com/OctopusDeploy/go-octopusdeploy/octopusdeploy"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func expandIdentityClaims(claims []interface{}) map[string]octopusdeploy.IdentityClaim {
expandedClaims := make(map[string]octopusdeploy.IdentityClaim, len(claims))
for _, claim := range claims {
claimMap := claim.(map[string]interface{})
name := claimMap["name"].(string)
identityClaim := octopusdeploy.IdentityClaim{
IsIdentifyingClaim: claimMap["is_identifying_claim"].(bool),
Value: claimMap["value"].(string),
}
expandedClaims[name] = identityClaim
}
return expandedClaims
}

func flattenIdentityClaims(identityClaims map[string]octopusdeploy.IdentityClaim) []interface{} {
if identityClaims == nil {
return nil
}

flattenedIdentityClaims := []interface{}{}
for key, identityClaim := range identityClaims {
rawIdentityClaim := map[string]interface{}{
"is_identifying_claim": identityClaim.IsIdentifyingClaim,
"name": key,
"value": identityClaim.Value,
}
flattenedIdentityClaims = append(flattenedIdentityClaims, rawIdentityClaim)
}

return flattenedIdentityClaims
}

func getIdentityClaimSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"name": {
Required: true,
Type: schema.TypeString,
},
"is_identifying_claim": {
Required: true,
Type: schema.TypeBool,
},
"value": {
Required: true,
Type: schema.TypeString,
},
}
}
104 changes: 2 additions & 102 deletions octopusdeploy/schema_user.go
Expand Up @@ -41,79 +41,6 @@ func expandUser(d *schema.ResourceData) *octopusdeploy.User {
return user
}

func expandClaims(claims []interface{}) map[string]octopusdeploy.IdentityClaim {
expandedClaims := make(map[string]octopusdeploy.IdentityClaim, len(claims))
for _, claim := range claims {
claimMap := claim.(map[string]interface{})
name := claimMap["name"].(string)
identityClaim := octopusdeploy.IdentityClaim{
IsIdentifyingClaim: claimMap["is_identifying_claim"].(bool),
Value: claimMap["value"].(string),
}
expandedClaims[name] = identityClaim
}
return expandedClaims
}

func expandIdentities(identities []interface{}) []octopusdeploy.Identity {
expandedIdentities := make([]octopusdeploy.Identity, 0, len(identities))
for _, identity := range identities {
if identity != nil {
rawIdentity := identity.(map[string]interface{})

identityProviderName := ""
if rawIdentity["provider"] != nil {
identityProviderName = rawIdentity["provider"].(string)
}

i := octopusdeploy.Identity{
IdentityProviderName: identityProviderName,
Claims: expandClaims(rawIdentity["claim"].(*schema.Set).List()),
}
expandedIdentities = append(expandedIdentities, i)
}
}
return expandedIdentities
}

func flattenIdentityClaims(identityClaims map[string]octopusdeploy.IdentityClaim) []interface{} {
if identityClaims == nil {
return nil
}

flattenedIdentityClaims := []interface{}{}
for key, identityClaim := range identityClaims {
rawIdentityClaim := map[string]interface{}{
"is_identifying_claim": identityClaim.IsIdentifyingClaim,
"name": key,
"value": identityClaim.Value,
}
flattenedIdentityClaims = append(flattenedIdentityClaims, rawIdentityClaim)
}

return flattenedIdentityClaims
}

func flattenIdentities(identities []octopusdeploy.Identity) []interface{} {
if identities == nil {
return nil
}

flattenedIdentities := make([]interface{}, len(identities))
for i, identity := range identities {
rawIdentity := map[string]interface{}{
"provider": identity.IdentityProviderName,
}
if identity.Claims != nil {
rawIdentity["claim"] = flattenIdentityClaims(identity.Claims)
}

flattenedIdentities[i] = rawIdentity
}

return flattenedIdentities
}

func flattenUser(ctx context.Context, d *schema.ResourceData, user *octopusdeploy.User) {
d.Set("can_password_be_edited", user.CanPasswordBeEdited)
d.Set("display_name", user.DisplayName)
Expand Down Expand Up @@ -143,35 +70,8 @@ func getUserSchema() map[string]*schema.Schema {
},
"identity": {
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"provider": {
Optional: true,
Type: schema.TypeString,
},
"claim": {
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Required: true,
Type: schema.TypeString,
},
"is_identifying_claim": {
Required: true,
Type: schema.TypeBool,
},
"value": {
Required: true,
Type: schema.TypeString,
},
},
},
Type: schema.TypeSet,
},
},
},
Type: schema.TypeSet,
Elem: &schema.Resource{Schema: getIdentitySchema()},
Type: schema.TypeSet,
},
"is_active": {
Optional: true,
Expand Down

0 comments on commit 37414f1

Please sign in to comment.