@@ -31,98 +31,71 @@ const (
31
31
32
32
// getDynamicClient returns a configured unstructured (dynamic) client instance
33
33
func (ps * RawProviderServer ) getDynamicClient () (dynamic.Interface , error ) {
34
- if ps .dynamicClient != nil {
35
- return ps .dynamicClient , nil
36
- }
37
34
if ps .clientConfig == nil {
38
35
return nil , fmt .Errorf ("cannot create dynamic client: no client config" )
39
36
}
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
+ })
46
41
}
47
42
48
43
// getDiscoveryClient returns a configured discovery client instance.
49
44
func (ps * RawProviderServer ) getDiscoveryClient () (discovery.DiscoveryInterface , error ) {
50
- if ps .discoveryClient != nil {
51
- return ps .discoveryClient , nil
52
- }
53
45
if ps .clientConfig == nil {
54
46
return nil , fmt .Errorf ("cannot create discovery client: no client config" )
55
47
}
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
+ })
62
52
}
63
53
64
54
// getRestMapper returns a RESTMapper client instance
65
55
func (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
+ }
79
61
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
+ })
83
65
}
84
66
85
67
// getRestClient returns a raw REST client instance
86
68
func (ps * RawProviderServer ) getRestClient () (rest.Interface , error ) {
87
- if ps .restClient != nil {
88
- return ps .restClient , nil
89
- }
90
69
if ps .clientConfig == nil {
91
70
return nil , fmt .Errorf ("cannot create REST client: no client config" )
92
71
}
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
+ })
99
76
}
100
77
101
78
// getOAPIv2Foundry returns an interface to request tftype types from an OpenAPIv2 spec
102
79
func (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
+ }
117
85
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
+ }
122
91
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
+ }
124
96
125
- return oapif , nil
97
+ return oapif , nil
98
+ })
126
99
}
127
100
128
101
func loggingTransport (rt http.RoundTripper ) http.RoundTripper {
@@ -145,34 +118,38 @@ func (t *loggingRountTripper) RoundTrip(req *http.Request) (*http.Response, erro
145
118
return t .lt .RoundTrip (req )
146
119
}
147
120
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 {
163
125
diags = append (diags , & tfprotov5.Diagnostic {
164
126
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 (),
173
129
})
130
+ return
174
131
}
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
178
155
}
0 commit comments