|
| 1 | +package bh |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | + bhv20230418 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/bh/v20230418" |
| 9 | + |
| 10 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 11 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 12 | +) |
| 13 | + |
| 14 | +func DataSourceTencentCloudBhAccountGroups() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Read: dataSourceTencentCloudBhAccountGroupsRead, |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "deep_in": { |
| 19 | + Type: schema.TypeInt, |
| 20 | + Optional: true, |
| 21 | + Description: "Whether to recursively query, 0 for non-recursive, 1 for recursive.", |
| 22 | + }, |
| 23 | + |
| 24 | + "parent_id": { |
| 25 | + Type: schema.TypeInt, |
| 26 | + Optional: true, |
| 27 | + Description: "Parent account group ID, default 0, query all groups under the root account group.", |
| 28 | + }, |
| 29 | + |
| 30 | + "group_name": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Optional: true, |
| 33 | + Description: "Account group name, fuzzy query.", |
| 34 | + }, |
| 35 | + |
| 36 | + "page_num": { |
| 37 | + Type: schema.TypeInt, |
| 38 | + Optional: true, |
| 39 | + Description: "Get data from which page.", |
| 40 | + }, |
| 41 | + |
| 42 | + "account_group_set": { |
| 43 | + Type: schema.TypeList, |
| 44 | + Computed: true, |
| 45 | + Description: "Account group information.", |
| 46 | + Elem: &schema.Resource{ |
| 47 | + Schema: map[string]*schema.Schema{ |
| 48 | + "id": { |
| 49 | + Type: schema.TypeInt, |
| 50 | + Computed: true, |
| 51 | + Description: "Account group ID.", |
| 52 | + }, |
| 53 | + "name": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Computed: true, |
| 56 | + Description: "Account group name.", |
| 57 | + }, |
| 58 | + "id_path": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Computed: true, |
| 61 | + Description: "Account group ID path.", |
| 62 | + }, |
| 63 | + "name_path": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Computed: true, |
| 66 | + Description: "Account group name path.", |
| 67 | + }, |
| 68 | + "parent_id": { |
| 69 | + Type: schema.TypeInt, |
| 70 | + Computed: true, |
| 71 | + Description: "Parent account group ID.", |
| 72 | + }, |
| 73 | + "source": { |
| 74 | + Type: schema.TypeInt, |
| 75 | + Computed: true, |
| 76 | + Description: "Account group source.", |
| 77 | + }, |
| 78 | + "user_total": { |
| 79 | + Type: schema.TypeInt, |
| 80 | + Computed: true, |
| 81 | + Description: "Total number of users under the account group.", |
| 82 | + }, |
| 83 | + "is_leaf": { |
| 84 | + Type: schema.TypeBool, |
| 85 | + Computed: true, |
| 86 | + Description: "Whether it is a leaf node.", |
| 87 | + }, |
| 88 | + "import_type": { |
| 89 | + Type: schema.TypeString, |
| 90 | + Computed: true, |
| 91 | + Description: "Account group import type.", |
| 92 | + }, |
| 93 | + "description": { |
| 94 | + Type: schema.TypeString, |
| 95 | + Computed: true, |
| 96 | + Description: "Account group description.", |
| 97 | + }, |
| 98 | + "parent_org_id": { |
| 99 | + Type: schema.TypeString, |
| 100 | + Computed: true, |
| 101 | + Description: "Parent source account organization ID. When using third-party import user sources, record the group ID of this group in the source organization structure.", |
| 102 | + }, |
| 103 | + "org_id": { |
| 104 | + Type: schema.TypeString, |
| 105 | + Computed: true, |
| 106 | + Description: "Source account organization ID. When using third-party import user sources, record the group ID of this group in the source organization structure.", |
| 107 | + }, |
| 108 | + "status": { |
| 109 | + Type: schema.TypeInt, |
| 110 | + Computed: true, |
| 111 | + Description: "Whether the account group has been connected, 0 means not connected, 1 means connected.", |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + |
| 117 | + "result_output_file": { |
| 118 | + Type: schema.TypeString, |
| 119 | + Optional: true, |
| 120 | + Description: "Used to save results.", |
| 121 | + }, |
| 122 | + }, |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func dataSourceTencentCloudBhAccountGroupsRead(d *schema.ResourceData, meta interface{}) error { |
| 127 | + defer tccommon.LogElapsed("data_source.tencentcloud_bh_account_groups.read")() |
| 128 | + defer tccommon.InconsistentCheck(d, meta)() |
| 129 | + |
| 130 | + var ( |
| 131 | + logId = tccommon.GetLogId(nil) |
| 132 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 133 | + service = BhService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 134 | + ) |
| 135 | + |
| 136 | + paramMap := make(map[string]interface{}) |
| 137 | + if v, ok := d.GetOkExists("deep_in"); ok { |
| 138 | + paramMap["DeepIn"] = helper.IntInt64(v.(int)) |
| 139 | + } |
| 140 | + |
| 141 | + if v, ok := d.GetOkExists("parent_id"); ok { |
| 142 | + paramMap["ParentId"] = helper.IntInt64(v.(int)) |
| 143 | + } |
| 144 | + |
| 145 | + if v, ok := d.GetOk("group_name"); ok { |
| 146 | + paramMap["GroupName"] = helper.String(v.(string)) |
| 147 | + } |
| 148 | + |
| 149 | + if v, ok := d.GetOkExists("page_num"); ok { |
| 150 | + paramMap["PageNum"] = helper.IntInt64(v.(int)) |
| 151 | + } |
| 152 | + |
| 153 | + var respData []*bhv20230418.AccountGroup |
| 154 | + reqErr := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { |
| 155 | + result, e := service.DescribeBhAccountGroupsByFilter(ctx, paramMap) |
| 156 | + if e != nil { |
| 157 | + return tccommon.RetryError(e) |
| 158 | + } |
| 159 | + |
| 160 | + respData = result |
| 161 | + return nil |
| 162 | + }) |
| 163 | + |
| 164 | + if reqErr != nil { |
| 165 | + return reqErr |
| 166 | + } |
| 167 | + |
| 168 | + accountGroupSetList := make([]map[string]interface{}, 0, len(respData)) |
| 169 | + if respData != nil { |
| 170 | + for _, accountGroupSet := range respData { |
| 171 | + accountGroupSetMap := map[string]interface{}{} |
| 172 | + if accountGroupSet.Id != nil { |
| 173 | + accountGroupSetMap["id"] = accountGroupSet.Id |
| 174 | + } |
| 175 | + |
| 176 | + if accountGroupSet.Name != nil { |
| 177 | + accountGroupSetMap["name"] = accountGroupSet.Name |
| 178 | + } |
| 179 | + |
| 180 | + if accountGroupSet.IdPath != nil { |
| 181 | + accountGroupSetMap["id_path"] = accountGroupSet.IdPath |
| 182 | + } |
| 183 | + |
| 184 | + if accountGroupSet.NamePath != nil { |
| 185 | + accountGroupSetMap["name_path"] = accountGroupSet.NamePath |
| 186 | + } |
| 187 | + |
| 188 | + if accountGroupSet.ParentId != nil { |
| 189 | + accountGroupSetMap["parent_id"] = accountGroupSet.ParentId |
| 190 | + } |
| 191 | + |
| 192 | + if accountGroupSet.Source != nil { |
| 193 | + accountGroupSetMap["source"] = accountGroupSet.Source |
| 194 | + } |
| 195 | + |
| 196 | + if accountGroupSet.UserTotal != nil { |
| 197 | + accountGroupSetMap["user_total"] = accountGroupSet.UserTotal |
| 198 | + } |
| 199 | + |
| 200 | + if accountGroupSet.IsLeaf != nil { |
| 201 | + accountGroupSetMap["is_leaf"] = accountGroupSet.IsLeaf |
| 202 | + } |
| 203 | + |
| 204 | + if accountGroupSet.ImportType != nil { |
| 205 | + accountGroupSetMap["import_type"] = accountGroupSet.ImportType |
| 206 | + } |
| 207 | + |
| 208 | + if accountGroupSet.Description != nil { |
| 209 | + accountGroupSetMap["description"] = accountGroupSet.Description |
| 210 | + } |
| 211 | + |
| 212 | + if accountGroupSet.ParentOrgId != nil { |
| 213 | + accountGroupSetMap["parent_org_id"] = accountGroupSet.ParentOrgId |
| 214 | + } |
| 215 | + |
| 216 | + if accountGroupSet.OrgId != nil { |
| 217 | + accountGroupSetMap["org_id"] = accountGroupSet.OrgId |
| 218 | + } |
| 219 | + |
| 220 | + if accountGroupSet.Status != nil { |
| 221 | + accountGroupSetMap["status"] = accountGroupSet.Status |
| 222 | + } |
| 223 | + |
| 224 | + accountGroupSetList = append(accountGroupSetList, accountGroupSetMap) |
| 225 | + } |
| 226 | + |
| 227 | + _ = d.Set("account_group_set", accountGroupSetList) |
| 228 | + } |
| 229 | + |
| 230 | + d.SetId(helper.BuildToken()) |
| 231 | + output, ok := d.GetOk("result_output_file") |
| 232 | + if ok && output.(string) != "" { |
| 233 | + if e := tccommon.WriteToFile(output.(string), accountGroupSetList); e != nil { |
| 234 | + return e |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + return nil |
| 239 | +} |
0 commit comments