Skip to content

Commit

Permalink
user-idp
Browse files Browse the repository at this point in the history
  • Loading branch information
kosta709 committed Jul 25, 2020
1 parent afaccea commit 11f52ba
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 76 deletions.
6 changes: 5 additions & 1 deletion client/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client
import (
"errors"
"fmt"
"log"
)

type Credentials struct {
Expand All @@ -14,6 +15,8 @@ type Login struct {
PersonalGit bool `json:"personalGit,omitempty"`
Permissions []string `json:"permissions,omitempty"`
IDP IDP `json:"idp,omitempty"`
Idp_ID string `json:"idp_id,omitempty"`
Sso bool `json:"sso,omitempty"`
}

type ShortProfile struct {
Expand Down Expand Up @@ -180,7 +183,8 @@ func (client *Client) GetAllUsers() (*[]User, error) {
}

var users []User

respStr := string(resp)
log.Printf("[INFO] GetAllUsers resp: %s", respStr)
err = DecodeResponseInto(resp, &users)
if err != nil {
return nil, err
Expand Down
25 changes: 17 additions & 8 deletions codefresh/data_idp.go → codefresh/resource_idp.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func IdpSchema() map[string]*schema.Schema {
Type: schema.TypeString,
Optional: true,
},
"client_name": {
Type: schema.TypeString,
Optional: true,
},
"display_name": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -61,14 +65,14 @@ func IdpSchema() map[string]*schema.Schema {
Computed: true,
},
"scopes": {
Type: schema.TypeList,
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"accounts": {
Type: schema.TypeList,
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
Expand All @@ -87,16 +91,20 @@ func dataSourceIdpRead(d *schema.ResourceData, meta interface{}) error {
}

_id, _idOk := d.GetOk("_id")
displayName, displayNameOk := d.GetOk("displayName")
clientType, clientTypeOk := d.GetOk("clientType")
clientName, clientNameOk := d.GetOk("client_name")
displayName, displayNameOk := d.GetOk("display_name")
clientType, clientTypeOk := d.GetOk("client_type")

if !(_idOk || displayNameOk || clientTypeOk) {
return fmt.Errorf("[EROOR] Idp data_source - no parameters specified")
if !(_idOk || clientNameOk || displayNameOk || clientTypeOk) {
return fmt.Errorf("[ERROR] data.codefresh_idp - no parameters specified")
}
for _, idp := range *idps {
if _idOk && _id.(string) != idp.ID {
if clientNameOk && clientName.(string) != idp.ClientName {
continue
}
if _idOk && _id.(string) != idp.ID {
continue
}
if displayNameOk && displayName.(string) != idp.DisplayName {
continue
}
Expand Down Expand Up @@ -150,4 +158,5 @@ func mapDataIdpToResource(idp cfClient.IDP, d *schema.ResourceData) error {
//d.Set("userProfileURL", idp.UserProfileURL) // string `json:"userProfileURL,omitempty"`

return nil
}
}

20 changes: 10 additions & 10 deletions codefresh/resource_idp_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ func resourceIDPAccounts() *schema.Resource {
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"idp": {
"idp_id": {
Type: schema.TypeString,
Required: true,
},
"accounts": {
"account_ids": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{
Expand All @@ -34,17 +34,17 @@ func resourceAccountIDPCreate(d *schema.ResourceData, meta interface{}) error {

client := meta.(*cfClient.Client)

accounts := convertStringArr(d.Get("accounts").(*schema.Set).List())
accountIds := convertStringArr(d.Get("account_ids").(*schema.Set).List())

idpName := d.Get("idp").(string)
idpID := d.Get("idp_id").(string)

idp, err := client.GetIdpByName(idpName)
idp, err := client.GetIdpByID(idpID)
if err != nil {
return err
}

for _, account := range accounts {
client.AddAccountToIDP(account, idp.ID)
for _, accountID := range accountIds {
client.AddAccountToIDP(accountID, idp.ID)
}

d.SetId(idp.ID)
Expand All @@ -67,12 +67,12 @@ func resourceAccountIDPRead(d *schema.ResourceData, meta interface{}) error {
return err
}

err = d.Set("idp", idp.ClientName)
err = d.Set("idp_id", idp.ID)
if err != nil {
return err
}

err = d.Set("accounts", idp.Accounts)
err = d.Set("account_ids", idp.Accounts)
if err != nil {
return err
}
Expand All @@ -99,7 +99,7 @@ func resourceAccountIDPUpdate(d *schema.ResourceData, meta interface{}) error {

existingAccounts := idp.Accounts

desiredAccounts := convertStringArr(d.Get("accounts").(*schema.Set).List())
desiredAccounts := convertStringArr(d.Get("account_ids").(*schema.Set).List())

for _, account := range desiredAccounts {
if ok := cfClient.FindInSlice(existingAccounts, account); !ok {
Expand Down
113 changes: 60 additions & 53 deletions codefresh/resource_user.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package codefresh

import (
"fmt"
"log"
cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -38,6 +38,7 @@ func resourceUser() *schema.Resource {
"personal": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"first_name": {
Expand Down Expand Up @@ -87,40 +88,33 @@ func resourceUser() *schema.Resource {
Computed: true,
},
"login": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"credentials": {
Type: schema.TypeList,
// "credentials": {
// Type: schema.TypeList,
// Optional: true,
// MaxItems: 1,
// Elem: &schema.Resource{
// Schema: map[string]*schema.Schema{
// "permissions": {
// Type: schema.TypeList,
// Optional: true,
// Elem: &schema.Schema{
// Type: schema.TypeString,
// },
// },
// },
// },
// },
"idp_id": {
Type: schema.TypeString,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"permissions": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"idp": {
Type: schema.TypeList,
"sso": {
Type: schema.TypeBool,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"idp_id": {
Type: schema.TypeString,
Optional: true,
},
"client_type": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
},
Expand Down Expand Up @@ -247,16 +241,12 @@ func flattenUserLogins(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["credentials"] = []map[string]interface{}{
// {"permissions": login.Credentials.Permissions},
// }

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

res[i] = m
}
Expand Down Expand Up @@ -287,22 +277,39 @@ func mapResourceToUser(d *schema.ResourceData) *cfClient.NewUser {
}
}

logins := d.Get("login").([]interface{})

for idx := range logins {

permissions := convertStringArr(d.Get(fmt.Sprintf("login.%v.credentials.0.permissions", idx)).([]interface{}))
login := cfClient.Login{
Credentials: cfClient.Credentials{
Permissions: permissions,
},
IDP: cfClient.IDP{
ID: d.Get(fmt.Sprintf("login.%v.idp.0.idp_id", idx)).(string),
ClientType: d.Get(fmt.Sprintf("login.%v.idp.0.client_type", idx)).(string),
},
}
user.Logins = append(user.Logins, login)
if logins, ok := d.GetOk("login"); ok {
loginsList := logins.(*schema.Set).List()
for _, loginDataI := range loginsList {
if loginData, isMap := loginDataI.(map[string]interface{}); isMap {
idpID := loginData["idp_id"].(string)
login := cfClient.Login{
// Credentials: cfClient.Credentials{
// Permissions: loginData.Get("credentials.permissions").([]string),
// },
IDP: cfClient.IDP{
ID: idpID,
},
Sso: loginData["sso"].(bool),
}
user.Logins = append(user.Logins, login)
log.Printf("[DEBUG] login = %v", login)
}
}
}
// logins := d.Get("login").(*schema.Set)

// for idx := range logins {

// permissions := convertStringArr(d.Get(fmt.Sprintf("login.%v.credentials.0.permissions", idx)).([]interface{}))
// login := cfClient.Login{
// Credentials: cfClient.Credentials{
// Permissions: permissions,
// },
// Idp: d.Get(fmt.Sprintf("login.%v.idp_id", idx)).(string),
// Sso: d.Get(fmt.Sprintf("login.%v.sso", idx)).(bool),
// }
// user.Logins = append(user.Logins, login)
// }

return user
}
12 changes: 8 additions & 4 deletions docs/resources/user.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ resource "codefresh_user" "new" {
]
login {
idp {
idp_id = <IDP ID>
client_type = "azure"
}
idp_id = data.codefresh_idps.idp_azure.id
sso = true
}
login {
idp_id = data.codefresh_idps.local.id
//sso = false
}
personal {
first_name = "John"
Expand Down

0 comments on commit 11f52ba

Please sign in to comment.