|
| 1 | +package billing |
| 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 | + billingv20180709 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/billing/v20180709" |
| 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 DataSourceTencentCloudBillingBudgetOperationLog() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Read: dataSourceTencentCloudBillingBudgetOperationLogRead, |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "budget_id": { |
| 19 | + Type: schema.TypeString, |
| 20 | + Required: true, |
| 21 | + Description: "Budget id.", |
| 22 | + }, |
| 23 | + |
| 24 | + "records": { |
| 25 | + Type: schema.TypeList, |
| 26 | + Computed: true, |
| 27 | + Description: "Query data list.", |
| 28 | + Elem: &schema.Resource{ |
| 29 | + Schema: map[string]*schema.Schema{ |
| 30 | + "payer_uin": { |
| 31 | + Type: schema.TypeInt, |
| 32 | + Computed: true, |
| 33 | + Description: "Payer uin.", |
| 34 | + }, |
| 35 | + "owner_uin": { |
| 36 | + Type: schema.TypeInt, |
| 37 | + Computed: true, |
| 38 | + Description: "Owner uin.", |
| 39 | + }, |
| 40 | + "operate_uin": { |
| 41 | + Type: schema.TypeInt, |
| 42 | + Computed: true, |
| 43 | + Description: "Operate uin.", |
| 44 | + }, |
| 45 | + "bill_day": { |
| 46 | + Type: schema.TypeInt, |
| 47 | + Computed: true, |
| 48 | + Description: "Bill day.", |
| 49 | + }, |
| 50 | + "bill_month": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Computed: true, |
| 53 | + Description: "Bill month.", |
| 54 | + }, |
| 55 | + "action": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Computed: true, |
| 58 | + Description: "Modification type: ADD, UPDATE.", |
| 59 | + }, |
| 60 | + "diff_value": { |
| 61 | + Type: schema.TypeList, |
| 62 | + Computed: true, |
| 63 | + Description: "change information.", |
| 64 | + Elem: &schema.Resource{ |
| 65 | + Schema: map[string]*schema.Schema{ |
| 66 | + "property": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Computed: true, |
| 69 | + Description: "Change attributes.", |
| 70 | + }, |
| 71 | + "before": { |
| 72 | + Type: schema.TypeString, |
| 73 | + Computed: true, |
| 74 | + Description: "Content before change.", |
| 75 | + }, |
| 76 | + "after": { |
| 77 | + Type: schema.TypeString, |
| 78 | + Computed: true, |
| 79 | + Description: "Content after change.", |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + "create_time": { |
| 85 | + Type: schema.TypeString, |
| 86 | + Computed: true, |
| 87 | + Description: "Create time.", |
| 88 | + }, |
| 89 | + "update_time": { |
| 90 | + Type: schema.TypeString, |
| 91 | + Computed: true, |
| 92 | + Description: "Update time.", |
| 93 | + }, |
| 94 | + "operation_channel": { |
| 95 | + Type: schema.TypeString, |
| 96 | + Computed: true, |
| 97 | + Description: "Operation channel.", |
| 98 | + }, |
| 99 | + "budget_id": { |
| 100 | + Type: schema.TypeString, |
| 101 | + Computed: true, |
| 102 | + Description: "Budget item id.", |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + |
| 108 | + "result_output_file": { |
| 109 | + Type: schema.TypeString, |
| 110 | + Optional: true, |
| 111 | + Description: "Used to save results.", |
| 112 | + }, |
| 113 | + }, |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func dataSourceTencentCloudBillingBudgetOperationLogRead(d *schema.ResourceData, meta interface{}) error { |
| 118 | + defer tccommon.LogElapsed("data_source.tencentcloud_billing_budget_operation_log.read")() |
| 119 | + defer tccommon.InconsistentCheck(d, meta)() |
| 120 | + |
| 121 | + logId := tccommon.GetLogId(nil) |
| 122 | + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 123 | + |
| 124 | + service := BillingService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 125 | + |
| 126 | + var budgetId string |
| 127 | + |
| 128 | + paramMap := make(map[string]interface{}) |
| 129 | + |
| 130 | + if v, ok := d.GetOk("budget_id"); ok { |
| 131 | + budgetId = v.(string) |
| 132 | + paramMap["BudgetId"] = helper.String(budgetId) |
| 133 | + } |
| 134 | + |
| 135 | + var respData []*billingv20180709.BudgetOperationLogEntity |
| 136 | + err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { |
| 137 | + result, e := service.DescribeBillingBudgetOperationLogByFilter(ctx, paramMap) |
| 138 | + if e != nil { |
| 139 | + return tccommon.RetryError(e) |
| 140 | + } |
| 141 | + respData = result |
| 142 | + return nil |
| 143 | + }) |
| 144 | + if err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | + |
| 148 | + if len(respData) > 0 { |
| 149 | + recordsList := make([]map[string]interface{}, 0, len(respData)) |
| 150 | + |
| 151 | + for _, records := range respData { |
| 152 | + recordsMap := map[string]interface{}{} |
| 153 | + |
| 154 | + if records.PayerUin != nil { |
| 155 | + recordsMap["payer_uin"] = records.PayerUin |
| 156 | + } |
| 157 | + |
| 158 | + if records.OwnerUin != nil { |
| 159 | + recordsMap["owner_uin"] = records.OwnerUin |
| 160 | + } |
| 161 | + |
| 162 | + if records.OperateUin != nil { |
| 163 | + recordsMap["operate_uin"] = records.OperateUin |
| 164 | + } |
| 165 | + |
| 166 | + if records.BillDay != nil { |
| 167 | + recordsMap["bill_day"] = records.BillDay |
| 168 | + } |
| 169 | + |
| 170 | + if records.BillMonth != nil { |
| 171 | + recordsMap["bill_month"] = records.BillMonth |
| 172 | + } |
| 173 | + |
| 174 | + if records.Action != nil { |
| 175 | + recordsMap["action"] = records.Action |
| 176 | + } |
| 177 | + |
| 178 | + diffValueList := make([]map[string]interface{}, 0, len(records.DiffValue)) |
| 179 | + if records.DiffValue != nil { |
| 180 | + for _, diffValue := range records.DiffValue { |
| 181 | + diffValueMap := map[string]interface{}{} |
| 182 | + |
| 183 | + if diffValue.Property != nil { |
| 184 | + diffValueMap["property"] = diffValue.Property |
| 185 | + } |
| 186 | + |
| 187 | + if diffValue.Before != nil { |
| 188 | + diffValueMap["before"] = diffValue.Before |
| 189 | + } |
| 190 | + |
| 191 | + if diffValue.After != nil { |
| 192 | + diffValueMap["after"] = diffValue.After |
| 193 | + } |
| 194 | + |
| 195 | + diffValueList = append(diffValueList, diffValueMap) |
| 196 | + } |
| 197 | + |
| 198 | + recordsMap["diff_value"] = diffValueList |
| 199 | + } |
| 200 | + if records.CreateTime != nil { |
| 201 | + recordsMap["create_time"] = records.CreateTime |
| 202 | + } |
| 203 | + |
| 204 | + if records.UpdateTime != nil { |
| 205 | + recordsMap["update_time"] = records.UpdateTime |
| 206 | + } |
| 207 | + |
| 208 | + if records.OperationChannel != nil { |
| 209 | + recordsMap["operation_channel"] = records.OperationChannel |
| 210 | + } |
| 211 | + |
| 212 | + if records.BudgetId != nil { |
| 213 | + recordsMap["budget_id"] = records.BudgetId |
| 214 | + } |
| 215 | + |
| 216 | + recordsList = append(recordsList, recordsMap) |
| 217 | + } |
| 218 | + |
| 219 | + _ = d.Set("records", recordsList) |
| 220 | + } |
| 221 | + |
| 222 | + d.SetId(budgetId) |
| 223 | + |
| 224 | + output, ok := d.GetOk("result_output_file") |
| 225 | + if ok && output.(string) != "" { |
| 226 | + if e := tccommon.WriteToFile(output.(string), d); e != nil { |
| 227 | + return e |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + return nil |
| 232 | +} |
0 commit comments