11package meta
22
33import (
4+ "bytes"
45 "context"
56 "errors"
67 "fmt"
78 "net/http"
89 "os"
10+ "strings"
11+ "text/tabwriter"
912
1013 "github.com/hashicorp/terraform-plugin-log/tflog"
1114 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -24,6 +27,7 @@ const (
2427)
2528
2629type CredentialsSource struct {
30+ Variables map [string ][]string
2731 AccessKey string
2832 SecretKey string
2933 ProjectID string
@@ -73,6 +77,41 @@ func (m Meta) ZoneSource() string {
7377 return m .credentialsSource .DefaultZone
7478}
7579
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 )
110+ }
111+
112+ return multiple , buf .String ()
113+ }
114+
76115type Config struct {
77116 ProviderSchema * schema.ResourceData
78117 HTTPClient * http.Client
@@ -270,29 +309,39 @@ func GetCredentialsSource(defaultZoneProfile, activeProfile, providerProfile, en
270309 },
271310 }
272311 credentialsSource := & CredentialsSource {}
312+ credentialsSource .Variables = map [string ][]string {}
273313
274314 for _ , pair := range profilesInOrder {
275315 source := pair .Source
276316 profile := pair .Profile
277317
278318 if profile .AccessKey != nil {
279319 credentialsSource .AccessKey = source
320+ credentialsSource .Variables [scw .ScwAccessKeyEnv ] = append (credentialsSource .Variables [scw .ScwAccessKeyEnv ], source )
280321 }
281322
282323 if profile .SecretKey != nil {
283324 credentialsSource .SecretKey = source
325+ credentialsSource .Variables [scw .ScwSecretKeyEnv ] = append (credentialsSource .Variables [scw .ScwSecretKeyEnv ], source )
284326 }
285327
286328 if profile .DefaultProjectID != nil {
287329 credentialsSource .ProjectID = source
330+ credentialsSource .Variables [scw .ScwDefaultProjectIDEnv ] = append (credentialsSource .Variables [scw .ScwDefaultProjectIDEnv ], source )
288331 }
289332
290333 if profile .DefaultRegion != nil {
291334 credentialsSource .DefaultRegion = source
335+ if source != CredentialsSourceDefault {
336+ credentialsSource .Variables [scw .ScwDefaultRegionEnv ] = append (credentialsSource .Variables [scw .ScwDefaultRegionEnv ], source )
337+ }
292338 }
293339
294340 if profile .DefaultZone != nil {
295341 credentialsSource .DefaultZone = source
342+ if source != CredentialsSourceDefault {
343+ credentialsSource .Variables [scw .ScwDefaultZoneEnv ] = append (credentialsSource .Variables [scw .ScwDefaultZoneEnv ], source )
344+ }
296345 }
297346 }
298347
0 commit comments