@@ -31,98 +31,71 @@ const (
3131
3232// getDynamicClient returns a configured unstructured (dynamic) client instance
3333func (ps * RawProviderServer ) getDynamicClient () (dynamic.Interface , error ) {
34- if ps .dynamicClient != nil {
35- return ps .dynamicClient , nil
36- }
3734 if ps .clientConfig == nil {
3835 return nil , fmt .Errorf ("cannot create dynamic client: no client config" )
3936 }
40- dynClient , err := dynamic .NewForConfig (ps .clientConfig )
41- if err != nil {
42- return nil , err
43- }
44- ps .dynamicClient = dynClient
45- return dynClient , nil
37+
38+ return ps .dynamicClient .Get (func () (dynamic.Interface , error ) {
39+ return dynamic .NewForConfig (ps .clientConfig )
40+ })
4641}
4742
4843// getDiscoveryClient returns a configured discovery client instance.
4944func (ps * RawProviderServer ) getDiscoveryClient () (discovery.DiscoveryInterface , error ) {
50- if ps .discoveryClient != nil {
51- return ps .discoveryClient , nil
52- }
5345 if ps .clientConfig == nil {
5446 return nil , fmt .Errorf ("cannot create discovery client: no client config" )
5547 }
56- discoClient , err := discovery .NewDiscoveryClientForConfig (ps .clientConfig )
57- if err != nil {
58- return nil , err
59- }
60- ps .discoveryClient = discoClient
61- return discoClient , nil
48+
49+ return ps .discoveryClient .Get (func () (discovery.DiscoveryInterface , error ) {
50+ return discovery .NewDiscoveryClientForConfig (ps .clientConfig )
51+ })
6252}
6353
6454// getRestMapper returns a RESTMapper client instance
6555func (ps * RawProviderServer ) getRestMapper () (meta.RESTMapper , error ) {
66- if ps .restMapper != nil {
67- return ps .restMapper , nil
68- }
69- dc , err := ps .getDiscoveryClient ()
70- if err != nil {
71- return nil , err
72- }
73-
74- // agr, err := restmapper.GetAPIGroupResources(dc)
75- // if err != nil {
76- // return nil, err
77- // }
78- // mapper := restmapper.NewDeferredDiscoveryRESTMapper(agr)
56+ return ps .restMapper .Get (func () (meta.RESTMapper , error ) {
57+ dc , err := ps .getDiscoveryClient ()
58+ if err != nil {
59+ return nil , err
60+ }
7961
80- cache := memory .NewMemCacheClient (dc )
81- ps . restMapper = restmapper .NewDeferredDiscoveryRESTMapper (cache )
82- return ps . restMapper , nil
62+ cacheClient := memory .NewMemCacheClient (dc )
63+ return restmapper .NewDeferredDiscoveryRESTMapper (cacheClient ), nil
64+ })
8365}
8466
8567// getRestClient returns a raw REST client instance
8668func (ps * RawProviderServer ) getRestClient () (rest.Interface , error ) {
87- if ps .restClient != nil {
88- return ps .restClient , nil
89- }
9069 if ps .clientConfig == nil {
9170 return nil , fmt .Errorf ("cannot create REST client: no client config" )
9271 }
93- restClient , err := rest .UnversionedRESTClientFor (ps .clientConfig )
94- if err != nil {
95- return nil , err
96- }
97- ps .restClient = restClient
98- return restClient , nil
72+
73+ return ps .restClient .Get (func () (rest.Interface , error ) {
74+ return rest .UnversionedRESTClientFor (ps .clientConfig )
75+ })
9976}
10077
10178// getOAPIv2Foundry returns an interface to request tftype types from an OpenAPIv2 spec
10279func (ps * RawProviderServer ) getOAPIv2Foundry () (openapi.Foundry , error ) {
103- if ps .OAPIFoundry != nil {
104- return ps .OAPIFoundry , nil
105- }
106-
107- rc , err := ps .getRestClient ()
108- if err != nil {
109- return nil , fmt .Errorf ("failed get OpenAPI spec: %s" , err )
110- }
111-
112- rq := rc .Verb ("GET" ).Timeout (30 * time .Second ).AbsPath ("openapi" , "v2" )
113- rs , err := rq .DoRaw (context .TODO ())
114- if err != nil {
115- return nil , fmt .Errorf ("failed get OpenAPI spec: %s" , err )
116- }
80+ return ps .OAPIFoundry .Get (func () (openapi.Foundry , error ) {
81+ rc , err := ps .getRestClient ()
82+ if err != nil {
83+ return nil , fmt .Errorf ("failed get OpenAPI spec: %s" , err )
84+ }
11785
118- oapif , err := openapi .NewFoundryFromSpecV2 (rs )
119- if err != nil {
120- return nil , fmt .Errorf ("failed construct OpenAPI foundry: %s" , err )
121- }
86+ rq := rc .Verb ("GET" ).Timeout (30 * time .Second ).AbsPath ("openapi" , "v2" )
87+ rs , err := rq .DoRaw (context .TODO ())
88+ if err != nil {
89+ return nil , fmt .Errorf ("failed get OpenAPI spec: %s" , err )
90+ }
12291
123- ps .OAPIFoundry = oapif
92+ oapif , err := openapi .NewFoundryFromSpecV2 (rs )
93+ if err != nil {
94+ return nil , fmt .Errorf ("failed construct OpenAPI foundry: %s" , err )
95+ }
12496
125- return oapif , nil
97+ return oapif , nil
98+ })
12699}
127100
128101func loggingTransport (rt http.RoundTripper ) http.RoundTripper {
@@ -145,34 +118,38 @@ func (t *loggingRountTripper) RoundTrip(req *http.Request) (*http.Response, erro
145118 return t .lt .RoundTrip (req )
146119}
147120
148- func (ps * RawProviderServer ) checkValidCredentials (ctx context.Context ) (diags []* tfprotov5.Diagnostic ) {
149- rc , err := ps .getRestClient ()
150- if err != nil {
151- diags = append (diags , & tfprotov5.Diagnostic {
152- Severity : tfprotov5 .DiagnosticSeverityError ,
153- Summary : "Failed to construct REST client" ,
154- Detail : err .Error (),
155- })
156- return
157- }
158- vpath := []string {"/apis" }
159- rs := rc .Get ().AbsPath (vpath ... ).Do (ctx )
160- if rs .Error () != nil {
161- switch {
162- case apierrors .IsUnauthorized (rs .Error ()):
121+ func (ps * RawProviderServer ) checkValidCredentials (ctx context.Context ) []* tfprotov5.Diagnostic {
122+ diagnostics , _ := ps .checkValidCredentialsResult .Get (func () (diags []* tfprotov5.Diagnostic , err error ) {
123+ rc , err := ps .getRestClient ()
124+ if err != nil {
163125 diags = append (diags , & tfprotov5.Diagnostic {
164126 Severity : tfprotov5 .DiagnosticSeverityError ,
165- Summary : "Invalid credentials" ,
166- Detail : fmt .Sprintf ("The credentials configured in the provider block are not accepted by the API server. Error: %s\n \n Set TF_LOG=debug and look for '[InvalidClientConfiguration]' in the log to see actual configuration." , rs .Error ().Error ()),
167- })
168- default :
169- diags = append (diags , & tfprotov5.Diagnostic {
170- Severity : tfprotov5 .DiagnosticSeverityError ,
171- Summary : "Invalid configuration for API client" ,
172- Detail : rs .Error ().Error (),
127+ Summary : "Failed to construct REST client" ,
128+ Detail : err .Error (),
173129 })
130+ return
174131 }
175- ps .logger .Debug ("[InvalidClientConfiguration]" , "Config" , dump (ps .clientConfig ))
176- }
177- return
132+ vpath := []string {"/apis" }
133+ rs := rc .Get ().AbsPath (vpath ... ).Do (ctx )
134+ if rs .Error () != nil {
135+ switch {
136+ case apierrors .IsUnauthorized (rs .Error ()):
137+ diags = append (diags , & tfprotov5.Diagnostic {
138+ Severity : tfprotov5 .DiagnosticSeverityError ,
139+ Summary : "Invalid credentials" ,
140+ Detail : fmt .Sprintf ("The credentials configured in the provider block are not accepted by the API server. Error: %s\n \n Set TF_LOG=debug and look for '[InvalidClientConfiguration]' in the log to see actual configuration." , rs .Error ().Error ()),
141+ })
142+ default :
143+ diags = append (diags , & tfprotov5.Diagnostic {
144+ Severity : tfprotov5 .DiagnosticSeverityError ,
145+ Summary : "Invalid configuration for API client" ,
146+ Detail : rs .Error ().Error (),
147+ })
148+ }
149+ ps .logger .Debug ("[InvalidClientConfiguration]" , "Config" , dump (ps .clientConfig ))
150+ }
151+ return
152+ })
153+
154+ return diagnostics
178155}
0 commit comments