Skip to content

Commit 1db8272

Browse files
authored
feat(billing): [126756696] add new resource (#3507)
* add * add
1 parent 2997618 commit 1db8272

File tree

17 files changed

+13464
-11
lines changed

17 files changed

+13464
-11
lines changed

.changelog/3507.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
tencentcloud_billing_allocation_tag
3+
```

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ require (
312312
github.com/subosito/gotenv v1.4.1 // indirect
313313
github.com/t-yuki/gocover-cobertura v0.0.0-20180217150009-aaee18c8195c // indirect
314314
github.com/tdakkota/asciicheck v0.2.0 // indirect
315+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/billing v1.1.0 // indirect
315316
github.com/tetafro/godot v1.4.11 // indirect
316317
github.com/tidwall/gjson v1.17.0 // indirect
317318
github.com/tidwall/match v1.1.1 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as v1.1.4 h1:9EguBiOzGy
837837
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as v1.1.4/go.mod h1:jUF68ck1rxCqXkdmYRT2B2GtLrXAxmp7T0CcyWZ8BbM=
838838
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/bi v1.0.824 h1:DVKvZ6h+qd7tadUrCjVAkCCmE3TsbK2ZmwGd3AJcpWc=
839839
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/bi v1.0.824/go.mod h1:DvBpDX/qdJG4KKLeULmRvhAjPYiw8za0HeTSu2y/lFw=
840+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/billing v1.1.0 h1:tfTh+fIqYPgWV2wi4oDvG963pi8tljtZ+5OdnhEqRnU=
841+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/billing v1.1.0/go.mod h1:OKowLuHQjXN5pk3KnYnbmZLQucfBUUEpqWsa/3Cl+jY=
840842
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam v1.0.1071 h1:2CeivXYc7PtD2kgZEJXk9/SDQA9dzLejH5FmNzAW3QU=
841843
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam v1.0.1071/go.mod h1:UAjX3Vq52SQLKTe6o2HyGA/841Rnc+f4ZRATEsFHJDM=
842844
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cat v1.0.825 h1:TgO9L1yNPkWeXqrvys/9RL3u958xx9dcTAy4WmaxBnE=

tencentcloud/connectivity/client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
apm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apm/v20210622"
2525
as "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as/v20180419"
2626
bi "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/bi/v20220105"
27+
billing "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/billing/v20180709"
2728
cam "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam/v20190116"
2829
cat "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cat/v20180409"
2930
cbs "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs/v20170312"
@@ -237,6 +238,7 @@ type TencentCloudClient struct {
237238
mqttv20240516Conn *mqtt.Client
238239
cdwpgv20201230Conn *cdwpg.Client
239240
gwlbv20240906Conn *gwlb.Client
241+
billingv20180709Conn *billing.Client
240242
}
241243

242244
// NewClientProfile returns a new ClientProfile
@@ -2081,3 +2083,16 @@ func (me *TencentCloudClient) UseGwlbV20240906Client() *gwlb.Client {
20812083

20822084
return me.gwlbv20240906Conn
20832085
}
2086+
2087+
// UseBillingV20180709Client return BILLING client for service
2088+
func (me *TencentCloudClient) UseBillingV20180709Client() *billing.Client {
2089+
if me.billingv20180709Conn != nil {
2090+
return me.billingv20180709Conn
2091+
}
2092+
cpf := me.NewClientProfile(300)
2093+
cpf.Language = "zh-CN"
2094+
me.billingv20180709Conn, _ = billing.NewClient(me.Credential, me.Region, cpf)
2095+
me.billingv20180709Conn.WithHttpTransport(&LogRoundTripper{})
2096+
2097+
return me.billingv20180709Conn
2098+
}

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/audit"
2929
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/bh"
3030
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/bi"
31+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/billing"
3132
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/cam"
3233
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/cat"
3334
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/cbs"
@@ -2373,6 +2374,7 @@ func Provider() *schema.Provider {
23732374
"tencentcloud_mqtt_jwt_authenticator": mqtt.ResourceTencentCloudMqttJwtAuthenticator(),
23742375
"tencentcloud_mqtt_jwks_authenticator": mqtt.ResourceTencentCloudMqttJwksAuthenticator(),
23752376
"tencentcloud_mqtt_http_authenticator": mqtt.ResourceTencentCloudMqttHttpAuthenticator(),
2377+
"tencentcloud_billing_allocation_tag": billing.ResourceTencentCloudBillingAllocationTag(),
23762378
},
23772379

23782380
ConfigureFunc: providerConfigure,

tencentcloud/provider.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2350,3 +2350,7 @@ tencentcloud_mqtt_user
23502350
tencentcloud_mqtt_jwt_authenticator
23512351
tencentcloud_mqtt_jwks_authenticator
23522352
tencentcloud_mqtt_http_authenticator
2353+
2354+
Billing
2355+
Resource
2356+
tencentcloud_billing_allocation_tag
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package billing
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
billingv20180709 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/billing/v20180709"
11+
12+
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
13+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
14+
)
15+
16+
func ResourceTencentCloudBillingAllocationTag() *schema.Resource {
17+
return &schema.Resource{
18+
Create: resourceTencentCloudBillingAllocationTagCreate,
19+
Read: resourceTencentCloudBillingAllocationTagRead,
20+
Delete: resourceTencentCloudBillingAllocationTagDelete,
21+
Importer: &schema.ResourceImporter{
22+
State: schema.ImportStatePassthrough,
23+
},
24+
Schema: map[string]*schema.Schema{
25+
"tag_key": {
26+
Type: schema.TypeString,
27+
Required: true,
28+
ForceNew: true,
29+
Description: "Cost allocation tag key.",
30+
},
31+
32+
// computed
33+
"status": {
34+
Type: schema.TypeInt,
35+
Computed: true,
36+
Description: "Tag type, 0 normal tag, 1 account tag.",
37+
},
38+
},
39+
}
40+
}
41+
42+
func resourceTencentCloudBillingAllocationTagCreate(d *schema.ResourceData, meta interface{}) error {
43+
defer tccommon.LogElapsed("resource.tencentcloud_billing_allocation_tag.create")()
44+
defer tccommon.InconsistentCheck(d, meta)()
45+
46+
var (
47+
logId = tccommon.GetLogId(tccommon.ContextNil)
48+
ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
49+
request = billingv20180709.NewCreateAllocationTagRequest()
50+
tagKey string
51+
)
52+
53+
if v, ok := d.GetOk("tag_key"); ok {
54+
tagKey = v.(string)
55+
request.TagKey = append(request.TagKey, helper.String(tagKey))
56+
}
57+
58+
reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
59+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseBillingV20180709Client().CreateAllocationTagWithContext(ctx, request)
60+
if e != nil {
61+
return tccommon.RetryError(e)
62+
} else {
63+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
64+
}
65+
66+
if result == nil || result.Response == nil {
67+
return resource.NonRetryableError(fmt.Errorf("Create billing allocation tag failed, Response is nil."))
68+
}
69+
70+
return nil
71+
})
72+
73+
if reqErr != nil {
74+
log.Printf("[CRITAL]%s create billing allocation tag failed, reason:%+v", logId, reqErr)
75+
return reqErr
76+
}
77+
78+
d.SetId(tagKey)
79+
return resourceTencentCloudBillingAllocationTagRead(d, meta)
80+
}
81+
82+
func resourceTencentCloudBillingAllocationTagRead(d *schema.ResourceData, meta interface{}) error {
83+
defer tccommon.LogElapsed("resource.tencentcloud_billing_allocation_tag.read")()
84+
defer tccommon.InconsistentCheck(d, meta)()
85+
86+
var (
87+
logId = tccommon.GetLogId(tccommon.ContextNil)
88+
ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
89+
service = BillingService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
90+
tagKey = d.Id()
91+
)
92+
93+
respData, err := service.DescribeBillingAllocationTagById(ctx, tagKey)
94+
if err != nil {
95+
return err
96+
}
97+
98+
if respData == nil {
99+
log.Printf("[WARN]%s resource `tencentcloud_billing_allocation_tag` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
100+
d.SetId("")
101+
return nil
102+
}
103+
104+
if respData.TagKey != nil {
105+
_ = d.Set("tag_key", respData.TagKey)
106+
}
107+
108+
if respData.Status != nil {
109+
_ = d.Set("status", respData.Status)
110+
}
111+
112+
return nil
113+
}
114+
115+
func resourceTencentCloudBillingAllocationTagDelete(d *schema.ResourceData, meta interface{}) error {
116+
defer tccommon.LogElapsed("resource.tencentcloud_billing_allocation_tag.delete")()
117+
defer tccommon.InconsistentCheck(d, meta)()
118+
119+
var (
120+
logId = tccommon.GetLogId(tccommon.ContextNil)
121+
ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
122+
request = billingv20180709.NewDeleteAllocationTagRequest()
123+
tagKey = d.Id()
124+
)
125+
126+
request.TagKey = append(request.TagKey, helper.String(tagKey))
127+
reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
128+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseBillingV20180709Client().DeleteAllocationTagWithContext(ctx, request)
129+
if e != nil {
130+
return tccommon.RetryError(e)
131+
} else {
132+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
133+
}
134+
135+
return nil
136+
})
137+
138+
if reqErr != nil {
139+
log.Printf("[CRITAL]%s delete billing allocation tag failed, reason:%+v", logId, reqErr)
140+
return reqErr
141+
}
142+
143+
return nil
144+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Provides a resource to create a Billing allocation tag
2+
3+
Example Usage
4+
5+
```hcl
6+
resource "tencentcloud_tag" "example" {
7+
tag_key = "tagKey"
8+
tag_value = "tagValue"
9+
}
10+
11+
resource "tencentcloud_billing_allocation_tag" "example" {
12+
tag_key = tencentcloud_tag.example.tag_key
13+
}
14+
```
15+
16+
Import
17+
18+
Billing allocation tag can be imported using the id, e.g.
19+
20+
```
21+
terraform import tencentcloud_billing_allocation_tag.example tagKey
22+
```
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 TestAccTencentCloudBillingAllocationTagResource_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+
{
20+
Config: testAccBillingAllocationTag,
21+
Check: resource.ComposeTestCheckFunc(
22+
resource.TestCheckResourceAttrSet("tencentcloud_billing_allocation_tag.example", "id"),
23+
),
24+
},
25+
{
26+
ResourceName: "tencentcloud_billing_allocation_tag.example",
27+
ImportState: true,
28+
ImportStateVerify: true,
29+
},
30+
},
31+
})
32+
}
33+
34+
const testAccBillingAllocationTag = `
35+
resource "tencentcloud_tag" "example" {
36+
tag_key = "tagKey"
37+
tag_value = "tagValue"
38+
}
39+
40+
resource "tencentcloud_billing_allocation_tag" "example" {
41+
tag_key = tencentcloud_tag.example.tag_key
42+
}
43+
`
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package billing
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
billingv20180709 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/billing/v20180709"
10+
11+
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
12+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/connectivity"
13+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
14+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/ratelimit"
15+
)
16+
17+
func NewBillingService(client *connectivity.TencentCloudClient) BillingService {
18+
return BillingService{client: client}
19+
}
20+
21+
type BillingService struct {
22+
client *connectivity.TencentCloudClient
23+
}
24+
25+
func (me *BillingService) DescribeBillingAllocationTagById(ctx context.Context, tagKey string) (ret *billingv20180709.TagDataInfo, errRet error) {
26+
logId := tccommon.GetLogId(ctx)
27+
28+
request := billingv20180709.NewDescribeTagListRequest()
29+
response := billingv20180709.NewDescribeTagListResponse()
30+
request.TagKey = helper.String(tagKey)
31+
request.Limit = helper.IntUint64(1)
32+
request.Offset = helper.IntUint64(0)
33+
34+
defer func() {
35+
if errRet != nil {
36+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
37+
}
38+
}()
39+
40+
var (
41+
limit uint64 = 1000
42+
offset uint64 = 0
43+
dataList []*billingv20180709.TagDataInfo
44+
)
45+
46+
for {
47+
request.Offset = &offset
48+
request.Limit = &limit
49+
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
50+
ratelimit.Check(request.GetAction())
51+
result, e := me.client.UseBillingV20180709Client().DescribeTagList(request)
52+
if e != nil {
53+
return tccommon.RetryError(e)
54+
} else {
55+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
56+
}
57+
58+
if result == nil || result.Response == nil {
59+
return resource.NonRetryableError(fmt.Errorf("Describe billing allocation tag failed, Response is nil."))
60+
}
61+
62+
response = result
63+
return nil
64+
})
65+
66+
if err != nil {
67+
errRet = err
68+
return
69+
}
70+
71+
if response == nil || len(response.Response.Data) < 1 {
72+
break
73+
74+
}
75+
dataList = append(dataList, response.Response.Data...)
76+
if len(response.Response.Data) < int(limit) {
77+
break
78+
}
79+
80+
offset += limit
81+
}
82+
83+
for _, item := range dataList {
84+
if item.TagKey != nil && *item.TagKey == tagKey {
85+
ret = item
86+
break
87+
}
88+
}
89+
90+
return
91+
}

0 commit comments

Comments
 (0)