Skip to content

Commit 1571636

Browse files
tongyimingmikatong
andauthored
fix(billing): [127476905]support billing budget (#3537)
* support billing budget * add changelog * update --------- Co-authored-by: mikatong <[email protected]>
1 parent 00f1949 commit 1571636

18 files changed

+3012
-10
lines changed

.changelog/3537.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:new-data-source
2+
tencentcloud_billing_budget_operation_log
3+
```
4+
5+
```release-note:new-resource
6+
tencentcloud_billing_budget
7+
```

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ require (
107107

108108
require (
109109
github.com/hashicorp/go-uuid v1.0.3
110-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/billing v1.1.0
110+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/billing v1.1.31
111111
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdc v1.0.1149
112112
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdwdoris v1.0.993
113113
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdwpg v1.0.1126

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/bi v1.0.824 h1:DVKvZ6h+
833833
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/bi v1.0.824/go.mod h1:DvBpDX/qdJG4KKLeULmRvhAjPYiw8za0HeTSu2y/lFw=
834834
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/billing v1.1.0 h1:tfTh+fIqYPgWV2wi4oDvG963pi8tljtZ+5OdnhEqRnU=
835835
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/billing v1.1.0/go.mod h1:OKowLuHQjXN5pk3KnYnbmZLQucfBUUEpqWsa/3Cl+jY=
836+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/billing v1.1.31 h1:teA14cAcXuGxas6+5YANQ5KCoPaQBKIb2Q8+sk08Mu0=
837+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/billing v1.1.31/go.mod h1:1P9mCHgZ9pk/xftRsGC05XanvIk/sC5kLIy5jfD6DqU=
836838
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam v1.0.1071 h1:2CeivXYc7PtD2kgZEJXk9/SDQA9dzLejH5FmNzAW3QU=
837839
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam v1.0.1071/go.mod h1:UAjX3Vq52SQLKTe6o2HyGA/841Rnc+f4ZRATEsFHJDM=
838840
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam v1.1.27 h1:Jo/5JV8oiPbXKXQg2xv3ZeKxLst/xXl3nWUgXalq9eg=

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,7 @@ func Provider() *schema.Provider {
12501250
"tencentcloud_mqtt_instances": mqtt.DataSourceTencentCloudMqttInstances(),
12511251
"tencentcloud_mqtt_instance_detail": mqtt.DataSourceTencentCloudMqttInstanceDetail(),
12521252
"tencentcloud_mqtt_topics": mqtt.DataSourceTencentCloudMqttTopics(),
1253+
"tencentcloud_billing_budget_operation_log": billing.DataSourceTencentCloudBillingBudgetOperationLog(),
12531254
},
12541255

12551256
ResourcesMap: map[string]*schema.Resource{
@@ -2376,6 +2377,7 @@ func Provider() *schema.Provider {
23762377
"tencentcloud_mqtt_jwks_authenticator": mqtt.ResourceTencentCloudMqttJwksAuthenticator(),
23772378
"tencentcloud_mqtt_http_authenticator": mqtt.ResourceTencentCloudMqttHttpAuthenticator(),
23782379
"tencentcloud_billing_allocation_tag": billing.ResourceTencentCloudBillingAllocationTag(),
2380+
"tencentcloud_billing_budget": billing.ResourceTencentCloudBillingBudget(),
23792381
},
23802382

23812383
ConfigureFunc: providerConfigure,

tencentcloud/provider.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2349,5 +2349,8 @@ tencentcloud_mqtt_jwks_authenticator
23492349
tencentcloud_mqtt_http_authenticator
23502350

23512351
Billing
2352+
Data Source
2353+
tencentcloud_billing_budget_operation_log
23522354
Resource
2353-
tencentcloud_billing_allocation_tag
2355+
tencentcloud_billing_allocation_tag
2356+
tencentcloud_billing_budget
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Use this data source to query detailed information of billing billing_budget_operation_log
2+
3+
Example Usage
4+
5+
```hcl
6+
data "tencentcloud_billing_budget_operation_log" "billing_budget_operation_log" {
7+
budget_id = "1971489821259956225"
8+
}
9+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package billing_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
8+
tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest"
9+
)
10+
11+
func TestAccTencentCloudBillingBudgetOperationLogDataSource_basic(t *testing.T) {
12+
t.Parallel()
13+
resource.Test(t, resource.TestCase{
14+
PreCheck: func() {
15+
tcacctest.AccPreCheck(t)
16+
},
17+
Providers: tcacctest.AccProviders,
18+
Steps: []resource.TestStep{{
19+
Config: testAccBillingBudgetOperationLogDataSource,
20+
Check: resource.ComposeTestCheckFunc(tcacctest.AccCheckTencentCloudDataSourceID("data.tencentcloud_billing_budget_operation_log.billing_budget_operation_log")),
21+
}},
22+
})
23+
}
24+
25+
const testAccBillingBudgetOperationLogDataSource = `
26+
data "tencentcloud_billing_budget_operation_log" "billing_budget_operation_log" {
27+
budget_id = "1971489821259956225"
28+
}
29+
`

0 commit comments

Comments
 (0)