Skip to content

Commit

Permalink
Update data users
Browse files Browse the repository at this point in the history
  • Loading branch information
palson-cf committed Jul 15, 2020
1 parent e110d81 commit 76379ac
Showing 1 changed file with 101 additions and 95 deletions.
196 changes: 101 additions & 95 deletions codefresh/data_users.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package codefresh

import (
"errors"
"fmt"

"time"
cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
Expand All @@ -13,97 +11,110 @@ func dataSourceUsers() *schema.Resource {
Read: dataSourceUsersRead,

Schema: map[string]*schema.Schema{
"user_name": {
Type: schema.TypeString,
Computed: true,
},
"email": {
Type: schema.TypeString,
Required: true,
},
"personal": {

"users": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"first_name": {
Type: schema.TypeString,
Optional: true,
},
"last_name": {
Type: schema.TypeString,
Optional: true,
},
"company_name": {
"user_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"phone_number": {
"email": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"country": {
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
},
},
"short_profile": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"user_name": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
"roles": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"logins": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"credentials": {
"personal": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"permissions": {
Type: schema.TypeSet,
"first_name": {
Type: schema.TypeString,
Optional: true,
},
"last_name": {
Type: schema.TypeString,
Optional: true,
},
"company_name": {
Type: schema.TypeString,
Optional: true,
},
"phone_number": {
Type: schema.TypeString,
Optional: true,
},
"country": {
Type: schema.TypeString,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"idp": {
"short_profile": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
"user_name": {
Type: schema.TypeString,
Optional: true,
},
"client_type": {
Type: schema.TypeString,
},
},
},
"roles": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"logins": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"credentials": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"permissions": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"idp": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
},
"client_type": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
},
Expand All @@ -124,39 +135,34 @@ func dataSourceUsersRead(d *schema.ResourceData, meta interface{}) error {
return err
}

email := d.Get("email").(string)
err = mapDataUsersToResource(*users, d)

for _, user := range *users {
if user.Email == email {
err = mapDataUsersToResource(user, d)
if err != nil {
return err
}
}
}

if d.Id() == "" {
return errors.New(fmt.Sprintf("[EROOR] User %s wasn't found", email))
}
d.SetId(time.Now().UTC().String())

return nil
}

func mapDataUsersToResource(user cfClient.User, d *schema.ResourceData) error {
func mapDataUsersToResource(users []cfClient.User, d *schema.ResourceData) error {

var res = make([]map[string]interface{}, len(users))
for i, user := range users {
m := make(map[string]interface{})
m["user_name"] = user.UserName
m["email"] = user.Email
m["status"] = user.Status
if user.Personal != nil {
m["personal"] = flattenPersonal(user.Personal)
}
m["short_profile"] = []map[string]interface{}{
{"user_name": user.ShortProfile.UserName},}
m["roles"] = user.Roles
m["logins"] = flattenLogins(&user.Logins)
m["id"] = user.ID

d.SetId(user.ID)
d.Set("user_name", user.UserName)
d.Set("email", user.Email)
d.Set("status", user.Status)
if user.Personal != nil {
d.Set("personal", flattenPersonal(user.Personal))
res[i] = m
}
d.Set("short_profile",
[]map[string]interface{}{
{"user_name": user.ShortProfile.UserName},
})
d.Set("roles", user.Roles)
d.Set("logins", flattenLogins(&user.Logins))

d.Set("users", res)

return nil
}
Expand Down

0 comments on commit 76379ac

Please sign in to comment.