Skip to content

Commit 11f52ba

Browse files
committed
user-idp
1 parent afaccea commit 11f52ba

File tree

5 files changed

+100
-76
lines changed

5 files changed

+100
-76
lines changed

client/user.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package client
33
import (
44
"errors"
55
"fmt"
6+
"log"
67
)
78

89
type Credentials struct {
@@ -14,6 +15,8 @@ type Login struct {
1415
PersonalGit bool `json:"personalGit,omitempty"`
1516
Permissions []string `json:"permissions,omitempty"`
1617
IDP IDP `json:"idp,omitempty"`
18+
Idp_ID string `json:"idp_id,omitempty"`
19+
Sso bool `json:"sso,omitempty"`
1720
}
1821

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

182185
var users []User
183-
186+
respStr := string(resp)
187+
log.Printf("[INFO] GetAllUsers resp: %s", respStr)
184188
err = DecodeResponseInto(resp, &users)
185189
if err != nil {
186190
return nil, err

codefresh/data_idp.go renamed to codefresh/resource_idp.go

+17-8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ func IdpSchema() map[string]*schema.Schema {
2020
Type: schema.TypeString,
2121
Optional: true,
2222
},
23+
"client_name": {
24+
Type: schema.TypeString,
25+
Optional: true,
26+
},
2327
"display_name": {
2428
Type: schema.TypeString,
2529
Optional: true,
@@ -61,14 +65,14 @@ func IdpSchema() map[string]*schema.Schema {
6165
Computed: true,
6266
},
6367
"scopes": {
64-
Type: schema.TypeList,
68+
Type: schema.TypeSet,
6569
Computed: true,
6670
Elem: &schema.Schema{
6771
Type: schema.TypeString,
6872
},
6973
},
7074
"accounts": {
71-
Type: schema.TypeList,
75+
Type: schema.TypeSet,
7276
Computed: true,
7377
Elem: &schema.Schema{
7478
Type: schema.TypeString,
@@ -87,16 +91,20 @@ func dataSourceIdpRead(d *schema.ResourceData, meta interface{}) error {
8791
}
8892

8993
_id, _idOk := d.GetOk("_id")
90-
displayName, displayNameOk := d.GetOk("displayName")
91-
clientType, clientTypeOk := d.GetOk("clientType")
94+
clientName, clientNameOk := d.GetOk("client_name")
95+
displayName, displayNameOk := d.GetOk("display_name")
96+
clientType, clientTypeOk := d.GetOk("client_type")
9297

93-
if !(_idOk || displayNameOk || clientTypeOk) {
94-
return fmt.Errorf("[EROOR] Idp data_source - no parameters specified")
98+
if !(_idOk || clientNameOk || displayNameOk || clientTypeOk) {
99+
return fmt.Errorf("[ERROR] data.codefresh_idp - no parameters specified")
95100
}
96101
for _, idp := range *idps {
97-
if _idOk && _id.(string) != idp.ID {
102+
if clientNameOk && clientName.(string) != idp.ClientName {
98103
continue
99104
}
105+
if _idOk && _id.(string) != idp.ID {
106+
continue
107+
}
100108
if displayNameOk && displayName.(string) != idp.DisplayName {
101109
continue
102110
}
@@ -150,4 +158,5 @@ func mapDataIdpToResource(idp cfClient.IDP, d *schema.ResourceData) error {
150158
//d.Set("userProfileURL", idp.UserProfileURL) // string `json:"userProfileURL,omitempty"`
151159

152160
return nil
153-
}
161+
}
162+

codefresh/resource_idp_accounts.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ func resourceIDPAccounts() *schema.Resource {
1515
State: schema.ImportStatePassthrough,
1616
},
1717
Schema: map[string]*schema.Schema{
18-
"idp": {
18+
"idp_id": {
1919
Type: schema.TypeString,
2020
Required: true,
2121
},
22-
"accounts": {
22+
"account_ids": {
2323
Type: schema.TypeSet,
2424
Required: true,
2525
Elem: &schema.Schema{
@@ -34,17 +34,17 @@ func resourceAccountIDPCreate(d *schema.ResourceData, meta interface{}) error {
3434

3535
client := meta.(*cfClient.Client)
3636

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

39-
idpName := d.Get("idp").(string)
39+
idpID := d.Get("idp_id").(string)
4040

41-
idp, err := client.GetIdpByName(idpName)
41+
idp, err := client.GetIdpByID(idpID)
4242
if err != nil {
4343
return err
4444
}
4545

46-
for _, account := range accounts {
47-
client.AddAccountToIDP(account, idp.ID)
46+
for _, accountID := range accountIds {
47+
client.AddAccountToIDP(accountID, idp.ID)
4848
}
4949

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

70-
err = d.Set("idp", idp.ClientName)
70+
err = d.Set("idp_id", idp.ID)
7171
if err != nil {
7272
return err
7373
}
7474

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

100100
existingAccounts := idp.Accounts
101101

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

104104
for _, account := range desiredAccounts {
105105
if ok := cfClient.FindInSlice(existingAccounts, account); !ok {

codefresh/resource_user.go

+60-53
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package codefresh
22

33
import (
4-
"fmt"
4+
"log"
55
cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
66
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
77
)
@@ -38,6 +38,7 @@ func resourceUser() *schema.Resource {
3838
"personal": {
3939
Type: schema.TypeList,
4040
Optional: true,
41+
MaxItems: 1,
4142
Elem: &schema.Resource{
4243
Schema: map[string]*schema.Schema{
4344
"first_name": {
@@ -87,40 +88,33 @@ func resourceUser() *schema.Resource {
8788
Computed: true,
8889
},
8990
"login": {
90-
Type: schema.TypeList,
91+
Type: schema.TypeSet,
9192
Optional: true,
9293
Elem: &schema.Resource{
9394
Schema: map[string]*schema.Schema{
94-
"credentials": {
95-
Type: schema.TypeList,
95+
// "credentials": {
96+
// Type: schema.TypeList,
97+
// Optional: true,
98+
// MaxItems: 1,
99+
// Elem: &schema.Resource{
100+
// Schema: map[string]*schema.Schema{
101+
// "permissions": {
102+
// Type: schema.TypeList,
103+
// Optional: true,
104+
// Elem: &schema.Schema{
105+
// Type: schema.TypeString,
106+
// },
107+
// },
108+
// },
109+
// },
110+
// },
111+
"idp_id": {
112+
Type: schema.TypeString,
96113
Optional: true,
97-
Elem: &schema.Resource{
98-
Schema: map[string]*schema.Schema{
99-
"permissions": {
100-
Type: schema.TypeList,
101-
Optional: true,
102-
Elem: &schema.Schema{
103-
Type: schema.TypeString,
104-
},
105-
},
106-
},
107-
},
108114
},
109-
"idp": {
110-
Type: schema.TypeList,
115+
"sso": {
116+
Type: schema.TypeBool,
111117
Optional: true,
112-
Elem: &schema.Resource{
113-
Schema: map[string]*schema.Schema{
114-
"idp_id": {
115-
Type: schema.TypeString,
116-
Optional: true,
117-
},
118-
"client_type": {
119-
Type: schema.TypeString,
120-
Optional: true,
121-
},
122-
},
123-
},
124118
},
125119
},
126120
},
@@ -247,16 +241,12 @@ func flattenUserLogins(logins *[]cfClient.Login) []map[string]interface{} {
247241
var res = make([]map[string]interface{}, len(*logins))
248242
for i, login := range *logins {
249243
m := make(map[string]interface{})
250-
m["credentials"] = []map[string]interface{}{
251-
{"permissions": login.Credentials.Permissions},
252-
}
244+
// m["credentials"] = []map[string]interface{}{
245+
// {"permissions": login.Credentials.Permissions},
246+
// }
253247

254-
m["idp"] = []map[string]interface{}{
255-
{
256-
"idp_id": login.IDP.ID,
257-
"client_type": login.IDP.ClientType,
258-
},
259-
}
248+
m["idp_id"] = login.IDP.ID
249+
m["sso"] = login.Sso
260250

261251
res[i] = m
262252
}
@@ -287,22 +277,39 @@ func mapResourceToUser(d *schema.ResourceData) *cfClient.NewUser {
287277
}
288278
}
289279

290-
logins := d.Get("login").([]interface{})
291-
292-
for idx := range logins {
293-
294-
permissions := convertStringArr(d.Get(fmt.Sprintf("login.%v.credentials.0.permissions", idx)).([]interface{}))
295-
login := cfClient.Login{
296-
Credentials: cfClient.Credentials{
297-
Permissions: permissions,
298-
},
299-
IDP: cfClient.IDP{
300-
ID: d.Get(fmt.Sprintf("login.%v.idp.0.idp_id", idx)).(string),
301-
ClientType: d.Get(fmt.Sprintf("login.%v.idp.0.client_type", idx)).(string),
302-
},
303-
}
304-
user.Logins = append(user.Logins, login)
280+
if logins, ok := d.GetOk("login"); ok {
281+
loginsList := logins.(*schema.Set).List()
282+
for _, loginDataI := range loginsList {
283+
if loginData, isMap := loginDataI.(map[string]interface{}); isMap {
284+
idpID := loginData["idp_id"].(string)
285+
login := cfClient.Login{
286+
// Credentials: cfClient.Credentials{
287+
// Permissions: loginData.Get("credentials.permissions").([]string),
288+
// },
289+
IDP: cfClient.IDP{
290+
ID: idpID,
291+
},
292+
Sso: loginData["sso"].(bool),
293+
}
294+
user.Logins = append(user.Logins, login)
295+
log.Printf("[DEBUG] login = %v", login)
296+
}
297+
}
305298
}
299+
// logins := d.Get("login").(*schema.Set)
300+
301+
// for idx := range logins {
302+
303+
// permissions := convertStringArr(d.Get(fmt.Sprintf("login.%v.credentials.0.permissions", idx)).([]interface{}))
304+
// login := cfClient.Login{
305+
// Credentials: cfClient.Credentials{
306+
// Permissions: permissions,
307+
// },
308+
// Idp: d.Get(fmt.Sprintf("login.%v.idp_id", idx)).(string),
309+
// Sso: d.Get(fmt.Sprintf("login.%v.sso", idx)).(bool),
310+
// }
311+
// user.Logins = append(user.Logins, login)
312+
// }
306313

307314
return user
308315
}

docs/resources/user.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@ resource "codefresh_user" "new" {
3131
]
3232
3333
login {
34-
idp {
35-
idp_id = <IDP ID>
36-
client_type = "azure"
37-
}
34+
idp_id = data.codefresh_idps.idp_azure.id
35+
sso = true
3836
}
37+
38+
login {
39+
idp_id = data.codefresh_idps.local.id
40+
//sso = false
41+
}
42+
3943
4044
personal {
4145
first_name = "John"

0 commit comments

Comments
 (0)