Skip to content

Commit

Permalink
Split data users to user and users
Browse files Browse the repository at this point in the history
  • Loading branch information
palson-cf committed Jul 15, 2020
1 parent 76379ac commit 5ed40b2
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 142 deletions.
202 changes: 202 additions & 0 deletions codefresh/data_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
package codefresh

import (
"errors"
"fmt"
cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceUser() *schema.Resource {
return &schema.Resource{
Read: dataSourceUserRead,
Schema: *UserSchema(),
}
}

func dataSourceUserRead(d *schema.ResourceData, meta interface{}) error {

client := meta.(*cfClient.Client)

users, err := client.ListUsers()
if err != nil {
return err
}

email := d.Get("email").(string)

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

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

return nil
}

func mapDataUserToResource(user cfClient.User, d *schema.ResourceData) error {

d.SetId(user.ID)
d.Set("user_id", 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))
}
d.Set("short_profile",
[]map[string]interface{}{
{"user_name": user.ShortProfile.UserName},
})
d.Set("roles", user.Roles)
d.Set("logins", flattenLogins(&user.Logins))

return nil
}

func flattenPersonal(personal *cfClient.Personal) []map[string]interface{} {
return []map[string]interface{}{
{
"first_name": personal.FirstName,
"last_name": personal.LastName,
"company_name": personal.CompanyName,
"phone_number": personal.PhoneNumber,
"country": personal.Country,
},
}
}

func flattenLogins(logins *[]cfClient.Login) []map[string]interface{} {

var res = make([]map[string]interface{}, len(*logins))
for i, login := range *logins {
m := make(map[string]interface{})
m["credentials"] = []map[string]interface{}{
{"permissions": login.Credentials.Permissions}}

m["idp"] = []map[string]interface{}{
{
"id": login.IDP.ID,
"client_type": login.IDP.ClientType,
},
}

res[i] = m
}

return res
}

func UserSchema() *map[string]*schema.Schema {
return &map[string]*schema.Schema{
"user_name": {
Type: schema.TypeString,
Computed: true,
},
"email": {
Type: schema.TypeString,
Required: true,
},
"user_id": {
Type: schema.TypeString,
Computed: true,
},
"personal": {
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": {
Type: schema.TypeString,
Optional: true,
},
"phone_number": {
Type: schema.TypeString,
Optional: true,
},
"country": {
Type: schema.TypeString,
Optional: 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": {
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,
},
},
},
},
},
},
},
}
}
146 changes: 4 additions & 142 deletions codefresh/data_users.go
Original file line number Diff line number Diff line change
@@ -1,125 +1,20 @@
package codefresh

import (
"time"
cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"time"
)

func dataSourceUsers() *schema.Resource {
return &schema.Resource{
Read: dataSourceUsersRead,

Schema: map[string]*schema.Schema{

"users": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"user_name": {
Type: schema.TypeString,
Computed: true,
},
"email": {
Type: schema.TypeString,
Computed: true,
},
"id": {
Type: schema.TypeString,
Computed: true,
},
"personal": {
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": {
Type: schema.TypeString,
Optional: true,
},
"phone_number": {
Type: schema.TypeString,
Optional: true,
},
"country": {
Type: schema.TypeString,
Optional: 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": {
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,
},
},
},
},
},
},
},
},
Schema: *UserSchema(),
},
},
},
Expand Down Expand Up @@ -154,10 +49,10 @@ func mapDataUsersToResource(users []cfClient.User, d *schema.ResourceData) error
m["personal"] = flattenPersonal(user.Personal)
}
m["short_profile"] = []map[string]interface{}{
{"user_name": user.ShortProfile.UserName},}
{"user_name": user.ShortProfile.UserName}}
m["roles"] = user.Roles
m["logins"] = flattenLogins(&user.Logins)
m["id"] = user.ID
m["user_id"] = user.ID

res[i] = m
}
Expand All @@ -166,36 +61,3 @@ func mapDataUsersToResource(users []cfClient.User, d *schema.ResourceData) error

return nil
}

func flattenPersonal(personal *cfClient.Personal) []map[string]interface{} {
return []map[string]interface{}{
{
"first_name": personal.FirstName,
"last_name": personal.LastName,
"company_name": personal.CompanyName,
"phone_number": personal.PhoneNumber,
"country": personal.Country,
},
}
}

func flattenLogins(logins *[]cfClient.Login) []map[string]interface{} {

var res = make([]map[string]interface{}, len(*logins))
for i, login := range *logins {
m := make(map[string]interface{})
m["credentials"] = []map[string]interface{}{
{"permissions": login.Credentials.Permissions}}

m["idp"] = []map[string]interface{}{
{
"id": login.IDP.ID,
"client_type": login.IDP.ClientType,
},
}

res[i] = m
}

return res
}
1 change: 1 addition & 0 deletions codefresh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func Provider() terraform.ResourceProvider {
},
DataSourcesMap: map[string]*schema.Resource{
"codefresh_users": dataSourceUsers(),
"codefresh_user": dataSourceUser(),
},
ResourcesMap: map[string]*schema.Resource{
"codefresh_project": resourceProject(),
Expand Down

0 comments on commit 5ed40b2

Please sign in to comment.