-
Notifications
You must be signed in to change notification settings - Fork 76
/
config.go
1225 lines (1065 loc) · 34.6 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package cfg
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
awsCredentials "github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
"github.com/aws/aws-sdk-go/aws/defaults"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/hashicorp/go-cleanhttp"
"github.com/jinzhu/copier"
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/openstack"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/pointerto"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/identity/v3/credentials"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/obs"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/helper/pathorcontents"
)
const (
osPrefix = "OS_"
)
type Config struct {
AccessKey string
SecretKey string
CACertFile string
AllowReauth bool
ClientCertFile string
ClientKeyFile string
Cloud string
DomainID string
DomainName string
EndpointType string
IdentityEndpoint string
Insecure bool
Password string
Passcode string
Region string
Swauth bool
TenantID string
TenantName string
Token string
SecurityToken string
Username string
UserID string
AgencyName string
AgencyDomainName string
DelegatedProject string
MaxRetries int
MaxBackoffRetries int
BackoffRetryTimeout int
UserAgent string
HwClient *golangsdk.ProviderClient
s3sess *session.Session
DomainClient *golangsdk.ProviderClient
environment *openstack.Env
}
func (c *Config) LoadAndValidate() error {
if c.MaxRetries < 0 {
return fmt.Errorf("max_retries should be a positive value")
}
if err := c.Load(); err != nil {
return err
}
if c.IdentityEndpoint == "" {
return fmt.Errorf("'auth_url' must be specified")
}
if err := c.validateEndpoint(); err != nil {
return err
}
if err := c.validateProject(); err != nil {
return err
}
var err error
switch {
case c.Token != "":
err = buildClientByToken(c)
case c.AccessKey != "" && c.SecretKey != "":
err = buildClientByAKSK(c)
case c.Password != "" && (c.Username != "" || c.UserID != ""):
err = buildClientByPassword(c)
default:
err = errors.New(
"no auth means provided. Token, AK/SK or username/password are required for authentication")
}
if err != nil {
return fmt.Errorf("failed to authenticate:\n%s", err)
}
var osDebug bool
if os.Getenv("OS_DEBUG") != "" {
osDebug = true
}
return c.newS3Session(osDebug)
}
// setIfEmpty set non-empty `loaded` value to empty `target` variable
func setIfEmpty(target *string, loaded string) {
if *target == "" && loaded != "" {
*target = loaded
}
}
// Load - load existing configuration from config files (`clouds.yaml`, etc.) and env variables
func (c *Config) Load() error {
if c.environment == nil {
c.environment = openstack.NewEnv(osPrefix)
}
cloud, err := c.environment.Cloud(c.Cloud)
if err != nil {
return err
}
// Auth data
setIfEmpty(&c.Username, cloud.AuthInfo.Username)
setIfEmpty(&c.UserID, cloud.AuthInfo.UserID)
if c.UserID != "" {
c.Username = ""
}
setIfEmpty(&c.TenantName, cloud.AuthInfo.ProjectName)
setIfEmpty(&c.TenantID, cloud.AuthInfo.ProjectID)
setIfEmpty(&c.DomainName, cloud.AuthInfo.DomainName)
setIfEmpty(&c.DomainID, cloud.AuthInfo.DomainID)
// project scope
setIfEmpty(&c.DomainName, cloud.AuthInfo.ProjectDomainName)
setIfEmpty(&c.DomainID, cloud.AuthInfo.ProjectDomainID)
// user scope
setIfEmpty(&c.DomainName, cloud.AuthInfo.UserDomainName)
setIfEmpty(&c.DomainID, cloud.AuthInfo.UserDomainID)
// default domain
setIfEmpty(&c.DomainID, cloud.AuthInfo.DefaultDomain)
setIfEmpty(&c.IdentityEndpoint, cloud.AuthInfo.AuthURL)
setIfEmpty(&c.Token, cloud.AuthInfo.Token)
setIfEmpty(&c.Password, cloud.AuthInfo.Password)
// General cloud info
setIfEmpty(&c.Region, cloud.RegionName)
setIfEmpty(&c.CACertFile, cloud.CACertFile)
setIfEmpty(&c.ClientCertFile, cloud.ClientCertFile)
setIfEmpty(&c.ClientKeyFile, cloud.ClientKeyFile)
if cloud.Verify != nil {
c.Insecure = !*cloud.Verify
}
return nil
}
func (c *Config) generateTLSConfig() (*tls.Config, error) {
config := &tls.Config{}
if c.CACertFile != "" {
caCert, _, err := pathorcontents.Read(c.CACertFile)
if err != nil {
return nil, fmt.Errorf("error reading CA Cert: %s", err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM([]byte(caCert))
config.RootCAs = caCertPool
}
if c.Insecure {
config.InsecureSkipVerify = true
}
if c.ClientCertFile != "" && c.ClientKeyFile != "" {
clientCert, _, err := pathorcontents.Read(c.ClientCertFile)
if err != nil {
return nil, fmt.Errorf("error reading Client Cert: %s", err)
}
clientKey, _, err := pathorcontents.Read(c.ClientKeyFile)
if err != nil {
return nil, fmt.Errorf("error reading Client Key: %s", err)
}
cert, err := tls.X509KeyPair([]byte(clientCert), []byte(clientKey))
if err != nil {
return nil, err
}
config.Certificates = []tls.Certificate{cert}
config.BuildNameToCertificate()
}
return config, nil
}
// GetCredentials This function is responsible for reading credentials from the
// environment in the case that they're not explicitly specified
// in the Terraform configuration.
func (c *Config) GetCredentials() (*awsCredentials.Credentials, error) {
// build a chain provider, lazy-evaluated by aws-sdk
providers := []awsCredentials.Provider{
&awsCredentials.StaticProvider{Value: awsCredentials.Value{
AccessKeyID: c.AccessKey,
SecretAccessKey: c.SecretKey,
SessionToken: c.SecurityToken,
}},
&awsCredentials.EnvProvider{},
&awsCredentials.SharedCredentialsProvider{
Filename: "",
Profile: "",
},
}
// Build isolated HTTP client to avoid issues with globally-shared settings
client := cleanhttp.DefaultClient()
// Keep the default timeout (100ms) low as we don't want to wait in non-EC2 environments
client.Timeout = 100 * time.Millisecond
const userTimeoutEnvVar = "AWS_METADATA_TIMEOUT"
userTimeout := os.Getenv(userTimeoutEnvVar)
if userTimeout != "" {
newTimeout, err := time.ParseDuration(userTimeout)
if err == nil {
if newTimeout.Nanoseconds() > 0 {
client.Timeout = newTimeout
} else {
log.Printf("[WARN] Non-positive value of %s (%s) is meaningless, ignoring", userTimeoutEnvVar, newTimeout.String())
}
} else {
log.Printf("[WARN] Error converting %s to time.Duration: %s", userTimeoutEnvVar, err)
}
}
log.Printf("[INFO] Setting AWS metadata API timeout to %s", client.Timeout.String())
config := &aws.Config{
HTTPClient: client,
}
usedEndpoint := SetOptionalEndpoint(config)
// Add the default AWS provider for ECS Task Roles if the relevant env variable is set
if uri := os.Getenv("AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"); len(uri) > 0 {
providers = append(providers, defaults.RemoteCredProvider(*config, defaults.Handlers()))
log.Print("[INFO] ECS container credentials detected, RemoteCredProvider added to auth chain")
}
// Real AWS should reply to a simple metadata request.
// We check it actually does to ensure something else didn't just
// happen to be listening on the same IP:Port
sess, err := session.NewSession(config)
if err != nil {
return nil, err
}
metadataClient := ec2metadata.New(sess)
if metadataClient.Available() {
providers = append(providers, &ec2rolecreds.EC2RoleProvider{
Client: metadataClient,
})
log.Print("[INFO] AWS EC2 instance detected via default metadata" +
" API endpoint, EC2RoleProvider added to the auth chain")
} else {
if usedEndpoint == "" {
usedEndpoint = "default location"
}
log.Printf("[INFO] Ignoring AWS metadata API endpoint at %s "+
"as it doesn't return any instance-id", usedEndpoint)
}
return awsCredentials.NewChainCredentials(providers), nil
}
func (c *Config) newS3Session(osDebug bool) error {
// Don't get AWS session unless we need it for AccessKey, SecretKey.
if c.AccessKey != "" && c.SecretKey != "" {
// Setup AWS/S3 client/config information for Swift S3 buckets
log.Println("[INFO] Building Swift S3 auth structure")
creds, err := c.GetCredentials()
if err != nil {
return err
}
// Call Get to check for credential provider. If nothing found, we'll get an
// error, and we can present it nicely to the user
cp, err := creds.Get()
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NoCredentialProviders" {
return fmt.Errorf(`no valid credential sources found for Swift S3 Provider.
Please see https://terraform.io/docs/providers/aws/index.html for more information on
providing credentials for the S3 Provider`)
}
return fmt.Errorf("error loading credentials for Swift S3 Provider: %s", err)
}
log.Printf("[INFO] Swift S3 Auth provider used: %q", cp.ProviderName)
awsConfig := &aws.Config{
Credentials: creds,
Region: aws.String(c.GetRegion(nil)),
// MaxRetries: aws.Int(c.MaxRetries),
HTTPClient: cleanhttp.DefaultClient(),
// S3ForcePathStyle: aws.Bool(c.S3ForcePathStyle),
}
if osDebug {
awsConfig.LogLevel = aws.LogLevel(aws.LogDebugWithHTTPBody | aws.LogDebugWithRequestRetries | aws.LogDebugWithRequestErrors)
awsConfig.Logger = awsLogger{}
}
if c.Insecure {
transport := awsConfig.HTTPClient.Transport.(*http.Transport)
transport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
}
// Set up base session for AWS/Swift S3
c.s3sess, err = session.NewSession(awsConfig)
if err != nil {
return fmt.Errorf("error creating Swift S3 session: %s", err)
}
}
return nil
}
var validEndpoints = []string{
"internal", "internalURL",
"admin", "adminURL",
"public", "publicURL",
"",
}
func (c *Config) validateEndpoint() error {
for _, endpoint := range validEndpoints {
if c.EndpointType == endpoint {
return nil
}
}
return fmt.Errorf("invalid endpoint type provided: %s", c.EndpointType)
}
// validateProject checks that `Project`(`Tenant`) value is set
func (c *Config) validateProject() error {
if c.TenantName == "" && c.TenantID == "" && c.DelegatedProject == "" {
return errors.New("no project name/id or delegated project is provided")
}
return nil
}
func buildClientByToken(c *Config) error {
var pao, dao golangsdk.AuthOptions
if c.AgencyDomainName != "" && c.AgencyName != "" {
pao = golangsdk.AuthOptions{
AgencyName: c.AgencyName,
AgencyDomainName: c.AgencyDomainName,
DelegatedProject: c.DelegatedProject,
}
dao = golangsdk.AuthOptions{
AgencyName: c.AgencyName,
AgencyDomainName: c.AgencyDomainName,
}
} else {
pao = golangsdk.AuthOptions{
DomainID: c.DomainID,
DomainName: c.DomainName,
TenantID: c.TenantID,
TenantName: c.TenantName,
}
dao = golangsdk.AuthOptions{
DomainID: c.DomainID,
DomainName: c.DomainName,
}
}
for _, ao := range []*golangsdk.AuthOptions{&pao, &dao} {
ao.IdentityEndpoint = c.IdentityEndpoint
ao.TokenID = c.Token
}
return c.genClients(pao, dao)
}
func buildClientByAKSK(c *Config) error {
var pao, dao golangsdk.AKSKAuthOptions
if c.AgencyDomainName != "" && c.AgencyName != "" {
pao = golangsdk.AKSKAuthOptions{
DomainID: c.DomainID,
Domain: c.DomainName,
AgencyName: c.AgencyName,
AgencyDomainName: c.AgencyDomainName,
DelegatedProject: c.DelegatedProject,
}
dao = golangsdk.AKSKAuthOptions{
DomainID: c.DomainID,
Domain: c.DomainName,
AgencyName: c.AgencyName,
AgencyDomainName: c.AgencyDomainName,
}
} else {
pao = golangsdk.AKSKAuthOptions{
ProjectName: c.TenantName,
ProjectId: c.TenantID,
}
dao = golangsdk.AKSKAuthOptions{
DomainID: c.DomainID,
Domain: c.DomainName,
}
}
if c.SecurityToken != "" {
dao.ProjectId = c.TenantID
dao.ProjectName = c.TenantName
}
for _, ao := range []*golangsdk.AKSKAuthOptions{&pao, &dao} {
ao.IdentityEndpoint = c.IdentityEndpoint
ao.AccessKey = c.AccessKey
ao.SecretKey = c.SecretKey
if c.SecurityToken != "" {
ao.SecurityToken = c.SecurityToken
}
}
return c.genClients(pao, dao)
}
func buildClientByPassword(c *Config) error {
var pao, dao golangsdk.AuthOptions
if c.AgencyDomainName != "" && c.AgencyName != "" {
pao = golangsdk.AuthOptions{
DomainID: c.DomainID,
DomainName: c.DomainName,
AgencyName: c.AgencyName,
AgencyDomainName: c.AgencyDomainName,
DelegatedProject: c.DelegatedProject,
}
dao = golangsdk.AuthOptions{
DomainID: c.DomainID,
DomainName: c.DomainName,
AgencyName: c.AgencyName,
AgencyDomainName: c.AgencyDomainName,
}
} else {
pao = golangsdk.AuthOptions{
DomainID: c.DomainID,
DomainName: c.DomainName,
TenantID: c.TenantID,
TenantName: c.TenantName,
}
dao = golangsdk.AuthOptions{
DomainID: c.DomainID,
DomainName: c.DomainName,
}
}
for _, ao := range []*golangsdk.AuthOptions{&pao, &dao} {
ao.IdentityEndpoint = c.IdentityEndpoint
ao.Password = c.Password
ao.Username = c.Username
ao.UserID = c.UserID
ao.Passcode = c.Passcode
ao.AllowReauth = c.AllowReauth
}
return c.genClients(pao, dao)
}
func (c *Config) genClients(pao, dao golangsdk.AuthOptionsProvider) error {
client, err := c.genClient(pao)
if err != nil {
return fmt.Errorf("error generating project client: %w", err)
}
c.HwClient = client
client, err = c.genClient(dao)
if err != nil {
return fmt.Errorf("error generating domain client: %w", err)
}
c.DomainClient = client
return nil
}
func (c *Config) genClient(ao golangsdk.AuthOptionsProvider) (*golangsdk.ProviderClient, error) {
client, err := openstack.NewClient(ao.GetIdentityEndpoint())
if err != nil {
return nil, err
}
// Set UserAgent
client.UserAgent.Prepend(c.UserAgent)
config, err := c.generateTLSConfig()
if err != nil {
return nil, err
}
transport := &http.Transport{Proxy: http.ProxyFromEnvironment, TLSClientConfig: config}
// if OS_DEBUG is set, log the requests and responses
var osDebug bool
if os.Getenv("OS_DEBUG") != "" {
osDebug = true
}
client.MaxBackoffRetries = pointerto.Int(c.MaxBackoffRetries)
defaultBackoffTimeout := time.Duration(c.BackoffRetryTimeout) * time.Second
client.BackoffRetryTimeout = &defaultBackoffTimeout
client.HTTPClient = http.Client{
Transport: &RoundTripper{
Rt: transport,
OsDebug: osDebug,
MaxRetries: c.MaxRetries,
},
CheckRedirect: func(req *http.Request, via []*http.Request) error {
if client.AKSKAuthOptions.AccessKey != "" {
golangsdk.ReSign(req, golangsdk.SignOptions{
AccessKey: client.AKSKAuthOptions.AccessKey,
SecretKey: client.AKSKAuthOptions.SecretKey,
})
}
return nil
},
}
// If using Swift Authentication, there's no need to validate authentication normally.
if !c.Swauth {
err = openstack.Authenticate(client, ao)
if err != nil {
return nil, err
}
}
c.Region = client.RegionID
return client, nil
}
type awsLogger struct{}
func (l awsLogger) Log(args ...interface{}) {
tokens := make([]string, 0, len(args))
for _, arg := range args {
if token, ok := arg.(string); ok {
tokens = append(tokens, token)
}
}
log.Printf("[DEBUG] [aws-sdk-go] %s", strings.Join(tokens, " "))
}
func (c *Config) determineRegion(region string) string {
// If a resource-level region was not specified, and a provider-level region was set,
// use the provider-level region.
if region == "" && c.Region != "" {
region = c.Region
}
log.Printf("[DEBUG] OpenTelekomCloud Region is: %s", region)
return region
}
func (c *Config) S3Client(region string) (*s3.S3, error) {
if c.s3sess == nil {
return nil, fmt.Errorf("missing credentials for Swift S3 Provider, need access_key and secret_key values for provider")
}
client, err := openstack.NewOBSService(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
if err != nil {
return nil, err
}
awsS3Sess := c.s3sess.Copy(&aws.Config{Endpoint: aws.String(client.Endpoint)})
s3conn := s3.New(awsS3Sess)
return s3conn, err
}
func setUpOBSLogging() {
// init log
if os.Getenv("OS_DEBUG") != "" {
var logfile = "./.obs-sdk.log"
// maxLogSize:10M, backups:10
if err := obs.InitLog(logfile, 1024*1024*10, 10, obs.LEVEL_DEBUG, false); err != nil {
log.Printf("[WARN] initial obs sdk log failed: %s", err)
}
}
}
// issueTemporaryCredentials creates temporary AK/SK, which can be used to auth in OBS when AK/SK is not provided
func (c *Config) issueTemporaryCredentials() (*credentials.TemporaryCredential, error) {
if c.AccessKey != "" && c.SecretKey != "" {
return &credentials.TemporaryCredential{
AccessKey: c.AccessKey,
SecretKey: c.SecretKey,
SecurityToken: c.SecurityToken,
}, nil
}
client, err := c.IdentityV3Client()
if err != nil {
return nil, fmt.Errorf("error creating identity v3 domain client: %s", err)
}
credential, err := credentials.CreateTemporary(client, credentials.CreateTemporaryOpts{
Methods: []string{"token"},
Token: client.Token(),
}).Extract()
if err != nil {
return nil, fmt.Errorf("error creating temporary AK/SK: %s", err)
}
return credential, nil
}
func (c *Config) NewObjectStorageClient(region string) (*obs.ObsClient, error) {
cred, err := c.issueTemporaryCredentials()
if err != nil {
return nil, fmt.Errorf("failed to construct OBS client without AK/SK: %s", err)
}
client, err := openstack.NewOBSService(c.HwClient, golangsdk.EndpointOpts{
Region: c.determineRegion(region),
Availability: c.getEndpointType(),
})
if err != nil {
return nil, err
}
setUpOBSLogging()
proxyConfigure, err := obsProxyConf()
if err != nil {
return nil, err
}
return obs.New(
cred.AccessKey, cred.SecretKey, client.Endpoint,
obs.WithSecurityToken(cred.SecurityToken), obs.WithSignature(obs.SignatureObs), proxyConfigure,
)
}
func obsProxyConf() (obs.Configurer, error) {
proxyConfigure := obs.WithProxyUrl("")
httpProxy := os.Getenv("HTTP_PROXY")
httpsProxy := os.Getenv("HTTPS_PROXY")
if httpsProxy != "" {
_, err := url.ParseRequestURI(httpsProxy)
if err != nil {
return proxyConfigure, err
}
return obs.WithProxyUrl(httpsProxy), nil
} else if httpProxy != "" {
_, err := url.ParseRequestURI(httpProxy)
if err != nil {
return proxyConfigure, err
}
return obs.WithProxyUrl(httpProxy), nil
}
return proxyConfigure, nil
}
func (c *Config) BlockStorageV2Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewBlockStorageV2(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) BlockStorageV3Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewBlockStorageV3(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) CbrV3Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewCBRService(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) DisV2Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewDISServiceV2(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) DrsV3Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewDRSServiceV3(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) ComputeV1Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewComputeV1(c.HwClient, golangsdk.EndpointOpts{
Region: c.determineRegion(region),
Availability: c.getEndpointType(),
})
}
func (c *Config) ComputeV2Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewComputeV2(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) DnsV2Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewDNSV2(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) GaussDBV3Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewGaussDBV3(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) IdentityV3Client(_ ...string) (*golangsdk.ServiceClient, error) {
return openstack.NewIdentityV3(c.DomainClient, golangsdk.EndpointOpts{
Availability: c.getEndpointType(),
})
}
// IdentityV30Client - provides client is used for use with endpoints with invalid "v3.0" URLs
func (c *Config) IdentityV30Client() (*golangsdk.ServiceClient, error) {
service, err := openstack.NewIdentityV3(c.DomainClient, golangsdk.EndpointOpts{
Availability: c.getEndpointType(),
})
if err != nil {
return nil, err
}
service.Endpoint = strings.Replace(service.IdentityEndpoint, "v3/", "v3.0/", 1)
return service, nil
}
func (c *Config) RegionIdentityV3Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewIdentityV3(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) ImageV1Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewIMSV1(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) ImageV2Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewIMSV2(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) NetworkingV1Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewNetworkV1(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) NetworkingV2Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewNetworkV2(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) NetworkingV3Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewVpcV3(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) SmnV2Client(projectName ProjectName) (*golangsdk.ServiceClient, error) {
newConfig, err := reconfigProjectName(*c, projectName)
if err != nil {
return nil, err
}
return openstack.NewSMNV2(newConfig.HwClient, golangsdk.EndpointOpts{
Region: c.GetRegion(nil),
Availability: c.getEndpointType(),
})
}
func (c *Config) SmnV2TagClient(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewSMNV2Tags(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) CesV1Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewCESClient(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) getEndpointType() golangsdk.Availability {
if c.EndpointType == "internal" || c.EndpointType == "internalURL" {
return golangsdk.AvailabilityInternal
}
if c.EndpointType == "admin" || c.EndpointType == "adminURL" {
return golangsdk.AvailabilityAdmin
}
return golangsdk.AvailabilityPublic
}
func (c *Config) KmsKeyV1Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewKMSV1(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) NatV2Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewNatV2(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) OrchestrationV1Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewOrchestrationV1(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) SfsV2Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewSharedFileSystemV2(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) SfsTurboV1Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewSharedFileSystemTurboV1(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) VbsV2Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewVBS(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) AutoscalingV1Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewAutoScalingV1(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) AutoscalingV2Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewAutoScalingV2(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) CsbsV1Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewCSBSService(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) DCaaSV2Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewDCaaSV2(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) DehV1Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewDeHServiceV1(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) DmsV1Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewDMSServiceV1(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) DmsV11Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewDMSServiceV11(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) DmsV2Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewDMSServiceV2(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) MrsV1Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewMapReduceV1(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) ElbV1Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewELBV1(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) ElbV2Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewELBV2(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) ElbV3Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewELBV3(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) RdsV1Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewRDSV1(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) AntiddosV1Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewAntiDDoSV1(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}
func (c *Config) CtsV1Client(projectName ProjectName) (*golangsdk.ServiceClient, error) {
newConfig, err := reconfigProjectName(*c, projectName)
if err != nil {
return nil, err
}
return openstack.NewCTSV1(newConfig.HwClient, golangsdk.EndpointOpts{
Region: c.GetRegion(nil),
Availability: c.getEndpointType(),
})
}
func (c *Config) CtsV2Client(projectName ProjectName) (*golangsdk.ServiceClient, error) {
newConfig, err := reconfigProjectName(*c, projectName)
if err != nil {
return nil, err
}
return openstack.NewCTSV2(newConfig.HwClient, golangsdk.EndpointOpts{
Region: c.GetRegion(nil),
Availability: c.getEndpointType(),
})