|
| 1 | +/* |
| 2 | +Use this data source to query user appid, uin and ownerUin. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_user_info" "foo" {} |
| 8 | +``` |
| 9 | +
|
| 10 | +*/ |
| 11 | +package tencentcloud |
| 12 | + |
| 13 | +import ( |
| 14 | + "context" |
| 15 | + "fmt" |
| 16 | + "log" |
| 17 | + "math/rand" |
| 18 | + "strconv" |
| 19 | + |
| 20 | + cam "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam/v20190116" |
| 21 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/ratelimit" |
| 22 | + |
| 23 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 24 | +) |
| 25 | + |
| 26 | +func datasourceTencentCloudUserInfo() *schema.Resource { |
| 27 | + return &schema.Resource{ |
| 28 | + Read: datasourceTencentCloudUserInfoRead, |
| 29 | + |
| 30 | + Importer: &schema.ResourceImporter{ |
| 31 | + State: schema.ImportStatePassthrough, |
| 32 | + }, |
| 33 | + Schema: map[string]*schema.Schema{ |
| 34 | + "app_id": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Computed: true, |
| 37 | + Description: "Current account App ID.", |
| 38 | + }, |
| 39 | + |
| 40 | + "uin": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Computed: true, |
| 43 | + Description: "Current account UIN.", |
| 44 | + }, |
| 45 | + |
| 46 | + "owner_uin": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Computed: true, |
| 49 | + Description: "Current account OwnerUIN.", |
| 50 | + }, |
| 51 | + "result_output_file": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Optional: true, |
| 54 | + Description: "Used for save results.", |
| 55 | + }, |
| 56 | + }, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func datasourceTencentCloudUserInfoRead(d *schema.ResourceData, meta interface{}) error { |
| 61 | + defer logElapsed("datasource.tencentcloud_user_info.read")() |
| 62 | + defer inconsistentCheck(d, meta)() |
| 63 | + |
| 64 | + logId := getLogId(contextNil) |
| 65 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 66 | + |
| 67 | + client := meta.(*TencentCloudClient).apiV3Conn |
| 68 | + |
| 69 | + logId = getLogId(ctx) |
| 70 | + request := cam.NewGetUserAppIdRequest() |
| 71 | + |
| 72 | + ratelimit.Check(request.GetAction()) |
| 73 | + response, err := client.UseCamClient().GetUserAppId(request) |
| 74 | + |
| 75 | + if err != nil { |
| 76 | + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", |
| 77 | + logId, request.GetAction(), request.ToJsonString(), err.Error()) |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", |
| 82 | + logId, request.GetAction(), request.ToJsonString(), response.ToJsonString()) |
| 83 | + |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + result := response.Response |
| 89 | + |
| 90 | + if result == nil { |
| 91 | + return fmt.Errorf("get user appid error: empty response") |
| 92 | + } |
| 93 | + |
| 94 | + appId := strconv.FormatUint(*result.AppId, 10) |
| 95 | + uin := *result.Uin |
| 96 | + ownerUin := *result.OwnerUin |
| 97 | + |
| 98 | + d.SetId(fmt.Sprintf("user-%s-%s-%d", uin, appId, rand.Intn(10000))) |
| 99 | + |
| 100 | + _ = d.Set("app_id", appId) |
| 101 | + _ = d.Set("uin", uin) |
| 102 | + _ = d.Set("owner_uin", ownerUin) |
| 103 | + |
| 104 | + output, ok := d.GetOk("result_output_file") |
| 105 | + if ok && output.(string) != "" { |
| 106 | + if e := writeToFile(output.(string), map[string]interface{}{ |
| 107 | + "app_id": appId, |
| 108 | + "uin": uin, |
| 109 | + "ownerUin": ownerUin, |
| 110 | + }); e != nil { |
| 111 | + return e |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return nil |
| 116 | +} |
0 commit comments