-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
config.go.tmpl
1354 lines (1164 loc) · 49.8 KB
/
config.go.tmpl
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 transport
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"regexp"
"strconv"
"strings"
"time"
"os"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/sirupsen/logrus"
"google.golang.org/api/option"
"google.golang.org/api/option/internaloption"
"github.com/hashicorp/terraform-provider-google/google/verify"
"golang.org/x/oauth2"
"google.golang.org/grpc"
googleoauth "golang.org/x/oauth2/google"
appengine "google.golang.org/api/appengine/v1"
"google.golang.org/api/bigquery/v2"
"google.golang.org/api/bigtableadmin/v2"
"google.golang.org/api/certificatemanager/v1"
"google.golang.org/api/cloudbilling/v1"
"google.golang.org/api/cloudbuild/v1"
{{- if ne $.TargetVersionName "ga" }}
cloudidentity "google.golang.org/api/cloudidentity/v1beta1"
{{- else }}
"google.golang.org/api/cloudidentity/v1"
{{- end }}
"google.golang.org/api/cloudfunctions/v1"
"google.golang.org/api/cloudiot/v1"
"google.golang.org/api/cloudkms/v1"
"google.golang.org/api/cloudresourcemanager/v1"
resourceManagerV3 "google.golang.org/api/cloudresourcemanager/v3"
{{- if eq $.TargetVersionName "ga" }}
"google.golang.org/api/composer/v1"
{{- else }}
"google.golang.org/api/composer/v1beta1"
{{- end }}
{{- if eq $.TargetVersionName "ga" }}
"google.golang.org/api/compute/v1"
{{- else }}
compute "google.golang.org/api/compute/v0.beta"
{{- end }}
{{- if eq $.TargetVersionName "ga" }}
"google.golang.org/api/container/v1"
{{- else }}
container "google.golang.org/api/container/v1beta1"
{{- end }}
dataflow "google.golang.org/api/dataflow/v1b3"
"google.golang.org/api/dataproc/v1"
"google.golang.org/api/dns/v1"
healthcare "google.golang.org/api/healthcare/v1"
"google.golang.org/api/iam/v1"
iamcredentials "google.golang.org/api/iamcredentials/v1"
cloudlogging "google.golang.org/api/logging/v2"
"google.golang.org/api/pubsub/v1"
runadminv2 "google.golang.org/api/run/v2"
{{- if ne $.TargetVersionName "ga" }}
runtimeconfig "google.golang.org/api/runtimeconfig/v1beta1"
{{- end }}
"google.golang.org/api/servicemanagement/v1"
"google.golang.org/api/servicenetworking/v1"
"google.golang.org/api/serviceusage/v1"
"google.golang.org/api/sourcerepo/v1"
"google.golang.org/api/spanner/v1"
sqladmin "google.golang.org/api/sqladmin/v1beta4"
"google.golang.org/api/storage/v1"
"google.golang.org/api/storagetransfer/v1"
"google.golang.org/api/transport"
)
type ProviderMeta struct {
ModuleName string `cty:"module_name"`
}
type Formatter struct {
TimestampFormat string
LogFormat string
}
// Borrowed logic from https://github.com/sirupsen/logrus/blob/master/json_formatter.go and https://github.com/t-tomalak/logrus-easy-formatter/blob/master/formatter.go
func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) {
// Suppress logs if TF_LOG is not DEBUG or TRACE
if !logging.IsDebugOrHigher() {
return nil, nil
}
// Also suppress based on log content
// - frequent transport spam
// - ListenSocket logs from gRPC
isTransportSpam := strings.Contains(entry.Message, "transport is closing")
listenSocketRegex := regexp.MustCompile(`\[Server #\d+( ListenSocket #\d+)*\]`) // Match patterns like `[Server #00]` or `[Server #00 ListenSocket #00]`
isListenSocketLog := listenSocketRegex.MatchString(entry.Message)
if isTransportSpam || isListenSocketLog {
return nil, nil
}
output := f.LogFormat
entry.Level = logrus.DebugLevel // Force Entries to be Debug
timestampFormat := f.TimestampFormat
output = strings.Replace(output, "%time%", entry.Time.Format(timestampFormat), 1)
output = strings.Replace(output, "%msg%", entry.Message, 1)
level := strings.ToUpper(entry.Level.String())
output = strings.Replace(output, "%lvl%", level, 1)
var gRPCMessageFlag bool
for k, val := range entry.Data {
switch v := val.(type) {
case string:
output = strings.Replace(output, "%"+k+"%", v, 1)
case int:
s := strconv.Itoa(v)
output = strings.Replace(output, "%"+k+"%", s, 1)
case bool:
s := strconv.FormatBool(v)
output = strings.Replace(output, "%"+k+"%", s, 1)
}
if k != "system" {
gRPCMessageFlag = true
}
}
if gRPCMessageFlag {
data := make(logrus.Fields, len(entry.Data)+4)
for k, v := range entry.Data {
switch v := v.(type) {
case error:
// Otherwise errors are ignored by `encoding/json`
// https://github.com/sirupsen/logrus/issues/137
data[k] = v.Error()
default:
data[k] = v
}
}
var b *bytes.Buffer
if entry.Buffer != nil {
b = entry.Buffer
} else {
b = &bytes.Buffer{}
}
encoder := json.NewEncoder(b)
encoder.SetIndent("", " ")
if err := encoder.Encode(data); err != nil {
return nil, fmt.Errorf("failed to marshal fields to JSON, %w", err)
}
finalOutput := append([]byte(output), b.Bytes()...)
return finalOutput, nil
}
return []byte(output), nil
}
// Config is the configuration structure used to instantiate the Google
// provider.
type Config struct {
DCLConfig
AccessToken string
Credentials string
ImpersonateServiceAccount string
ImpersonateServiceAccountDelegates []string
Project string
Region string
BillingProject string
Zone string
UniverseDomain string
Scopes []string
BatchingConfig *BatchingConfig
UserProjectOverride bool
RequestReason string
RequestTimeout time.Duration
DefaultLabels map[string]string
AddTerraformAttributionLabel bool
TerraformAttributionLabelAdditionStrategy string
// PollInterval is passed to retry.StateChangeConf in common_operation.go
// It controls the interval at which we poll for successful operations
PollInterval time.Duration
Client *http.Client
Context context.Context
UserAgent string
gRPCLoggingOptions []option.ClientOption
tokenSource oauth2.TokenSource
{{ range $product := $.Products }}
{{ $product.Name }}BasePath string
{{- end }}
CloudBillingBasePath string
ContainerBasePath string
DataflowBasePath string
IamCredentialsBasePath string
ResourceManagerV3BasePath string
IAMBasePath string
CloudIoTBasePath string
BigtableAdminBasePath string
TagsLocationBasePath string
// dcl
ContainerAwsBasePath string
ContainerAzureBasePath string
RequestBatcherServiceUsage *RequestBatcher
RequestBatcherIam *RequestBatcher
}
{{- range $product := $.Products }}
const {{ $product.Name }}BasePathKey = "{{ $product.Name }}"
{{- end }}
const CloudBillingBasePathKey = "CloudBilling"
const ContainerBasePathKey = "Container"
const DataflowBasePathKey = "Dataflow"
const IAMBasePathKey = "IAM"
const IamCredentialsBasePathKey = "IamCredentials"
const ResourceManagerV3BasePathKey = "ResourceManagerV3"
const BigtableAdminBasePathKey = "BigtableAdmin"
const ContainerAwsBasePathKey = "ContainerAws"
const ContainerAzureBasePathKey = "ContainerAzure"
const TagsLocationBasePathKey = "TagsLocation"
// Generated product base paths
var DefaultBasePaths = map[string]string{
{{- range $product := $.Products }}
{{ $product.Name }}BasePathKey : "{{ $product.BaseUrl }}",
{{- end }}
CloudBillingBasePathKey : "https://cloudbilling.googleapis.com/v1/",
{{- if eq $.TargetVersionName "ga" }}
ContainerBasePathKey : "https://container.googleapis.com/v1/",
{{- else }}
ContainerBasePathKey : "https://container.googleapis.com/v1beta1/",
{{- end }}
DataflowBasePathKey : "https://dataflow.googleapis.com/v1b3/",
IAMBasePathKey : "https://iam.googleapis.com/v1/",
IamCredentialsBasePathKey : "https://iamcredentials.googleapis.com/v1/",
ResourceManagerV3BasePathKey : "https://cloudresourcemanager.googleapis.com/v3/",
BigtableAdminBasePathKey : "https://bigtableadmin.googleapis.com/v2/",
ContainerAwsBasePathKey: "https://{{"{{"}}location{{"}}"}}-gkemulticloud.googleapis.com/v1/",
ContainerAzureBasePathKey: "https://{{"{{"}}location{{"}}"}}-gkemulticloud.googleapis.com/v1/",
TagsLocationBasePathKey: "https://{{"{{"}}location{{"}}"}}-cloudresourcemanager.googleapis.com/v3/",
}
var DefaultClientScopes = []string{
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/userinfo.email",
}
const AttributionKey = "goog-terraform-provisioned"
const AttributionValue = "true"
const CreateOnlyAttributionStrategy = "CREATION_ONLY"
const ProactiveAttributionStrategy = "PROACTIVE"
func HandleSDKDefaults(d *schema.ResourceData) error {
if d.Get("impersonate_service_account") == "" {
d.Set("impersonate_service_account", MultiEnvDefault([]string{
"GOOGLE_IMPERSONATE_SERVICE_ACCOUNT",
}, nil))
}
if d.Get("project") == "" {
d.Set("project", MultiEnvDefault([]string{
"GOOGLE_PROJECT",
"GOOGLE_CLOUD_PROJECT",
"GCLOUD_PROJECT",
"CLOUDSDK_CORE_PROJECT",
}, nil))
}
if d.Get("billing_project") == "" {
d.Set("billing_project", MultiEnvDefault([]string{
"GOOGLE_BILLING_PROJECT",
}, nil))
}
if d.Get("region") == "" {
d.Set("region", MultiEnvDefault([]string{
"GOOGLE_REGION",
"GCLOUD_REGION",
"CLOUDSDK_COMPUTE_REGION",
}, nil))
}
if d.Get("zone") == "" {
d.Set("zone", MultiEnvDefault([]string{
"GOOGLE_ZONE",
"GCLOUD_ZONE",
"CLOUDSDK_COMPUTE_ZONE",
}, nil))
}
if _, ok := d.GetOkExists("user_project_override"); !ok {
override := MultiEnvDefault([]string{
"USER_PROJECT_OVERRIDE",
}, nil)
if override != nil {
b, err := strconv.ParseBool(override.(string))
if err != nil {
return err
}
d.Set("user_project_override", b)
}
}
if d.Get("request_reason") == "" {
d.Set("request_reason", MultiEnvDefault([]string{
"CLOUDSDK_CORE_REQUEST_REASON",
}, nil))
}
return nil
}
func SetEndpointDefaults(d *schema.ResourceData) error {
// Generated Products
{{- range $product := $.Products }}
if d.Get("{{ underscore $product.Name }}_custom_endpoint") == "" {
d.Set("{{ underscore $product.Name }}_custom_endpoint", MultiEnvDefault([]string{
"GOOGLE_{{ upper (underscore $product.Name) }}_CUSTOM_ENDPOINT",
}, DefaultBasePaths[{{ $product.Name }}BasePathKey]))
}
{{- end }}
if d.Get(CloudBillingCustomEndpointEntryKey) == "" {
d.Set(CloudBillingCustomEndpointEntryKey, MultiEnvDefault([]string{
"GOOGLE_CLOUD_BILLING_CUSTOM_ENDPOINT",
}, DefaultBasePaths[CloudBillingBasePathKey]))
}
if d.Get(ComposerCustomEndpointEntryKey) == "" {
d.Set(ComposerCustomEndpointEntryKey, MultiEnvDefault([]string{
"GOOGLE_COMPOSER_CUSTOM_ENDPOINT",
}, DefaultBasePaths[ComposerBasePathKey]))
}
if d.Get(ContainerCustomEndpointEntryKey) == "" {
d.Set(ContainerCustomEndpointEntryKey, MultiEnvDefault([]string{
"GOOGLE_CONTAINER_CUSTOM_ENDPOINT",
}, DefaultBasePaths[ContainerBasePathKey]))
}
if d.Get(DataflowCustomEndpointEntryKey) == "" {
d.Set(DataflowCustomEndpointEntryKey, MultiEnvDefault([]string{
"GOOGLE_DATAFLOW_CUSTOM_ENDPOINT",
}, DefaultBasePaths[DataflowBasePathKey]))
}
if d.Get(IamCredentialsCustomEndpointEntryKey) == "" {
d.Set(IamCredentialsCustomEndpointEntryKey, MultiEnvDefault([]string{
"GOOGLE_IAM_CREDENTIALS_CUSTOM_ENDPOINT",
}, DefaultBasePaths[IamCredentialsBasePathKey]))
}
if d.Get(ResourceManagerV3CustomEndpointEntryKey) == "" {
d.Set(ResourceManagerV3CustomEndpointEntryKey, MultiEnvDefault([]string{
"GOOGLE_RESOURCE_MANAGER_V3_CUSTOM_ENDPOINT",
}, DefaultBasePaths[ResourceManagerV3BasePathKey]))
}
{{ if ne $.TargetVersionName `ga` -}}
if d.Get(RuntimeConfigCustomEndpointEntryKey) == "" {
d.Set(RuntimeConfigCustomEndpointEntryKey, MultiEnvDefault([]string{
"GOOGLE_RUNTIMECONFIG_CUSTOM_ENDPOINT",
}, DefaultBasePaths[RuntimeConfigBasePathKey]))
}
{{- end }}
if d.Get(IAMCustomEndpointEntryKey) == "" {
d.Set(IAMCustomEndpointEntryKey, MultiEnvDefault([]string{
"GOOGLE_IAM_CUSTOM_ENDPOINT",
}, DefaultBasePaths[IAMBasePathKey]))
}
if d.Get(ServiceNetworkingCustomEndpointEntryKey) == "" {
d.Set(ServiceNetworkingCustomEndpointEntryKey, MultiEnvDefault([]string{
"GOOGLE_SERVICE_NETWORKING_CUSTOM_ENDPOINT",
}, DefaultBasePaths[ServiceNetworkingBasePathKey]))
}
if d.Get(TagsLocationCustomEndpointEntryKey) == "" {
d.Set(TagsLocationCustomEndpointEntryKey, MultiEnvDefault([]string{
"GOOGLE_TAGS_LOCATION_CUSTOM_ENDPOINT",
}, DefaultBasePaths[TagsLocationBasePathKey]))
}
if d.Get(ContainerAwsCustomEndpointEntryKey) == "" {
d.Set(ContainerAwsCustomEndpointEntryKey, MultiEnvDefault([]string{
"GOOGLE_CONTAINERAWS_CUSTOM_ENDPOINT",
}, DefaultBasePaths[ContainerAwsBasePathKey]))
}
if d.Get(ContainerAzureCustomEndpointEntryKey) == "" {
d.Set(ContainerAzureCustomEndpointEntryKey, MultiEnvDefault([]string{
"GOOGLE_CONTAINERAZURE_CUSTOM_ENDPOINT",
}, DefaultBasePaths[ContainerAzureBasePathKey]))
}
return nil
}
func (c *Config) LoadAndValidate(ctx context.Context) error {
if len(c.Scopes) == 0 {
c.Scopes = DefaultClientScopes
}
c.Context = ctx
tokenSource, err := c.getTokenSource(c.Scopes, false)
if err != nil {
return err
}
c.tokenSource = tokenSource
cleanCtx := context.WithValue(ctx, oauth2.HTTPClient, cleanhttp.DefaultClient())
// 1. MTLS TRANSPORT/CLIENT - sets up proper auth headers
client, _, err := transport.NewHTTPClient(cleanCtx, option.WithTokenSource(tokenSource))
if err != nil {
return err
}
// Userinfo is fetched before request logging is enabled to reduce additional noise.
err = c.logGoogleIdentities()
if err != nil {
return err
}
// 2. Logging Transport - ensure we log HTTP requests to GCP APIs.
loggingTransport := logging.NewTransport("Google", client.Transport)
// 3. Retry Transport - retries common temporary errors
// Keep order for wrapping logging so we log each retried request as well.
// This value should be used if needed to create shallow copies with additional retry predicates.
// See ClientWithAdditionalRetries
retryTransport := NewTransportWithDefaultRetries(loggingTransport)
// 4. Header Transport - outer wrapper to inject additional headers we want to apply
// before making requests
headerTransport := NewTransportWithHeaders(retryTransport)
if c.RequestReason != "" {
headerTransport.Set("X-Goog-Request-Reason", c.RequestReason)
}
// Ensure $userProject is set for all HTTP requests using the client if specified by the provider config
// See https://cloud.google.com/apis/docs/system-parameters
if c.UserProjectOverride && c.BillingProject != "" {
headerTransport.Set("X-Goog-User-Project", c.BillingProject)
}
// Set final transport value.
client.Transport = headerTransport
// This timeout is a timeout per HTTP request, not per logical operation.
client.Timeout = c.synchronousTimeout()
c.Client = client
c.Context = ctx
c.Region = GetRegionFromRegionSelfLink(c.Region)
c.RequestBatcherServiceUsage = NewRequestBatcher("Service Usage", ctx, c.BatchingConfig)
c.RequestBatcherIam = NewRequestBatcher("IAM", ctx, c.BatchingConfig)
c.PollInterval = 10 * time.Second
// gRPC Logging setup
logger := logrus.StandardLogger()
logrus.SetLevel(logrus.DebugLevel)
logrus.SetFormatter(&Formatter{
TimestampFormat: "2006/01/02 15:04:05",
LogFormat: "%time% [%lvl%] %msg% \n",
})
alwaysLoggingDeciderClient := func(ctx context.Context, fullMethodName string) bool { return true }
grpc_logrus.ReplaceGrpcLogger(logrus.NewEntry(logger))
c.gRPCLoggingOptions = append(
c.gRPCLoggingOptions, option.WithGRPCDialOption(grpc.WithUnaryInterceptor(
grpc_logrus.PayloadUnaryClientInterceptor(logrus.NewEntry(logger), alwaysLoggingDeciderClient))),
option.WithGRPCDialOption(grpc.WithStreamInterceptor(
grpc_logrus.PayloadStreamClientInterceptor(logrus.NewEntry(logger), alwaysLoggingDeciderClient))),
)
return nil
}
func ExpandProviderBatchingConfig(v interface{}) (*BatchingConfig, error) {
config := &BatchingConfig{
SendAfter: time.Second * DefaultBatchSendIntervalSec,
EnableBatching: true,
}
if v == nil {
return config, nil
}
ls := v.([]interface{})
if len(ls) == 0 || ls[0] == nil {
return config, nil
}
cfgV := ls[0].(map[string]interface{})
if sendAfterV, ok := cfgV["send_after"]; ok && sendAfterV != "" {
SendAfter, err := time.ParseDuration(sendAfterV.(string))
if err != nil {
return nil, fmt.Errorf("unable to parse duration from 'send_after' value %q", sendAfterV)
}
config.SendAfter = SendAfter
}
if enable, ok := cfgV["enable_batching"]; ok {
config.EnableBatching = enable.(bool)
}
return config, nil
}
func (c *Config) synchronousTimeout() time.Duration {
if c.RequestTimeout == 0 {
return 120 * time.Second
}
return c.RequestTimeout
}
// Print Identities executing terraform API Calls.
func (c *Config) logGoogleIdentities() error {
if c.ImpersonateServiceAccount == "" {
tokenSource, err := c.getTokenSource(c.Scopes, true)
if err != nil {
return err
}
c.Client = oauth2.NewClient(c.Context, tokenSource) // c.Client isn't initialised fully when this code is called.
email, err := GetCurrentUserEmail(c, c.UserAgent)
if err != nil {
log.Printf("[INFO] error retrieving userinfo for your provider credentials. have you enabled the 'https://www.googleapis.com/auth/userinfo.email' scope? error: %s", err)
}
log.Printf("[INFO] Terraform is using this identity: %s", email)
return nil
}
// Drop Impersonated ClientOption from OAuth2 TokenSource to infer original identity
tokenSource, err := c.getTokenSource(c.Scopes, true)
if err != nil {
return err
}
c.Client = oauth2.NewClient(c.Context, tokenSource) // c.Client isn't initialised fully when this code is called.
email, err := GetCurrentUserEmail(c, c.UserAgent)
if err != nil {
log.Printf("[INFO] error retrieving userinfo for your provider credentials. have you enabled the 'https://www.googleapis.com/auth/userinfo.email' scope? error: %s", err)
}
log.Printf("[INFO] Terraform is configured with service account impersonation, original identity: %s, impersonated identity: %s", email, c.ImpersonateServiceAccount)
// Add the Impersonated ClientOption back in to the OAuth2 TokenSource
tokenSource, err = c.getTokenSource(c.Scopes, false)
if err != nil {
return err
}
c.Client = oauth2.NewClient(c.Context, tokenSource) // c.Client isn't initialised fully when this code is called.
return nil
}
// Get a TokenSource based on the Google Credentials configured.
// If initialCredentialsOnly is true, don't follow the impersonation settings and return the initial set of creds.
func (c *Config) getTokenSource(clientScopes []string, initialCredentialsOnly bool) (oauth2.TokenSource, error) {
creds, err := c.GetCredentials(clientScopes, initialCredentialsOnly)
if err != nil {
return nil, fmt.Errorf("%s", err)
}
return creds.TokenSource, nil
}
// Methods to create new services from config
// Some base paths below need the version and possibly more of the path
// set on them. The client libraries are inconsistent about which values they need;
// while most only want the host URL, some older ones also want the version and some
// of those "projects" as well. You can find out if this is required by looking at
// the basePath value in the client library file.
func (c *Config) NewCertificateManagerClient(userAgent string) *certificatemanager.Service {
certificateManagerClientBasePath := RemoveBasePathVersion(c.CertificateManagerBasePath)
log.Printf("[INFO] Instantiating Certificate Manager client for path %s", certificateManagerClientBasePath)
clientCertificateManager, err := certificatemanager.NewService(c.Context, option.WithHTTPClient(c.Client))
if err != nil {
log.Printf("[WARN] Error creating client certificate manager: %s", err)
return nil
}
clientCertificateManager.UserAgent = userAgent
clientCertificateManager.BasePath = certificateManagerClientBasePath
return clientCertificateManager
}
func (c *Config) NewComputeClient(userAgent string) *compute.Service {
log.Printf("[INFO] Instantiating GCE client for path %s", c.ComputeBasePath)
clientCompute, err := compute.NewService(c.Context, option.WithHTTPClient(c.Client))
if err != nil {
log.Printf("[WARN] Error creating client compute: %s", err)
return nil
}
clientCompute.UserAgent = userAgent
clientCompute.BasePath = c.ComputeBasePath
return clientCompute
}
func (c *Config) NewContainerClient(userAgent string) *container.Service {
containerClientBasePath := RemoveBasePathVersion(c.ContainerBasePath)
log.Printf("[INFO] Instantiating GKE client for path %s", containerClientBasePath)
clientContainer, err := container.NewService(c.Context, option.WithHTTPClient(c.Client))
if err != nil {
log.Printf("[WARN] Error creating client container: %s", err)
return nil
}
clientContainer.UserAgent = userAgent
clientContainer.BasePath = containerClientBasePath
return clientContainer
}
func (c *Config) NewDnsClient(userAgent string) *dns.Service {
dnsClientBasePath := RemoveBasePathVersion(c.DNSBasePath)
dnsClientBasePath = strings.ReplaceAll(dnsClientBasePath, "/dns/", "")
log.Printf("[INFO] Instantiating Google Cloud DNS client for path %s", dnsClientBasePath)
clientDns, err := dns.NewService(c.Context, option.WithHTTPClient(c.Client))
if err != nil {
log.Printf("[WARN] Error creating client dns: %s", err)
return nil
}
clientDns.UserAgent = userAgent
clientDns.BasePath = dnsClientBasePath
return clientDns
}
func (c *Config) NewKmsClientWithCtx(ctx context.Context, userAgent string) *cloudkms.Service {
kmsClientBasePath := RemoveBasePathVersion(c.KMSBasePath)
log.Printf("[INFO] Instantiating Google Cloud KMS client for path %s", kmsClientBasePath)
clientKms, err := cloudkms.NewService(ctx, option.WithHTTPClient(c.Client))
if err != nil {
log.Printf("[WARN] Error creating client kms: %s", err)
return nil
}
clientKms.UserAgent = userAgent
clientKms.BasePath = kmsClientBasePath
return clientKms
}
func (c *Config) NewKmsClient(userAgent string) *cloudkms.Service {
return c.NewKmsClientWithCtx(c.Context, userAgent)
}
func (c *Config) NewLoggingClient(userAgent string) *cloudlogging.Service {
loggingClientBasePath := RemoveBasePathVersion(c.LoggingBasePath)
log.Printf("[INFO] Instantiating Google Stackdriver Logging client for path %s", loggingClientBasePath)
clientLogging, err := cloudlogging.NewService(c.Context, option.WithHTTPClient(c.Client))
if err != nil {
log.Printf("[WARN] Error creating client logging: %s", err)
return nil
}
clientLogging.UserAgent = userAgent
clientLogging.BasePath = loggingClientBasePath
return clientLogging
}
func (c *Config) NewStorageClient(userAgent string) *storage.Service {
storageClientBasePath := c.StorageBasePath
log.Printf("[INFO] Instantiating Google Storage client for path %s", storageClientBasePath)
clientStorage, err := storage.NewService(c.Context, option.WithHTTPClient(c.Client))
if err != nil {
log.Printf("[WARN] Error creating client storage: %s", err)
return nil
}
clientStorage.UserAgent = userAgent
clientStorage.BasePath = storageClientBasePath
return clientStorage
}
// For object uploads, we need to override the specific timeout because they are long, synchronous operations.
func (c *Config) NewStorageClientWithTimeoutOverride(userAgent string, timeout time.Duration) *storage.Service {
storageClientBasePath := c.StorageBasePath
log.Printf("[INFO] Instantiating Google Storage client for path %s", storageClientBasePath)
// Copy the existing HTTP client (which has no unexported fields [as of Oct 2021 at least], so this is safe).
// We have to do this because otherwise we will accidentally change the timeout for all other
// synchronous operations, which would not be desirable.
httpClient := &http.Client{
Transport: c.Client.Transport,
CheckRedirect: c.Client.CheckRedirect,
Jar: c.Client.Jar,
Timeout: timeout,
}
clientStorage, err := storage.NewService(c.Context, option.WithHTTPClient(httpClient))
if err != nil {
log.Printf("[WARN] Error creating client storage: %s", err)
return nil
}
clientStorage.UserAgent = userAgent
clientStorage.BasePath = storageClientBasePath
return clientStorage
}
func (c *Config) NewSqlAdminClient(userAgent string) *sqladmin.Service {
sqlClientBasePath := RemoveBasePathVersion(RemoveBasePathVersion(c.SQLBasePath))
log.Printf("[INFO] Instantiating Google SqlAdmin client for path %s", sqlClientBasePath)
clientSqlAdmin, err := sqladmin.NewService(c.Context, option.WithHTTPClient(c.Client))
if err != nil {
log.Printf("[WARN] Error creating client storage: %s", err)
return nil
}
clientSqlAdmin.UserAgent = userAgent
clientSqlAdmin.BasePath = sqlClientBasePath
return clientSqlAdmin
}
func (c *Config) NewPubsubClient(userAgent string) *pubsub.Service {
pubsubClientBasePath := RemoveBasePathVersion(c.PubsubBasePath)
log.Printf("[INFO] Instantiating Google Pubsub client for path %s", pubsubClientBasePath)
wrappedPubsubClient := ClientWithAdditionalRetries(c.Client, PubsubTopicProjectNotReady)
clientPubsub, err := pubsub.NewService(c.Context, option.WithHTTPClient(wrappedPubsubClient))
if err != nil {
log.Printf("[WARN] Error creating client pubsub: %s", err)
return nil
}
clientPubsub.UserAgent = userAgent
clientPubsub.BasePath = pubsubClientBasePath
return clientPubsub
}
func (c *Config) NewDataflowClient(userAgent string) *dataflow.Service {
dataflowClientBasePath := RemoveBasePathVersion(c.DataflowBasePath)
log.Printf("[INFO] Instantiating Google Dataflow client for path %s", dataflowClientBasePath)
clientDataflow, err := dataflow.NewService(c.Context, option.WithHTTPClient(c.Client))
if err != nil {
log.Printf("[WARN] Error creating client dataflow: %s", err)
return nil
}
clientDataflow.UserAgent = userAgent
clientDataflow.BasePath = dataflowClientBasePath
return clientDataflow
}
func (c *Config) NewResourceManagerClient(userAgent string) *cloudresourcemanager.Service {
resourceManagerBasePath := RemoveBasePathVersion(c.ResourceManagerBasePath)
log.Printf("[INFO] Instantiating Google Cloud ResourceManager client for path %s", resourceManagerBasePath)
clientResourceManager, err := cloudresourcemanager.NewService(c.Context, option.WithHTTPClient(c.Client))
if err != nil {
log.Printf("[WARN] Error creating client resource manager: %s", err)
return nil
}
clientResourceManager.UserAgent = userAgent
clientResourceManager.BasePath = resourceManagerBasePath
return clientResourceManager
}
func (c *Config) NewResourceManagerV3Client(userAgent string) *resourceManagerV3.Service {
resourceManagerV3BasePath := RemoveBasePathVersion(c.ResourceManagerV3BasePath)
log.Printf("[INFO] Instantiating Google Cloud ResourceManager V3 client for path %s", resourceManagerV3BasePath)
clientResourceManagerV3, err := resourceManagerV3.NewService(c.Context, option.WithHTTPClient(c.Client))
if err != nil {
log.Printf("[WARN] Error creating client resource manager v3: %s", err)
return nil
}
clientResourceManagerV3.UserAgent = userAgent
clientResourceManagerV3.BasePath = resourceManagerV3BasePath
return clientResourceManagerV3
}
{{ if ne $.TargetVersionName `ga` -}}
func(c *Config) NewRuntimeconfigClient(userAgent string) *runtimeconfig.Service {
runtimeConfigClientBasePath := RemoveBasePathVersion(c.RuntimeConfigBasePath)
log.Printf("[INFO] Instantiating Google Cloud Runtimeconfig client for path %s", runtimeConfigClientBasePath)
clientRuntimeconfig, err := runtimeconfig.NewService(c.Context, option.WithHTTPClient(c.Client))
if err != nil {
log.Printf("[WARN] Error creating client runtime config: %s", err)
return nil
}
clientRuntimeconfig.UserAgent = userAgent
clientRuntimeconfig.BasePath = runtimeConfigClientBasePath
return clientRuntimeconfig
}
{{- end }}
func (c *Config) NewIamClient(userAgent string) *iam.Service {
iamClientBasePath := RemoveBasePathVersion(c.IAMBasePath)
log.Printf("[INFO] Instantiating Google Cloud IAM client for path %s", iamClientBasePath)
clientIAM, err := iam.NewService(c.Context, option.WithHTTPClient(c.Client))
if err != nil {
log.Printf("[WARN] Error creating client iam: %s", err)
return nil
}
clientIAM.UserAgent = userAgent
clientIAM.BasePath = iamClientBasePath
return clientIAM
}
func (c *Config) NewIamCredentialsClient(userAgent string) *iamcredentials.Service {
iamCredentialsClientBasePath := RemoveBasePathVersion(c.IamCredentialsBasePath)
log.Printf("[INFO] Instantiating Google Cloud IAMCredentials client for path %s", iamCredentialsClientBasePath)
clientIamCredentials, err := iamcredentials.NewService(c.Context, option.WithHTTPClient(c.Client))
if err != nil {
log.Printf("[WARN] Error creating client iam credentials: %s", err)
return nil
}
clientIamCredentials.UserAgent = userAgent
clientIamCredentials.BasePath = iamCredentialsClientBasePath
return clientIamCredentials
}
func (c *Config) NewServiceManClient(userAgent string) *servicemanagement.APIService {
serviceManagementClientBasePath := RemoveBasePathVersion(c.ServiceManagementBasePath)
log.Printf("[INFO] Instantiating Google Cloud Service Management client for path %s", serviceManagementClientBasePath)
clientServiceMan, err := servicemanagement.NewService(c.Context, option.WithHTTPClient(c.Client))
if err != nil {
log.Printf("[WARN] Error creating client service management: %s", err)
return nil
}
clientServiceMan.UserAgent = userAgent
clientServiceMan.BasePath = serviceManagementClientBasePath
return clientServiceMan
}
func (c *Config) NewServiceUsageClient(userAgent string) *serviceusage.Service {
serviceUsageClientBasePath := RemoveBasePathVersion(c.ServiceUsageBasePath)
log.Printf("[INFO] Instantiating Google Cloud Service Usage client for path %s", serviceUsageClientBasePath)
clientServiceUsage, err := serviceusage.NewService(c.Context, option.WithHTTPClient(c.Client))
if err != nil {
log.Printf("[WARN] Error creating client service usage: %s", err)
return nil
}
clientServiceUsage.UserAgent = userAgent
clientServiceUsage.BasePath = serviceUsageClientBasePath
return clientServiceUsage
}
func (c *Config) NewBillingClient(userAgent string) *cloudbilling.APIService {
cloudBillingClientBasePath := RemoveBasePathVersion(c.CloudBillingBasePath)
log.Printf("[INFO] Instantiating Google Cloud Billing client for path %s", cloudBillingClientBasePath)
clientBilling, err := cloudbilling.NewService(c.Context, option.WithHTTPClient(c.Client))
if err != nil {
log.Printf("[WARN] Error creating client billing: %s", err)
return nil
}
clientBilling.UserAgent = userAgent
clientBilling.BasePath = cloudBillingClientBasePath
return clientBilling
}
func (c *Config) NewBuildClient(userAgent string) *cloudbuild.Service {
cloudBuildClientBasePath := RemoveBasePathVersion(c.CloudBuildBasePath)
log.Printf("[INFO] Instantiating Google Cloud Build client for path %s", cloudBuildClientBasePath)
clientBuild, err := cloudbuild.NewService(c.Context, option.WithHTTPClient(c.Client))
if err != nil {
log.Printf("[WARN] Error creating client build: %s", err)
return nil
}
clientBuild.UserAgent = userAgent
clientBuild.BasePath = cloudBuildClientBasePath
return clientBuild
}
func (c *Config) NewCloudFunctionsClient(userAgent string) *cloudfunctions.Service {
cloudFunctionsClientBasePath := RemoveBasePathVersion(c.CloudFunctionsBasePath)
log.Printf("[INFO] Instantiating Google Cloud CloudFunctions Client for path %s", cloudFunctionsClientBasePath)
clientCloudFunctions, err := cloudfunctions.NewService(c.Context, option.WithHTTPClient(c.Client))
if err != nil {
log.Printf("[WARN] Error creating client cloud functions: %s", err)
return nil
}
clientCloudFunctions.UserAgent = userAgent
clientCloudFunctions.BasePath = cloudFunctionsClientBasePath
return clientCloudFunctions
}
func (c *Config) NewSourceRepoClient(userAgent string) *sourcerepo.Service {
sourceRepoClientBasePath := RemoveBasePathVersion(c.SourceRepoBasePath)
log.Printf("[INFO] Instantiating Google Cloud Source Repo client for path %s", sourceRepoClientBasePath)
clientSourceRepo, err := sourcerepo.NewService(c.Context, option.WithHTTPClient(c.Client))
if err != nil {
log.Printf("[WARN] Error creating client source repo: %s", err)
return nil
}
clientSourceRepo.UserAgent = userAgent
clientSourceRepo.BasePath = sourceRepoClientBasePath
return clientSourceRepo
}
func (c *Config) NewBigQueryClient(userAgent string) *bigquery.Service {
bigQueryClientBasePath := c.BigQueryBasePath
log.Printf("[INFO] Instantiating Google Cloud BigQuery client for path %s", bigQueryClientBasePath)
wrappedBigQueryClient := ClientWithAdditionalRetries(c.Client, IamMemberMissing)
clientBigQuery, err := bigquery.NewService(c.Context, option.WithHTTPClient(wrappedBigQueryClient))
if err != nil {
log.Printf("[WARN] Error creating client big query: %s", err)
return nil
}
clientBigQuery.UserAgent = userAgent
clientBigQuery.BasePath = bigQueryClientBasePath
return clientBigQuery
}
func (c *Config) NewSpannerClient(userAgent string) *spanner.Service {
spannerClientBasePath := RemoveBasePathVersion(c.SpannerBasePath)
log.Printf("[INFO] Instantiating Google Cloud Spanner client for path %s", spannerClientBasePath)
clientSpanner, err := spanner.NewService(c.Context, option.WithHTTPClient(c.Client))
if err != nil {
log.Printf("[WARN] Error creating client source repo: %s", err)
return nil
}
clientSpanner.UserAgent = userAgent
clientSpanner.BasePath = spannerClientBasePath
return clientSpanner
}
func (c *Config) NewDataprocClient(userAgent string) *dataproc.Service {
dataprocClientBasePath := RemoveBasePathVersion(c.DataprocBasePath)
log.Printf("[INFO] Instantiating Google Cloud Dataproc client for path %s", dataprocClientBasePath)
clientDataproc, err := dataproc.NewService(c.Context, option.WithHTTPClient(c.Client))
if err != nil {
log.Printf("[WARN] Error creating client dataproc: %s", err)
return nil
}
clientDataproc.UserAgent = userAgent
clientDataproc.BasePath = dataprocClientBasePath
return clientDataproc
}
func (c *Config) NewCloudIoTClient(userAgent string) *cloudiot.Service {
cloudIoTClientBasePath := RemoveBasePathVersion(c.CloudIoTBasePath)
log.Printf("[INFO] Instantiating Google Cloud IoT Core client for path %s", cloudIoTClientBasePath)
clientCloudIoT, err := cloudiot.NewService(c.Context, option.WithHTTPClient(c.Client))
if err != nil {
log.Printf("[WARN] Error creating client cloud iot: %s", err)
return nil
}
clientCloudIoT.UserAgent = userAgent
clientCloudIoT.BasePath = cloudIoTClientBasePath
return clientCloudIoT
}
func (c *Config) NewAppEngineClient(userAgent string) *appengine.APIService {
appEngineClientBasePath := RemoveBasePathVersion(c.AppEngineBasePath)
log.Printf("[INFO] Instantiating App Engine client for path %s", appEngineClientBasePath)
clientAppEngine, err := appengine.NewService(c.Context, option.WithHTTPClient(c.Client))
if err != nil {
log.Printf("[WARN] Error creating client appengine: %s", err)
return nil
}
clientAppEngine.UserAgent = userAgent
clientAppEngine.BasePath = appEngineClientBasePath
return clientAppEngine