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,47 @@ 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 , 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+
76121type Config struct {
77122 ProviderSchema * schema.ResourceData
78123 HTTPClient * http.Client
@@ -270,29 +315,39 @@ func GetCredentialsSource(defaultZoneProfile, activeProfile, providerProfile, en
270315 },
271316 }
272317 credentialsSource := & CredentialsSource {}
318+ credentialsSource .Variables = map [string ][]string {}
273319
274320 for _ , pair := range profilesInOrder {
275321 source := pair .Source
276322 profile := pair .Profile
277323
278324 if profile .AccessKey != nil {
279325 credentialsSource .AccessKey = source
326+ credentialsSource .Variables [scw .ScwAccessKeyEnv ] = append (credentialsSource .Variables [scw .ScwAccessKeyEnv ], source )
280327 }
281328
282329 if profile .SecretKey != nil {
283330 credentialsSource .SecretKey = source
331+ credentialsSource .Variables [scw .ScwSecretKeyEnv ] = append (credentialsSource .Variables [scw .ScwSecretKeyEnv ], source )
284332 }
285333
286334 if profile .DefaultProjectID != nil {
287335 credentialsSource .ProjectID = source
336+ credentialsSource .Variables [scw .ScwDefaultProjectIDEnv ] = append (credentialsSource .Variables [scw .ScwDefaultProjectIDEnv ], source )
288337 }
289338
290339 if profile .DefaultRegion != nil {
291340 credentialsSource .DefaultRegion = source
341+ if source != CredentialsSourceDefault {
342+ credentialsSource .Variables [scw .ScwDefaultRegionEnv ] = append (credentialsSource .Variables [scw .ScwDefaultRegionEnv ], source )
343+ }
292344 }
293345
294346 if profile .DefaultZone != nil {
295347 credentialsSource .DefaultZone = source
348+ if source != CredentialsSourceDefault {
349+ credentialsSource .Variables [scw .ScwDefaultZoneEnv ] = append (credentialsSource .Variables [scw .ScwDefaultZoneEnv ], source )
350+ }
296351 }
297352 }
298353
0 commit comments