Skip to content

Commit c618174

Browse files
authored
feat: sts support (#1340)
* feat: sts support * feat: add changelog Co-authored-by: arunma <arunma@tencent.com>
1 parent 02077c9 commit c618174

File tree

14 files changed

+842
-63
lines changed

14 files changed

+842
-63
lines changed

.changelog/1340.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
tencentcloud_sts_caller_identity
3+
```

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ require (
3434
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.445
3535
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.199
3636
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.412
37-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.519
37+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.524
3838
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.445
3939
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.488
4040
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dayu v1.0.335
@@ -55,7 +55,7 @@ require (
5555
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sqlserver v1.0.406
5656
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ssl v1.0.199
5757
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ssm v1.0.199
58-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sts v1.0.199
58+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sts v1.0.524
5959
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tag v1.0.199
6060
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcaplusdb v1.0.199
6161
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcm v1.0.519

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.518 h1:RI2c
508508
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.518/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
509509
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.519 h1:EuhhXQDmc2zTZmFMmZPWp6PaPIM/gnRcqqbRyFA6UGw=
510510
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.519/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
511+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.524 h1:wyEALuNNpyGVx/sEjgLsHKT+mzQPujrzZ+59IpSDsGo=
512+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.524/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
511513
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.445 h1:Bh7XD0ypNMHYyBOM8hhKsSu+y0VVKUnJVS+YKKhfpGg=
512514
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.445/go.mod h1:jMDD351efCFpT1+KVFbcpu6SbmP4TYmp4qkoCfr63nQ=
513515
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.488 h1:A1seXWtMf2atBjSNYvcwxyDoFzCMgqyVnsxnWzhqJEA=
@@ -559,6 +561,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ssm v1.0.199 h1:iXRHMlR
559561
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ssm v1.0.199/go.mod h1:a5yWxx1yem1PXNFV+JRdJnqdVxZeQ8mMrCTs3ZjwOto=
560562
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sts v1.0.199 h1:z6cmMQPDnAjokEloeui3/HJ02tFKVrtPLz1pG3Bimkk=
561563
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sts v1.0.199/go.mod h1:3LRL4bjS4JieTruoWSqnMA/rPOxd2TXsstNBKtN+2qQ=
564+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sts v1.0.524 h1:Hda8mBEoQVwkRAaZqztUAWn/n4HCYHZYEwzFBwYKaaM=
565+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sts v1.0.524/go.mod h1:DkzQ5MJmuv5iHgQotrsSzfvP4D1zwiWLRR4gqLK/i/w=
562566
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tag v1.0.199 h1:/s979h2G0mvPlKk+//hWPkGl2V4tiohJKyPBYhReen0=
563567
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tag v1.0.199/go.mod h1:sX14+NSvMjOhNFaMtP2aDy6Bss8PyFXij21gpY6+DAs=
564568
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcaplusdb v1.0.199 h1:i17zUWDw6iN7EMkQMGDXIXpur73vwUvbZrX4M5S0xhQ=
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
// go test -i; go test -test.run TestAccTencentCloudStsCallerIdentityDataSource -v
10+
func TestAccTencentCloudStsCallerIdentityDataSource(t *testing.T) {
11+
t.Parallel()
12+
13+
resource.Test(t, resource.TestCase{
14+
PreCheck: func() { testAccPreCheck(t) },
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccDataSourceStsCallerIdentity,
19+
Check: resource.ComposeTestCheckFunc(
20+
testAccCheckTencentCloudDataSourceID("data.tencentcloud_sts_caller_identity.caller_identity"),
21+
),
22+
},
23+
},
24+
})
25+
}
26+
27+
const testAccDataSourceStsCallerIdentity = `
28+
29+
data "tencentcloud_sts_caller_identity" "caller_identity" {
30+
}
31+
32+
`

tencentcloud/provider.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,10 @@ TencentCloud ServiceMesh(TCM)
699699
tencentcloud_tcm_mesh
700700
tencentcloud_tcm_cluster_attachment
701701
702+
Security Token Service(STS)
703+
Data Source
704+
tencentcloud_sts_caller_identity
705+
702706
*/
703707
package tencentcloud
704708

@@ -999,6 +1003,7 @@ func Provider() terraform.ResourceProvider {
9991003
"tencentcloud_teo_security_policy_regions": dataSourceTencentCloudTeoSecurityPolicyRegions(),
10001004
"tencentcloud_teo_waf_rule_groups": dataSourceTencentCloudTeoWafRuleGroups(),
10011005
"tencentcloud_teo_zone_ddos_policy": dataSourceTencentCloudTeoZoneDDoSPolicy(),
1006+
"tencentcloud_sts_caller_identity": dataSourceTencentCloudStsCallerIdentity(),
10021007
},
10031008

10041009
ResourcesMap: map[string]*schema.Resource{
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package tencentcloud
2+
3+
import (
4+
"context"
5+
"log"
6+
7+
sts "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sts/v20180813"
8+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/connectivity"
9+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/ratelimit"
10+
)
11+
12+
type StsService struct {
13+
client *connectivity.TencentCloudClient
14+
}
15+
16+
func (me *StsService) DescribeStsCallerIdentityByFilter(ctx context.Context) (callerIdentity *sts.GetCallerIdentityResponseParams, errRet error) {
17+
var (
18+
logId = getLogId(ctx)
19+
request = sts.NewGetCallerIdentityRequest()
20+
)
21+
22+
defer func() {
23+
if errRet != nil {
24+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
25+
logId, "query object", request.ToJsonString(), errRet.Error())
26+
}
27+
}()
28+
29+
ratelimit.Check(request.GetAction())
30+
response, err := me.client.UseStsClient().GetCallerIdentity(request)
31+
if err != nil {
32+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
33+
logId, request.GetAction(), request.ToJsonString(), err.Error())
34+
errRet = err
35+
return
36+
}
37+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
38+
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
39+
callerIdentity = response.Response
40+
return
41+
}

vendor/github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/http/request.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)