Skip to content

Commit 155fc02

Browse files
tongyimingmikatong
and
mikatong
authored
fix(tco): [123456789] identity center role assignment (#3064)
* fix identity-center-role-assignment * add changelog * add retry * update * update --------- Co-authored-by: mikatong <[email protected]>
1 parent 33e11b7 commit 155fc02

File tree

4 files changed

+92
-16
lines changed

4 files changed

+92
-16
lines changed

.changelog/3064.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_identity_center_role_assignment: process failed task status
3+
```

tencentcloud/services/tco/extension_tco.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ const (
1414

1515
DescribeTargetTypeNode = "Node"
1616
DescribeTargetTypeMember = "User"
17+
18+
TASK_STATUS_SUCCESS = "Success"
19+
TASK_STATUS_FAILED = "Failed"
1720
)

tencentcloud/services/tco/resource_tc_identity_center_role_assignment.go

Lines changed: 77 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,29 @@ func resourceTencentCloudIdentityCenterRoleAssignmentCreate(d *schema.ResourceDa
164164

165165
if len(response.Response.Tasks) > 0 {
166166
task := response.Response.Tasks[0]
167+
if task == nil {
168+
return fmt.Errorf("task is nil")
169+
}
170+
if task.Status != nil && *task.Status == TASK_STATUS_FAILED {
171+
if task.FailureReason != nil {
172+
return fmt.Errorf("create role assignment task failed, failure reason:%s", *task.FailureReason)
173+
}
174+
return fmt.Errorf("create role assignment task failed")
175+
}
176+
177+
if task.TaskId == nil {
178+
return fmt.Errorf("create role assignment task id is nil")
179+
}
167180
taskId := *task.TaskId
168181
roleConfigurationId := *task.RoleConfigurationId
169-
conf := tccommon.BuildStateChangeConf([]string{}, []string{"Success"}, 2*tccommon.ReadRetryTimeout, time.Second, service.AssignmentTaskStatusStateRefreshFunc(zoneId, taskId, []string{}))
170-
if _, e := conf.WaitForState(); e != nil {
182+
conf := tccommon.BuildStateChangeConf([]string{}, []string{TASK_STATUS_SUCCESS, TASK_STATUS_FAILED}, 2*tccommon.ReadRetryTimeout, time.Second, service.AssignmentTaskStatusStateRefreshFunc(zoneId, taskId, []string{}))
183+
if object, e := conf.WaitForState(); e != nil {
171184
return e
185+
} else {
186+
taskStatus := object.(*organization.TaskStatus)
187+
if taskStatus.Status != nil && *taskStatus.Status == TASK_STATUS_FAILED {
188+
return fmt.Errorf("create role assignment task failed")
189+
}
172190
}
173191

174192
targetUinString := strconv.FormatInt(targetUin, 10)
@@ -188,18 +206,26 @@ func resourceTencentCloudIdentityCenterRoleAssignmentRead(d *schema.ResourceData
188206

189207
service := OrganizationService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
190208

191-
respData, err := service.DescribeIdentityCenterRoleAssignmentById(ctx, d.Id())
209+
var roleAssignmentsResponseParams *organization.ListRoleAssignmentsResponseParams
210+
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
211+
result, e := service.DescribeIdentityCenterRoleAssignmentById(ctx, d.Id())
212+
if e != nil {
213+
return tccommon.RetryError(e)
214+
}
215+
roleAssignmentsResponseParams = result
216+
return nil
217+
})
192218
if err != nil {
193219
return err
194220
}
195221

196-
if respData == nil {
222+
if roleAssignmentsResponseParams == nil {
197223
d.SetId("")
198224
log.Printf("[WARN]%s resource `identity_center_role_assignment` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
199225
return nil
200226
}
201-
if len(respData.RoleAssignments) > 0 {
202-
roleAssignment := respData.RoleAssignments[0]
227+
if len(roleAssignmentsResponseParams.RoleAssignments) > 0 {
228+
roleAssignment := roleAssignmentsResponseParams.RoleAssignments[0]
203229
if roleAssignment.RoleConfigurationId != nil {
204230
_ = d.Set("role_configuration_id", roleAssignment.RoleConfigurationId)
205231
}
@@ -292,10 +318,29 @@ func resourceTencentCloudIdentityCenterRoleAssignmentDelete(d *schema.ResourceDa
292318
return err
293319
}
294320

295-
if deleteRoleAssignmentResponse.Response != nil && deleteRoleAssignmentResponse.Response.Task != nil && deleteRoleAssignmentResponse.Response.Task.TaskId != nil {
296-
conf := tccommon.BuildStateChangeConf([]string{}, []string{"Success"}, 2*tccommon.ReadRetryTimeout, time.Second, service.AssignmentTaskStatusStateRefreshFunc(zoneId, *deleteRoleAssignmentResponse.Response.Task.TaskId, []string{}))
297-
if _, e := conf.WaitForState(); e != nil {
298-
return e
321+
if deleteRoleAssignmentResponse == nil || deleteRoleAssignmentResponse.Response == nil {
322+
return fmt.Errorf("delete role assignment response is nil")
323+
}
324+
if deleteRoleAssignmentResponse.Response.Task == nil {
325+
return fmt.Errorf("delete role assignment task is nil")
326+
}
327+
task := deleteRoleAssignmentResponse.Response.Task
328+
if task.Status != nil && *task.Status == TASK_STATUS_FAILED {
329+
if task.FailureReason != nil {
330+
return fmt.Errorf("delete role assignment failed, failure reason:%s", *task.FailureReason)
331+
}
332+
return fmt.Errorf("delete role assignment failed")
333+
}
334+
if task.TaskId == nil {
335+
return fmt.Errorf("delete role assignment task id is nil")
336+
}
337+
conf := tccommon.BuildStateChangeConf([]string{}, []string{TASK_STATUS_SUCCESS, TASK_STATUS_FAILED}, 2*tccommon.ReadRetryTimeout, time.Second, service.AssignmentTaskStatusStateRefreshFunc(zoneId, *task.TaskId, []string{}))
338+
if object, e := conf.WaitForState(); e != nil {
339+
return e
340+
} else {
341+
taskStatus := object.(*organization.TaskStatus)
342+
if taskStatus.Status != nil && *taskStatus.Status == TASK_STATUS_FAILED {
343+
return fmt.Errorf("delete role assignment failed")
299344
}
300345
}
301346

@@ -318,10 +363,28 @@ func resourceTencentCloudIdentityCenterRoleAssignmentDelete(d *schema.ResourceDa
318363
return err
319364
}
320365

321-
if dismantleRoleConfigurationResponse.Response != nil && dismantleRoleConfigurationResponse.Response.Task != nil && dismantleRoleConfigurationResponse.Response.Task.TaskId != nil {
322-
conf := tccommon.BuildStateChangeConf([]string{}, []string{"Success"}, 2*tccommon.ReadRetryTimeout, time.Second, service.AssignmentTaskStatusStateRefreshFunc(zoneId, *dismantleRoleConfigurationResponse.Response.Task.TaskId, []string{}))
323-
if _, e := conf.WaitForState(); e != nil {
324-
return e
366+
if dismantleRoleConfigurationResponse == nil || dismantleRoleConfigurationResponse.Response == nil {
367+
return fmt.Errorf("dismantle role assignment response is nil")
368+
}
369+
if dismantleRoleConfigurationResponse.Response.Task == nil {
370+
return fmt.Errorf("dismantle role assignment task is nil")
371+
}
372+
dismantleTask := dismantleRoleConfigurationResponse.Response.Task
373+
374+
if dismantleTask.TaskStatus != nil && *dismantleTask.TaskStatus == TASK_STATUS_FAILED {
375+
return fmt.Errorf("dismantle role assignment task failed")
376+
}
377+
378+
if dismantleTask.TaskId == nil {
379+
return fmt.Errorf("dismantle role assignment task id is nil")
380+
}
381+
conf = tccommon.BuildStateChangeConf([]string{}, []string{TASK_STATUS_SUCCESS, TASK_STATUS_FAILED}, 2*tccommon.ReadRetryTimeout, time.Second, service.AssignmentTaskStatusStateRefreshFunc(zoneId, *dismantleTask.TaskId, []string{}))
382+
if object, e := conf.WaitForState(); e != nil {
383+
return e
384+
} else {
385+
taskStatus := object.(*organization.TaskStatus)
386+
if taskStatus.Status != nil && *taskStatus.Status == TASK_STATUS_FAILED {
387+
return fmt.Errorf("dismantle role assignment task failed")
325388
}
326389
}
327390

tencentcloud/services/tco/service_tencentcloud_organization.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,8 +1613,15 @@ func (me *OrganizationService) AssignmentTaskStatusStateRefreshFunc(zoneId, task
16131613
return func() (interface{}, string, error) {
16141614
ctx := tccommon.ContextNil
16151615

1616-
object, err := me.GetAssignmentTaskStatus(ctx, zoneId, taskId)
1617-
1616+
var object *organization.TaskStatus
1617+
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
1618+
result, e := me.GetAssignmentTaskStatus(ctx, zoneId, taskId)
1619+
if e != nil {
1620+
return tccommon.RetryError(e)
1621+
}
1622+
object = result
1623+
return nil
1624+
})
16181625
if err != nil {
16191626
return nil, "", err
16201627
}

0 commit comments

Comments
 (0)