Skip to content

Commit e5705e5

Browse files
author
hellertang
authored
Feature/scf function support layer (#714)
* scf function support layers * fix layer bug
1 parent 833871b commit e5705e5

File tree

4 files changed

+65
-7
lines changed

4 files changed

+65
-7
lines changed

tencentcloud/resource_tc_scf_function.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,25 @@ func resourceTencentCloudScfFunction() *schema.Resource {
187187
Default: false,
188188
Description: "Enable L5 for SCF function, default is `false`.",
189189
},
190+
"layers": {
191+
Type: schema.TypeList,
192+
Optional: true,
193+
Description: "The list of association layers.",
194+
Elem: &schema.Resource{
195+
Schema: map[string]*schema.Schema{
196+
"layer_name": {
197+
Type: schema.TypeString,
198+
Required: true,
199+
Description: "The name of Layer.",
200+
},
201+
"layer_version": {
202+
Type: schema.TypeInt,
203+
Required: true,
204+
Description: "The version of layer.",
205+
},
206+
},
207+
},
208+
},
190209
"tags": {
191210
Type: schema.TypeMap,
192211
Optional: true,
@@ -490,6 +509,18 @@ func resourceTencentCloudScfFunctionCreate(d *schema.ResourceData, m interface{}
490509
functionInfo.cosBucketRegion = helper.String(raw.(string))
491510
}
492511

512+
if v, ok := d.GetOk("layers"); ok {
513+
layers := make([]*scf.LayerVersionSimple, 0, 10)
514+
for _, item := range v.([]interface{}) {
515+
m := item.(map[string]interface{})
516+
layer := scf.LayerVersionSimple{}
517+
layer.LayerName = helper.String(m["layer_name"].(string))
518+
layer.LayerVersion = helper.IntInt64(m["layer_version"].(int))
519+
layers = append(layers, &layer)
520+
}
521+
functionInfo.layers = layers
522+
}
523+
493524
enablePublicNet, enablePublicNetOk := d.GetOk("enable_public_net")
494525
enableEipConfig, enableEipConfigOk := d.GetOk("enable_eip_config")
495526

@@ -565,9 +596,9 @@ func resourceTencentCloudScfFunctionCreate(d *schema.ResourceData, m interface{}
565596
imageConfigs = append(imageConfigs, config)
566597
}
567598
codeType = scfFunctionImageCode
599+
functionInfo.imageConfig = imageConfigs[0]
568600
}
569601

570-
functionInfo.imageConfig = imageConfigs[0]
571602

572603
switch codeType {
573604
case scfFunctionCosCode:

tencentcloud/resource_tc_scf_layer.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ $ terraform import tencentcloud_scf_layer.layer layerId#layerVersion
2828
package tencentcloud
2929

3030
import (
31+
"encoding/base64"
3132
"fmt"
33+
"github.com/mitchellh/go-homedir"
34+
"io/ioutil"
3235
"log"
36+
"os"
3337
"strings"
3438

3539
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
@@ -114,7 +118,7 @@ func resourceTencentCloudScfLayer() *schema.Resource {
114118

115119
//compute
116120
"layer_version": {
117-
Type: schema.TypeString,
121+
Type: schema.TypeInt,
118122
Computed: true,
119123
Description: "The version of layer.",
120124
},
@@ -172,17 +176,32 @@ func resourceTencentCloudScfLayerCreate(d *schema.ResourceData, meta interface{}
172176
}
173177
item := items[0].(map[string]interface{})
174178
var content = scf.Code{}
175-
if item["cos_bucket_name"] != nil {
179+
if item["cos_bucket_name"] != "" {
176180
content.CosBucketName = helper.String(item["cos_bucket_name"].(string))
177181
}
178-
if item["cos_object_name"] != nil {
182+
if item["cos_object_name"] != "" {
179183
content.CosObjectName = helper.String(item["cos_object_name"].(string))
180184
}
181-
if item["cos_bucket_region"] != nil {
185+
if item["cos_bucket_region"] != "" {
182186
content.CosBucketRegion = helper.String(item["cos_bucket_region"].(string))
183187
}
184-
if item["zip_file"] != nil {
185-
content.ZipFile = helper.String(item["zip_file"].(string))
188+
if item["zip_file"] != "" {
189+
path, err := homedir.Expand(item["zip_file"].(string))
190+
if err != nil {
191+
return fmt.Errorf("zip file (%s) homedir expand error: %s", item["zip_file"].(string), err.Error())
192+
}
193+
file, err := os.Open(path)
194+
if err != nil {
195+
return fmt.Errorf("zip file (%s) open error: %s", path, err.Error())
196+
}
197+
defer file.Close()
198+
body, err := ioutil.ReadAll(file)
199+
if err != nil {
200+
return fmt.Errorf("zip file (%s) read error: %s", path, err.Error())
201+
}
202+
203+
zipContent := base64.StdEncoding.EncodeToString(body)
204+
content.ZipFile = &zipContent
186205
}
187206
request.Content = &content
188207
}

tencentcloud/service_tencentcloud_scf.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type scfFunctionInfo struct {
2727
clsLogsetId *string
2828
clsTopicId *string
2929
namespace *string
30+
layers []*scf.LayerVersionSimple
3031
l5Enable *bool
3132
publicNetConfig *scf.PublicNetConfigIn
3233

@@ -81,6 +82,7 @@ func (me *ScfService) CreateFunction(ctx context.Context, info scfFunctionInfo)
8182
request.Role = info.role
8283
request.ClsLogsetId = info.clsLogsetId
8384
request.ClsTopicId = info.clsTopicId
85+
request.Layers =info.layers
8486
request.Type = helper.String(SCF_FUNCTION_TYPE_EVENT)
8587

8688
request.Code = &scf.Code{

website/docs/r/scf_function.html.markdown

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ The following arguments are supported:
4343
* `environment` - (Optional) Environment of the SCF function.
4444
* `image_config` - (Optional) Image of the SCF function, conflict with ``.
4545
* `l5_enable` - (Optional) Enable L5 for SCF function, default is `false`.
46+
* `layers` - (Optional) The list of association layers.
4647
* `mem_size` - (Optional) Memory size of the SCF function, unit is MB. The default is `128`MB. The range is 128M-1536M, and the ladder is 128M.
4748
* `namespace` - (Optional, ForceNew) Namespace of the SCF function, default is `default`.
4849
* `role` - (Optional) Role of the SCF function.
@@ -62,6 +63,11 @@ The `image_config` object supports the following:
6263
* `entry_point` - (Optional) The entrypoint of app.
6364
* `registry_id` - (Optional) The registry id of TCR. When image type is enterprise, it must be set.
6465

66+
The `layers` object supports the following:
67+
68+
* `layer_name` - (Required) The name of Layer.
69+
* `layer_version` - (Required) The version of layer.
70+
6571
The `triggers` object supports the following:
6672

6773
* `name` - (Required) Name of the SCF function trigger, if `type` is `ckafka`, the format of name must be `<ckafkaInstanceId>-<topicId>`; if `type` is `cos`, the name is cos bucket id, other In any case, it can be combined arbitrarily. It can only contain English letters, numbers, connectors and underscores. The maximum length is 100.

0 commit comments

Comments
 (0)