Skip to content

Commit 2713ed3

Browse files
tongyimingmikatong
and
mikatong
authored
fix(tcr): [123875920]support reset_password (#3346)
* support reset_password * add changelog * update test * support update password * update doc * rm reset_password * update --------- Co-authored-by: mikatong <[email protected]>
1 parent 0336560 commit 2713ed3

File tree

11 files changed

+2179
-2200
lines changed

11 files changed

+2179
-2200
lines changed

.changelog/3346.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_tcr_service_account: support update password
3+
```

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ require (
8686
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tat v1.0.634
8787
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcaplusdb v1.0.199
8888
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcm v1.0.547
89-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcr v1.0.696
89+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcr v1.0.1142
9090
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tdcpg v1.0.533
9191
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tdmq v1.0.955
9292
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tem v1.0.578

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcm v1.0.547 h1:6bukohy
10891089
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcm v1.0.547/go.mod h1:C7b++Lr8Xh+2KtTUMBjbb+/BrBhfFhAxDMjXzT2GLhY=
10901090
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcr v1.0.696 h1:hTfJtxk5WDj84SfOFKg5Mk+DVtSmjVtkyj3gvVKAYkk=
10911091
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcr v1.0.696/go.mod h1:DgY9Kgh9093fBl2M/vEFGJZwytDIrPQlYoxoT4rE7O0=
1092+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcr v1.0.1142 h1:X5eKYvyDNA69jXB2AtsLIoa8lwZ3haUZymSHHk4JUiI=
1093+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcr v1.0.1142/go.mod h1:GLR0nyoKy8qfcWpfDeA8wgRDY+pqq5UE/SLbB2l9NzU=
10921094
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcss v1.0.1031 h1:3OgSLiQAfgjQ3kH+lu5WBXRtE6JaZ+FY1Yr+LUv1JEw=
10931095
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcss v1.0.1031/go.mod h1:e5FteF6ukTKsD6wergqmPn/jww3oPwi/QJYtTfYbFuY=
10941096
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tdcpg v1.0.533 h1:r6HQhmHzPp1oSGhwkNzUzIRlpnpb8Jhtcn1yKhg9ml4=

tencentcloud/services/tcr/resource_tc_tcr_service_account.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ func ResourceTencentCloudTcrServiceAccount() *schema.Resource {
8787
},
8888

8989
"password": {
90+
Optional: true,
9091
Computed: true,
92+
Sensitive: true,
9193
Type: schema.TypeString,
9294
Description: "Password of the service account.",
9395
},
@@ -195,6 +197,15 @@ func resourceTencentCloudTcrServiceAccountCreate(d *schema.ResourceData, meta in
195197
}
196198
}
197199

200+
service := TCRService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
201+
if v, ok := d.GetOk("password"); ok {
202+
password, err := service.ModifyServiceAccountPassword(ctx, registryId, name, v.(string))
203+
if err != nil {
204+
return err
205+
}
206+
_ = d.Set("password", password)
207+
}
208+
198209
return resourceTencentCloudTcrServiceAccountRead(d, meta)
199210
}
200211

@@ -277,6 +288,8 @@ func resourceTencentCloudTcrServiceAccountUpdate(d *schema.ResourceData, meta in
277288
defer tccommon.InconsistentCheck(d, meta)()
278289

279290
logId := tccommon.GetLogId(tccommon.ContextNil)
291+
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
292+
service := TCRService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
280293

281294
request := tcr.NewModifyServiceAccountRequest()
282295

@@ -370,6 +383,15 @@ func resourceTencentCloudTcrServiceAccountUpdate(d *schema.ResourceData, meta in
370383
}
371384
}
372385

386+
if d.HasChange("password") {
387+
if v, ok := d.GetOk("password"); ok {
388+
password, err := service.ModifyServiceAccountPassword(ctx, registryId, name, v.(string))
389+
if err != nil {
390+
return err
391+
}
392+
_ = d.Set("password", password)
393+
}
394+
}
373395
return resourceTencentCloudTcrServiceAccountRead(d, meta)
374396
}
375397

tencentcloud/services/tcr/resource_tc_tcr_service_account_test.go

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ func TestAccTencentCloudTcrServiceAccountResource_basic(t *testing.T) {
5555
// resource.TestCheckResourceAttr("tencentcloud_tcr_service_account.example", "disable", "true"),
5656
),
5757
},
58+
{
59+
Config: fmt.Sprintf(testAccTcrServiceAccount_updatePassword, expireTime),
60+
PreConfig: func() {
61+
tcacctest.AccStepSetRegion(t, "ap-shanghai")
62+
tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_COMMON)
63+
},
64+
Check: resource.ComposeTestCheckFunc(
65+
resource.TestCheckResourceAttrSet("tencentcloud_tcr_service_account.example", "id"),
66+
resource.TestCheckResourceAttr("tencentcloud_tcr_service_account.example", "password", "passwordTest1"),
67+
),
68+
},
5869
{
5970
ResourceName: "tencentcloud_tcr_service_account.example",
6071
ImportState: true,
@@ -68,7 +79,7 @@ func TestAccTencentCloudTcrServiceAccountResource_basic(t *testing.T) {
6879
const testAccTcrServiceAccount = `
6980
7081
resource "tencentcloud_tcr_instance" "example" {
71-
name = "tf-example-tcr-instance"
82+
name = "tf-example-tcr-instance3"
7283
instance_type = "premium"
7384
delete_bucket = true
7485
tags = {
@@ -83,9 +94,6 @@ resource "tencentcloud_tcr_instance" "example" {
8394
is_auto_scan = true
8495
is_prevent_vul = true
8596
severity = "medium"
86-
cve_whitelist_items {
87-
cve_id = "tf_example_cve_id"
88-
}
8997
}
9098
9199
resource "tencentcloud_tcr_service_account" "example" {
@@ -108,7 +116,7 @@ resource "tencentcloud_tcr_instance" "example" {
108116
const testAccTcrServiceAccount_Update = `
109117
110118
resource "tencentcloud_tcr_instance" "example" {
111-
name = "tf-example-tcr-instance"
119+
name = "tf-example-tcr-instance3"
112120
instance_type = "premium"
113121
delete_bucket = true
114122
tags = {
@@ -123,11 +131,45 @@ resource "tencentcloud_tcr_instance" "example" {
123131
is_auto_scan = true
124132
is_prevent_vul = true
125133
severity = "medium"
126-
cve_whitelist_items {
127-
cve_id = "tf_example_cve_id"
134+
}
135+
136+
resource "tencentcloud_tcr_service_account" "example" {
137+
registry_id = tencentcloud_tcr_instance.example.id
138+
name = "tf_example_account"
139+
permissions {
140+
resource = tencentcloud_tcr_namespace.example.name
141+
actions = ["tcr:PushRepository"]
142+
}
143+
description = "CHANGED tf example for tcr service account"
144+
expires_at = %d
145+
disable = false
146+
tags = {
147+
"createdBy" = "terraform"
148+
}
149+
}
150+
151+
`
152+
153+
const testAccTcrServiceAccount_updatePassword = `
154+
155+
resource "tencentcloud_tcr_instance" "example" {
156+
name = "tf-example-tcr-instance3"
157+
instance_type = "premium"
158+
delete_bucket = true
159+
tags = {
160+
"createdBy" = "terraform"
128161
}
129162
}
130163
164+
resource "tencentcloud_tcr_namespace" "example" {
165+
instance_id = tencentcloud_tcr_instance.example.id
166+
name = "tf_test_tcr_namespace"
167+
is_public = true
168+
is_auto_scan = true
169+
is_prevent_vul = true
170+
severity = "medium"
171+
}
172+
131173
resource "tencentcloud_tcr_service_account" "example" {
132174
registry_id = tencentcloud_tcr_instance.example.id
133175
name = "tf_example_account"
@@ -141,6 +183,7 @@ resource "tencentcloud_tcr_instance" "example" {
141183
tags = {
142184
"createdBy" = "terraform"
143185
}
186+
password = "passwordTest1"
144187
}
145188
146189
`

tencentcloud/services/tcr/service_tencentcloud_tcr.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,3 +1801,33 @@ func (me *TCRService) DeleteTcrServiceAccountById(ctx context.Context, registryI
18011801

18021802
return
18031803
}
1804+
1805+
func (me *TCRService) ModifyServiceAccountPassword(ctx context.Context, registryId string, name string, password string) (passwordResp string, errRet error) {
1806+
logId := tccommon.GetLogId(ctx)
1807+
1808+
request := tcr.NewModifyServiceAccountPasswordRequest()
1809+
request.RegistryId = &registryId
1810+
request.Name = &name
1811+
request.Random = helper.Bool(false)
1812+
request.Password = helper.String(password)
1813+
1814+
defer func() {
1815+
if errRet != nil {
1816+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
1817+
}
1818+
}()
1819+
1820+
ratelimit.Check(request.GetAction())
1821+
1822+
response, err := me.client.UseTCRClient().ModifyServiceAccountPassword(request)
1823+
if err != nil {
1824+
errRet = err
1825+
return
1826+
}
1827+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
1828+
1829+
if response.Response.Password != nil {
1830+
passwordResp = *response.Response.Password
1831+
}
1832+
return
1833+
}

0 commit comments

Comments
 (0)