|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of organization org_financial_by_member |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_organization_org_financial_by_member" "org_financial_by_member" { |
| 8 | + month = "2023-05" |
| 9 | + end_month = "2023-10" |
| 10 | + member_uins = [100015591986,100029796005] |
| 11 | + } |
| 12 | +``` |
| 13 | +*/ |
| 14 | +package tencentcloud |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + |
| 19 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 20 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 21 | + organization "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization/v20210331" |
| 22 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 23 | +) |
| 24 | + |
| 25 | +func dataSourceTencentCloudOrganizationOrgFinancialByMember() *schema.Resource { |
| 26 | + return &schema.Resource{ |
| 27 | + Read: dataSourceTencentCloudOrganizationOrgFinancialByMemberRead, |
| 28 | + Schema: map[string]*schema.Schema{ |
| 29 | + "month": { |
| 30 | + Required: true, |
| 31 | + Type: schema.TypeString, |
| 32 | + Description: "Query for the start month. Format:yyyy-mm, for example:2021-01.", |
| 33 | + }, |
| 34 | + |
| 35 | + "end_month": { |
| 36 | + Optional: true, |
| 37 | + Type: schema.TypeString, |
| 38 | + Description: "Query for the end month. Format:yyyy-mm, for example:2021-01.The default value is the `Month`.", |
| 39 | + }, |
| 40 | + |
| 41 | + "member_uins": { |
| 42 | + Optional: true, |
| 43 | + Type: schema.TypeSet, |
| 44 | + Elem: &schema.Schema{ |
| 45 | + Type: schema.TypeInt, |
| 46 | + }, |
| 47 | + Description: "Member uin list. Up to 100.", |
| 48 | + }, |
| 49 | + |
| 50 | + "product_codes": { |
| 51 | + Optional: true, |
| 52 | + Type: schema.TypeSet, |
| 53 | + Elem: &schema.Schema{ |
| 54 | + Type: schema.TypeString, |
| 55 | + }, |
| 56 | + Description: "Product code list. Up to 100.", |
| 57 | + }, |
| 58 | + |
| 59 | + "total_cost": { |
| 60 | + Computed: true, |
| 61 | + Type: schema.TypeFloat, |
| 62 | + Description: "Total cost of the member.", |
| 63 | + }, |
| 64 | + |
| 65 | + "items": { |
| 66 | + Computed: true, |
| 67 | + Type: schema.TypeList, |
| 68 | + Description: "Member financial detail.", |
| 69 | + Elem: &schema.Resource{ |
| 70 | + Schema: map[string]*schema.Schema{ |
| 71 | + "member_uin": { |
| 72 | + Type: schema.TypeInt, |
| 73 | + Computed: true, |
| 74 | + Description: "Member uin.", |
| 75 | + }, |
| 76 | + "member_name": { |
| 77 | + Type: schema.TypeString, |
| 78 | + Computed: true, |
| 79 | + Description: "Member name.", |
| 80 | + }, |
| 81 | + "total_cost": { |
| 82 | + Type: schema.TypeFloat, |
| 83 | + Computed: true, |
| 84 | + Description: "Total cost of the member.", |
| 85 | + }, |
| 86 | + "ratio": { |
| 87 | + Type: schema.TypeString, |
| 88 | + Computed: true, |
| 89 | + Description: "The percentage of the organization total cost that is accounted for by the member.", |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + |
| 95 | + "result_output_file": { |
| 96 | + Type: schema.TypeString, |
| 97 | + Optional: true, |
| 98 | + Description: "Used to save results.", |
| 99 | + }, |
| 100 | + }, |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func dataSourceTencentCloudOrganizationOrgFinancialByMemberRead(d *schema.ResourceData, meta interface{}) error { |
| 105 | + defer logElapsed("data_source.tencentcloud_organization_org_financial_by_member.read")() |
| 106 | + defer inconsistentCheck(d, meta)() |
| 107 | + |
| 108 | + logId := getLogId(contextNil) |
| 109 | + |
| 110 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 111 | + |
| 112 | + paramMap := make(map[string]interface{}) |
| 113 | + if v, ok := d.GetOk("month"); ok { |
| 114 | + paramMap["Month"] = helper.String(v.(string)) |
| 115 | + } |
| 116 | + |
| 117 | + if v, ok := d.GetOk("end_month"); ok { |
| 118 | + paramMap["EndMonth"] = helper.String(v.(string)) |
| 119 | + } |
| 120 | + |
| 121 | + if v, ok := d.GetOk("member_uins"); ok { |
| 122 | + memberUinsSet := v.(*schema.Set).List() |
| 123 | + paramMap["MemberUins"] = helper.InterfacesIntInt64Point(memberUinsSet) |
| 124 | + } |
| 125 | + |
| 126 | + if v, ok := d.GetOk("product_codes"); ok { |
| 127 | + productCodesSet := v.(*schema.Set).List() |
| 128 | + paramMap["ProductCodes"] = helper.InterfacesStringsPoint(productCodesSet) |
| 129 | + } |
| 130 | + |
| 131 | + service := OrganizationService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 132 | + var response *organization.DescribeOrganizationFinancialByMemberResponseParams |
| 133 | + |
| 134 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 135 | + result, e := service.DescribeOrganizationOrgFinancialByMemberByFilter(ctx, paramMap) |
| 136 | + if e != nil { |
| 137 | + return retryError(e) |
| 138 | + } |
| 139 | + response = result |
| 140 | + return nil |
| 141 | + }) |
| 142 | + if err != nil { |
| 143 | + return err |
| 144 | + } |
| 145 | + |
| 146 | + ids := make([]string, 0, len(response.Items)) |
| 147 | + tmpList := make([]map[string]interface{}, 0, len(response.Items)) |
| 148 | + if response.TotalCost != nil { |
| 149 | + _ = d.Set("total_cost", response.TotalCost) |
| 150 | + } |
| 151 | + |
| 152 | + if response.Items != nil { |
| 153 | + for _, orgMemberFinancial := range response.Items { |
| 154 | + orgMemberFinancialMap := map[string]interface{}{} |
| 155 | + |
| 156 | + if orgMemberFinancial.MemberUin != nil { |
| 157 | + orgMemberFinancialMap["member_uin"] = orgMemberFinancial.MemberUin |
| 158 | + } |
| 159 | + |
| 160 | + if orgMemberFinancial.MemberName != nil { |
| 161 | + orgMemberFinancialMap["member_name"] = orgMemberFinancial.MemberName |
| 162 | + } |
| 163 | + |
| 164 | + if orgMemberFinancial.TotalCost != nil { |
| 165 | + orgMemberFinancialMap["total_cost"] = orgMemberFinancial.TotalCost |
| 166 | + } |
| 167 | + |
| 168 | + if orgMemberFinancial.Ratio != nil { |
| 169 | + orgMemberFinancialMap["ratio"] = orgMemberFinancial.Ratio |
| 170 | + } |
| 171 | + |
| 172 | + ids = append(ids, helper.Int64ToStr(*orgMemberFinancial.MemberUin)) |
| 173 | + tmpList = append(tmpList, orgMemberFinancialMap) |
| 174 | + } |
| 175 | + |
| 176 | + _ = d.Set("items", tmpList) |
| 177 | + } |
| 178 | + |
| 179 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 180 | + output, ok := d.GetOk("result_output_file") |
| 181 | + if ok && output.(string) != "" { |
| 182 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 183 | + return e |
| 184 | + } |
| 185 | + } |
| 186 | + return nil |
| 187 | +} |
0 commit comments