|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of sts callerIdentity |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_sts_caller_identity" "callerIdentity" { |
| 8 | +} |
| 9 | +``` |
| 10 | +*/ |
| 11 | +package tencentcloud |
| 12 | + |
| 13 | +import ( |
| 14 | + "context" |
| 15 | + "log" |
| 16 | + |
| 17 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 18 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 19 | + sts "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sts/v20180813" |
| 20 | +) |
| 21 | + |
| 22 | +func dataSourceTencentCloudStsCallerIdentity() *schema.Resource { |
| 23 | + return &schema.Resource{ |
| 24 | + Read: dataSourceTencentCloudStsCallerIdentityRead, |
| 25 | + Schema: map[string]*schema.Schema{ |
| 26 | + "arn": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Computed: true, |
| 29 | + Description: "Current caller ARN.", |
| 30 | + }, |
| 31 | + |
| 32 | + "account_id": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Computed: true, |
| 35 | + Description: "The primary account Uin to which the current caller belongs.", |
| 36 | + }, |
| 37 | + |
| 38 | + "user_id": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Computed: true, |
| 41 | + Description: "Identity:- When the caller is a cloud account, the current account `Uin` is returned.- When the caller is a role, it returns `roleId:roleSessionName`- When the caller is a federated identity, it returns `uin:federatedUserName`.", |
| 42 | + }, |
| 43 | + |
| 44 | + "principal_id": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Computed: true, |
| 47 | + Description: "Account Uin to which the key belongs:- The caller is a cloud account, and the returned current account Uin- The caller is a role, and the returned account Uin that applies for the role key.", |
| 48 | + }, |
| 49 | + |
| 50 | + "type": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Computed: true, |
| 53 | + Description: "Identity type.", |
| 54 | + }, |
| 55 | + |
| 56 | + "result_output_file": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Optional: true, |
| 59 | + Description: "Used to save results.", |
| 60 | + }, |
| 61 | + }, |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func dataSourceTencentCloudStsCallerIdentityRead(d *schema.ResourceData, meta interface{}) error { |
| 66 | + defer logElapsed("data_source.tencentcloud_sts_caller_identity.read")() |
| 67 | + defer inconsistentCheck(d, meta)() |
| 68 | + |
| 69 | + logId := getLogId(contextNil) |
| 70 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 71 | + |
| 72 | + stsService := StsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 73 | + |
| 74 | + var callerIdentity *sts.GetCallerIdentityResponseParams |
| 75 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 76 | + results, e := stsService.DescribeStsCallerIdentityByFilter(ctx) |
| 77 | + if e != nil { |
| 78 | + return retryError(e) |
| 79 | + } |
| 80 | + callerIdentity = results |
| 81 | + return nil |
| 82 | + }) |
| 83 | + if err != nil { |
| 84 | + log.Printf("[CRITAL]%s read Sts instances failed, reason:%+v", logId, err) |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + if callerIdentity.Arn != nil { |
| 89 | + _ = d.Set("arn", callerIdentity.Arn) |
| 90 | + } |
| 91 | + |
| 92 | + if callerIdentity.AccountId != nil { |
| 93 | + _ = d.Set("account_id", callerIdentity.AccountId) |
| 94 | + } |
| 95 | + |
| 96 | + if callerIdentity.UserId != nil { |
| 97 | + _ = d.Set("user_id", callerIdentity.UserId) |
| 98 | + } |
| 99 | + |
| 100 | + if callerIdentity.PrincipalId != nil { |
| 101 | + _ = d.Set("principal_id", callerIdentity.PrincipalId) |
| 102 | + } |
| 103 | + |
| 104 | + if callerIdentity.Type != nil { |
| 105 | + _ = d.Set("type", callerIdentity.Type) |
| 106 | + } |
| 107 | + |
| 108 | + d.SetId(*callerIdentity.Arn) |
| 109 | + |
| 110 | + output, ok := d.GetOk("result_output_file") |
| 111 | + if ok && output.(string) != "" { |
| 112 | + if e := writeToFile(output.(string), map[string]interface{}{ |
| 113 | + "arn": callerIdentity.Arn, |
| 114 | + "account_id": callerIdentity.AccountId, |
| 115 | + "user_id": callerIdentity.UserId, |
| 116 | + "principal_id": callerIdentity.PrincipalId, |
| 117 | + "type": callerIdentity.Type, |
| 118 | + }); e != nil { |
| 119 | + return e |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return nil |
| 124 | +} |
0 commit comments