|
| 1 | +/* |
| 2 | +Provides a resource to create a mps word_sample |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_mps_word_sample" "word_sample" { |
| 8 | + usages = ["Recognition.Ocr","Review.Ocr","Review.Asr"] |
| 9 | + keyword = "tf_test_kw_1" |
| 10 | + tags = ["tags_1", "tags_2"] |
| 11 | +} |
| 12 | +``` |
| 13 | +
|
| 14 | +Import |
| 15 | +
|
| 16 | +mps word_sample can be imported using the id, e.g. |
| 17 | +
|
| 18 | +``` |
| 19 | +terraform import tencentcloud_mps_word_sample.word_sample keyword |
| 20 | +``` |
| 21 | +*/ |
| 22 | +package tencentcloud |
| 23 | + |
| 24 | +import ( |
| 25 | + "context" |
| 26 | + "fmt" |
| 27 | + "log" |
| 28 | + |
| 29 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 30 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 31 | + mps "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mps/v20190612" |
| 32 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 33 | +) |
| 34 | + |
| 35 | +func resourceTencentCloudMpsWordSample() *schema.Resource { |
| 36 | + return &schema.Resource{ |
| 37 | + Create: resourceTencentCloudMpsWordSampleCreate, |
| 38 | + Read: resourceTencentCloudMpsWordSampleRead, |
| 39 | + Update: resourceTencentCloudMpsWordSampleUpdate, |
| 40 | + Delete: resourceTencentCloudMpsWordSampleDelete, |
| 41 | + Importer: &schema.ResourceImporter{ |
| 42 | + State: schema.ImportStatePassthrough, |
| 43 | + }, |
| 44 | + Schema: map[string]*schema.Schema{ |
| 45 | + "usages": { |
| 46 | + Required: true, |
| 47 | + Type: schema.TypeSet, |
| 48 | + Elem: &schema.Schema{ |
| 49 | + Type: schema.TypeString, |
| 50 | + }, |
| 51 | + 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.", |
| 52 | + }, |
| 53 | + |
| 54 | + "keyword": { |
| 55 | + Type: schema.TypeString, |
| 56 | + Required: true, |
| 57 | + Description: "Keyword. Length limit: 20 characters.", |
| 58 | + }, |
| 59 | + "tags": { |
| 60 | + Type: schema.TypeSet, |
| 61 | + Elem: &schema.Schema{ |
| 62 | + Type: schema.TypeString, |
| 63 | + }, |
| 64 | + Optional: true, |
| 65 | + Description: "Keyword tag. Array length limit: 20 tags. Each tag length limit: 128 characters.", |
| 66 | + }, |
| 67 | + }, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func resourceTencentCloudMpsWordSampleCreate(d *schema.ResourceData, meta interface{}) error { |
| 72 | + defer logElapsed("resource.tencentcloud_mps_word_sample.create")() |
| 73 | + defer inconsistentCheck(d, meta)() |
| 74 | + |
| 75 | + logId := getLogId(contextNil) |
| 76 | + |
| 77 | + var ( |
| 78 | + request = mps.NewCreateWordSamplesRequest() |
| 79 | + // response = mps.NewCreateWordSamplesResponse() |
| 80 | + keyword string |
| 81 | + ) |
| 82 | + if v, ok := d.GetOk("usages"); ok { |
| 83 | + usagesSet := v.(*schema.Set).List() |
| 84 | + for i := range usagesSet { |
| 85 | + if usagesSet[i] != nil { |
| 86 | + usages := usagesSet[i].(string) |
| 87 | + request.Usages = append(request.Usages, &usages) |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + aiSampleWordInfo := mps.AiSampleWordInfo{} |
| 93 | + if v, ok := d.GetOk("keyword"); ok { |
| 94 | + aiSampleWordInfo.Keyword = helper.String(v.(string)) |
| 95 | + keyword = v.(string) |
| 96 | + } |
| 97 | + |
| 98 | + if v, ok := d.GetOk("tags"); ok { |
| 99 | + tagsSet := v.(*schema.Set).List() |
| 100 | + for i := range tagsSet { |
| 101 | + if tagsSet[i] != nil { |
| 102 | + tags := tagsSet[i].(string) |
| 103 | + aiSampleWordInfo.Tags = append(aiSampleWordInfo.Tags, &tags) |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + request.Words = append(request.Words, &aiSampleWordInfo) |
| 108 | + |
| 109 | + //if v, ok := d.GetOk("words"); ok { |
| 110 | + // for _, item := range v.([]interface{}) { |
| 111 | + // dMap := item.(map[string]interface{}) |
| 112 | + // aiSampleWordInfo := mps.AiSampleWordInfo{} |
| 113 | + // if v, ok := dMap["keyword"]; ok { |
| 114 | + // aiSampleWordInfo.Keyword = helper.String(v.(string)) |
| 115 | + // keywords = append(keywords, v.(string)) |
| 116 | + // } |
| 117 | + // if v, ok := dMap["tags"]; ok { |
| 118 | + // tagsSet := v.(*schema.Set).List() |
| 119 | + // for i := range tagsSet { |
| 120 | + // if tagsSet[i] != nil { |
| 121 | + // tags := tagsSet[i].(string) |
| 122 | + // aiSampleWordInfo.Tags = append(aiSampleWordInfo.Tags, &tags) |
| 123 | + // } |
| 124 | + // } |
| 125 | + // } |
| 126 | + // request.Words = append(request.Words, &aiSampleWordInfo) |
| 127 | + // } |
| 128 | + //} |
| 129 | + |
| 130 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 131 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseMpsClient().CreateWordSamples(request) |
| 132 | + if e != nil { |
| 133 | + return retryError(e) |
| 134 | + } else { |
| 135 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 136 | + } |
| 137 | + return nil |
| 138 | + }) |
| 139 | + if err != nil { |
| 140 | + log.Printf("[CRITAL]%s create mps wordSample failed, reason:%+v", logId, err) |
| 141 | + return err |
| 142 | + } |
| 143 | + |
| 144 | + d.SetId(keyword) |
| 145 | + |
| 146 | + return resourceTencentCloudMpsWordSampleRead(d, meta) |
| 147 | +} |
| 148 | + |
| 149 | +func resourceTencentCloudMpsWordSampleRead(d *schema.ResourceData, meta interface{}) error { |
| 150 | + defer logElapsed("resource.tencentcloud_mps_word_sample.read")() |
| 151 | + defer inconsistentCheck(d, meta)() |
| 152 | + |
| 153 | + logId := getLogId(contextNil) |
| 154 | + |
| 155 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 156 | + |
| 157 | + service := MpsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 158 | + |
| 159 | + //keywords := strings.Split(d.Id(), FILED_SP) |
| 160 | + //if len(keywords) == 0 { |
| 161 | + // return fmt.Errorf("id is broken,%s", d.Id()) |
| 162 | + //} |
| 163 | + keyword := d.Id() |
| 164 | + |
| 165 | + wordSample, err := service.DescribeMpsWordSampleById(ctx, keyword) |
| 166 | + if err != nil { |
| 167 | + return err |
| 168 | + } |
| 169 | + |
| 170 | + if wordSample == nil { |
| 171 | + d.SetId("") |
| 172 | + log.Printf("[WARN]%s resource `MpsWordSample` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 173 | + return nil |
| 174 | + } |
| 175 | + |
| 176 | + if len(wordSample.UsageSet) > 0 { |
| 177 | + _ = d.Set("usages", helper.StringsInterfaces(wordSample.UsageSet)) |
| 178 | + } |
| 179 | + |
| 180 | + if wordSample.Keyword != nil { |
| 181 | + _ = d.Set("keyword", wordSample.Keyword) |
| 182 | + } |
| 183 | + |
| 184 | + if len(wordSample.TagSet) > 0 { |
| 185 | + _ = d.Set("tags", helper.StringsInterfaces(wordSample.TagSet)) |
| 186 | + } |
| 187 | + |
| 188 | + //wordsList := []interface{}{} |
| 189 | + //for _, kw := range keywords{ |
| 190 | + // for _, ws :=range wordSamples{ |
| 191 | + // wordsMap := map[string]interface{}{} |
| 192 | + // if ws.Keyword!=nil && kw == *ws.Keyword{ |
| 193 | + // wordsMap["keyword"] = ws.Keyword |
| 194 | + // if len(ws.TagSet)>0 { |
| 195 | + // wordsMap["tags"] = ws.TagSet |
| 196 | + // } |
| 197 | + // wordsList = append(wordsList, wordsMap) |
| 198 | + // break |
| 199 | + // } |
| 200 | + // } |
| 201 | + //} |
| 202 | + //_ = d.Set("words", wordsList) |
| 203 | + |
| 204 | + return nil |
| 205 | +} |
| 206 | + |
| 207 | +func resourceTencentCloudMpsWordSampleUpdate(d *schema.ResourceData, meta interface{}) error { |
| 208 | + defer logElapsed("resource.tencentcloud_mps_word_sample.update")() |
| 209 | + defer inconsistentCheck(d, meta)() |
| 210 | + |
| 211 | + logId := getLogId(contextNil) |
| 212 | + |
| 213 | + request := mps.NewModifyWordSampleRequest() |
| 214 | + |
| 215 | + //keywords := strings.Split(d.Id(), FILED_SP) |
| 216 | + //if len(keywords) == 0 { |
| 217 | + // return fmt.Errorf("id is broken,%s", d.Id()) |
| 218 | + //} |
| 219 | + keyword := d.Id() |
| 220 | + |
| 221 | + request.Keyword = &keyword |
| 222 | + |
| 223 | + immutableArgs := []string{"keyword"} |
| 224 | + |
| 225 | + for _, v := range immutableArgs { |
| 226 | + if d.HasChange(v) { |
| 227 | + return fmt.Errorf("argument `%s` cannot be changed", v) |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + if d.HasChange("usages") { |
| 232 | + if v, ok := d.GetOk("usages"); ok { |
| 233 | + usagesSet := v.(*schema.Set).List() |
| 234 | + for i := range usagesSet { |
| 235 | + if usagesSet[i] != nil { |
| 236 | + usages := usagesSet[i].(string) |
| 237 | + request.Usages = append(request.Usages, &usages) |
| 238 | + } |
| 239 | + } |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + if d.HasChange("tags") { |
| 244 | + if v, ok := d.GetOk("tags"); ok { |
| 245 | + tagSet := v.(*schema.Set).List() |
| 246 | + |
| 247 | + request.TagOperationInfo = &mps.AiSampleTagOperation{ |
| 248 | + Type: helper.String("reset"), |
| 249 | + Tags: helper.InterfacesStringsPoint(tagSet), |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 255 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseMpsClient().ModifyWordSample(request) |
| 256 | + if e != nil { |
| 257 | + return retryError(e) |
| 258 | + } else { |
| 259 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 260 | + } |
| 261 | + return nil |
| 262 | + }) |
| 263 | + if err != nil { |
| 264 | + log.Printf("[CRITAL]%s update mps wordSample failed, reason:%+v", logId, err) |
| 265 | + return err |
| 266 | + } |
| 267 | + |
| 268 | + return resourceTencentCloudMpsWordSampleRead(d, meta) |
| 269 | +} |
| 270 | + |
| 271 | +func resourceTencentCloudMpsWordSampleDelete(d *schema.ResourceData, meta interface{}) error { |
| 272 | + defer logElapsed("resource.tencentcloud_mps_word_sample.delete")() |
| 273 | + defer inconsistentCheck(d, meta)() |
| 274 | + |
| 275 | + logId := getLogId(contextNil) |
| 276 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 277 | + |
| 278 | + service := MpsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 279 | + keyword := d.Id() |
| 280 | + |
| 281 | + if err := service.DeleteMpsWordSamplesById(ctx, []string{keyword}); err != nil { |
| 282 | + return err |
| 283 | + } |
| 284 | + |
| 285 | + return nil |
| 286 | +} |
0 commit comments