|
| 1 | +/* |
| 2 | +Provides a resource to create a mps person_sample |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_mps_person_sample" "person_sample" { |
| 8 | + name = "test" |
| 9 | + usages = [ |
| 10 | + "Review.Face" |
| 11 | + ] |
| 12 | + description = "test" |
| 13 | + face_contents = [ |
| 14 | + filebase64("./person.png") |
| 15 | + ] |
| 16 | +} |
| 17 | +``` |
| 18 | +
|
| 19 | +Import |
| 20 | +
|
| 21 | +mps person_sample can be imported using the id, e.g. |
| 22 | +
|
| 23 | +``` |
| 24 | +terraform import tencentcloud_mps_person_sample.person_sample person_sample_id |
| 25 | +``` |
| 26 | +*/ |
| 27 | +package tencentcloud |
| 28 | + |
| 29 | +import ( |
| 30 | + "context" |
| 31 | + "encoding/base64" |
| 32 | + "io/ioutil" |
| 33 | + "log" |
| 34 | + "net/http" |
| 35 | + |
| 36 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 37 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 38 | + mps "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mps/v20190612" |
| 39 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 40 | +) |
| 41 | + |
| 42 | +func resourceTencentCloudMpsPersonSample() *schema.Resource { |
| 43 | + return &schema.Resource{ |
| 44 | + Create: resourceTencentCloudMpsPersonSampleCreate, |
| 45 | + Read: resourceTencentCloudMpsPersonSampleRead, |
| 46 | + Update: resourceTencentCloudMpsPersonSampleUpdate, |
| 47 | + Delete: resourceTencentCloudMpsPersonSampleDelete, |
| 48 | + Importer: &schema.ResourceImporter{ |
| 49 | + State: schema.ImportStatePassthrough, |
| 50 | + }, |
| 51 | + Schema: map[string]*schema.Schema{ |
| 52 | + "name": { |
| 53 | + Required: true, |
| 54 | + Type: schema.TypeString, |
| 55 | + Description: "Material name, length limit: 20 characters.", |
| 56 | + }, |
| 57 | + |
| 58 | + "usages": { |
| 59 | + Required: true, |
| 60 | + Type: schema.TypeSet, |
| 61 | + Elem: &schema.Schema{ |
| 62 | + Type: schema.TypeString, |
| 63 | + }, |
| 64 | + Description: "Material application scene, optional value:1. Recognition.Face: used for content recognition 2. Review.Face: used for inappropriate content identification 3. All: contains all of the above, equivalent to 1+2.", |
| 65 | + }, |
| 66 | + |
| 67 | + "description": { |
| 68 | + Optional: true, |
| 69 | + Type: schema.TypeString, |
| 70 | + Description: "Material description, length limit: 1024 characters.", |
| 71 | + }, |
| 72 | + |
| 73 | + "face_contents": { |
| 74 | + Optional: true, |
| 75 | + Type: schema.TypeSet, |
| 76 | + Elem: &schema.Schema{ |
| 77 | + Type: schema.TypeString, |
| 78 | + }, |
| 79 | + Description: "Material image [Base64](https://tools.ietf.org/html/rfc4648) encoded string only supports jpeg and png image formats. Array length limit: 5 images.Note: The picture must be a single portrait with clearer facial features, with a pixel size of not less than 200*200.", |
| 80 | + }, |
| 81 | + }, |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func resourceTencentCloudMpsPersonSampleCreate(d *schema.ResourceData, meta interface{}) error { |
| 86 | + defer logElapsed("resource.tencentcloud_mps_person_sample.create")() |
| 87 | + defer inconsistentCheck(d, meta)() |
| 88 | + |
| 89 | + logId := getLogId(contextNil) |
| 90 | + |
| 91 | + var ( |
| 92 | + request = mps.NewCreatePersonSampleRequest() |
| 93 | + response = mps.NewCreatePersonSampleResponse() |
| 94 | + personId string |
| 95 | + ) |
| 96 | + if v, ok := d.GetOk("name"); ok { |
| 97 | + request.Name = helper.String(v.(string)) |
| 98 | + } |
| 99 | + |
| 100 | + if v, ok := d.GetOk("usages"); ok { |
| 101 | + usagesSet := v.(*schema.Set).List() |
| 102 | + for i := range usagesSet { |
| 103 | + usages := usagesSet[i].(string) |
| 104 | + request.Usages = append(request.Usages, &usages) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if v, ok := d.GetOk("description"); ok { |
| 109 | + request.Description = helper.String(v.(string)) |
| 110 | + } |
| 111 | + |
| 112 | + if v, ok := d.GetOk("face_contents"); ok { |
| 113 | + faceContentsSet := v.(*schema.Set).List() |
| 114 | + for i := range faceContentsSet { |
| 115 | + faceContents := faceContentsSet[i].(string) |
| 116 | + request.FaceContents = append(request.FaceContents, &faceContents) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 121 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseMpsClient().CreatePersonSample(request) |
| 122 | + if e != nil { |
| 123 | + return retryError(e) |
| 124 | + } else { |
| 125 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 126 | + } |
| 127 | + response = result |
| 128 | + return nil |
| 129 | + }) |
| 130 | + if err != nil { |
| 131 | + log.Printf("[CRITAL]%s create mps personSample failed, reason:%+v", logId, err) |
| 132 | + return err |
| 133 | + } |
| 134 | + |
| 135 | + personId = *response.Response.Person.PersonId |
| 136 | + d.SetId(personId) |
| 137 | + |
| 138 | + return resourceTencentCloudMpsPersonSampleRead(d, meta) |
| 139 | +} |
| 140 | + |
| 141 | +func resourceTencentCloudMpsPersonSampleRead(d *schema.ResourceData, meta interface{}) error { |
| 142 | + defer logElapsed("resource.tencentcloud_mps_person_sample.read")() |
| 143 | + defer inconsistentCheck(d, meta)() |
| 144 | + |
| 145 | + logId := getLogId(contextNil) |
| 146 | + |
| 147 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 148 | + |
| 149 | + service := MpsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 150 | + |
| 151 | + personId := d.Id() |
| 152 | + |
| 153 | + personSample, err := service.DescribeMpsPersonSampleById(ctx, personId) |
| 154 | + if err != nil { |
| 155 | + return err |
| 156 | + } |
| 157 | + |
| 158 | + if personSample == nil { |
| 159 | + d.SetId("") |
| 160 | + log.Printf("[WARN]%s resource `MpsPersonSample` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 161 | + return nil |
| 162 | + } |
| 163 | + |
| 164 | + if personSample.Name != nil { |
| 165 | + _ = d.Set("name", personSample.Name) |
| 166 | + } |
| 167 | + |
| 168 | + if personSample.UsageSet != nil { |
| 169 | + _ = d.Set("usages", personSample.UsageSet) |
| 170 | + } |
| 171 | + |
| 172 | + if personSample.Description != nil { |
| 173 | + _ = d.Set("description", personSample.Description) |
| 174 | + } |
| 175 | + |
| 176 | + if personSample.FaceInfoSet != nil { |
| 177 | + faceContents := []*string{} |
| 178 | + for _, faceInfo := range personSample.FaceInfoSet { |
| 179 | + url := faceInfo.Url |
| 180 | + res, err := http.Get(*url) |
| 181 | + if err != nil { |
| 182 | + return err |
| 183 | + } |
| 184 | + content, err := ioutil.ReadAll(res.Body) |
| 185 | + if err != nil { |
| 186 | + return err |
| 187 | + } |
| 188 | + base64Encode := base64.StdEncoding.EncodeToString(content) |
| 189 | + faceContents = append(faceContents, &base64Encode) |
| 190 | + } |
| 191 | + _ = d.Set("face_contents", faceContents) |
| 192 | + } |
| 193 | + |
| 194 | + return nil |
| 195 | +} |
| 196 | + |
| 197 | +func resourceTencentCloudMpsPersonSampleUpdate(d *schema.ResourceData, meta interface{}) error { |
| 198 | + defer logElapsed("resource.tencentcloud_mps_person_sample.update")() |
| 199 | + defer inconsistentCheck(d, meta)() |
| 200 | + |
| 201 | + logId := getLogId(contextNil) |
| 202 | + |
| 203 | + request := mps.NewModifyPersonSampleRequest() |
| 204 | + |
| 205 | + personId := d.Id() |
| 206 | + |
| 207 | + needChange := false |
| 208 | + request.PersonId = &personId |
| 209 | + |
| 210 | + mutableArgs := []string{"name", "usages", "description", "face_contents"} |
| 211 | + |
| 212 | + for _, v := range mutableArgs { |
| 213 | + if d.HasChange(v) { |
| 214 | + needChange = true |
| 215 | + break |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + if needChange { |
| 220 | + |
| 221 | + if v, ok := d.GetOk("name"); ok { |
| 222 | + request.Name = helper.String(v.(string)) |
| 223 | + } |
| 224 | + |
| 225 | + if v, ok := d.GetOk("usages"); ok { |
| 226 | + usagesSet := v.(*schema.Set).List() |
| 227 | + for i := range usagesSet { |
| 228 | + usages := usagesSet[i].(string) |
| 229 | + request.Usages = append(request.Usages, &usages) |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + if v, ok := d.GetOk("description"); ok { |
| 234 | + request.Description = helper.String(v.(string)) |
| 235 | + } |
| 236 | + |
| 237 | + if v, ok := d.GetOk("face_contents"); ok { |
| 238 | + faceContentsSet := v.(*schema.Set).List() |
| 239 | + operationInfo := mps.AiSampleFaceOperation{} |
| 240 | + for i := range faceContentsSet { |
| 241 | + faceContents := faceContentsSet[i].(string) |
| 242 | + operationInfo.FaceContents = append(operationInfo.FaceContents, &faceContents) |
| 243 | + } |
| 244 | + operationInfo.Type = helper.String("reset") |
| 245 | + request.FaceOperationInfo = &operationInfo |
| 246 | + } |
| 247 | + |
| 248 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 249 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseMpsClient().ModifyPersonSample(request) |
| 250 | + if e != nil { |
| 251 | + return retryError(e) |
| 252 | + } else { |
| 253 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 254 | + } |
| 255 | + return nil |
| 256 | + }) |
| 257 | + if err != nil { |
| 258 | + log.Printf("[CRITAL]%s update mps personSample failed, reason:%+v", logId, err) |
| 259 | + return err |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + return resourceTencentCloudMpsPersonSampleRead(d, meta) |
| 264 | +} |
| 265 | + |
| 266 | +func resourceTencentCloudMpsPersonSampleDelete(d *schema.ResourceData, meta interface{}) error { |
| 267 | + defer logElapsed("resource.tencentcloud_mps_person_sample.delete")() |
| 268 | + defer inconsistentCheck(d, meta)() |
| 269 | + |
| 270 | + logId := getLogId(contextNil) |
| 271 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 272 | + |
| 273 | + service := MpsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 274 | + personId := d.Id() |
| 275 | + |
| 276 | + if err := service.DeleteMpsPersonSampleById(ctx, personId); err != nil { |
| 277 | + return err |
| 278 | + } |
| 279 | + |
| 280 | + return nil |
| 281 | +} |
0 commit comments