|
5 | 5 | "errors"
|
6 | 6 | "fmt"
|
7 | 7 | "log"
|
| 8 | + "sort" |
8 | 9 | "strconv"
|
| 10 | + "strings" |
9 | 11 | "time"
|
10 | 12 |
|
11 | 13 | tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
|
@@ -824,33 +826,74 @@ func (me *PostgresqlService) CheckDBInstanceStatus(ctx context.Context, instance
|
824 | 826 |
|
825 | 827 | func (me *PostgresqlService) DescribeRootUser(ctx context.Context, instanceId string) (accounts []*postgresql.AccountInfo, errRet error) {
|
826 | 828 | logId := tccommon.GetLogId(ctx)
|
827 |
| - orderBy := "createTime" |
828 |
| - orderByType := "asc" |
829 |
| - |
830 | 829 | request := postgresql.NewDescribeAccountsRequest()
|
831 | 830 | request.DBInstanceId = &instanceId
|
832 |
| - request.OrderBy = &orderBy |
833 |
| - request.OrderByType = &orderByType |
834 |
| - request.Offset = helper.Int64(0) |
835 |
| - request.Limit = helper.Int64(100) |
| 831 | + request.OrderByType = helper.String("asc") |
| 832 | + request.OrderBy = helper.String("createTime") |
| 833 | + |
836 | 834 | var response *postgresql.DescribeAccountsResponse
|
837 |
| - errRet = resource.Retry(2*tccommon.ReadRetryTimeout, func() *resource.RetryError { |
838 |
| - response, errRet = me.client.UsePostgresqlClient().DescribeAccounts(request) |
| 835 | + var tmpList []*postgresql.AccountInfo |
| 836 | + |
| 837 | + var offset, limit int64 = 0, 100 |
| 838 | + |
| 839 | + for { |
| 840 | + request.Offset = &offset |
| 841 | + request.Limit = &limit |
| 842 | + ratelimit.Check(request.GetAction()) |
| 843 | + errRet = resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { |
| 844 | + result, e := me.client.UsePostgresqlClient().DescribeAccounts(request) |
| 845 | + if e != nil { |
| 846 | + log.Printf("[CRITAL]%s describe account failed, reason: %v", logId, e) |
| 847 | + return tccommon.RetryError(e) |
| 848 | + } |
| 849 | + |
| 850 | + if result == nil || result.Response == nil || result.Response.Details == nil { |
| 851 | + errRet = fmt.Errorf("TencentCloud SDK return nil response, %+v, %s", result, request.GetAction()) |
| 852 | + } |
| 853 | + |
| 854 | + response = result |
| 855 | + return nil |
| 856 | + }) |
| 857 | + |
839 | 858 | if errRet != nil {
|
840 |
| - log.Printf("[CRITAL]%s describe account failed, reason: %v", logId, errRet) |
841 |
| - return tccommon.RetryError(errRet) |
| 859 | + return nil, errRet |
842 | 860 | }
|
843 |
| - return nil |
844 |
| - }) |
845 |
| - if errRet != nil { |
846 |
| - return nil, errRet |
| 861 | + |
| 862 | + tmpList = append(tmpList, response.Response.Details...) |
| 863 | + if len(response.Response.Details) < int(limit) { |
| 864 | + break |
| 865 | + } |
| 866 | + |
| 867 | + offset += limit |
847 | 868 | }
|
848 |
| - if response == nil || response.Response == nil || response.Response.Details == nil { |
849 |
| - errRet = fmt.Errorf("TencentCloud SDK return nil response, %+v, %s", response, request.GetAction()) |
850 |
| - } else { |
851 |
| - accounts = response.Response.Details |
| 869 | + |
| 870 | + for _, item := range tmpList { |
| 871 | + if item.CreateTime != nil && strings.Contains(*item.CreateTime, "0000-00-00") { |
| 872 | + continue |
| 873 | + } |
| 874 | + |
| 875 | + accounts = append(accounts, item) |
852 | 876 | }
|
853 | 877 |
|
| 878 | + sort.Slice(accounts, func(i, j int) bool { |
| 879 | + timeStrI := accounts[i].CreateTime |
| 880 | + timeStrJ := accounts[j].CreateTime |
| 881 | + |
| 882 | + timeI, errI := time.Parse(tccommon.TENCENTCLOUD_COMMON_TIME_LAYOUT, *timeStrI) |
| 883 | + if errI != nil { |
| 884 | + fmt.Printf("Error parsing time string %s: %v\n", *timeStrI, errI) |
| 885 | + return false |
| 886 | + } |
| 887 | + |
| 888 | + timeJ, errJ := time.Parse(tccommon.TENCENTCLOUD_COMMON_TIME_LAYOUT, *timeStrJ) |
| 889 | + if errJ != nil { |
| 890 | + fmt.Printf("Error parsing time string %s: %v\n", *timeStrJ, errJ) |
| 891 | + return false |
| 892 | + } |
| 893 | + |
| 894 | + return timeI.Before(timeJ) |
| 895 | + }) |
| 896 | + |
854 | 897 | return accounts, errRet
|
855 | 898 | }
|
856 | 899 |
|
|
0 commit comments