/
config.go
875 lines (730 loc) · 29.1 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
package ibm
import (
"errors"
"fmt"
"log"
gohttp "net/http"
"os"
"strings"
"time"
// Added code for the Power Colo Offering
apigateway "github.com/IBM/apigateway-go-sdk"
"github.com/apache/incubator-openwhisk-client-go/whisk"
jwt "github.com/dgrijalva/jwt-go"
slsession "github.com/softlayer/softlayer-go/session"
issession "github.ibm.com/Bluemix/riaas-go-client/session"
bluemix "github.com/IBM-Cloud/bluemix-go"
"github.com/IBM-Cloud/bluemix-go/api/account/accountv1"
"github.com/IBM-Cloud/bluemix-go/api/account/accountv2"
"github.com/IBM-Cloud/bluemix-go/api/certificatemanager"
"github.com/IBM-Cloud/bluemix-go/api/cis/cisv1"
"github.com/IBM-Cloud/bluemix-go/api/container/containerv1"
"github.com/IBM-Cloud/bluemix-go/api/container/containerv2"
"github.com/IBM-Cloud/bluemix-go/api/globalsearch/globalsearchv2"
"github.com/IBM-Cloud/bluemix-go/api/globaltagging/globaltaggingv3"
"github.com/IBM-Cloud/bluemix-go/api/iam/iamv1"
"github.com/IBM-Cloud/bluemix-go/api/iampap/iampapv1"
"github.com/IBM-Cloud/bluemix-go/api/iamuum/iamuumv1"
"github.com/IBM-Cloud/bluemix-go/api/iamuum/iamuumv2"
"github.com/IBM-Cloud/bluemix-go/api/icd/icdv4"
"github.com/IBM-Cloud/bluemix-go/api/mccp/mccpv2"
"github.com/IBM-Cloud/bluemix-go/api/resource/resourcev1/catalog"
"github.com/IBM-Cloud/bluemix-go/api/resource/resourcev1/controller"
"github.com/IBM-Cloud/bluemix-go/api/resource/resourcev1/management"
"github.com/IBM-Cloud/bluemix-go/api/resource/resourcev2/managementv2"
"github.com/IBM-Cloud/bluemix-go/api/schematics"
"github.com/IBM-Cloud/bluemix-go/api/usermanagement/usermanagementv2"
"github.com/IBM-Cloud/bluemix-go/authentication"
"github.com/IBM-Cloud/bluemix-go/http"
"github.com/IBM-Cloud/bluemix-go/rest"
bxsession "github.com/IBM-Cloud/bluemix-go/session"
ibmpisession "github.com/IBM-Cloud/power-go-client/ibmpisession"
"github.com/IBM-Cloud/terraform-provider-ibm/version"
dns "github.com/IBM/dns-svcs-go-sdk/dnssvcsv1"
"github.com/IBM/go-sdk-core/v3/core"
kp "github.com/IBM/keyprotect-go-client"
vpcclassic "github.ibm.com/ibmcloud/vpc-go-sdk/vpcclassicv1"
vpc "github.ibm.com/ibmcloud/vpc-go-sdk/vpcv1"
)
//RetryDelay
const RetryAPIDelay = 5 * time.Second
//BluemixRegion ...
var BluemixRegion string
//VPCAPIVersion ...
var VPCAPIVersion = "2020-03-24"
var (
errEmptySoftLayerCredentials = errors.New("iaas_classic_username and iaas_classic_api_key must be provided. Please see the documentation on how to configure them")
errEmptyBluemixCredentials = errors.New("ibmcloud_api_key or bluemix_api_key or iam_token and iam_refresh_token must be provided. Please see the documentation on how to configure it")
)
//UserConfig ...
type UserConfig struct {
userID string
userEmail string
userAccount string
cloudName string `default:"bluemix"`
cloudType string `default:"public"`
generation int `default:"2"`
}
//Config stores user provider input
type Config struct {
//BluemixAPIKey is the Bluemix api key
BluemixAPIKey string
//Bluemix region
Region string
//Resource group id
ResourceGroup string
//Bluemix API timeout
BluemixTimeout time.Duration
//Softlayer end point url
SoftLayerEndpointURL string
//Softlayer API timeout
SoftLayerTimeout time.Duration
// Softlayer User Name
SoftLayerUserName string
// Softlayer API Key
SoftLayerAPIKey string
//Retry Count for API calls
//Unexposed in the schema at this point as they are used only during session creation for a few calls
//When sdk implements it we an expose them for expected behaviour
//https://github.com/softlayer/softlayer-go/issues/41
RetryCount int
//Constant Retry Delay for API calls
RetryDelay time.Duration
// FunctionNameSpace ...
FunctionNameSpace string
//Riaas End point
RiaasEndPoint string
//Generation
Generation int
//IAM Token
IAMToken string
//IAM Refresh Token
IAMRefreshToken string
// PowerService Instance
PowerServiceInstance string
// Zone
Zone string
}
//Session stores the information required for communication with the SoftLayer and Bluemix API
type Session struct {
// SoftLayerSesssion is the the SoftLayer session used to connect to the SoftLayer API
SoftLayerSession *slsession.Session
// BluemixSession is the the Bluemix session used to connect to the Bluemix API
BluemixSession *bxsession.Session
}
// ClientSession ...
type ClientSession interface {
BluemixSession() (*bxsession.Session, error)
BluemixAcccountAPI() (accountv2.AccountServiceAPI, error)
BluemixAcccountv1API() (accountv1.AccountServiceAPI, error)
BluemixUserDetails() (*UserConfig, error)
ContainerAPI() (containerv1.ContainerServiceAPI, error)
VpcContainerAPI() (containerv2.ContainerServiceAPI, error)
CisAPI() (cisv1.CisServiceAPI, error)
FunctionClient() (*whisk.Client, error)
GlobalSearchAPI() (globalsearchv2.GlobalSearchServiceAPI, error)
GlobalTaggingAPI() (globaltaggingv3.GlobalTaggingServiceAPI, error)
ICDAPI() (icdv4.ICDServiceAPI, error)
IAMAPI() (iamv1.IAMServiceAPI, error)
IAMPAPAPI() (iampapv1.IAMPAPAPI, error)
IAMUUMAPI() (iamuumv1.IAMUUMServiceAPI, error)
IAMUUMAPIV2() (iamuumv2.IAMUUMServiceAPIv2, error)
ISSession() (*issession.Session, error)
MccpAPI() (mccpv2.MccpServiceAPI, error)
ResourceCatalogAPI() (catalog.ResourceCatalogAPI, error)
ResourceManagementAPI() (management.ResourceManagementAPI, error)
ResourceManagementAPIv2() (managementv2.ResourceManagementAPIv2, error)
ResourceControllerAPI() (controller.ResourceControllerAPI, error)
SoftLayerSession() *slsession.Session
IBMPISession() (*ibmpisession.IBMPISession, error)
SchematicsAPI() (schematics.SchematicsServiceAPI, error)
UserManagementAPI() (usermanagementv2.UserManagementAPI, error)
CertificateManagerAPI() (certificatemanager.CertificateManagerServiceAPI, error)
keyProtectAPI() (*kp.Client, error)
VpcClassicV1API() (*vpcclassic.VpcClassicV1, error)
VpcV1API() (*vpc.VpcV1, error)
APIGateway() (*apigateway.ApiGatewayControllerApiV1, error)
PrivateDnsClientSession() (*dns.DnsSvcsV1, error)
}
type clientSession struct {
session *Session
apigatewayErr error
apigatewayAPI *apigateway.ApiGatewayControllerApiV1
accountConfigErr error
bmxAccountServiceAPI accountv2.AccountServiceAPI
accountV1ConfigErr error
bmxAccountv1ServiceAPI accountv1.AccountServiceAPI
bmxUserDetails *UserConfig
bmxUserFetchErr error
csConfigErr error
csServiceAPI containerv1.ContainerServiceAPI
csv2ConfigErr error
csv2ServiceAPI containerv2.ContainerServiceAPI
stxConfigErr error
stxServiceAPI schematics.SchematicsServiceAPI
certManagementErr error
certManagementAPI certificatemanager.CertificateManagerServiceAPI
cfConfigErr error
cfServiceAPI mccpv2.MccpServiceAPI
cisConfigErr error
cisServiceAPI cisv1.CisServiceAPI
functionConfigErr error
functionClient *whisk.Client
globalSearchConfigErr error
globalSearchServiceAPI globalsearchv2.GlobalSearchServiceAPI
globalTaggingConfigErr error
globalTaggingServiceAPI globaltaggingv3.GlobalTaggingServiceAPI
iamPAPConfigErr error
iamPAPServiceAPI iampapv1.IAMPAPAPI
iamUUMConfigErr error
iamUUMServiceAPI iamuumv1.IAMUUMServiceAPI
iamUUMConfigErrV2 error
iamUUMServiceAPIV2 iamuumv2.IAMUUMServiceAPIv2
iamConfigErr error
iamServiceAPI iamv1.IAMServiceAPI
userManagementErr error
userManagementAPI usermanagementv2.UserManagementAPI
icdConfigErr error
icdServiceAPI icdv4.ICDServiceAPI
isConfigErr error
isSession *issession.Session
resourceControllerConfigErr error
resourceControllerServiceAPI controller.ResourceControllerAPI
resourceManagementConfigErr error
resourceManagementServiceAPI management.ResourceManagementAPI
resourceManagementConfigErrv2 error
resourceManagementServiceAPIv2 managementv2.ResourceManagementAPIv2
resourceCatalogConfigErr error
resourceCatalogServiceAPI catalog.ResourceCatalogAPI
powerConfigErr error
ibmpiConfigErr error
ibmpiSession *ibmpisession.IBMPISession
kpErr error
kpAPI *kp.API
pDnsClient *dns.DnsSvcsV1
pDnsErr error
bluemixSessionErr error
vpcClassicErr error
vpcClassicAPI *vpcclassic.VpcClassicV1
vpcErr error
vpcAPI *vpc.VpcV1
}
// BluemixAcccountAPI ...
func (sess clientSession) BluemixAcccountAPI() (accountv2.AccountServiceAPI, error) {
return sess.bmxAccountServiceAPI, sess.accountConfigErr
}
// BluemixAcccountAPI ...
func (sess clientSession) BluemixAcccountv1API() (accountv1.AccountServiceAPI, error) {
return sess.bmxAccountv1ServiceAPI, sess.accountV1ConfigErr
}
// BluemixSession to provide the Bluemix Session
func (sess clientSession) BluemixSession() (*bxsession.Session, error) {
return sess.session.BluemixSession, sess.bluemixSessionErr
}
// BluemixUserDetails ...
func (sess clientSession) BluemixUserDetails() (*UserConfig, error) {
return sess.bmxUserDetails, sess.bmxUserFetchErr
}
// ContainerAPI provides Container Service APIs ...
func (sess clientSession) ContainerAPI() (containerv1.ContainerServiceAPI, error) {
return sess.csServiceAPI, sess.csConfigErr
}
// VpcContainerAPI provides v2Container Service APIs ...
func (sess clientSession) VpcContainerAPI() (containerv2.ContainerServiceAPI, error) {
return sess.csv2ServiceAPI, sess.csv2ConfigErr
}
// SchematicsAPI provides schematics Service APIs ...
func (sess clientSession) SchematicsAPI() (schematics.SchematicsServiceAPI, error) {
return sess.stxServiceAPI, sess.stxConfigErr
}
// CisAPI provides Cloud Internet Services APIs ...
func (sess clientSession) CisAPI() (cisv1.CisServiceAPI, error) {
return sess.cisServiceAPI, sess.cisConfigErr
}
// FunctionClient ...
func (sess clientSession) FunctionClient() (*whisk.Client, error) {
return sess.functionClient, sess.functionConfigErr
}
// GlobalSearchAPI provides Global Search APIs ...
func (sess clientSession) GlobalSearchAPI() (globalsearchv2.GlobalSearchServiceAPI, error) {
return sess.globalSearchServiceAPI, sess.globalSearchConfigErr
}
// GlobalTaggingAPI provides Global Search APIs ...
func (sess clientSession) GlobalTaggingAPI() (globaltaggingv3.GlobalTaggingServiceAPI, error) {
return sess.globalTaggingServiceAPI, sess.globalTaggingConfigErr
}
// IAMAPI provides IAM PAP APIs ...
func (sess clientSession) IAMAPI() (iamv1.IAMServiceAPI, error) {
return sess.iamServiceAPI, sess.iamConfigErr
}
// UserManagementAPI provides User management APIs ...
func (sess clientSession) UserManagementAPI() (usermanagementv2.UserManagementAPI, error) {
return sess.userManagementAPI, sess.userManagementErr
}
// IAMPAPAPI provides IAM PAP APIs ...
func (sess clientSession) IAMPAPAPI() (iampapv1.IAMPAPAPI, error) {
return sess.iamPAPServiceAPI, sess.iamPAPConfigErr
}
// IAMUUMAPI provides IAM UUM APIs ...
func (sess clientSession) IAMUUMAPI() (iamuumv1.IAMUUMServiceAPI, error) {
return sess.iamUUMServiceAPI, sess.iamUUMConfigErr
}
// IAMUUMAPIV2 provides IAM UUM APIs ...
func (sess clientSession) IAMUUMAPIV2() (iamuumv2.IAMUUMServiceAPIv2, error) {
return sess.iamUUMServiceAPIV2, sess.iamUUMConfigErrV2
}
// IcdAPI provides IBM Cloud Databases APIs ...
func (sess clientSession) ICDAPI() (icdv4.ICDServiceAPI, error) {
return sess.icdServiceAPI, sess.icdConfigErr
}
// ISSession to provide the IS RIASS Session
func (sess clientSession) ISSession() (*issession.Session, error) {
return sess.isSession, sess.isConfigErr
}
// MccpAPI provides Multi Cloud Controller Proxy APIs ...
func (sess clientSession) MccpAPI() (mccpv2.MccpServiceAPI, error) {
return sess.cfServiceAPI, sess.cfConfigErr
}
// ResourceCatalogAPI ...
func (sess clientSession) ResourceCatalogAPI() (catalog.ResourceCatalogAPI, error) {
return sess.resourceCatalogServiceAPI, sess.resourceCatalogConfigErr
}
// ResourceManagementAPI ...
func (sess clientSession) ResourceManagementAPI() (management.ResourceManagementAPI, error) {
return sess.resourceManagementServiceAPI, sess.resourceManagementConfigErr
}
// ResourceManagementAPIv2 ...
func (sess clientSession) ResourceManagementAPIv2() (managementv2.ResourceManagementAPIv2, error) {
return sess.resourceManagementServiceAPIv2, sess.resourceManagementConfigErrv2
}
// ResourceControllerAPI ...
func (sess clientSession) ResourceControllerAPI() (controller.ResourceControllerAPI, error) {
return sess.resourceControllerServiceAPI, sess.resourceControllerConfigErr
}
// SoftLayerSession providers SoftLayer Session
func (sess clientSession) SoftLayerSession() *slsession.Session {
return sess.session.SoftLayerSession
}
// CertManagementAPI provides Certificate management APIs ...
func (sess clientSession) CertificateManagerAPI() (certificatemanager.CertificateManagerServiceAPI, error) {
return sess.certManagementAPI, sess.certManagementErr
}
//apigatewayAPI provides API Gateway APIs
func (sess clientSession) APIGateway() (*apigateway.ApiGatewayControllerApiV1, error) {
return sess.apigatewayAPI, sess.apigatewayErr
}
func (sess clientSession) keyProtectAPI() (*kp.Client, error) {
return sess.kpAPI, sess.kpErr
}
func (sess clientSession) VpcClassicV1API() (*vpcclassic.VpcClassicV1, error) {
return sess.vpcClassicAPI, sess.vpcClassicErr
}
func (sess clientSession) VpcV1API() (*vpc.VpcV1, error) {
return sess.vpcAPI, sess.vpcErr
}
// Session to the Power Colo Service
func (sess clientSession) IBMPISession() (*ibmpisession.IBMPISession, error) {
return sess.ibmpiSession, sess.powerConfigErr
}
// Private DNS Service
func (sess clientSession) PrivateDnsClientSession() (*dns.DnsSvcsV1, error) {
return sess.pDnsClient, sess.pDnsErr
}
// ClientSession configures and returns a fully initialized ClientSession
func (c *Config) ClientSession() (interface{}, error) {
sess, err := newSession(c)
if err != nil {
return nil, err
}
session := clientSession{
session: sess,
}
if sess.BluemixSession == nil {
//Can be nil only if bluemix_api_key is not provided
log.Println("Skipping Bluemix Clients configuration")
session.bluemixSessionErr = errEmptyBluemixCredentials
session.accountConfigErr = errEmptyBluemixCredentials
session.accountV1ConfigErr = errEmptyBluemixCredentials
session.csConfigErr = errEmptyBluemixCredentials
session.csv2ConfigErr = errEmptyBluemixCredentials
session.kpErr = errEmptyBluemixCredentials
session.stxConfigErr = errEmptyBluemixCredentials
session.cfConfigErr = errEmptyBluemixCredentials
session.cisConfigErr = errEmptyBluemixCredentials
session.functionConfigErr = errEmptyBluemixCredentials
session.globalSearchConfigErr = errEmptyBluemixCredentials
session.globalTaggingConfigErr = errEmptyBluemixCredentials
session.iamConfigErr = errEmptyBluemixCredentials
session.iamPAPConfigErr = errEmptyBluemixCredentials
session.iamUUMConfigErr = errEmptyBluemixCredentials
session.iamUUMConfigErrV2 = errEmptyBluemixCredentials
session.icdConfigErr = errEmptyBluemixCredentials
session.resourceCatalogConfigErr = errEmptyBluemixCredentials
session.resourceManagementConfigErr = errEmptyBluemixCredentials
session.resourceManagementConfigErrv2 = errEmptyBluemixCredentials
session.resourceControllerConfigErr = errEmptyBluemixCredentials
session.isConfigErr = errEmptyBluemixCredentials
session.powerConfigErr = errEmptyBluemixCredentials
session.ibmpiConfigErr = errEmptyBluemixCredentials
session.userManagementErr = errEmptyBluemixCredentials
session.certManagementErr = errEmptyBluemixCredentials
session.vpcClassicErr = errEmptyBluemixCredentials
session.vpcErr = errEmptyBluemixCredentials
session.apigatewayErr = errEmptyBluemixCredentials
session.pDnsErr = errEmptyBluemixCredentials
return session, nil
}
if sess.BluemixSession.Config.BluemixAPIKey != "" {
err = authenticateAPIKey(sess.BluemixSession)
if err != nil {
session.bmxUserFetchErr = fmt.Errorf("Error occured while fetching account user details: %q", err)
session.functionConfigErr = fmt.Errorf("Error occured while fetching auth key for function: %q", err)
session.isConfigErr = fmt.Errorf("Error occured while fetching auth key for vpc: %q", err)
session.powerConfigErr = fmt.Errorf("Error occured while fetching the auth key for power iaas: %q", err)
session.ibmpiConfigErr = fmt.Errorf("Error occured while fetching the auth key for power iaas: %q", err)
}
err = authenticateCF(sess.BluemixSession)
if err != nil {
session.functionConfigErr = fmt.Errorf("Error occured while fetching auth key for function: %q", err)
}
}
if sess.BluemixSession.Config.IAMAccessToken != "" && sess.BluemixSession.Config.BluemixAPIKey == "" {
err := refreshToken(sess.BluemixSession)
if err != nil {
return nil, err
}
}
userConfig, err := fetchUserDetails(sess.BluemixSession, c.Generation)
if err != nil {
session.bmxUserFetchErr = fmt.Errorf("Error occured while fetching account user details: %q", err)
}
session.bmxUserDetails = userConfig
if sess.SoftLayerSession != nil && sess.SoftLayerSession.IAMToken != "" {
sess.SoftLayerSession.IAMToken = sess.BluemixSession.Config.IAMAccessToken
sess.SoftLayerSession.IAMRefreshToken = sess.BluemixSession.Config.IAMRefreshToken
}
session.functionClient, session.functionConfigErr = FunctionClient(sess.BluemixSession.Config, c.FunctionNameSpace)
BluemixRegion = sess.BluemixSession.Config.Region
accv1API, err := accountv1.New(sess.BluemixSession)
if err != nil {
session.accountV1ConfigErr = fmt.Errorf("Error occured while configuring Bluemix Accountv1 Service: %q", err)
}
session.bmxAccountv1ServiceAPI = accv1API
accAPI, err := accountv2.New(sess.BluemixSession)
if err != nil {
session.accountConfigErr = fmt.Errorf("Error occured while configuring Account Service: %q", err)
}
session.bmxAccountServiceAPI = accAPI
cfAPI, err := mccpv2.New(sess.BluemixSession)
if err != nil {
session.cfConfigErr = fmt.Errorf("Error occured while configuring MCCP service: %q", err)
}
session.cfServiceAPI = cfAPI
clusterAPI, err := containerv1.New(sess.BluemixSession)
if err != nil {
session.csConfigErr = fmt.Errorf("Error occured while configuring Container Service for K8s cluster: %q", err)
}
session.csServiceAPI = clusterAPI
v2clusterAPI, err := containerv2.New(sess.BluemixSession)
if err != nil {
session.csv2ConfigErr = fmt.Errorf("Error occured while configuring vpc Container Service for K8s cluster: %q", err)
}
session.csv2ServiceAPI = v2clusterAPI
kpurl := fmt.Sprintf("https://%s.kms.cloud.ibm.com", c.Region)
options := kp.ClientConfig{
BaseURL: envFallBack([]string{"IBMCLOUD_KP_API_ENDPOINT"}, kpurl),
Authorization: sess.BluemixSession.Config.IAMAccessToken,
// InstanceID: "42fET57nnadurKXzXAedFLOhGqETfIGYxOmQXkFgkJV9",
Verbose: kp.VerboseFailOnly,
}
kpAPIclient, err := kp.New(options, kp.DefaultTransport())
if err != nil {
session.kpErr = fmt.Errorf("Error occured while configuring Key Protect Service: %q", err)
}
session.kpAPI = kpAPIclient
var authenticator *core.BearerTokenAuthenticator
if strings.HasPrefix(sess.BluemixSession.Config.IAMAccessToken, "Bearer") {
authenticator = &core.BearerTokenAuthenticator{
BearerToken: sess.BluemixSession.Config.IAMAccessToken[7:],
}
} else {
authenticator = &core.BearerTokenAuthenticator{
BearerToken: sess.BluemixSession.Config.IAMAccessToken,
}
}
vpcclassicurl := fmt.Sprintf("https://%s.iaas.cloud.ibm.com/v1", c.Region)
vpcclassicoptions := &vpcclassic.VpcClassicV1Options{
URL: envFallBack([]string{"IBMCLOUD_IS_API_ENDPOINT"}, vpcclassicurl),
Version: &VPCAPIVersion,
Authenticator: authenticator,
}
vpcclassicclient, err := vpcclassic.NewVpcClassicV1(vpcclassicoptions)
if err != nil {
session.vpcErr = fmt.Errorf("Error occured while configuring vpc classic service: %q", err)
}
session.vpcClassicAPI = vpcclassicclient
vpcurl := fmt.Sprintf("https://%s.iaas.cloud.ibm.com/v1", c.Region)
vpcoptions := &vpc.VpcV1Options{
URL: envFallBack([]string{"IBMCLOUD_IS_NG_API_ENDPOINT"}, vpcurl),
Version: &VPCAPIVersion,
Authenticator: authenticator,
}
vpcclient, err := vpc.NewVpcV1(vpcoptions)
if err != nil {
session.vpcErr = fmt.Errorf("Error occured while configuring vpc classic service: %q", err)
}
session.vpcAPI = vpcclient
schematicService, err := schematics.New(sess.BluemixSession)
if err != nil {
session.stxConfigErr = fmt.Errorf("Error occured while fetching schematics Configuration: %q", err)
}
session.stxServiceAPI = schematicService
cisAPI, err := cisv1.New(sess.BluemixSession)
if err != nil {
session.cisConfigErr = fmt.Errorf("Error occured while configuring Cloud Internet Services: %q", err)
}
session.cisServiceAPI = cisAPI
globalSearchAPI, err := globalsearchv2.New(sess.BluemixSession)
if err != nil {
session.globalSearchConfigErr = fmt.Errorf("Error occured while configuring Global Search: %q", err)
}
session.globalSearchServiceAPI = globalSearchAPI
globalTaggingAPI, err := globaltaggingv3.New(sess.BluemixSession)
if err != nil {
session.globalTaggingConfigErr = fmt.Errorf("Error occured while configuring Global Tagging: %q", err)
}
session.globalTaggingServiceAPI = globalTaggingAPI
iampap, err := iampapv1.New(sess.BluemixSession)
if err != nil {
session.iamPAPConfigErr = fmt.Errorf("Error occured while configuring Bluemix IAMPAP Service: %q", err)
}
session.iamPAPServiceAPI = iampap
iam, err := iamv1.New(sess.BluemixSession)
if err != nil {
session.iamConfigErr = fmt.Errorf("Error occured while configuring Bluemix IAM Service: %q", err)
}
session.iamServiceAPI = iam
iamuum, err := iamuumv1.New(sess.BluemixSession)
if err != nil {
session.iamUUMConfigErr = fmt.Errorf("Error occured while configuring Bluemix IAMUUM Service: %q", err)
}
session.iamUUMServiceAPI = iamuum
iamuumv2, err := iamuumv2.New(sess.BluemixSession)
if err != nil {
session.iamUUMConfigErrV2 = fmt.Errorf("Error occured while configuring Bluemix IAMUUM Service: %q", err)
}
session.iamUUMServiceAPIV2 = iamuumv2
issession, err := issession.New(sess.BluemixSession.Config.IAMAccessToken, c.Region, c.Generation, false, c.BluemixTimeout)
if err != nil {
session.isConfigErr = err
return nil, err
}
session.isSession = issession
icdAPI, err := icdv4.New(sess.BluemixSession)
if err != nil {
session.icdConfigErr = fmt.Errorf("Error occured while configuring IBM Cloud Database Services: %q", err)
}
session.icdServiceAPI = icdAPI
resourceCatalogAPI, err := catalog.New(sess.BluemixSession)
if err != nil {
session.resourceCatalogConfigErr = fmt.Errorf("Error occured while configuring Resource Catalog service: %q", err)
}
session.resourceCatalogServiceAPI = resourceCatalogAPI
resourceManagementAPI, err := management.New(sess.BluemixSession)
if err != nil {
session.resourceManagementConfigErr = fmt.Errorf("Error occured while configuring Resource Management service: %q", err)
}
session.resourceManagementServiceAPI = resourceManagementAPI
resourceManagementAPIv2, err := managementv2.New(sess.BluemixSession)
if err != nil {
session.resourceManagementConfigErrv2 = fmt.Errorf("Error occured while configuring Resource Management service: %q", err)
}
session.resourceManagementServiceAPIv2 = resourceManagementAPIv2
resourceControllerAPI, err := controller.New(sess.BluemixSession)
if err != nil {
session.resourceControllerConfigErr = fmt.Errorf("Error occured while configuring Resource Controller service: %q", err)
}
session.resourceControllerServiceAPI = resourceControllerAPI
userManagementAPI, err := usermanagementv2.New(sess.BluemixSession)
if err != nil {
session.userManagementErr = fmt.Errorf("Error occured while configuring user management service: %q", err)
}
session.userManagementAPI = userManagementAPI
certManagementAPI, err := certificatemanager.New(sess.BluemixSession)
if err != nil {
session.certManagementErr = fmt.Errorf("Error occured while configuring Certificate manager service: %q", err)
}
session.certManagementAPI = certManagementAPI
apicurl := fmt.Sprintf("https://api.%s.apigw.cloud.ibm.com/controller", c.Region)
APIGatewayControllerAPIV1Options := &apigateway.ApiGatewayControllerApiV1Options{
URL: envFallBack([]string{"IBMCLOUD_API_GATEWAY_ENDPOINT"}, apicurl),
Authenticator: &core.NoAuthAuthenticator{},
}
apigatewayAPI, err := apigateway.NewApiGatewayControllerApiV1(APIGatewayControllerAPIV1Options)
if err != nil {
session.apigatewayErr = fmt.Errorf("Error occured while configuring APIGateway service: %q", err)
}
session.apigatewayAPI = apigatewayAPI
ibmpisession, err := ibmpisession.New(sess.BluemixSession.Config.IAMAccessToken, c.Region, false, c.BluemixTimeout, session.bmxUserDetails.userAccount, c.Zone)
if err != nil {
session.ibmpiConfigErr = err
return nil, err
}
session.ibmpiSession = ibmpisession
dnsOptions := &dns.DnsSvcsV1Options{
URL: envFallBack([]string{"IBMCLOUD_PRIVATE_DNS_API_ENDPOINT"}, "https://api.dns-svcs.cloud.ibm.com/v1"),
Authenticator: &core.BearerTokenAuthenticator{
BearerToken: sess.BluemixSession.Config.IAMAccessToken,
},
}
session.pDnsClient, session.pDnsErr = dns.NewDnsSvcsV1(dnsOptions)
if session.pDnsErr != nil {
session.pDnsErr = fmt.Errorf("Error occured while configuring PrivateDNS Service: %s", err)
}
return session, nil
}
func newSession(c *Config) (*Session, error) {
ibmSession := &Session{}
softlayerSession := &slsession.Session{
Endpoint: c.SoftLayerEndpointURL,
Timeout: c.SoftLayerTimeout,
UserName: c.SoftLayerUserName,
APIKey: c.SoftLayerAPIKey,
Debug: os.Getenv("TF_LOG") != "",
Retries: c.RetryCount,
RetryWait: c.RetryDelay,
}
if c.IAMToken != "" {
log.Println("Configuring SoftLayer Session with token")
softlayerSession.IAMToken = c.IAMToken
softlayerSession.IAMRefreshToken = c.IAMRefreshToken
}
if c.SoftLayerAPIKey != "" && c.SoftLayerUserName != "" {
log.Println("Configuring SoftLayer Session with API key")
softlayerSession.APIKey = c.SoftLayerAPIKey
softlayerSession.UserName = c.SoftLayerUserName
}
softlayerSession.AppendUserAgent(fmt.Sprintf("terraform-provider-ibm/%s", version.Version))
ibmSession.SoftLayerSession = softlayerSession
if (c.IAMToken != "" && c.IAMRefreshToken == "") || (c.IAMToken == "" && c.IAMRefreshToken != "") {
return nil, fmt.Errorf("iam_token and iam_refresh_token must be provided")
}
if c.IAMToken != "" && c.IAMRefreshToken != "" {
log.Println("Configuring IBM Cloud Session with token")
var sess *bxsession.Session
bmxConfig := &bluemix.Config{
IAMAccessToken: c.IAMToken,
IAMRefreshToken: c.IAMRefreshToken,
//Comment out debug mode for v0.12
//Debug: os.Getenv("TF_LOG") != "",
HTTPTimeout: c.BluemixTimeout,
Region: c.Region,
ResourceGroup: c.ResourceGroup,
RetryDelay: &c.RetryDelay,
MaxRetries: &c.RetryCount,
}
sess, err := bxsession.New(bmxConfig)
if err != nil {
return nil, err
}
ibmSession.BluemixSession = sess
}
if c.BluemixAPIKey != "" {
log.Println("Configuring IBM Cloud Session with API key")
var sess *bxsession.Session
bmxConfig := &bluemix.Config{
BluemixAPIKey: c.BluemixAPIKey,
//Comment out debug mode for v0.12
//Debug: os.Getenv("TF_LOG") != "",
HTTPTimeout: c.BluemixTimeout,
Region: c.Region,
ResourceGroup: c.ResourceGroup,
RetryDelay: &c.RetryDelay,
MaxRetries: &c.RetryCount,
//PowerServiceInstance: c.PowerServiceInstance,
}
sess, err := bxsession.New(bmxConfig)
if err != nil {
return nil, err
}
ibmSession.BluemixSession = sess
}
return ibmSession, nil
}
func authenticateAPIKey(sess *bxsession.Session) error {
config := sess.Config
tokenRefresher, err := authentication.NewIAMAuthRepository(config, &rest.Client{
DefaultHeader: gohttp.Header{
"User-Agent": []string{http.UserAgent()},
},
})
if err != nil {
return err
}
return tokenRefresher.AuthenticateAPIKey(config.BluemixAPIKey)
}
func authenticateCF(sess *bxsession.Session) error {
config := sess.Config
tokenRefresher, err := authentication.NewUAARepository(config, &rest.Client{
DefaultHeader: gohttp.Header{
"User-Agent": []string{http.UserAgent()},
},
})
if err != nil {
return err
}
return tokenRefresher.AuthenticateAPIKey(config.BluemixAPIKey)
}
func fetchUserDetails(sess *bxsession.Session, generation int) (*UserConfig, error) {
config := sess.Config
user := UserConfig{}
var bluemixToken string
if strings.HasPrefix(config.IAMAccessToken, "Bearer") {
bluemixToken = config.IAMAccessToken[7:len(config.IAMAccessToken)]
} else {
bluemixToken = config.IAMAccessToken
}
token, err := jwt.Parse(bluemixToken, func(token *jwt.Token) (interface{}, error) {
return "", nil
})
//TODO validate with key
if err != nil && !strings.Contains(err.Error(), "key is of invalid type") {
return &user, err
}
claims := token.Claims.(jwt.MapClaims)
if email, ok := claims["email"]; ok {
user.userEmail = email.(string)
}
user.userID = claims["id"].(string)
user.userAccount = claims["account"].(map[string]interface{})["bss"].(string)
iss := claims["iss"].(string)
if strings.Contains(iss, "https://iam.cloud.ibm.com") {
user.cloudName = "bluemix"
} else {
user.cloudName = "staging"
}
user.cloudType = "public"
user.generation = generation
return &user, nil
}
func refreshToken(sess *bxsession.Session) error {
config := sess.Config
tokenRefresher, err := authentication.NewIAMAuthRepository(config, &rest.Client{
DefaultHeader: gohttp.Header{
"User-Agent": []string{http.UserAgent()},
},
})
if err != nil {
return err
}
_, err = tokenRefresher.RefreshToken()
return err
}
func envFallBack(envs []string, defaultValue string) string {
for _, k := range envs {
if v := os.Getenv(k); v != "" {
return v
}
}
return defaultValue
}