From 333cb18e7aae5bf99425a0450343121f4d5e2dec Mon Sep 17 00:00:00 2001 From: Yin Luo Date: Sun, 8 Oct 2023 19:46:47 +0800 Subject: [PATCH] support tencentcloud_mps_word_sample (#2176) * add tencentcloud_mps_word_sample * add changelog --- .changelog/2176.txt | 3 + tencentcloud/provider.go | 2 + tencentcloud/resource_tc_mps_word_sample.go | 286 ++++++++++++++++++ .../resource_tc_mps_word_sample_test.go | 71 +++++ tencentcloud/service_tencentcloud_mps.go | 84 +++++ website/docs/r/mps_word_sample.html.markdown | 47 +++ website/tencentcloud.erb | 3 + 7 files changed, 496 insertions(+) create mode 100644 .changelog/2176.txt create mode 100644 tencentcloud/resource_tc_mps_word_sample.go create mode 100644 tencentcloud/resource_tc_mps_word_sample_test.go create mode 100644 website/docs/r/mps_word_sample.html.markdown diff --git a/.changelog/2176.txt b/.changelog/2176.txt new file mode 100644 index 0000000000..b460754399 --- /dev/null +++ b/.changelog/2176.txt @@ -0,0 +1,3 @@ +```release-note:new-resource +tencentcloud_mps_word_sample +``` \ No newline at end of file diff --git a/tencentcloud/provider.go b/tencentcloud/provider.go index 4f82438687..251ff51199 100644 --- a/tencentcloud/provider.go +++ b/tencentcloud/provider.go @@ -1571,6 +1571,7 @@ Media Processing Service(MPS) tencentcloud_mps_adaptive_dynamic_streaming_template tencentcloud_mps_person_sample tencentcloud_mps_withdraws_watermark_operation + tencentcloud_mps_word_sample Cloud HDFS(CHDFS) Data Source @@ -3065,6 +3066,7 @@ func Provider() *schema.Provider { "tencentcloud_mps_adaptive_dynamic_streaming_template": resourceTencentCloudMpsAdaptiveDynamicStreamingTemplate(), "tencentcloud_mps_person_sample": resourceTencentCloudMpsPersonSample(), "tencentcloud_mps_withdraws_watermark_operation": resourceTencentCloudMpsWithdrawsWatermarkOperation(), + "tencentcloud_mps_word_sample": resourceTencentCloudMpsWordSample(), "tencentcloud_cbs_disk_backup": resourceTencentCloudCbsDiskBackup(), "tencentcloud_cbs_snapshot_share_permission": resourceTencentCloudCbsSnapshotSharePermission(), "tencentcloud_cbs_disk_backup_rollback_operation": resourceTencentCloudCbsDiskBackupRollbackOperation(), diff --git a/tencentcloud/resource_tc_mps_word_sample.go b/tencentcloud/resource_tc_mps_word_sample.go new file mode 100644 index 0000000000..a365c451d8 --- /dev/null +++ b/tencentcloud/resource_tc_mps_word_sample.go @@ -0,0 +1,286 @@ +/* +Provides a resource to create a mps word_sample + +Example Usage + +```hcl +resource "tencentcloud_mps_word_sample" "word_sample" { + usages = ["Recognition.Ocr","Review.Ocr","Review.Asr"] + keyword = "tf_test_kw_1" + tags = ["tags_1", "tags_2"] +} +``` + +Import + +mps word_sample can be imported using the id, e.g. + +``` +terraform import tencentcloud_mps_word_sample.word_sample keyword +``` +*/ +package tencentcloud + +import ( + "context" + "fmt" + "log" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + mps "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mps/v20190612" + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" +) + +func resourceTencentCloudMpsWordSample() *schema.Resource { + return &schema.Resource{ + Create: resourceTencentCloudMpsWordSampleCreate, + Read: resourceTencentCloudMpsWordSampleRead, + Update: resourceTencentCloudMpsWordSampleUpdate, + Delete: resourceTencentCloudMpsWordSampleDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ + "usages": { + Required: true, + Type: schema.TypeSet, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + Description: "Keyword usage. Valid values: 1.`Recognition.Ocr`: OCR-based content recognition. 2.`Recognition.Asr`: ASR-based content recognition. 3.`Review.Ocr`: OCR-based inappropriate information recognition. 4.`Review.Asr`: ASR-based inappropriate information recognition.", + }, + + "keyword": { + Type: schema.TypeString, + Required: true, + Description: "Keyword. Length limit: 20 characters.", + }, + "tags": { + Type: schema.TypeSet, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + Optional: true, + Description: "Keyword tag. Array length limit: 20 tags. Each tag length limit: 128 characters.", + }, + }, + } +} + +func resourceTencentCloudMpsWordSampleCreate(d *schema.ResourceData, meta interface{}) error { + defer logElapsed("resource.tencentcloud_mps_word_sample.create")() + defer inconsistentCheck(d, meta)() + + logId := getLogId(contextNil) + + var ( + request = mps.NewCreateWordSamplesRequest() + // response = mps.NewCreateWordSamplesResponse() + keyword string + ) + if v, ok := d.GetOk("usages"); ok { + usagesSet := v.(*schema.Set).List() + for i := range usagesSet { + if usagesSet[i] != nil { + usages := usagesSet[i].(string) + request.Usages = append(request.Usages, &usages) + } + } + } + + aiSampleWordInfo := mps.AiSampleWordInfo{} + if v, ok := d.GetOk("keyword"); ok { + aiSampleWordInfo.Keyword = helper.String(v.(string)) + keyword = v.(string) + } + + if v, ok := d.GetOk("tags"); ok { + tagsSet := v.(*schema.Set).List() + for i := range tagsSet { + if tagsSet[i] != nil { + tags := tagsSet[i].(string) + aiSampleWordInfo.Tags = append(aiSampleWordInfo.Tags, &tags) + } + } + } + request.Words = append(request.Words, &aiSampleWordInfo) + + //if v, ok := d.GetOk("words"); ok { + // for _, item := range v.([]interface{}) { + // dMap := item.(map[string]interface{}) + // aiSampleWordInfo := mps.AiSampleWordInfo{} + // if v, ok := dMap["keyword"]; ok { + // aiSampleWordInfo.Keyword = helper.String(v.(string)) + // keywords = append(keywords, v.(string)) + // } + // if v, ok := dMap["tags"]; ok { + // tagsSet := v.(*schema.Set).List() + // for i := range tagsSet { + // if tagsSet[i] != nil { + // tags := tagsSet[i].(string) + // aiSampleWordInfo.Tags = append(aiSampleWordInfo.Tags, &tags) + // } + // } + // } + // request.Words = append(request.Words, &aiSampleWordInfo) + // } + //} + + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { + result, e := meta.(*TencentCloudClient).apiV3Conn.UseMpsClient().CreateWordSamples(request) + if e != nil { + return retryError(e) + } else { + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) + } + return nil + }) + if err != nil { + log.Printf("[CRITAL]%s create mps wordSample failed, reason:%+v", logId, err) + return err + } + + d.SetId(keyword) + + return resourceTencentCloudMpsWordSampleRead(d, meta) +} + +func resourceTencentCloudMpsWordSampleRead(d *schema.ResourceData, meta interface{}) error { + defer logElapsed("resource.tencentcloud_mps_word_sample.read")() + defer inconsistentCheck(d, meta)() + + logId := getLogId(contextNil) + + ctx := context.WithValue(context.TODO(), logIdKey, logId) + + service := MpsService{client: meta.(*TencentCloudClient).apiV3Conn} + + //keywords := strings.Split(d.Id(), FILED_SP) + //if len(keywords) == 0 { + // return fmt.Errorf("id is broken,%s", d.Id()) + //} + keyword := d.Id() + + wordSample, err := service.DescribeMpsWordSampleById(ctx, keyword) + if err != nil { + return err + } + + if wordSample == nil { + d.SetId("") + log.Printf("[WARN]%s resource `MpsWordSample` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) + return nil + } + + if len(wordSample.UsageSet) > 0 { + _ = d.Set("usages", helper.StringsInterfaces(wordSample.UsageSet)) + } + + if wordSample.Keyword != nil { + _ = d.Set("keyword", wordSample.Keyword) + } + + if len(wordSample.TagSet) > 0 { + _ = d.Set("tags", helper.StringsInterfaces(wordSample.TagSet)) + } + + //wordsList := []interface{}{} + //for _, kw := range keywords{ + // for _, ws :=range wordSamples{ + // wordsMap := map[string]interface{}{} + // if ws.Keyword!=nil && kw == *ws.Keyword{ + // wordsMap["keyword"] = ws.Keyword + // if len(ws.TagSet)>0 { + // wordsMap["tags"] = ws.TagSet + // } + // wordsList = append(wordsList, wordsMap) + // break + // } + // } + //} + //_ = d.Set("words", wordsList) + + return nil +} + +func resourceTencentCloudMpsWordSampleUpdate(d *schema.ResourceData, meta interface{}) error { + defer logElapsed("resource.tencentcloud_mps_word_sample.update")() + defer inconsistentCheck(d, meta)() + + logId := getLogId(contextNil) + + request := mps.NewModifyWordSampleRequest() + + //keywords := strings.Split(d.Id(), FILED_SP) + //if len(keywords) == 0 { + // return fmt.Errorf("id is broken,%s", d.Id()) + //} + keyword := d.Id() + + request.Keyword = &keyword + + immutableArgs := []string{"keyword"} + + for _, v := range immutableArgs { + if d.HasChange(v) { + return fmt.Errorf("argument `%s` cannot be changed", v) + } + } + + if d.HasChange("usages") { + if v, ok := d.GetOk("usages"); ok { + usagesSet := v.(*schema.Set).List() + for i := range usagesSet { + if usagesSet[i] != nil { + usages := usagesSet[i].(string) + request.Usages = append(request.Usages, &usages) + } + } + } + } + + if d.HasChange("tags") { + if v, ok := d.GetOk("tags"); ok { + tagSet := v.(*schema.Set).List() + + request.TagOperationInfo = &mps.AiSampleTagOperation{ + Type: helper.String("reset"), + Tags: helper.InterfacesStringsPoint(tagSet), + } + } + } + + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { + result, e := meta.(*TencentCloudClient).apiV3Conn.UseMpsClient().ModifyWordSample(request) + if e != nil { + return retryError(e) + } else { + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) + } + return nil + }) + if err != nil { + log.Printf("[CRITAL]%s update mps wordSample failed, reason:%+v", logId, err) + return err + } + + return resourceTencentCloudMpsWordSampleRead(d, meta) +} + +func resourceTencentCloudMpsWordSampleDelete(d *schema.ResourceData, meta interface{}) error { + defer logElapsed("resource.tencentcloud_mps_word_sample.delete")() + defer inconsistentCheck(d, meta)() + + logId := getLogId(contextNil) + ctx := context.WithValue(context.TODO(), logIdKey, logId) + + service := MpsService{client: meta.(*TencentCloudClient).apiV3Conn} + keyword := d.Id() + + if err := service.DeleteMpsWordSamplesById(ctx, []string{keyword}); err != nil { + return err + } + + return nil +} diff --git a/tencentcloud/resource_tc_mps_word_sample_test.go b/tencentcloud/resource_tc_mps_word_sample_test.go new file mode 100644 index 0000000000..80df6ab9c2 --- /dev/null +++ b/tencentcloud/resource_tc_mps_word_sample_test.go @@ -0,0 +1,71 @@ +package tencentcloud + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccTencentCloudMpsWordSampleResource_basic(t *testing.T) { + t.Parallel() + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccMpsWordSample, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("tencentcloud_mps_word_sample.word_sample", "id"), + resource.TestCheckResourceAttr("tencentcloud_mps_word_sample.word_sample", "usages.#", "3"), + resource.TestCheckTypeSetElemAttr("tencentcloud_mps_word_sample.word_sample", "usages.*", "Recognition.Ocr"), + resource.TestCheckTypeSetElemAttr("tencentcloud_mps_word_sample.word_sample", "usages.*", "Review.Ocr"), + resource.TestCheckTypeSetElemAttr("tencentcloud_mps_word_sample.word_sample", "usages.*", "Review.Asr"), + resource.TestCheckResourceAttr("tencentcloud_mps_word_sample.word_sample", "keyword", "tf_test_kw_1"), + resource.TestCheckResourceAttr("tencentcloud_mps_word_sample.word_sample", "tags.#", "2"), + resource.TestCheckTypeSetElemAttr("tencentcloud_mps_word_sample.word_sample", "tags.*", "tags_1"), + resource.TestCheckTypeSetElemAttr("tencentcloud_mps_word_sample.word_sample", "tags.*", "tags_2"), + ), + }, + { + Config: testAccMpsWordSample_update, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("tencentcloud_mps_word_sample.word_sample", "id"), + resource.TestCheckResourceAttr("tencentcloud_mps_word_sample.word_sample", "usages.#", "2"), + resource.TestCheckTypeSetElemAttr("tencentcloud_mps_word_sample.word_sample", "usages.*", "Recognition.Asr"), + resource.TestCheckTypeSetElemAttr("tencentcloud_mps_word_sample.word_sample", "usages.*", "Review.Ocr"), + resource.TestCheckResourceAttr("tencentcloud_mps_word_sample.word_sample", "keyword", "tf_test_kw_1"), + resource.TestCheckResourceAttr("tencentcloud_mps_word_sample.word_sample", "tags.#", "2"), + resource.TestCheckTypeSetElemAttr("tencentcloud_mps_word_sample.word_sample", "tags.*", "tags_1"), + resource.TestCheckTypeSetElemAttr("tencentcloud_mps_word_sample.word_sample", "tags.*", "tags_3"), + ), + }, + { + ResourceName: "tencentcloud_mps_word_sample.word_sample", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +const testAccMpsWordSample = ` + +resource "tencentcloud_mps_word_sample" "word_sample" { + usages = ["Recognition.Ocr","Review.Ocr","Review.Asr"] + keyword = "tf_test_kw_1" + tags = ["tags_1", "tags_2"] +} + +` + +const testAccMpsWordSample_update = ` + +resource "tencentcloud_mps_word_sample" "word_sample" { + usages = ["Recognition.Asr","Review.Ocr"] + keyword = "tf_test_kw_1" + tags = ["tags_1", "tags_3"] +} + +` diff --git a/tencentcloud/service_tencentcloud_mps.go b/tencentcloud/service_tencentcloud_mps.go index 43e5f1cd7f..2d198c2d5e 100644 --- a/tencentcloud/service_tencentcloud_mps.go +++ b/tencentcloud/service_tencentcloud_mps.go @@ -644,3 +644,87 @@ func (me *MpsService) DeleteMpsPersonSampleById(ctx context.Context, personId st return } + +func (me *MpsService) DescribeMpsWordSamplesById(ctx context.Context, keywords []string) (wordSamples []*mps.AiSampleWord, errRet error) { + logId := getLogId(ctx) + + request := mps.NewDescribeWordSamplesRequest() + request.Keywords = helper.Strings(keywords) + + defer func() { + if errRet != nil { + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error()) + } + }() + + ratelimit.Check(request.GetAction()) + + response, err := me.client.UseMpsClient().DescribeWordSamples(request) + if err != nil { + errRet = err + return + } + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString()) + + if len(response.Response.WordSet) < 1 { + return + } + + wordSamples = response.Response.WordSet + return +} + +func (me *MpsService) DescribeMpsWordSampleById(ctx context.Context, keyword string) (wordSample *mps.AiSampleWord, errRet error) { + logId := getLogId(ctx) + + request := mps.NewDescribeWordSamplesRequest() + request.Keywords = []*string{ + helper.String(keyword), + } + + defer func() { + if errRet != nil { + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error()) + } + }() + + ratelimit.Check(request.GetAction()) + + response, err := me.client.UseMpsClient().DescribeWordSamples(request) + if err != nil { + errRet = err + return + } + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString()) + + if len(response.Response.WordSet) < 1 { + return + } + + wordSample = response.Response.WordSet[0] + return +} + +func (me *MpsService) DeleteMpsWordSamplesById(ctx context.Context, keywords []string) (errRet error) { + logId := getLogId(ctx) + + request := mps.NewDeleteWordSamplesRequest() + request.Keywords = helper.Strings(keywords) + + defer func() { + if errRet != nil { + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error()) + } + }() + + ratelimit.Check(request.GetAction()) + + response, err := me.client.UseMpsClient().DeleteWordSamples(request) + if err != nil { + errRet = err + return + } + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString()) + + return +} diff --git a/website/docs/r/mps_word_sample.html.markdown b/website/docs/r/mps_word_sample.html.markdown new file mode 100644 index 0000000000..6849f456ec --- /dev/null +++ b/website/docs/r/mps_word_sample.html.markdown @@ -0,0 +1,47 @@ +--- +subcategory: "Media Processing Service(MPS)" +layout: "tencentcloud" +page_title: "TencentCloud: tencentcloud_mps_word_sample" +sidebar_current: "docs-tencentcloud-resource-mps_word_sample" +description: |- + Provides a resource to create a mps word_sample +--- + +# tencentcloud_mps_word_sample + +Provides a resource to create a mps word_sample + +## Example Usage + +```hcl +resource "tencentcloud_mps_word_sample" "word_sample" { + usages = ["Recognition.Ocr", "Review.Ocr", "Review.Asr"] + keyword = "tf_test_kw_1" + tags = ["tags_1", "tags_2"] +} +``` + +## Argument Reference + +The following arguments are supported: + +* `keyword` - (Required, String) Keyword. Length limit: 20 characters. +* `usages` - (Required, Set: [`String`]) Keyword usage. Valid values: 1.`Recognition.Ocr`: OCR-based content recognition. 2.`Recognition.Asr`: ASR-based content recognition. 3.`Review.Ocr`: OCR-based inappropriate information recognition. 4.`Review.Asr`: ASR-based inappropriate information recognition. +* `tags` - (Optional, Set: [`String`]) Keyword tag. Array length limit: 20 tags. Each tag length limit: 128 characters. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - ID of the resource. + + + +## Import + +mps word_sample can be imported using the id, e.g. + +``` +terraform import tencentcloud_mps_word_sample.word_sample keyword +``` + diff --git a/website/tencentcloud.erb b/website/tencentcloud.erb index 32917f1b42..4234e93952 100644 --- a/website/tencentcloud.erb +++ b/website/tencentcloud.erb @@ -2098,6 +2098,9 @@
  • tencentcloud_mps_withdraws_watermark_operation
  • +
  • + tencentcloud_mps_word_sample +
  • tencentcloud_mps_workflow