1+ package codefresh
2+
3+ import (
4+ "fmt"
5+ cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
6+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+ )
8+
9+ func dataSourceIdps () * schema.Resource {
10+ return & schema.Resource {
11+ Read : dataSourceIdpRead ,
12+ Schema : IdpSchema (),
13+ }
14+ }
15+
16+ //IdpSchema -
17+ func IdpSchema () map [string ]* schema.Schema {
18+ return map [string ]* schema.Schema {
19+ "_id" : {
20+ Type : schema .TypeString ,
21+ Optional : true ,
22+ },
23+ "display_name" : {
24+ Type : schema .TypeString ,
25+ Optional : true ,
26+ },
27+ "client_type" : {
28+ Type : schema .TypeString ,
29+ Optional : true ,
30+ },
31+ "client_host" : {
32+ Type : schema .TypeString ,
33+ Computed : true ,
34+ },
35+ "access_token" : {
36+ Type : schema .TypeString ,
37+ Computed : true ,
38+ },
39+ "client_id" : {
40+ Type : schema .TypeString ,
41+ Computed : true ,
42+ },
43+ "client_secret" : {
44+ Type : schema .TypeString ,
45+ Computed : true ,
46+ },
47+ "app_id" : {
48+ Type : schema .TypeString ,
49+ Computed : true ,
50+ },
51+ "cookie_key" : {
52+ Type : schema .TypeString ,
53+ Computed : true ,
54+ },
55+ "cookie_iv" : {
56+ Type : schema .TypeString ,
57+ Computed : true ,
58+ },
59+ "tenant" : {
60+ Type : schema .TypeString ,
61+ Computed : true ,
62+ },
63+ "scopes" : {
64+ Type : schema .TypeList ,
65+ Computed : true ,
66+ Elem : & schema.Schema {
67+ Type : schema .TypeString ,
68+ },
69+ },
70+ "accounts" : {
71+ Type : schema .TypeList ,
72+ Computed : true ,
73+ Elem : & schema.Schema {
74+ Type : schema .TypeString ,
75+ },
76+ },
77+ }
78+ }
79+
80+ func dataSourceIdpRead (d * schema.ResourceData , meta interface {}) error {
81+
82+ client := meta .(* cfClient.Client )
83+
84+ idps , err := client .GetIDPs ()
85+ if err != nil {
86+ return err
87+ }
88+
89+ _id , _idOk := d .GetOk ("_id" )
90+ displayName , displayNameOk := d .GetOk ("displayName" )
91+ clientType , clientTypeOk := d .GetOk ("clientType" )
92+
93+ if ! (_idOk || displayNameOk || clientTypeOk ) {
94+ return fmt .Errorf ("[EROOR] Idp data_source - no parameters specified" )
95+ }
96+ for _ , idp := range * idps {
97+ if _idOk && _id .(string ) != idp .ID {
98+ continue
99+ }
100+ if displayNameOk && displayName .(string ) != idp .DisplayName {
101+ continue
102+ }
103+ if clientTypeOk && clientType .(string ) != idp .ClientType {
104+ continue
105+ }
106+ err = mapDataIdpToResource (idp , d )
107+ if err != nil {
108+ return err
109+ }
110+ }
111+
112+ if d .Id () == "" {
113+ return fmt .Errorf ("[EROOR] Idp wasn't found" )
114+ }
115+
116+ return nil
117+ }
118+
119+ func mapDataIdpToResource (idp cfClient.IDP , d * schema.ResourceData ) error {
120+
121+ d .SetId (idp .ID )
122+
123+ d .Set ("access_token" , idp .Access_token )// string `json:"access_token,omitempty"`
124+
125+
126+ d .Set ("accounts" , flattenStringArr (idp .Accounts )) //
127+ //d.Set("apiHost", idp.ApiHost) // string `json:"apiHost,omitempty"`
128+ //d.Set("apiPathPrefix", idp.ApiPathPrefix) // string `json:"apiPathPrefix,omitempty"`
129+ //d.Set("apiURL", idp.ApiURL) // string `json:"apiURL,omitempty"`
130+ //d.Set("appId", idp.AppId) // string `json:"appId,omitempty"`
131+ //d.Set("authURL", idp.AuthURL) // string `json:"authURL,omitempty"`
132+ d .Set ("client_host" , idp .ClientHost ) // string `json:"clientHost,omitempty"`
133+ d .Set ("client_id" , idp .ClientId ) // string `json:"clientId,omitempty"`
134+ d .Set ("client_name" , idp .ClientName ) // string `json:"clientName,omitempty"`
135+ d .Set ("client_secret" ,idp .ClientSecret ) // string `json:"clientSecret,omitempty"`
136+ d .Set ("client_type" , idp .ClientType ) // string `json:"clientType,omitempty"`
137+ d .Set ("cookie_iv" , idp .CookieIv ) // string `json:"cookieIv,omitempty"`
138+ d .Set ("cookie_key" , idp .CookieKey ) // string `json:"cookieKey,omitempty"`
139+ d .Set ("display_name" , idp .DisplayName ) // string `json:"displayName,omitempty"`
140+ d .Set ("_id" , idp .ID ) // string `json:"_id,omitempty"`
141+ //d.Set("IDPLoginUrl", idp.IDPLoginUrl) // string `json:"IDPLoginUrl,omitempty"`
142+ //d.Set("loginUrl", idp.LoginUrl) // string `json:"loginUrl,omitempty"`
143+ //d.Set("redirectUiUrl", idp.RedirectUiUrl) // string `json:"redirectUiUrl,omitempty"`
144+ //d.Set("redirectUrl", idp.RedirectUrl) // string `json:"redirectUrl,omitempty"`
145+ //d.Set("refreshTokenURL", idp.RefreshTokenURL) // string `json:"refreshTokenURL,omitempty"`
146+ d .Set ("scopes" , flattenStringArr (idp .Scopes )) // []string `json:"scopes,omitempty"`
147+ d .Set ("tenant" , idp .Tenant ) // string `json:"tenant,omitempty"`
148+ //d.Set("tokenSecret", idp.TokenSecret) // string `json:"tokenSecret,omitempty"`
149+ //d.Set("tokenURL", idp.TokenURL) // string `json:"tokenURL,omitempty"`
150+ //d.Set("userProfileURL", idp.UserProfileURL) // string `json:"userProfileURL,omitempty"`
151+
152+ return nil
153+ }
0 commit comments