forked from huaweicloud/terraform-provider-huaweicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_huaweicloud_fgs_function.go
2084 lines (1893 loc) · 73.2 KB
/
resource_huaweicloud_fgs_function.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 fgs
import (
"context"
"fmt"
"log"
"reflect"
"strconv"
"strings"
"time"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/chnsz/golangsdk"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/common"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)
// @API FunctionGraph POST /v2/{project_id}/fgs/functions
// @API FunctionGraph PUT /v2/{project_id}/fgs/functions/{function_urn}/config
// @API FunctionGraph PUT /v2/{project_id}/fgs/functions/{function_urn}/config-max-instance
// @API FunctionGraph GET /v2/{project_id}/fgs/functions/{function_urn}/config
// @API FunctionGraph GET /v2/{project_id}/fgs/functions/{function_urn}/versions
// @API FunctionGraph POST /v2/{project_id}/fgs/functions/{function_urn}/tags/create
// @API FunctionGraph DELETE /v2/{project_id}/fgs/functions/{function_urn}/tags/delete
// @API FunctionGraph PUT /v2/{project_id}/fgs/functions/{function_urn}/code
// @API FunctionGraph POST /v2/{project_id}/fgs/functions/{function_urn}/versions
// @API FunctionGraph GET /v2/{project_id}/fgs/functions/{function_urn}/aliases
// @API FunctionGraph POST /v2/{project_id}/fgs/functions/{function_urn}/aliases
// @API FunctionGraph DELETE /v2/{project_id}/fgs/functions/{function_urn}/aliases/{alias_name}
// @API FunctionGraph DELETE /v2/{project_id}/fgs/functions/{function_urn}
// @API FunctionGraph PUT /v2/{project_id}/fgs/functions/{function_urn}/reservedinstances
// @API FunctionGraph GET /v2/{project_id}/fgs/functions/reservedinstanceconfigs
func ResourceFgsFunction() *schema.Resource {
return &schema.Resource{
CreateContext: resourceFunctionCreate,
ReadContext: resourceFunctionRead,
UpdateContext: resourceFunctionUpdate,
DeleteContext: resourceFunctionDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Description: `The region where the function is located.`,
},
// Required parameters.
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The name of the function.`,
},
"memory_size": {
Type: schema.TypeInt,
Required: true,
Description: `The memory size allocated to the function, in MByte (MB).`,
},
"runtime": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The environment for executing the function.`,
},
"timeout": {
Type: schema.TypeInt,
Required: true,
Description: `The timeout interval of the function, in seconds.`,
},
// Optional parameters but required in documentation.
"app": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"package"},
Description: utils.SchemaDesc(
`The group to which the function belongs.`,
utils.SchemaDescInput{
Required: true,
},
),
},
"code_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: utils.SchemaDesc(
`The code type of the function.`,
utils.SchemaDescInput{
Required: true,
},
),
},
"handler": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: utils.SchemaDesc(
`The entry point of the function.`,
utils.SchemaDescInput{
Required: true,
},
),
},
// Optional parameters.
"description": {
Type: schema.TypeString,
Optional: true,
Description: `The description of the function.`,
},
"functiongraph_version": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"v1", "v2",
}, false), // The current default value is v1, which may be adjusted in the future.
Description: `The description of the function.`,
},
"func_code": {
Type: schema.TypeString,
Optional: true,
StateFunc: utils.DecodeHashAndHexEncode,
Description: `The function code.`,
},
"code_url": {
Type: schema.TypeString,
Optional: true,
Description: `The URL where the function code is stored in OBS.`,
},
"code_filename": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `The name of the function file.`,
},
"depend_list": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `The ID list of the dependencies.`,
},
"user_data": {
Type: schema.TypeString,
Optional: true,
Description: `The key/value information defined for the function.`,
},
"encrypted_user_data": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Description: `The key/value information defined to be encrypted for the function.`,
},
"agency": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"xrole"},
Description: `The agency configuration of the function.`,
},
"app_agency": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `The execution agency enables you to obtain a token or an AK/SK for accessing other cloud services.`,
},
"initializer_handler": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `The initializer of the function.`,
},
"initializer_timeout": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: `The maximum duration the function can be initialized.`,
},
"enterprise_project_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `The ID of the enterprise project to which the function belongs.`,
},
"vpc_id": {
Type: schema.TypeString,
Optional: true,
RequiredWith: []string{"network_id"},
Description: `The ID of the VPC to which the function belongs.`,
},
"network_id": {
Type: schema.TypeString,
Optional: true,
RequiredWith: []string{"vpc_id"},
Description: `The network ID of subnet.`,
},
"dns_list": {
Type: schema.TypeString,
Optional: true,
Computed: true,
RequiredWith: []string{"vpc_id"},
Description: `The private DNS configuration of the function network.`,
},
"mount_user_id": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: `The mount user ID.`,
},
"mount_user_group_id": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: `The mount user group ID.`,
},
"func_mounts": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"mount_type": {
Type: schema.TypeString,
Required: true,
Description: `The mount type.`,
},
"mount_resource": {
Type: schema.TypeString,
Required: true,
Description: `The ID of the mounted resource (corresponding cloud service).`,
},
"mount_share_path": {
Type: schema.TypeString,
Required: true,
Description: `The remote mount path.`,
},
"local_mount_path": {
Type: schema.TypeString,
Required: true,
Description: `The function access path.`,
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: `The mount status.`,
},
},
},
Description: `The list of function mount configuration.`,
},
"custom_image": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"url": {
Type: schema.TypeString,
Required: true,
Description: `The URL of SWR image.`,
},
"command": {
Type: schema.TypeString,
Optional: true,
Description: `The startup commands of the SWR image.`,
},
"args": {
Type: schema.TypeString,
Optional: true,
Description: `The command line arguments used to start the SWR image.`,
},
"working_dir": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `The working directory of the SWR image.`,
},
"user_id": {
Type: schema.TypeString,
Optional: true,
Description: utils.SchemaDesc(
`The user ID that used to run SWR image.`,
utils.SchemaDescInput{
Internal: true,
},
),
},
"user_group_id": {
Type: schema.TypeString,
Optional: true,
Description: utils.SchemaDesc(
`The user group ID that used to run SWR image.`,
utils.SchemaDescInput{
Internal: true,
},
),
},
},
},
ConflictsWith: []string{
"code_type",
},
Description: `The custom image configuration of the function.`,
},
"max_instance_num": {
// The original type of this parameter is int, but its zero value is meaningful.
// So, the following types of parameter passing are realized through the logic of terraform's implicit
// conversion of int:
// + -1: the number of instances is unlimited.
// + 0: this function is disabled.
// + (0, +1000]: Specific value (2023.06.26).
// + empty: keep the default (latest updated) value.
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `The maximum number of instances of the function.`,
},
"versions": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: `The version name.`,
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: `The description of the version.`,
},
"aliases": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: `The name of the version alias.`,
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: `The description of the version alias.`,
},
"additional_version_weights": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringIsJSON,
Description: `The percentage grayscale configuration of the version alias.`,
},
"additional_version_strategy": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringIsJSON,
Description: `The description of the version alias.`,
},
},
},
Description: `The aliases management for specified version.`,
},
},
},
Description: `The versions management of the function.`,
},
"tags": common.TagsSchema(
`The key/value pairs to associate with the function.`,
),
"log_group_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
RequiredWith: []string{
"log_stream_id",
"log_group_name",
"log_stream_name",
},
Description: `The LTS group ID for collecting logs.`,
},
"log_group_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
RequiredWith: []string{"log_group_id"},
Description: `The LTS group name for collecting logs.`,
},
"log_stream_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
RequiredWith: []string{"log_group_id"},
Description: `The LTS stream ID for collecting logs.`,
},
"log_stream_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
RequiredWith: []string{"log_group_id"},
Description: `The LTS stream name for collecting logs.`,
},
"reserved_instances": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"qualifier_type": {
Type: schema.TypeString,
Required: true,
Description: `The qualifier type of reserved instance.`,
},
"qualifier_name": {
Type: schema.TypeString,
Required: true,
Description: `The version name or alias name.`,
},
"count": {
Type: schema.TypeInt,
Required: true,
Description: `The number of reserved instance.`,
},
"idle_mode": {
Type: schema.TypeBool,
Optional: true,
Description: `Whether to enable the idle mode.`,
},
"tactics_config": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: tracticsConfigsSchema(),
Description: `The auto scaling policies for reserved instance.`,
},
},
},
Description: `The reserved instance policies of the function.`,
},
// The value in the api document is -1 to 1000, After confirmation, when the parameter set to -1 or 0,
// the actual number of concurrent requests is 1, so the value range is set to 1 to 1000, and the document
// will be modified later (2024.02.29).
"concurrency_num": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: `The number of concurrent requests of the function.`,
},
"gpu_type": {
Type: schema.TypeString,
Optional: true,
RequiredWith: []string{"gpu_memory"},
Description: `The GPU type of the function.`,
},
"gpu_memory": {
Type: schema.TypeInt,
Optional: true,
RequiredWith: []string{"gpu_type"},
Description: `The GPU memory size allocated to the function, in MByte (MB).`,
},
// Currently, the "pre_stop_timeout" and "pre_stop_timeout" are not visible on the page,
// so they are temporarily used as internal parameters.
"pre_stop_handler": {
Type: schema.TypeString,
Optional: true,
Description: utils.SchemaDesc(
`The pre-stop handler of a function.`,
utils.SchemaDescInput{
Internal: true,
},
),
},
"pre_stop_timeout": {
Type: schema.TypeInt,
Optional: true,
Description: utils.SchemaDesc(
`The maximum duration that the function can be initialized.`,
utils.SchemaDescInput{
Internal: true,
},
),
},
"enable_dynamic_memory": {
Type: schema.TypeBool,
Optional: true,
// The dynamic memory function can be closed, so computed behavior cannot be supported.
Description: `Whether the dynamic memory configuration is enabled.`,
},
"is_stateful_function": {
Type: schema.TypeBool,
Optional: true,
// The stateful function can be closed, so computed behavior cannot be supported.
Description: `Whether the function is a stateful function.`,
},
"network_controller": {
Type: schema.TypeList,
Optional: true,
// The network controller can be closed, so computed behavior cannot be supported.
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"trigger_access_vpcs": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"vpc_id": {
Type: schema.TypeString,
Required: true,
Description: `The ID of the VPC that can trigger the function.`,
},
},
},
Description: `The configuration of the VPCs that can trigger the function.`,
},
"disable_public_network": {
Type: schema.TypeBool,
Optional: true,
Description: `Whether to disable the public network access.`,
},
},
},
Description: `The network configuration of the function.`,
},
"peering_cidr": {
Type: schema.TypeString,
Optional: true,
// The perring CIDR can be canceled, so computed behavior cannot be supported.
Description: `The VPC CIDR blocks used in the function code to detect whether it conflicts with the VPC
CIDR blocks used by the service.`,
},
"enable_auth_in_header": {
Type: schema.TypeBool,
Optional: true,
// The auth function can be closed, so computed behavior cannot be supported.
// And the default value (in the service API) is false.
Description: `Whether the authentication in the request header is enabled.`,
},
"enable_class_isolation": {
Type: schema.TypeBool,
Optional: true,
// The isolation function can be closed, so computed behavior cannot be supported.
// And the default value (in the service API) is false.
Description: `Whether the class isolation is enabled for the JAVA runtime functions.`,
},
"ephemeral_storage": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: `The size of the function ephemeral storage.`,
},
"heartbeat_handler": {
Type: schema.TypeString,
Optional: true,
// The handler of heartbeat can be omitted, and if this parameter is not configured, the default value
// will not be returned. So, the computed behavior cannot be supported.
Description: `The heartbeat handler of the function.`,
},
"restore_hook_handler": {
Type: schema.TypeString,
Optional: true,
// The handler of restore hook can be omitted, and if this parameter is not configured, the default
// value will not be returned. So, the computed behavior cannot be supported.
Description: `The restore hook handler of the function.`,
},
"restore_hook_timeout": {
Type: schema.TypeInt,
Optional: true,
// The timeout of restore hook can be omitted, and if this parameter is not configured, the default
// value will not be returned. So, the computed behavior cannot be supported.
Description: `The timeout of the function restore hook.`,
},
"lts_custom_tag": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
DiffSuppressFunc: utils.SuppressMapDiffs(),
// The custom tags can be set to empty, so computed behavior cannot be supported.
Description: `The custom tags configuration that used to filter the LTS logs.`,
},
// Deprecated parameters.
"package": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"app"},
Deprecated: `use app instead`,
},
"xrole": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"agency"},
Deprecated: `use agency instead`,
},
// Attributes.
"urn": {
Type: schema.TypeString,
Computed: true,
Description: `The URN (Uniform Resource Name) of the function.`,
},
"version": {
Type: schema.TypeString,
Computed: true,
Description: `The version of the function.`,
},
"lts_custom_tag_origin": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: utils.SchemaDesc(
`The script configuration value of this change is also the original value used for comparison with
the new value next time the change is made. The corresponding parameter name is 'lts_custom_tag'.`,
utils.SchemaDescInput{
Internal: true,
},
),
},
},
}
}
func tracticsConfigsSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"cron_configs": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: `The name of scheduled policy configuration.`,
},
"cron": {
Type: schema.TypeString,
Required: true,
Description: `The cron expression.`,
},
"count": {
Type: schema.TypeInt,
Required: true,
Description: `The number of reserved instance to which the policy belongs.`,
},
"start_time": {
Type: schema.TypeInt,
Required: true,
Description: `The effective timestamp of policy.`,
},
"expired_time": {
Type: schema.TypeInt,
Required: true,
Description: `The expiration timestamp of the policy.`,
},
},
},
Description: `The list of scheduled policy configurations.`,
},
"metric_configs": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: `The name of metric policy.`,
},
"type": {
Type: schema.TypeString,
Required: true,
Description: `The type of metric policy.`,
},
"threshold": {
Type: schema.TypeInt,
Required: true,
Description: `The metric policy threshold.`,
},
"min": {
Type: schema.TypeInt,
Required: true,
Description: `The minimun of traffic.`,
},
},
},
Description: `The list of metric policy configurations.`,
},
},
}
}
func buildFunctionCustomImage(imageConfigs []interface{}) map[string]interface{} {
if len(imageConfigs) < 1 {
return nil
}
imageConfig := imageConfigs[0]
return map[string]interface{}{
"enabled": true,
"image": utils.ValueIgnoreEmpty(utils.PathSearch("url", imageConfig, nil)),
"command": utils.ValueIgnoreEmpty(utils.PathSearch("command", imageConfig, nil)),
"args": utils.ValueIgnoreEmpty(utils.PathSearch("args", imageConfig, nil)),
"working_dir": utils.ValueIgnoreEmpty(utils.PathSearch("working_dir", imageConfig, nil)),
"uid": utils.ValueIgnoreEmpty(utils.PathSearch("user_id", imageConfig, nil)),
"gid": utils.ValueIgnoreEmpty(utils.PathSearch("user_group_id", imageConfig, nil)),
}
}
func buildFunctionCodeConfig(funcCode string) map[string]interface{} {
if funcCode == "" {
return nil
}
return map[string]interface{}{
"file": utils.TryBase64EncodeString(funcCode),
}
}
func buildFunctionLogConfig(d *schema.ResourceData) map[string]interface{} {
// If the LTS log parameters is not configured (in the creation phase), the service will automatically create an
// LTS stream (group will also be created) and associate it with the function.
// So, the tfstate records will always have the log group ID and the log stream ID.
groupName, ok := d.GetOk("log_group_name") // Only log group name and the log stream name are specified by users.
if !ok {
return nil
}
return map[string]interface{}{
"group_id": d.Get("log_group_id"),
"group_name": groupName,
"stream_id": d.Get("log_stream_id"),
"stream_name": utils.ValueIgnoreEmpty(d.Get("log_stream_name")),
}
}
func buildNetworkControllerTriggerAccessVpcs(triggerAccessVpcs []interface{}) []map[string]interface{} {
if len(triggerAccessVpcs) < 1 {
return nil
}
result := make([]map[string]interface{}, 0, len(triggerAccessVpcs))
for _, triggerAccessVpc := range triggerAccessVpcs {
result = append(result, map[string]interface{}{
"vpc_id": utils.PathSearch("vpc_id", triggerAccessVpc, nil),
})
}
return result
}
func buildFunctionNetworkController(networkControlers []interface{}) map[string]interface{} {
if len(networkControlers) < 1 {
return nil
}
networkControler := networkControlers[0]
return map[string]interface{}{
"trigger_access_vpcs": buildNetworkControllerTriggerAccessVpcs(utils.PathSearch("trigger_access_vpcs",
networkControler, schema.NewSet(schema.HashString, nil)).(*schema.Set).List()),
"disable_public_network": utils.PathSearch("disable_public_network", networkControler, nil),
}
}
func buildCreateFunctionBodyParams(cfg *config.Config, d *schema.ResourceData) map[string]interface{} {
// Parameter app is recommended to replace parameter package.
pkg, ok := d.GetOk("app")
if !ok {
pkg = d.Get("package")
}
// Parameter agency is recommended to replace parameter xrole.
agency, ok := d.GetOk("agency")
if !ok {
agency = d.Get("xrole")
}
return map[string]interface{}{
// Required parameters.
"func_name": d.Get("name"),
"runtime": d.Get("runtime"),
"timeout": d.Get("timeout"),
"memory_size": d.Get("memory_size"),
// Optional parameters but required in documentation.
"package": utils.ValueIgnoreEmpty(pkg),
"handler": utils.ValueIgnoreEmpty(d.Get("handler")),
"code_type": utils.ValueIgnoreEmpty(d.Get("code_type")),
// Optional parameters.
"description": utils.ValueIgnoreEmpty(d.Get("description")),
"type": utils.ValueIgnoreEmpty(d.Get("functiongraph_version")),
"code_url": utils.ValueIgnoreEmpty(d.Get("code_url")),
"code_filename": utils.ValueIgnoreEmpty(d.Get("code_filename")),
"user_data": utils.ValueIgnoreEmpty(d.Get("user_data")),
"encrypted_user_data": utils.ValueIgnoreEmpty(d.Get("encrypted_user_data")),
"xrole": utils.ValueIgnoreEmpty(agency),
"enterprise_project_id": cfg.GetEnterpriseProjectID(d),
"custom_image": buildFunctionCustomImage(d.Get("custom_image").([]interface{})),
"gpu_memory": utils.ValueIgnoreEmpty(d.Get("gpu_memory")),
"gpu_type": utils.ValueIgnoreEmpty(d.Get("gpu_type")),
"pre_stop_handler": utils.ValueIgnoreEmpty(d.Get("pre_stop_handler")),
"pre_stop_timeout": utils.ValueIgnoreEmpty(d.Get("pre_stop_timeout")),
"func_code": buildFunctionCodeConfig(d.Get("func_code").(string)),
"log_config": buildFunctionLogConfig(d),
"enable_dynamic_memory": d.Get("enable_dynamic_memory"),
"is_stateful_function": d.Get("is_stateful_function"),
"network_controller": buildFunctionNetworkController(d.Get("network_controller").([]interface{})),
"lts_custom_tag": utils.ValueIgnoreEmpty(d.Get("lts_custom_tag")),
}
}
func createFunction(cfg *config.Config, client *golangsdk.ServiceClient, d *schema.ResourceData) (string, error) {
httpUrl := "v2/{project_id}/fgs/functions"
createPath := client.Endpoint + httpUrl
createPath = strings.ReplaceAll(createPath, "{project_id}", client.ProjectID)
createOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
MoreHeaders: map[string]string{
"Content-Type": "application/json",
},
JSONBody: utils.RemoveNil(buildCreateFunctionBodyParams(cfg, d)),
}
requestResp, err := client.Request("POST", createPath, &createOpt)
if err != nil {
return "", err
}
respBody, err := utils.FlattenResponse(requestResp)
if err != nil {
return "", err
}
return utils.PathSearch("func_urn", respBody, "").(string), nil
}
func buildFunctionVpcConfig(d *schema.ResourceData) map[string]interface{} {
vpcId, ok := d.GetOk("vpc_id")
if !ok {
return nil
}
return map[string]interface{}{
"vpc_id": vpcId,
"subnet_id": d.Get("network_id"),
}
}
func parseFunctionMountId(mountId int) int {
if mountId < 1 {
return -1
}
return mountId
}
func buildFunctionMountConfig(mounts []interface{}, mountUserId, mountGroupId int) map[string]interface{} {
if len(mounts) < 1 {
return nil
}
parsedMounts := make([]interface{}, 0, len(mounts))
for _, mount := range mounts {
parsedMounts = append(parsedMounts, map[string]interface{}{
"mount_type": utils.PathSearch("mount_type", mount, nil),
"mount_resource": utils.PathSearch("mount_resource", mount, nil),
"mount_share_path": utils.PathSearch("mount_share_path", mount, nil),
"local_mount_path": utils.PathSearch("local_mount_path", mount, nil),
})
}
return map[string]interface{}{
"mount_config": parsedMounts,
"mount_user": map[string]interface{}{
"mount_user_id": parseFunctionMountId(mountUserId),
"mount_user_group_id": parseFunctionMountId(mountGroupId),
},
}
}
func buildFunctionStrategyConfig(concurrencyNum int) map[string]interface{} {
if concurrencyNum < 1 {
return nil
}
return map[string]interface{}{
"concurrent_num": concurrencyNum,
}
}
func buildUpdateFunctionMetadataBodyParams(cfg *config.Config, d *schema.ResourceData) map[string]interface{} {
// Parameter app is recommended to replace parameter package.
pkg, ok := d.GetOk("app")
if !ok {
pkg = d.Get("package")
}
// Parameter agency is recommended to replace parameter xrole.
agency, ok := d.GetOk("agency")
if !ok {
pkg = d.Get("xrole")
}
return map[string]interface{}{
// Required parameters.
"runtime": d.Get("runtime"),
"timeout": d.Get("timeout"),
"memory_size": d.Get("memory_size"),
// Optional parameters but required in documentation.
"package": utils.ValueIgnoreEmpty(pkg),
"handler": utils.ValueIgnoreEmpty(d.Get("handler")),
// Optional parameters.
"description": d.Get("description"),
"user_data": utils.ValueIgnoreEmpty(d.Get("user_data")),
"encrypted_user_data": utils.ValueIgnoreEmpty(d.Get("encrypted_user_data")),
"xrole": utils.ValueIgnoreEmpty(agency),
"app_xrole": utils.ValueIgnoreEmpty(d.Get("app_agency")),
"custom_image": buildFunctionCustomImage(d.Get("custom_image").([]interface{})),
"gpu_memory": utils.ValueIgnoreEmpty(d.Get("gpu_memory")),
"gpu_type": utils.ValueIgnoreEmpty(d.Get("gpu_type")),
"pre_stop_handler": utils.ValueIgnoreEmpty(d.Get("pre_stop_handler")),
"pre_stop_timeout": utils.ValueIgnoreEmpty(d.Get("pre_stop_timeout")),
"log_config": buildFunctionLogConfig(d),
"domain_names": utils.ValueIgnoreEmpty(d.Get("dns_list")),
"func_vpc": buildFunctionVpcConfig(d),
"func_mounts": buildFunctionMountConfig(d.Get("func_mounts").([]interface{}),
d.Get("mount_user_id").(int), d.Get("mount_user_group_id").(int)),
"strategy_config": buildFunctionStrategyConfig(d.Get("concurrency_num").(int)),
"enable_dynamic_memory": d.Get("enable_dynamic_memory"),
"is_stateful_function": d.Get("is_stateful_function"),
"network_controller": buildFunctionNetworkController(d.Get("network_controller").([]interface{})),
"enterprise_project_id": cfg.GetEnterpriseProjectID(d),
"peering_cidr": d.Get("peering_cidr"),
"enable_auth_in_header": d.Get("enable_auth_in_header"),
"enable_class_isolation": d.Get("enable_class_isolation"),
"ephemeral_storage": utils.ValueIgnoreEmpty(d.Get("ephemeral_storage")),
"heartbeat_handler": d.Get("heartbeat_handler"),
"restore_hook_handler": d.Get("restore_hook_handler"),
"restore_hook_timeout": d.Get("restore_hook_timeout"),
"lts_custom_tag": utils.ValueIgnoreEmpty(d.Get("lts_custom_tag")),
}
}
func updateFunctionMetadata(client *golangsdk.ServiceClient, cfg *config.Config, d *schema.ResourceData, functionUrn string) error {
httpUrl := "v2/{project_id}/fgs/functions/{function_urn}/config"
updatePath := client.Endpoint + httpUrl
updatePath = strings.ReplaceAll(updatePath, "{project_id}", client.ProjectID)
updatePath = strings.ReplaceAll(updatePath, "{function_urn}", functionUrn)
updateOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
MoreHeaders: map[string]string{
"Content-Type": "application/json",
},
JSONBody: utils.RemoveNil(buildUpdateFunctionMetadataBodyParams(cfg, d)),
}
_, err := client.Request("PUT", updatePath, &updateOpt)
if err != nil {
return fmt.Errorf("failed to update the function metadata: %s", err)
}
return nil
}
func buildUpdateFunctionCodeBodyParams(d *schema.ResourceData) map[string]interface{} {
return map[string]interface{}{
"code_type": d.Get("code_type"),
"code_url": d.Get("code_url"),
"code_filename": d.Get("code_filename"),
"depend_version_list": d.Get("depend_list").(*schema.Set).List(),
"func_code": buildFunctionCodeConfig(d.Get("func_code").(string)),
}
}
func updateFunctionCode(client *golangsdk.ServiceClient, d *schema.ResourceData, functionUrn string) error {
httpUrl := "v2/{project_id}/fgs/functions/{function_urn}/code"
updatePath := client.Endpoint + httpUrl
updatePath = strings.ReplaceAll(updatePath, "{project_id}", client.ProjectID)
updatePath = strings.ReplaceAll(updatePath, "{function_urn}", functionUrn)
updateOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
MoreHeaders: map[string]string{