Skip to content

Commit 60ca12b

Browse files
author
hellertang
authored
support create scf by image (#699)
1 parent 1e83b55 commit 60ca12b

File tree

9 files changed

+363
-29
lines changed

9 files changed

+363
-29
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ require (
3939
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor v1.0.199
4040
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.199
4141
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis v1.0.199
42-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/scf v1.0.199
42+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/scf v1.0.267
4343
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sqlserver v1.0.199
4444
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ssl v1.0.199
4545
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ssm v1.0.199

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis v1.0.199 h1:lXCng
494494
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis v1.0.199/go.mod h1:5bwboqeXqVnRvUlKn2G9Y9DbOnWMSVQ0zWhhPZKUVZE=
495495
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/scf v1.0.199 h1:vvxCCj6RiRM4FkSdxncroAx9JGD6xBBhAnXugQrE3j8=
496496
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/scf v1.0.199/go.mod h1:Pew6DV5oBGrzHYWZ8ssiHeJS/Z39ggVv1y5ADGWdO4s=
497+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/scf v1.0.267 h1:ipvvQOzGyUUGohsVLElJnDDZn4Xoq1axk8my+GLyRYs=
498+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/scf v1.0.267/go.mod h1:Pew6DV5oBGrzHYWZ8ssiHeJS/Z39ggVv1y5ADGWdO4s=
497499
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sqlserver v1.0.199 h1:Ms62XLYCuqrdG4mD5S72oj/ZxdNTxJ+Mc4w0Kxqucwo=
498500
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sqlserver v1.0.199/go.mod h1:ySz4zbciCFruAviNMeBcu7wW2+BY9Maw8qGWawTywkM=
499501
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ssl v1.0.199 h1:UkF3qBxMbpOf2VjxgzMgqvBCS+Hqr8XXSrtRd+rABMk=

tencentcloud/resource_tc_scf_function.go

Lines changed: 106 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,31 +207,75 @@ func resourceTencentCloudScfFunction() *schema.Resource {
207207
"cos_bucket_name": {
208208
Type: schema.TypeString,
209209
Optional: true,
210-
ConflictsWith: []string{"zip_file"},
210+
ConflictsWith: []string{"zip_file", "image_config"},
211211
Description: "Cos bucket name of the SCF function, such as `cos-1234567890`, conflict with `zip_file`.",
212212
},
213213
"cos_object_name": {
214214
Type: schema.TypeString,
215215
Optional: true,
216-
ConflictsWith: []string{"zip_file"},
216+
ConflictsWith: []string{"zip_file", "image_config"},
217217
ValidateFunc: validateStringSuffix(".zip", ".jar"),
218218
Description: "Cos object name of the SCF function, should have suffix `.zip` or `.jar`, conflict with `zip_file`.",
219219
},
220220
"cos_bucket_region": {
221221
Type: schema.TypeString,
222222
Optional: true,
223-
ConflictsWith: []string{"zip_file"},
223+
ConflictsWith: []string{"zip_file", "image_config"},
224224
Description: "Cos bucket region of the SCF function, conflict with `zip_file`.",
225225
},
226226

227227
// zip upload
228228
"zip_file": {
229229
Type: schema.TypeString,
230230
Optional: true,
231-
ConflictsWith: []string{"cos_bucket_name", "cos_object_name", "cos_bucket_region"},
231+
ConflictsWith: []string{"cos_bucket_name", "cos_object_name", "cos_bucket_region", "image_config"},
232232
Description: "Zip file of the SCF function, conflict with `cos_bucket_name`, `cos_object_name`, `cos_bucket_region`.",
233233
},
234234

235+
// image
236+
"image_config": {
237+
Type: schema.TypeList,
238+
Optional: true,
239+
ConflictsWith: []string{"cos_bucket_name", "cos_object_name", "cos_bucket_region", "zip_file"},
240+
Description: "Image of the SCF function, conflict with ``.",
241+
Elem: &schema.Resource{
242+
Schema: map[string]*schema.Schema{
243+
"image_type": {
244+
Type: schema.TypeString,
245+
Required: true,
246+
ValidateFunc: validateAllowedStringValue([]string{"personal", "enterprise"}),
247+
Description: "The image type. personal or enterprise.",
248+
},
249+
"image_uri": {
250+
Type: schema.TypeString,
251+
Required: true,
252+
Description: "The uri of image.",
253+
},
254+
"registry_id": {
255+
Type: schema.TypeString,
256+
Optional: true,
257+
Description: "The registry id of TCR. When image type is enterprise, it must be set.",
258+
},
259+
"entry_point": {
260+
Type: schema.TypeString,
261+
Optional: true,
262+
Description: "The entrypoint of app.",
263+
},
264+
"command": {
265+
Type: schema.TypeString,
266+
Optional: true,
267+
Description: "The command of entrypoint.",
268+
},
269+
"args": {
270+
Type: schema.TypeString,
271+
Optional: true,
272+
Description: "the parameters of command.",
273+
},
274+
275+
},
276+
},
277+
},
278+
235279
"triggers": {
236280
Type: schema.TypeSet,
237281
Optional: true,
@@ -423,6 +467,7 @@ func resourceTencentCloudScfFunctionCreate(d *schema.ResourceData, m interface{}
423467
const (
424468
scfFunctionCosCode scfFunctionCodeType = iota + 1 // start at 1 so we can check if codeType set or not
425469
scfFunctionZipFileCode
470+
scfFunctionImageCode
426471
)
427472

428473
var codeType scfFunctionCodeType
@@ -496,14 +541,42 @@ func resourceTencentCloudScfFunctionCreate(d *schema.ResourceData, m interface{}
496541
functionInfo.zipFile = &content
497542
}
498543

544+
var imageConfigs = make([]*scf.ImageConfig, 0)
545+
546+
if raw, ok := d.GetOk("image_config"); ok {
547+
configs := raw.([]interface{})
548+
for _, v := range configs {
549+
value := v.(map[string]interface{})
550+
imageType := value["image_type"].(string)
551+
imageUri := value["image_uri"].(string)
552+
registryId := value["registry_id"].(string)
553+
entryPoint := value["entry_point"].(string)
554+
command := value["command"].(string)
555+
args := value["args"].(string)
556+
557+
config := &scf.ImageConfig{
558+
ImageType : &imageType,
559+
ImageUri: &imageUri,
560+
RegistryId: &registryId,
561+
EntryPoint: &entryPoint,
562+
Command: &command,
563+
Args: &args,
564+
}
565+
imageConfigs = append(imageConfigs, config)
566+
}
567+
codeType = scfFunctionImageCode
568+
}
569+
570+
functionInfo.imageConfig = imageConfigs[0]
571+
499572
switch codeType {
500573
case scfFunctionCosCode:
501574
if err := helper.CheckIfSetTogether(d, "cos_bucket_name", "cos_object_name", "cos_bucket_region"); err != nil {
502575
return err
503576
}
504577

505578
case scfFunctionZipFileCode:
506-
579+
case scfFunctionImageCode:
507580
default:
508581
return errors.New("no function code set")
509582
}
@@ -764,6 +837,34 @@ func resourceTencentCloudScfFunctionUpdate(d *schema.ResourceData, m interface{}
764837
functionInfo.zipFile = &content
765838
}
766839

840+
if d.HasChange("image_config") {
841+
updateAttrs = append(updateAttrs, "image_config")
842+
if raw, ok := d.GetOk("image_config"); ok {
843+
var imageConfigs = make([]*scf.ImageConfig, 0)
844+
configs := raw.([]interface{})
845+
for _, v := range configs {
846+
value := v.(map[string]interface{})
847+
imageType := value["image_type"].(string)
848+
imageUri := value["image_uri"].(string)
849+
registryId := value["registry_id"].(string)
850+
entryPoint := value["entry_point"].(string)
851+
command := value["command"].(string)
852+
args := value["args"].(string)
853+
854+
config := &scf.ImageConfig{
855+
ImageType : &imageType,
856+
ImageUri: &imageUri,
857+
RegistryId: &registryId,
858+
EntryPoint: &entryPoint,
859+
Command: &command,
860+
Args: &args,
861+
}
862+
imageConfigs = append(imageConfigs, config)
863+
}
864+
functionInfo.imageConfig = imageConfigs[0]
865+
}
866+
}
867+
767868
// update function code
768869
if len(updateAttrs) > 0 {
769870
if len(updateAttrs) == 0 && updateAttrs[0] == "handler" {

tencentcloud/service_tencentcloud_scf.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ type scfFunctionInfo struct {
3535
cosBucketRegion *string
3636

3737
zipFile *string
38+
39+
imageConfig *scf.ImageConfig
3840
}
3941

4042
type scfTrigger struct {
@@ -86,6 +88,7 @@ func (me *ScfService) CreateFunction(ctx context.Context, info scfFunctionInfo)
8688
CosObjectName: info.cosObjectName,
8789
CosBucketRegion: info.cosBucketRegion,
8890
ZipFile: info.zipFile,
91+
ImageConfig: info.imageConfig,
8992
}
9093

9194
if err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
@@ -194,6 +197,7 @@ func (me *ScfService) ModifyFunctionCode(ctx context.Context, info scfFunctionIn
194197
CosObjectName: info.cosObjectName,
195198
CosBucketRegion: info.cosBucketRegion,
196199
ZipFile: info.zipFile,
200+
ImageConfig: info.imageConfig,
197201
}
198202

199203
if err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {

0 commit comments

Comments
 (0)