-
Notifications
You must be signed in to change notification settings - Fork 7
/
client.go
1239 lines (1126 loc) · 45 KB
/
client.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 connectivity
import (
"encoding/json"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/endpoints"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
"github.com/aliyun/alibaba-cloud-sdk-go/services/adb"
"github.com/aliyun/alibaba-cloud-sdk-go/services/alidns"
"github.com/aliyun/alibaba-cloud-sdk-go/services/bssopenapi"
cdn_new "github.com/aliyun/alibaba-cloud-sdk-go/services/cdn"
"github.com/aliyun/alibaba-cloud-sdk-go/services/cms"
"github.com/aliyun/alibaba-cloud-sdk-go/services/cr"
"github.com/aliyun/alibaba-cloud-sdk-go/services/cr_ee"
"github.com/aliyun/alibaba-cloud-sdk-go/services/dds"
"github.com/aliyun/alibaba-cloud-sdk-go/services/elasticsearch"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ess"
"github.com/aliyun/alibaba-cloud-sdk-go/services/gpdb"
"github.com/aliyun/alibaba-cloud-sdk-go/services/hbase"
"github.com/aliyun/alibaba-cloud-sdk-go/services/location"
"github.com/aliyun/alibaba-cloud-sdk-go/services/maxcompute"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ons"
"github.com/aliyun/alibaba-cloud-sdk-go/services/polardb"
"github.com/aliyun/alibaba-cloud-sdk-go/services/r-kvstore"
"github.com/aliyun/alibaba-cloud-sdk-go/services/rds"
"github.com/aliyun/alibaba-cloud-sdk-go/services/slb"
slsPop "github.com/aliyun/alibaba-cloud-sdk-go/services/sls"
sls "github.com/aliyun/aliyun-log-go-sdk"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/aliyun/fc-go-sdk"
"log"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
"github.com/aliyun/alibaba-cloud-sdk-go/services/kms"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ram"
"github.com/aliyun/alibaba-cloud-sdk-go/services/vpc"
"github.com/denverdino/aliyungo/cdn"
"github.com/denverdino/aliyungo/cs"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"sync"
"fmt"
"net/http"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"time"
)
type ApsaraStackClient struct {
Region Region
RegionId string
Domain string
AccessKey string
SecretKey string
Department string
ResourceGroup string
Config *Config
accountId string
ecsconn *ecs.Client
accountIdMutex sync.RWMutex
vpcconn *vpc.Client
slbconn *slb.Client
csconn *cs.Client
polarDBconn *polardb.Client
cdnconn *cdn.CdnClient
cdnconn_new *cdn_new.Client
kmsconn *kms.Client
bssopenapiconn *bssopenapi.Client
rdsconn *rds.Client
ramconn *ram.Client
essconn *ess.Client
gpdbconn *gpdb.Client
elasticsearchconn *elasticsearch.Client
hbaseconn *hbase.Client
adbconn *adb.Client
ossconn *oss.Client
rkvconn *r_kvstore.Client
fcconn *fc.Client
ddsconn *dds.Client
onsconn *ons.Client
logconn *sls.Client
logpopconn *slsPop.Client
dnsconn *alidns.Client
creeconn *cr_ee.Client
crconn *cr.Client
cmsconn *cms.Client
maxcomputeconn *maxcompute.Client
}
const (
ApiVersion20140526 = ApiVersion("2014-05-26")
ApiVersion20160815 = ApiVersion("2016-08-15")
ApiVersion20140515 = ApiVersion("2014-05-15")
ApiVersion20190510 = ApiVersion("2019-05-10")
)
const DefaultClientRetryCountSmall = 5
const Terraform = "HashiCorp-Terraform"
const Provider = "Terraform-Provider"
const Module = "Terraform-Module"
type ApiVersion string
// The main version number that is being run at the moment.
var ProviderVersion = "1.0.9"
var TerraformVersion = strings.TrimSuffix(schema.Provider{}.TerraformVersion, "-dev")
var goSdkMutex = sync.RWMutex{} // The Go SDK is not thread-safe
// Client for ApsaraStackClient
func (c *Config) Client() (*ApsaraStackClient, error) {
// Get the auth and region. This can fail if keys/regions were not
// specified and we're attempting to use the environment.
if !c.SkipRegionValidation {
err := c.loadAndValidate()
if err != nil {
return nil, err
}
}
return &ApsaraStackClient{
Config: c,
Region: c.Region,
RegionId: c.RegionId,
AccessKey: c.AccessKey,
SecretKey: c.SecretKey,
Department: c.Department,
ResourceGroup: c.ResourceGroup,
Domain: c.Domain,
}, nil
}
func (client *ApsaraStackClient) WithEcsClient(do func(*ecs.Client) (interface{}, error)) (interface{}, error) {
// Initialize the ECS client if necessary
if client.ecsconn == nil {
endpoint := client.Config.EcsEndpoint
if endpoint == "" {
return nil, fmt.Errorf("unable to initialize the ecs client: endpoint or domain is not provided for ecs service")
}
if endpoint != "" {
endpoints.AddEndpointMapping(client.Config.RegionId, string(ECSCode), endpoint)
}
if strings.HasPrefix(endpoint, "http") {
endpoint = strings.TrimPrefix(strings.TrimPrefix(endpoint, "http://"), "https://")
}
ecsconn, err := ecs.NewClientWithOptions(client.Config.RegionId, client.getSdkConfig().WithTimeout(time.Duration(60)*time.Second), client.Config.getAuthCredential(true))
if err != nil {
return nil, fmt.Errorf("unable to initialize the ECS client: %#v", err)
}
ecsconn.Domain = endpoint
ecsconn.AppendUserAgent(Terraform, TerraformVersion)
ecsconn.AppendUserAgent(Provider, ProviderVersion)
ecsconn.AppendUserAgent(Module, client.Config.ConfigurationSource)
ecsconn.SetHTTPSInsecure(client.Config.Insecure)
if client.Config.Proxy != "" {
ecsconn.SetHttpsProxy(client.Config.Proxy)
ecsconn.SetHttpProxy(client.Config.Proxy)
}
client.ecsconn = ecsconn
}
return do(client.ecsconn)
}
func (client *ApsaraStackClient) WithPolarDBClient(do func(*polardb.Client) (interface{}, error)) (interface{}, error) {
// Initialize the PolarDB client if necessary
if client.polarDBconn == nil {
endpoint := client.Config.PolarDBEndpoint
if endpoint == "" {
return nil, fmt.Errorf("unable to initialize the polardb client: endpoint or domain is not provided for polardb service")
}
polarDBconn, err := polardb.NewClientWithOptions(client.Config.RegionId, client.getSdkConfig(), client.Config.getAuthCredential(true))
if err != nil {
return nil, fmt.Errorf("unable to initialize the PolarDB client: %#v", err)
}
polarDBconn.Domain = endpoint
polarDBconn.AppendUserAgent(Terraform, TerraformVersion)
polarDBconn.AppendUserAgent(Provider, ProviderVersion)
polarDBconn.AppendUserAgent(Module, client.Config.ConfigurationSource)
polarDBconn.SetHTTPSInsecure(client.Config.Insecure)
if client.Config.Proxy != "" {
polarDBconn.SetHttpProxy(client.Config.Proxy)
polarDBconn.SetHTTPSInsecure(client.Config.Insecure)
}
client.polarDBconn = polarDBconn
}
return do(client.polarDBconn)
}
func (client *ApsaraStackClient) WithElasticsearchClient(do func(*elasticsearch.Client) (interface{}, error)) (interface{}, error) {
// Initialize the Elasticsearch client if necessary
if client.elasticsearchconn == nil {
endpoint := client.Config.ElasticsearchEndpoint
if endpoint == "" {
return nil, fmt.Errorf("unable to initialize the ElasticSearch client: endpoint or domain is not provided for ElasticSearch service")
}
if endpoint != "" {
endpoints.AddEndpointMapping(client.Config.RegionId, string(ELASTICSEARCHCode), endpoint)
}
elasticsearchconn, err := elasticsearch.NewClientWithOptions(client.Config.RegionId, client.getSdkConfig(), client.Config.getAuthCredential(true))
if err != nil {
return nil, fmt.Errorf("unable to initialize the Elasticsearch client: %#v", err)
}
elasticsearchconn.AppendUserAgent(Terraform, TerraformVersion)
elasticsearchconn.AppendUserAgent(Provider, ProviderVersion)
elasticsearchconn.AppendUserAgent(Module, client.Config.ConfigurationSource)
elasticsearchconn.SetHTTPSInsecure(client.Config.Insecure)
if client.Config.Proxy != "" {
elasticsearchconn.SetHttpProxy(client.Config.Proxy)
}
client.elasticsearchconn = elasticsearchconn
}
return do(client.elasticsearchconn)
}
func (client *ApsaraStackClient) WithEssClient(do func(*ess.Client) (interface{}, error)) (interface{}, error) {
// Initialize the ESS client if necessary
if client.essconn == nil {
endpoint := client.Config.EssEndpoint
if endpoint == "" {
return nil, fmt.Errorf("unable to initialize the ess client: endpoint or domain is not provided for ess service")
}
if endpoint != "" {
endpoints.AddEndpointMapping(client.Config.RegionId, string(ESSCode), endpoint)
}
if strings.HasPrefix(endpoint, "http") {
endpoint = strings.TrimPrefix(strings.TrimPrefix(endpoint, "http://"), "https://")
}
essconn, err := ess.NewClientWithOptions(client.Config.RegionId, client.getSdkConfig(), client.Config.getAuthCredential(true))
if err != nil {
return nil, fmt.Errorf("unable to initialize the ESS client: %#v", err)
}
essconn.Domain = endpoint
essconn.AppendUserAgent(Terraform, TerraformVersion)
essconn.AppendUserAgent(Provider, ProviderVersion)
essconn.AppendUserAgent(Module, client.Config.ConfigurationSource)
essconn.SetHTTPSInsecure(client.Config.Insecure)
if client.Config.Proxy != "" {
essconn.SetHttpsProxy(client.Config.Proxy)
essconn.SetHttpProxy(client.Config.Proxy)
}
client.essconn = essconn
}
return do(client.essconn)
}
func (client *ApsaraStackClient) WithRkvClient(do func(*r_kvstore.Client) (interface{}, error)) (interface{}, error) {
// Initialize the RKV client if necessary
if client.rkvconn == nil {
endpoint := client.Config.KVStoreEndpoint
if endpoint == "" {
return nil, fmt.Errorf("unable to initialize the kvstore client: endpoint or domain is not provided for logpop service")
}
if endpoint != "" {
endpoints.AddEndpointMapping(client.Config.RegionId, fmt.Sprintf("R-%s", string(KVSTORECode)), endpoint)
}
if strings.HasPrefix(endpoint, "http") {
endpoint = strings.TrimPrefix(strings.TrimPrefix(endpoint, "http://"), "https://")
}
rkvconn, err := r_kvstore.NewClientWithOptions(client.Config.RegionId, client.getSdkConfig(), client.Config.getAuthCredential(true))
if err != nil {
return nil, fmt.Errorf("unable to initialize the RKV client: %#v", err)
}
rkvconn.Domain = endpoint
rkvconn.AppendUserAgent(Terraform, TerraformVersion)
rkvconn.AppendUserAgent(Provider, ProviderVersion)
rkvconn.AppendUserAgent(Module, client.Config.ConfigurationSource)
rkvconn.SetHTTPSInsecure(client.Config.Insecure)
if client.Config.Proxy != "" {
rkvconn.SetHttpProxy(client.Config.Proxy)
}
client.rkvconn = rkvconn
}
return do(client.rkvconn)
}
func (client *ApsaraStackClient) WithGpdbClient(do func(*gpdb.Client) (interface{}, error)) (interface{}, error) {
// Initialize the GPDB client if necessary
if client.gpdbconn == nil {
endpoint := client.Config.GpdbEndpoint
if endpoint != "" {
endpoints.AddEndpointMapping(client.Config.RegionId, string(GPDBCode), endpoint)
}
gpdbconn, err := gpdb.NewClientWithOptions(client.Config.RegionId, client.getSdkConfig(), client.Config.getAuthCredential(true))
if err != nil {
return nil, fmt.Errorf("unable to initialize the GPDB client: %#v", err)
}
gpdbconn.Domain = endpoint
gpdbconn.AppendUserAgent(Terraform, TerraformVersion)
gpdbconn.AppendUserAgent(Provider, ProviderVersion)
gpdbconn.AppendUserAgent(Module, client.Config.ConfigurationSource)
gpdbconn.SetHTTPSInsecure(client.Config.Insecure)
if client.Config.Proxy != "" {
gpdbconn.SetHttpProxy(client.Config.Proxy)
}
client.gpdbconn = gpdbconn
}
return do(client.gpdbconn)
}
func (client *ApsaraStackClient) WithAdbClient(do func(*adb.Client) (interface{}, error)) (interface{}, error) {
// Initialize the adb client if necessary
if client.adbconn == nil {
endpoint := client.Config.AdbEndpoint
if endpoint == "" {
return nil, fmt.Errorf("unable to initialize the client: endpoint or domain is not provided for service")
}
adbconn, err := adb.NewClientWithOptions(client.Config.RegionId, client.getSdkConfig(), client.Config.getAuthCredential(true))
if err != nil {
return nil, fmt.Errorf("unable to initialize the adb client: %#v", err)
}
adbconn.Domain = endpoint
adbconn.AppendUserAgent(Terraform, TerraformVersion)
adbconn.AppendUserAgent(Provider, ProviderVersion)
adbconn.AppendUserAgent(Module, client.Config.ConfigurationSource)
adbconn.SetHTTPSInsecure(client.Config.Insecure)
if client.Config.Proxy != "" {
adbconn.SetHttpProxy(client.Config.Proxy)
}
client.adbconn = adbconn
}
return do(client.adbconn)
}
func (client *ApsaraStackClient) WithHbaseClient(do func(*hbase.Client) (interface{}, error)) (interface{}, error) {
// Initialize the HBase client if necessary
if client.hbaseconn == nil {
endpoint := client.Config.HBaseEndpoint
if endpoint == "" {
return nil, fmt.Errorf("unable to initialize the client: endpoint or domain is not provided for service")
}
if endpoint != "" {
endpoints.AddEndpointMapping(client.Config.RegionId, string(HBASECode), endpoint)
}
hbaseconn, err := hbase.NewClientWithOptions(client.Config.RegionId, client.getSdkConfig(), client.Config.getAuthCredential(true))
if err != nil {
return nil, fmt.Errorf("unable to initialize the hbase client: %#v", err)
}
hbaseconn.AppendUserAgent(Terraform, TerraformVersion)
hbaseconn.AppendUserAgent(Provider, ProviderVersion)
hbaseconn.AppendUserAgent(Module, client.Config.ConfigurationSource)
hbaseconn.SetHTTPSInsecure(client.Config.Insecure)
if client.Config.Proxy != "" {
hbaseconn.SetHttpProxy(client.Config.Proxy)
}
client.hbaseconn = hbaseconn
}
return do(client.hbaseconn)
}
func (client *ApsaraStackClient) WithFcClient(do func(*fc.Client) (interface{}, error)) (interface{}, error) {
goSdkMutex.Lock()
defer goSdkMutex.Unlock()
// Initialize the FC client if necessary
if client.fcconn == nil {
endpoint := client.Config.FcEndpoint
if endpoint == "" {
return nil, fmt.Errorf("unable to initialize the client: endpoint or domain is not provided for service")
}
if strings.HasPrefix(endpoint, "http") {
endpoint = strings.TrimPrefix(strings.TrimPrefix(endpoint, "http://"), "https://")
}
accountId, err := client.AccountId()
if err != nil {
return nil, err
}
config := client.getSdkConfig()
clientOptions := []fc.ClientOption{fc.WithSecurityToken(client.Config.SecurityToken), fc.WithTransport(config.HttpTransport),
fc.WithTimeout(30), fc.WithRetryCount(DefaultClientRetryCountSmall)}
fcconn, err := fc.NewClient(fmt.Sprintf("https://%s.%s", accountId, endpoint), string(ApiVersion20160815), client.Config.AccessKey, client.Config.SecretKey, clientOptions...)
if err != nil {
return nil, fmt.Errorf("unable to initialize the FC client: %#v", err)
}
fcconn.Config.UserAgent = client.getUserAgent()
fcconn.Config.SecurityToken = client.Config.SecurityToken
client.fcconn = fcconn
}
return do(client.fcconn)
}
func (client *ApsaraStackClient) WithVpcClient(do func(*vpc.Client) (interface{}, error)) (interface{}, error) {
// Initialize the VPC client if necessary
if client.vpcconn == nil {
endpoint := client.Config.VpcEndpoint
if endpoint == "" {
return nil, fmt.Errorf("unable to initialize the vpc client: endpoint or domain is not provided for vpc service")
}
if endpoint != "" {
endpoints.AddEndpointMapping(client.Config.RegionId, string(VPCCode), endpoint)
}
if strings.HasPrefix(endpoint, "http") {
endpoint = strings.TrimPrefix(strings.TrimPrefix(endpoint, "http://"), "https://")
}
vpcconn, err := vpc.NewClientWithOptions(client.Config.RegionId, client.getSdkConfig(), client.Config.getAuthCredential(true))
if err != nil {
return nil, fmt.Errorf("unable to initialize the VPC client: %#v", err)
}
vpcconn.Domain = endpoint
vpcconn.AppendUserAgent(Terraform, TerraformVersion)
vpcconn.AppendUserAgent(Provider, ProviderVersion)
vpcconn.AppendUserAgent(Module, client.Config.ConfigurationSource)
vpcconn.SetHTTPSInsecure(client.Config.Insecure)
if client.Config.Proxy != "" {
vpcconn.SetHttpsProxy(client.Config.Proxy)
vpcconn.SetHttpProxy(client.Config.Proxy)
}
client.vpcconn = vpcconn
}
return do(client.vpcconn)
}
func (client *ApsaraStackClient) WithSlbClient(do func(*slb.Client) (interface{}, error)) (interface{}, error) {
// Initialize the SLB client if necessary
if client.slbconn == nil {
endpoint := client.Config.SlbEndpoint
if endpoint == "" {
return nil, fmt.Errorf("unable to initialize the slb client: endpoint or domain is not provided for slb service")
}
if endpoint != "" {
endpoints.AddEndpointMapping(client.Config.RegionId, string(SLBCode), endpoint)
}
slbconn, err := slb.NewClientWithOptions(client.Config.RegionId, client.getSdkConfig(), client.Config.getAuthCredential(true))
if err != nil {
return nil, fmt.Errorf("unable to initialize the SLB client: %#v", err)
}
slbconn.Domain = endpoint
slbconn.AppendUserAgent(Terraform, TerraformVersion)
slbconn.AppendUserAgent(Provider, ProviderVersion)
slbconn.AppendUserAgent(Module, client.Config.ConfigurationSource)
slbconn.SetHTTPSInsecure(client.Config.Insecure)
if client.Config.Proxy != "" {
slbconn.SetHttpsProxy(client.Config.Proxy)
slbconn.SetHttpProxy(client.Config.Proxy)
}
client.slbconn = slbconn
}
return do(client.slbconn)
}
func (client *ApsaraStackClient) WithDdsClient(do func(*dds.Client) (interface{}, error)) (interface{}, error) {
// Initialize the DDS client if necessary
if client.ddsconn == nil {
endpoint := client.Config.DdsEndpoint
if endpoint == "" {
return nil, fmt.Errorf("unable to initialize the client: endpoint or domain is not provided for service")
}
if endpoint != "" {
endpoints.AddEndpointMapping(client.Config.RegionId, string(DDSCode), endpoint)
}
ddsconn, err := dds.NewClientWithOptions(client.Config.RegionId, client.getSdkConfig(), client.Config.getAuthCredential(true))
if err != nil {
return nil, fmt.Errorf("unable to initialize the DDS client: %#v", err)
}
ddsconn.Domain = endpoint
ddsconn.AppendUserAgent(Terraform, TerraformVersion)
ddsconn.AppendUserAgent(Provider, ProviderVersion)
ddsconn.AppendUserAgent(Module, client.Config.ConfigurationSource)
ddsconn.SetHTTPSInsecure(client.Config.Insecure)
if client.Config.Proxy != "" {
ddsconn.SetHttpProxy(client.Config.Proxy)
}
client.ddsconn = ddsconn
}
return do(client.ddsconn)
}
func (client *ApsaraStackClient) WithOssNewClient(do func(*ecs.Client) (interface{}, error)) (interface{}, error) {
// Initialize the ECS client if necessary
if client.ecsconn == nil {
endpoint := client.Config.OssEndpoint
if endpoint == "" {
return nil, fmt.Errorf("unable to initialize the oss client: endpoint or domain is not provided for ecs service")
}
if endpoint != "" {
endpoints.AddEndpointMapping(client.Config.RegionId, string(ECSCode), endpoint)
}
if strings.HasPrefix(endpoint, "http") {
endpoint = strings.TrimPrefix(strings.TrimPrefix(endpoint, "http://"), "https://")
}
ecsconn, err := ecs.NewClientWithOptions(client.Config.RegionId, client.getSdkConfig().WithTimeout(time.Duration(60)*time.Second), client.Config.getAuthCredential(true))
if err != nil {
return nil, fmt.Errorf("unable to initialize the ECS client: %#v", err)
}
ecsconn.Domain = endpoint
ecsconn.AppendUserAgent(Terraform, TerraformVersion)
ecsconn.AppendUserAgent(Provider, ProviderVersion)
ecsconn.AppendUserAgent(Module, client.Config.ConfigurationSource)
ecsconn.SetHTTPSInsecure(client.Config.Insecure)
if client.Config.Proxy != "" {
ecsconn.SetHttpsProxy(client.Config.Proxy)
ecsconn.SetHttpProxy(client.Config.Proxy)
}
client.ecsconn = ecsconn
}
return do(client.ecsconn)
}
func (client *ApsaraStackClient) describeEndpointForService(serviceCode string) (*location.Endpoint, error) {
args := location.CreateDescribeEndpointsRequest()
args.ServiceCode = serviceCode
args.Id = client.Config.RegionId
args.Domain = client.Config.LocationEndpoint
if args.Domain == "" {
args.Domain = "location-readonly.aliyuncs.com"
}
locationClient, err := location.NewClientWithOptions(client.Config.RegionId, client.getSdkConfig(), client.Config.getAuthCredential(true))
if err != nil {
return nil, fmt.Errorf("Unable to initialize the location client: %#v", err)
}
locationClient.AppendUserAgent(Terraform, TerraformVersion)
locationClient.AppendUserAgent(Provider, ProviderVersion)
locationClient.AppendUserAgent(Module, client.Config.ConfigurationSource)
locationClient.SetHTTPSInsecure(client.Config.Insecure)
if client.Config.Proxy != "" {
locationClient.SetHttpsProxy(client.Config.Proxy)
}
endpointsResponse, err := locationClient.DescribeEndpoints(args)
if err != nil {
return nil, fmt.Errorf("Describe %s endpoint using region: %#v got an error: %#v.", serviceCode, client.RegionId, err)
}
if endpointsResponse != nil && len(endpointsResponse.Endpoints.Endpoint) > 0 {
for _, e := range endpointsResponse.Endpoints.Endpoint {
if e.Type == "openAPI" {
return &e, nil
}
}
}
return nil, fmt.Errorf("There is no any available endpoint for %s in region %s.", serviceCode, client.RegionId)
}
func (client *ApsaraStackClient) NewCommonRequest(product, serviceCode, schema string, apiVersion ApiVersion) (*requests.CommonRequest, error) {
request := requests.NewCommonRequest()
if strings.ToLower(client.Config.Protocol) == "https" {
request.Scheme = "https"
} else {
request.Scheme = "http"
}
if client.Config.Insecure {
request.SetHTTPSInsecure(client.Config.Insecure)
}
var endpoint string
if strings.ToUpper(product) == "SLB" {
endpoint = client.Config.SlbEndpoint
}
if strings.ToUpper(product) == "ECS" {
endpoint = client.Config.EcsEndpoint
}
if strings.ToUpper(product) == "ASCM" {
endpoint = client.Config.AscmEndpoint
}
if endpoint == "" {
endpointItem, err := client.describeEndpointForService(serviceCode)
if err != nil {
return nil, fmt.Errorf("describeEndpointForService got an error: %#v.", err)
}
if endpointItem != nil {
endpoint = endpointItem.Endpoint
}
}
// Use product code to find product domain
if endpoint != "" {
request.Domain = endpoint
} else {
// When getting endpoint failed by location, using custom endpoint instead
request.Domain = fmt.Sprintf("%s.%s.aliyuncs.com", strings.ToLower(serviceCode), client.RegionId)
}
request.Version = string(apiVersion)
request.RegionId = client.RegionId
request.Product = product
if strings.ToUpper(product) == "SLB" {
request.QueryParams = map[string]string{"AccessKeySecret": client.SecretKey, "Product": "slb", "Department": client.Department, "ResourceGroup": client.ResourceGroup, "Version": string(apiVersion)}
}
if strings.ToUpper(product) == "ECS" {
request.QueryParams = map[string]string{"AccessKeySecret": client.SecretKey, "Product": "ecs", "Department": client.Department, "ResourceGroup": client.ResourceGroup, "Version": string(apiVersion)}
}
if strings.ToUpper(product) == "ASCM" {
request.QueryParams = map[string]string{"AccessKeySecret": client.SecretKey, "Product": "ascm", "Department": client.Department, "ResourceGroup": client.ResourceGroup, "Version": string(apiVersion)}
}
request.AppendUserAgent(Terraform, TerraformVersion)
request.AppendUserAgent(Provider, ProviderVersion)
request.AppendUserAgent(Module, client.Config.ConfigurationSource)
request.SetHTTPSInsecure(client.Config.Insecure)
return request, nil
}
func (client *ApsaraStackClient) getSdkConfig() *sdk.Config {
log.Printf("Protocol is set to %s", client.Config.Protocol)
return sdk.NewConfig().
WithMaxRetryTime(DefaultClientRetryCountSmall).
WithTimeout(time.Duration(30) * time.Second).
WithEnableAsync(true).
WithGoRoutinePoolSize(100).
WithMaxTaskQueueSize(10000).
WithDebug(false).
WithHttpTransport(client.getTransport()).
WithScheme(strings.ToLower(client.Config.Protocol))
}
func (client *ApsaraStackClient) getTransport() *http.Transport {
handshakeTimeout, err := strconv.Atoi(os.Getenv("TLSHandshakeTimeout"))
if err != nil {
handshakeTimeout = 120
}
transport := &http.Transport{}
transport.TLSHandshakeTimeout = time.Duration(handshakeTimeout) * time.Second
return transport
}
func (client *ApsaraStackClient) AccountId() (string, error) {
client.accountIdMutex.Lock()
defer client.accountIdMutex.Unlock()
if client.accountId == "" {
log.Printf("[DEBUG] account_id not provided, attempting to retrieve it automatically...")
identity, err := client.GetCallerIdentity()
if err != nil {
return "", err
}
if identity == "" {
return "", fmt.Errorf("caller identity doesn't contain any AccountId")
}
client.accountId = identity
}
return client.accountId, nil
}
func (client *ApsaraStackClient) getHttpProxy() (proxy *url.URL, err error) {
if client.Config.Protocol == "HTTPS" {
if rawurl := os.Getenv("HTTPS_PROXY"); rawurl != "" {
proxy, err = url.Parse(rawurl)
} else if rawurl := os.Getenv("https_proxy"); rawurl != "" {
proxy, err = url.Parse(rawurl)
}
} else {
if rawurl := os.Getenv("HTTP_PROXY"); rawurl != "" {
proxy, err = url.Parse(rawurl)
} else if rawurl := os.Getenv("http_proxy"); rawurl != "" {
proxy, err = url.Parse(rawurl)
}
}
return proxy, err
}
func (client *ApsaraStackClient) skipProxy(endpoint string) (bool, error) {
var urls []string
if rawurl := os.Getenv("NO_PROXY"); rawurl != "" {
urls = strings.Split(rawurl, ",")
} else if rawurl := os.Getenv("no_proxy"); rawurl != "" {
urls = strings.Split(rawurl, ",")
}
for _, value := range urls {
if strings.HasPrefix(value, "*") {
value = fmt.Sprintf(".%s", value)
}
noProxyReg, err := regexp.Compile(value)
if err != nil {
return false, err
}
if noProxyReg.MatchString(endpoint) {
return true, nil
}
}
return false, nil
}
func (client *ApsaraStackClient) WithKmsClient(do func(*kms.Client) (interface{}, error)) (interface{}, error) {
// Initialize the KMS client if necessary
if client.kmsconn == nil {
endpoint := client.Config.KmsEndpoint
if endpoint == "" {
return nil, fmt.Errorf("unable to initialize the kms client: endpoint or domain is not provided for KMS service")
}
if endpoint != "" {
endpoints.AddEndpointMapping(client.Config.RegionId, string(KMSCode), endpoint)
}
kmsconn, err := kms.NewClientWithOptions(client.Config.RegionId, client.getSdkConfig(), client.Config.getAuthCredential(true))
if err != nil {
return nil, fmt.Errorf("unable to initialize the kms client: %#v", err)
}
kmsconn.AppendUserAgent(Terraform, TerraformVersion)
kmsconn.Domain = endpoint
kmsconn.AppendUserAgent(Provider, ProviderVersion)
kmsconn.AppendUserAgent(Module, client.Config.ConfigurationSource)
kmsconn.SetHTTPSInsecure(client.Config.Insecure)
if client.Config.Proxy != "" {
kmsconn.SetHttpProxy(client.Config.Proxy)
}
client.kmsconn = kmsconn
}
return do(client.kmsconn)
}
func (client *ApsaraStackClient) GetCallerIdentity() (string, error) {
endpoint := client.Config.AscmEndpoint
if endpoint == "" {
return "", fmt.Errorf("unable to initialize the ascm client: endpoint or domain is not provided for ascm service")
}
if endpoint != "" {
endpoints.AddEndpointMapping(client.Config.RegionId, string(ASCMCode), endpoint)
}
ascmClient, err := sdk.NewClientWithAccessKey(client.Config.RegionId, client.Config.AccessKey, client.Config.SecretKey)
if err != nil {
return "", fmt.Errorf("unable to initialize the ascm client: %#v", err)
}
ascmClient.AppendUserAgent(Terraform, TerraformVersion)
ascmClient.AppendUserAgent(Provider, ProviderVersion)
ascmClient.AppendUserAgent(Module, client.Config.ConfigurationSource)
ascmClient.SetHTTPSInsecure(client.Config.Insecure)
ascmClient.Domain = endpoint
if client.Config.Proxy != "" {
ascmClient.SetHttpProxy(client.Config.Proxy)
}
if client.Config.Department == "" || client.Config.ResourceGroup == "" {
return "", fmt.Errorf("unable to initialize the ascm client: department or resource_group is not provided")
}
request := requests.NewCommonRequest()
if strings.ToLower(client.Config.Protocol) == "https" {
request.Scheme = "https"
} else {
request.Scheme = "http"
}
if client.Config.Insecure {
request.SetHTTPSInsecure(client.Config.Insecure)
}
request.Method = "GET" // Set request method
request.Product = "ascm" // Specify product
request.Domain = endpoint // Location Service will not be enabled if the host is specified. For example, service with a Certification type-Bearer Token should be specified
request.Version = "2019-05-10" // Specify product version
request.ApiName = "GetUserInfo"
request.QueryParams = map[string]string{
"AccessKeySecret": client.Config.SecretKey,
"Product": "ascm",
"Department": client.Config.Department,
"ResourceGroup": client.Config.ResourceGroup,
"RegionId": client.RegionId,
"Action": "GetAllNavigationInfo",
"Version": "2019-05-10",
"SignatureVersion": "1.0",
}
resp := responses.BaseResponse{}
request.TransToAcsRequest()
err = ascmClient.DoAction(request, &resp)
if err != nil {
return "", err
}
response := &AccountId{}
err = json.Unmarshal(resp.GetHttpContentBytes(), response)
ownerId := response.Data.PrimaryKey
if ownerId == "" {
return "", fmt.Errorf("ownerId not found")
}
return ownerId, err
}
type AccountId struct {
Data struct {
PrimaryKey string `json:"primaryKey"`
} `json:"data"`
}
func (client *ApsaraStackClient) WithBssopenapiClient(do func(*bssopenapi.Client) (interface{}, error)) (interface{}, error) {
// Initialize the bssopenapi client if necessary
if client.bssopenapiconn == nil {
endpoint := client.Config.BssOpenApiEndpoint
if endpoint == "" {
return nil, fmt.Errorf("unable to initialize the bss client: endpoint or domain is not provided for bss service")
}
if endpoint != "" {
endpoints.AddEndpointMapping(client.Config.RegionId, string(BSSOPENAPICode), endpoint)
}
bssopenapiconn, err := bssopenapi.NewClientWithOptions(client.Config.RegionId, client.getSdkConfig(), client.Config.getAuthCredential(true))
if err != nil {
return nil, fmt.Errorf("unable to initialize the BSSOPENAPI client: %#v", err)
}
bssopenapiconn.AppendUserAgent(Terraform, TerraformVersion)
bssopenapiconn.AppendUserAgent(Provider, ProviderVersion)
bssopenapiconn.AppendUserAgent(Module, client.Config.ConfigurationSource)
bssopenapiconn.SetHTTPSInsecure(client.Config.Insecure)
if client.Config.Proxy != "" {
bssopenapiconn.SetHttpsProxy(client.Config.Proxy)
}
client.bssopenapiconn = bssopenapiconn
}
return do(client.bssopenapiconn)
}
func (client *ApsaraStackClient) WithOssClient(do func(*oss.Client) (interface{}, error)) (interface{}, error) {
goSdkMutex.Lock()
defer goSdkMutex.Unlock()
// Initialize the OSS client if necessary
if client.ossconn == nil {
schma := "http"
endpoint := client.Config.OssEndpoint
if endpoint == "" {
return nil, fmt.Errorf("unable to initialize the oss client: endpoint or domain is not provided for OSS service")
}
if endpoint == "" {
endpointItem, _ := client.describeEndpointForService(strings.ToLower(string(OSSCode)))
if endpointItem != nil {
if len(endpointItem.Protocols.Protocols) > 0 {
// HTTP or HTTPS
schma = strings.ToLower(endpointItem.Protocols.Protocols[0])
for _, p := range endpointItem.Protocols.Protocols {
if strings.ToLower(p) == "http" {
schma = strings.ToLower(p)
break
}
}
}
endpoint = endpointItem.Endpoint
}
}
if !strings.HasPrefix(endpoint, "http") {
endpoint = fmt.Sprintf("%s://%s", schma, endpoint)
}
clientOptions := []oss.ClientOption{oss.UserAgent(client.getUserAgent()),
oss.SecurityToken(client.Config.SecurityToken)}
if client.Config.Proxy != "" {
clientOptions = append(clientOptions, oss.Proxy(client.Config.Proxy))
}
clientOptions = append(clientOptions, oss.UseCname(false))
ossconn, err := oss.New(endpoint, client.Config.AccessKey, client.Config.SecretKey, clientOptions...)
if err != nil {
return nil, fmt.Errorf("unable to initialize the OSS client: %#v", err)
}
client.ossconn = ossconn
}
return do(client.ossconn)
}
func (client *ApsaraStackClient) WithRamClient(do func(*ram.Client) (interface{}, error)) (interface{}, error) {
// Initialize the RAM client if necessary
if client.ramconn == nil {
endpoint := client.Config.RamEndpoint
if endpoint == "" {
return nil, fmt.Errorf("unable to initialize the ram client: endpoint or domain is not provided for ram operation")
}
if strings.HasPrefix(endpoint, "http") {
endpoint = fmt.Sprintf("https://%s", strings.TrimPrefix(endpoint, "http://"))
}
if endpoint != "" {
endpoints.AddEndpointMapping(client.Config.RegionId, string(RAMCode), endpoint)
}
ramconn, err := ram.NewClientWithOptions(client.Config.RegionId, client.getSdkConfig(), client.Config.getAuthCredential(true))
if err != nil {
return nil, fmt.Errorf("unable to initialize the RAM client: %#v", err)
}
ramconn.AppendUserAgent(Terraform, TerraformVersion)
ramconn.AppendUserAgent(Provider, ProviderVersion)
ramconn.AppendUserAgent(Module, client.Config.ConfigurationSource)
ramconn.SetHTTPSInsecure(client.Config.Insecure)
if client.Config.Proxy != "" {
ramconn.SetHttpsProxy(client.Config.Proxy)
}
client.ramconn = ramconn
}
return do(client.ramconn)
}
func (client *ApsaraStackClient) WithRdsClient(do func(*rds.Client) (interface{}, error)) (interface{}, error) {
// Initialize the RDS client if necessary
if client.rdsconn == nil {
endpoint := client.Config.RdsEndpoint
if endpoint == "" {
return nil, fmt.Errorf("unable to initialize the rds client: endpoint or domain is not provided for RDS service")
}
if endpoint != "" {
endpoints.AddEndpointMapping(client.Config.RegionId, string(RDSCode), endpoint)
}
rdsconn, err := rds.NewClientWithOptions(client.Config.RegionId, client.getSdkConfig(), client.Config.getAuthCredential(true))
if err != nil {
return nil, fmt.Errorf("unable to initialize the RDS client: %#v", err)
}
rdsconn.Domain = endpoint
rdsconn.AppendUserAgent(Terraform, TerraformVersion)
rdsconn.AppendUserAgent(Provider, ProviderVersion)
rdsconn.AppendUserAgent(Module, client.Config.ConfigurationSource)
rdsconn.SetHTTPSInsecure(client.Config.Insecure)
if client.Config.Proxy != "" {
rdsconn.SetHttpProxy(client.Config.Proxy)
}
client.rdsconn = rdsconn
}
return do(client.rdsconn)
}
func (client *ApsaraStackClient) WithCdnClient_new(do func(*cdn_new.Client) (interface{}, error)) (interface{}, error) {
// Initialize the CDN client if necessary
if client.cdnconn_new == nil {
endpoint := client.Config.CdnEndpoint
if endpoint == "" {
return nil, fmt.Errorf("unable to initialize the CDN client: endpoint or domain is not provided for CDN service")
}
if endpoint != "" {
endpoints.AddEndpointMapping(client.Config.RegionId, string(CDNCode), endpoint)
}
cdnconn, err := cdn_new.NewClientWithOptions(client.Config.RegionId, client.getSdkConfig(), client.Config.getAuthCredential(true))
if err != nil {
return nil, fmt.Errorf("unable to initialize the CDN client: %#v", err)
}
cdnconn.AppendUserAgent(Terraform, TerraformVersion)
cdnconn.AppendUserAgent(Provider, ProviderVersion)
cdnconn.AppendUserAgent(Module, client.Config.ConfigurationSource)
cdnconn.SetHTTPSInsecure(client.Config.Insecure)
if client.Config.Proxy != "" {
cdnconn.SetHttpsProxy(client.Config.Proxy)
}
client.cdnconn_new = cdnconn
}
return do(client.cdnconn_new)
}
func (client *ApsaraStackClient) getUserAgent() string {
return fmt.Sprintf("%s/%s %s/%s %s/%s", Terraform, TerraformVersion, Provider, ProviderVersion, Module, client.Config.ConfigurationSource)
}
func (client *ApsaraStackClient) WithCsClient(do func(*cs.Client) (interface{}, error)) (interface{}, error) {
goSdkMutex.Lock()
defer goSdkMutex.Unlock()
// Initialize the CS client if necessary
if client.csconn == nil {
csconn := cs.NewClientForAussumeRole(client.Config.AccessKey, client.Config.SecretKey, client.Config.SecurityToken)
csconn.SetUserAgent(client.getUserAgent())
endpoint := client.Config.CsEndpoint
if endpoint == "" {
return nil, fmt.Errorf("unable to initialize the cs client: endpoint or domain is not provided for cs service")
}
if endpoint != "" {
if !strings.HasPrefix(endpoint, "http") {
endpoint = fmt.Sprintf("https://%s", strings.TrimPrefix(endpoint, "://"))
}
csconn.SetEndpoint(endpoint)
}
if client.Config.Proxy != "" {
os.Setenv("http_proxy", client.Config.Proxy)
}
client.csconn = csconn
}
return do(client.csconn)
}
func (client *ApsaraStackClient) getHttpProxyUrl() *url.URL {
for _, v := range []string{"HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy"} {
value := strings.Trim(os.Getenv(v), " ")
if value != "" {
if !regexp.MustCompile(`^http(s)?://`).MatchString(value) {
value = fmt.Sprintf("https://%s", value)
}
proxyUrl, err := url.Parse(value)
if err == nil {
return proxyUrl
}
break