1
1
package meta
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"errors"
6
7
"fmt"
7
8
"net/http"
8
9
"os"
10
+ "strings"
11
+ "text/tabwriter"
9
12
10
13
"github.com/hashicorp/terraform-plugin-log/tflog"
11
14
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -24,6 +27,7 @@ const (
24
27
)
25
28
26
29
type CredentialsSource struct {
30
+ Variables map [string ][]string
27
31
AccessKey string
28
32
SecretKey string
29
33
ProjectID string
@@ -73,6 +77,41 @@ func (m Meta) ZoneSource() string {
73
77
return m .credentialsSource .DefaultZone
74
78
}
75
79
80
+ // HasMultipleVariableSources return an informative message during the Provider initialization
81
+ // if there are multiple sources of configuration that could confuse the user
82
+ //
83
+ // Variable AvailableSources Using
84
+ // SCW_ACCESS_KEY Active Profile in config.yaml, Environment variable Environment variable
85
+ // SCW_SECRET_KEY Active Profile in config.yaml, Environment variable Environment variable
86
+ func (m Meta ) HasMultipleVariableSources () (bool , string ) {
87
+ multiple := false
88
+
89
+ variables := []string {scw .ScwAccessKeyEnv , scw .ScwSecretKeyEnv , scw .ScwDefaultProjectIDEnv , scw .ScwDefaultRegionEnv , scw .ScwDefaultZoneEnv }
90
+
91
+ w := new (tabwriter.Writer )
92
+ buf := & bytes.Buffer {}
93
+ w .Init (buf , 0 , 8 , 0 , '\t' , 0 )
94
+
95
+ fmt .Fprintln (w , "Variable\t AvailableSources\t Using" ) //nolint:errcheck
96
+
97
+ for _ , variable := range variables {
98
+ values , ok := m .credentialsSource .Variables [variable ]
99
+ if ok {
100
+ if len (values ) > 1 {
101
+ fmt .Fprintf (w , "%s\t %s\t %s\n " , variable , strings .Join (values , ", " ), values [len (values )- 1 ]) //nolint:errcheck
102
+
103
+ multiple = true
104
+ }
105
+ }
106
+ }
107
+
108
+ if err := w .Flush (); err != nil {
109
+ panic (err ) // lintignore: R009
110
+ }
111
+
112
+ return multiple , buf .String ()
113
+ }
114
+
76
115
type Config struct {
77
116
ProviderSchema * schema.ResourceData
78
117
HTTPClient * http.Client
@@ -270,29 +309,39 @@ func GetCredentialsSource(defaultZoneProfile, activeProfile, providerProfile, en
270
309
},
271
310
}
272
311
credentialsSource := & CredentialsSource {}
312
+ credentialsSource .Variables = map [string ][]string {}
273
313
274
314
for _ , pair := range profilesInOrder {
275
315
source := pair .Source
276
316
profile := pair .Profile
277
317
278
318
if profile .AccessKey != nil {
279
319
credentialsSource .AccessKey = source
320
+ credentialsSource .Variables [scw .ScwAccessKeyEnv ] = append (credentialsSource .Variables [scw .ScwAccessKeyEnv ], source )
280
321
}
281
322
282
323
if profile .SecretKey != nil {
283
324
credentialsSource .SecretKey = source
325
+ credentialsSource .Variables [scw .ScwSecretKeyEnv ] = append (credentialsSource .Variables [scw .ScwSecretKeyEnv ], source )
284
326
}
285
327
286
328
if profile .DefaultProjectID != nil {
287
329
credentialsSource .ProjectID = source
330
+ credentialsSource .Variables [scw .ScwDefaultProjectIDEnv ] = append (credentialsSource .Variables [scw .ScwDefaultProjectIDEnv ], source )
288
331
}
289
332
290
333
if profile .DefaultRegion != nil {
291
334
credentialsSource .DefaultRegion = source
335
+ if source != CredentialsSourceDefault {
336
+ credentialsSource .Variables [scw .ScwDefaultRegionEnv ] = append (credentialsSource .Variables [scw .ScwDefaultRegionEnv ], source )
337
+ }
292
338
}
293
339
294
340
if profile .DefaultZone != nil {
295
341
credentialsSource .DefaultZone = source
342
+ if source != CredentialsSourceDefault {
343
+ credentialsSource .Variables [scw .ScwDefaultZoneEnv ] = append (credentialsSource .Variables [scw .ScwDefaultZoneEnv ], source )
344
+ }
296
345
}
297
346
}
298
347
0 commit comments