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,47 @@ 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 , error ) {
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
+ _ , err := fmt .Fprintln (w , "Variable\t AvailableSources\t Using" )
96
+ if err != nil {
97
+ return false , "" , err
98
+ }
99
+
100
+ for _ , variable := range variables {
101
+ values , ok := m .credentialsSource .Variables [variable ]
102
+ if ok {
103
+ if len (values ) > 1 {
104
+ _ , err := fmt .Fprintf (w , "%s\t %s\t %s\n " , variable , strings .Join (values , ", " ), values [len (values )- 1 ])
105
+ if err != nil {
106
+ return false , "" , err
107
+ }
108
+
109
+ multiple = true
110
+ }
111
+ }
112
+ }
113
+
114
+ if err := w .Flush (); err != nil {
115
+ return false , "" , err
116
+ }
117
+
118
+ return multiple , buf .String (), nil
119
+ }
120
+
76
121
type Config struct {
77
122
ProviderSchema * schema.ResourceData
78
123
HTTPClient * http.Client
@@ -270,29 +315,39 @@ func GetCredentialsSource(defaultZoneProfile, activeProfile, providerProfile, en
270
315
},
271
316
}
272
317
credentialsSource := & CredentialsSource {}
318
+ credentialsSource .Variables = map [string ][]string {}
273
319
274
320
for _ , pair := range profilesInOrder {
275
321
source := pair .Source
276
322
profile := pair .Profile
277
323
278
324
if profile .AccessKey != nil {
279
325
credentialsSource .AccessKey = source
326
+ credentialsSource .Variables [scw .ScwAccessKeyEnv ] = append (credentialsSource .Variables [scw .ScwAccessKeyEnv ], source )
280
327
}
281
328
282
329
if profile .SecretKey != nil {
283
330
credentialsSource .SecretKey = source
331
+ credentialsSource .Variables [scw .ScwSecretKeyEnv ] = append (credentialsSource .Variables [scw .ScwSecretKeyEnv ], source )
284
332
}
285
333
286
334
if profile .DefaultProjectID != nil {
287
335
credentialsSource .ProjectID = source
336
+ credentialsSource .Variables [scw .ScwDefaultProjectIDEnv ] = append (credentialsSource .Variables [scw .ScwDefaultProjectIDEnv ], source )
288
337
}
289
338
290
339
if profile .DefaultRegion != nil {
291
340
credentialsSource .DefaultRegion = source
341
+ if source != CredentialsSourceDefault {
342
+ credentialsSource .Variables [scw .ScwDefaultRegionEnv ] = append (credentialsSource .Variables [scw .ScwDefaultRegionEnv ], source )
343
+ }
292
344
}
293
345
294
346
if profile .DefaultZone != nil {
295
347
credentialsSource .DefaultZone = source
348
+ if source != CredentialsSourceDefault {
349
+ credentialsSource .Variables [scw .ScwDefaultZoneEnv ] = append (credentialsSource .Variables [scw .ScwDefaultZoneEnv ], source )
350
+ }
296
351
}
297
352
}
298
353
0 commit comments