Skip to content

Commit cd6b7b4

Browse files
committed
feat: vpc support
1 parent ac5f7e6 commit cd6b7b4

8 files changed

+179
-135
lines changed

tencentcloud/provider.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ Virtual Private Cloud(VPC)
611611
tencentcloud_ha_vip
612612
tencentcloud_ha_vip_eip_attachment
613613
tencentcloud_vpc_bandwidth_package
614-
tencentcloud_vpc_bandwidth_package_resources
614+
tencentcloud_vpc_bandwidth_package_attachment
615615
616616
VPN
617617
Data Source
@@ -1259,10 +1259,10 @@ func Provider() terraform.ResourceProvider {
12591259
"tencentcloud_teo_custom_error_page": resourceTencentCloudTeoCustomErrorPage(),
12601260
// "tencentcloud_teo_host_certificate": resourceTencentCloudTeoHostCertificate(),
12611261
// "tencentcloud_teo_default_certificate": resourceTencentCloudTeoDefaultCertificate(),
1262-
"tencentcloud_tcm_mesh": resourceTencentCloudTcmMesh(),
1263-
"tencentcloud_tcm_cluster_attachment": resourceTencentCloudTcmClusterAttachment(),
1264-
"tencentcloud_vpc_bandwidth_package": resourceTencentCloudVpcBandwidthPackage(),
1265-
"tencentcloud_vpc_bandwidth_package_resources": resourceTencentCloudVpcBandwidthPackageResources(),
1262+
"tencentcloud_tcm_mesh": resourceTencentCloudTcmMesh(),
1263+
"tencentcloud_tcm_cluster_attachment": resourceTencentCloudTcmClusterAttachment(),
1264+
"tencentcloud_vpc_bandwidth_package": resourceTencentCloudVpcBandwidthPackage(),
1265+
"tencentcloud_vpc_bandwidth_package_attachment": resourceTencentCloudVpcBandwidthPackageAttachment(),
12661266
},
12671267

12681268
ConfigureFunc: providerConfigure,

tencentcloud/resource_tc_vpc_bandwidth_package.go

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030

3131
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
3232
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
33-
bwp "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
33+
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
3434
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
3535
)
3636

@@ -78,8 +78,8 @@ func resourceTencentCloudVpcBandwidthPackageCreate(d *schema.ResourceData, meta
7878
logId := getLogId(contextNil)
7979

8080
var (
81-
request = bwp.NewCreateBandwidthPackageRequest()
82-
response *bwp.CreateBandwidthPackageResponse
81+
request = vpc.NewCreateBandwidthPackageRequest()
82+
response *vpc.CreateBandwidthPackageResponse
8383
)
8484

8585
if v, ok := d.GetOk("network_type"); ok {
@@ -188,22 +188,68 @@ func resourceTencentCloudVpcBandwidthPackageRead(d *schema.ResourceData, meta in
188188
}
189189

190190
func resourceTencentCloudVpcBandwidthPackageUpdate(d *schema.ResourceData, meta interface{}) error {
191-
defer logElapsed("resource.tencentcloud_bwp_bandwidth_package.update")()
191+
defer logElapsed("resource.tencentcloud_vpc_bandwidth_package.update")()
192192
defer inconsistentCheck(d, meta)()
193193

194194
logId := getLogId(contextNil)
195195
ctx := context.WithValue(context.TODO(), logIdKey, logId)
196196

197+
request := vpc.NewModifyBandwidthPackageAttributeRequest()
198+
199+
bandwidthPackageId := d.Id()
200+
201+
request.BandwidthPackageId = &bandwidthPackageId
202+
197203
if d.HasChange("network_type") {
204+
198205
return fmt.Errorf("`network_type` do not support change now.")
206+
207+
}
208+
209+
if d.HasChange("bandwidth_package_count") {
210+
211+
return fmt.Errorf("`bandwidth_package_count` do not support change now.")
212+
213+
}
214+
215+
if d.HasChange("internet_max_bandwidth") {
216+
217+
return fmt.Errorf("`internet_max_bandwidth` do not support change now.")
218+
219+
}
220+
221+
if d.HasChange("protocol") {
222+
223+
return fmt.Errorf("`protocol` do not support change now.")
224+
199225
}
200226

201227
if d.HasChange("charge_type") {
202-
return fmt.Errorf("`charge_type` do not support change now.")
228+
if v, ok := d.GetOk("charge_type"); ok {
229+
request.ChargeType = helper.String(v.(string))
230+
}
203231
}
204232

205233
if d.HasChange("bandwidth_package_name") {
206-
return fmt.Errorf("`bandwidth_package_name` do not support change now.")
234+
if v, ok := d.GetOk("bandwidth_package_name"); ok {
235+
request.BandwidthPackageName = helper.String(v.(string))
236+
}
237+
}
238+
239+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
240+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseVpcClient().ModifyBandwidthPackageAttribute(request)
241+
if e != nil {
242+
return retryError(e)
243+
} else {
244+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
245+
logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
246+
}
247+
return nil
248+
})
249+
250+
if err != nil {
251+
log.Printf("[CRITAL]%s create vpc bandwidthPackage failed, reason:%+v", logId, err)
252+
return err
207253
}
208254

209255
if d.HasChange("tags") {

tencentcloud/resource_tc_vpc_bandwidth_package_resources.go renamed to tencentcloud/resource_tc_vpc_bandwidth_package_attachment.go

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
/*
2-
Provides a resource to create a vpc bandwidth_package_resources
2+
Provides a resource to create a vpc bandwidth_package_attachment
33
44
Example Usage
55
66
```hcl
7-
resource "tencentcloud_vpc_bandwidth_package_resources" "bandwidth_package_resources" {
7+
resource "tencentcloud_vpc_bandwidth_package_attachment" "bandwidth_package_attachment" {
88
resource_ids = "lb-dv1ai6ma"
99
bandwidth_package_id = "bwp-atmf0p9g"
1010
network_type = "BGP"
1111
resource_type = "LoadBalance"
1212
protocol = ""
1313
}
1414
15+
```
16+
Import
17+
18+
vpc bandwidth_package_attachment can be imported using the id, e.g.
19+
```
20+
$ terraform import tencentcloud_vpc_bandwidth_package_attachment.bandwidth_package_attachment bandwidthPackageAttachment_id
1521
```
1622
*/
1723
package tencentcloud
@@ -28,67 +34,62 @@ import (
2834
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
2935
)
3036

31-
func resourceTencentCloudVpcBandwidthPackageResources() *schema.Resource {
37+
func resourceTencentCloudVpcBandwidthPackageAttachment() *schema.Resource {
3238
return &schema.Resource{
33-
Read: resourceTencentCloudVpcBandwidthPackageResourcesRead,
34-
Create: resourceTencentCloudVpcBandwidthPackageResourcesCreate,
35-
Delete: resourceTencentCloudVpcBandwidthPackageResourcesDelete,
39+
Read: resourceTencentCloudVpcBandwidthPackageAttachmentRead,
40+
Create: resourceTencentCloudVpcBandwidthPackageAttachmentCreate,
41+
Delete: resourceTencentCloudVpcBandwidthPackageAttachmentDelete,
3642
Importer: &schema.ResourceImporter{
3743
State: schema.ImportStatePassthrough,
3844
},
3945
Schema: map[string]*schema.Schema{
40-
"resource_ids": {
46+
"resource_id": {
4147
Type: schema.TypeString,
4248
Required: true,
43-
ForceNew: true,
4449
Description: "The unique ID of the resource, currently supports EIP resources and LB resources, such as `eip-xxxx`, `lb-xxxx`.",
4550
},
4651

4752
"bandwidth_package_id": {
4853
Type: schema.TypeString,
4954
Optional: true,
50-
ForceNew: true,
5155
Description: "Bandwidth package unique ID, in the form of `bwp-xxxx`.",
5256
},
5357

5458
"network_type": {
5559
Type: schema.TypeString,
5660
Optional: true,
57-
ForceNew: true,
5861
Description: "Bandwidth packet type, currently supports `BGP` type, indicating that the internal resource is BGP IP.",
5962
},
6063

6164
"resource_type": {
6265
Type: schema.TypeString,
6366
Optional: true,
64-
ForceNew: true,
6567
Description: "Resource types, including `Address`, `LoadBalance`.",
6668
},
6769

6870
"protocol": {
6971
Type: schema.TypeString,
7072
Optional: true,
71-
ForceNew: true,
7273
Description: "Bandwidth packet protocol type. Currently `ipv4` and `ipv6` protocol types are supported.",
7374
},
7475
},
7576
}
7677
}
7778

78-
func resourceTencentCloudVpcBandwidthPackageResourcesCreate(d *schema.ResourceData, meta interface{}) error {
79-
defer logElapsed("resource.tencentcloud_vpc_bandwidth_package_resources.create")()
79+
func resourceTencentCloudVpcBandwidthPackageAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
80+
defer logElapsed("resource.tencentcloud_vpc_bandwidth_package_attachment.create")()
8081
defer inconsistentCheck(d, meta)()
8182

8283
logId := getLogId(contextNil)
8384

8485
var (
8586
request = vpc.NewAddBandwidthPackageResourcesRequest()
86-
resourceId string
87+
response *vpc.AddBandwidthPackageResourcesResponse
8788
bandwidthPackageId string
88-
resourceType string
89+
resourceId string
8990
)
9091

91-
if v, ok := d.GetOk("resource_ids"); ok {
92+
if v, ok := d.GetOk("resource_id"); ok {
9293
resourceId = v.(string)
9394
request.ResourceIds = append(request.ResourceIds, helper.String(v.(string)))
9495
}
@@ -99,11 +100,12 @@ func resourceTencentCloudVpcBandwidthPackageResourcesCreate(d *schema.ResourceDa
99100
}
100101

101102
if v, ok := d.GetOk("network_type"); ok {
103+
102104
request.NetworkType = helper.String(v.(string))
103105
}
104106

105107
if v, ok := d.GetOk("resource_type"); ok {
106-
resourceType = v.(string)
108+
107109
request.ResourceType = helper.String(v.(string))
108110
}
109111

@@ -120,87 +122,84 @@ func resourceTencentCloudVpcBandwidthPackageResourcesCreate(d *schema.ResourceDa
120122
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
121123
logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
122124
}
125+
response = result
123126
return nil
124127
})
125128

126129
if err != nil {
127-
log.Printf("[CRITAL]%s create vpc bandwidthPackageResources failed, reason:%+v", logId, err)
130+
log.Printf("[CRITAL]%s create vpc bandwidthPackageAttachment failed, reason:%+v", logId, err)
128131
return err
129132
}
130133

131-
d.SetId(strings.Join([]string{bandwidthPackageId, resourceId, resourceType}, FILED_SP))
132-
return resourceTencentCloudVpcBandwidthPackageResourcesRead(d, meta)
134+
d.SetId(bandwidthPackageId + FILED_SP + resourceId)
135+
return resourceTencentCloudVpcBandwidthPackageAttachmentRead(d, meta)
133136
}
134137

135-
func resourceTencentCloudVpcBandwidthPackageResourcesRead(d *schema.ResourceData, meta interface{}) error {
136-
defer logElapsed("resource.tencentcloud_vpc_bandwidth_package_resources.read")()
138+
func resourceTencentCloudVpcBandwidthPackageAttachmentRead(d *schema.ResourceData, meta interface{}) error {
139+
defer logElapsed("resource.tencentcloud_vpc_bandwidth_package_attachment.read")()
137140
defer inconsistentCheck(d, meta)()
138141

139142
logId := getLogId(contextNil)
140143
ctx := context.WithValue(context.TODO(), logIdKey, logId)
141144

142145
service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
143146

144-
ids := strings.Split(d.Id(), FILED_SP)
145-
if len(ids) != 3 {
146-
return fmt.Errorf("id is broken, id is %s", d.Id())
147+
idSplit := strings.Split(d.Id(), FILED_SP)
148+
if len(idSplit) != 2 {
149+
return fmt.Errorf("id is broken,%s", d.Id())
147150
}
151+
bandwidthPackageId := idSplit[0]
152+
resourceId := idSplit[1]
148153

149-
bandwidthPackageId := ids[0]
150-
resourceId := ids[1]
151-
resourceType := ids[2]
152-
153-
bandwidthPackageResources, err := service.DescribeVpcBandwidthPackageResources(ctx, bandwidthPackageId, resourceId, resourceType)
154+
bandwidthPackageAttachment, err := service.DescribeVpcBandwidthPackageAttachment(ctx, bandwidthPackageId, resourceId)
154155

155156
if err != nil {
156157
return err
157158
}
158159

159-
if bandwidthPackageResources == nil {
160+
if bandwidthPackageAttachment == nil {
160161
d.SetId("")
161-
return fmt.Errorf("resource `bandwidthPackageResources` %s does not exist", resourceId)
162+
return fmt.Errorf("resource `bandwidthPackageAttachment` %s does not exist", bandwidthPackageId)
162163
}
163164

164165
_ = d.Set("bandwidth_package_id", bandwidthPackageId)
165166

166-
if bandwidthPackageResources.ResourceId != nil {
167-
_ = d.Set("resource_ids", bandwidthPackageResources.ResourceId)
167+
if bandwidthPackageAttachment.ResourceId != nil {
168+
_ = d.Set("resource_id", bandwidthPackageAttachment.ResourceId)
168169
}
169170

170-
//if bandwidthPackageResources.NetworkType != nil {
171-
// _ = d.Set("network_type", bandwidthPackageResources.NetworkType)
171+
//if bandwidthPackageAttachment.NetworkType != nil {
172+
// _ = d.Set("network_type", bandwidthPackageAttachment.NetworkType)
172173
//}
173174

174-
if bandwidthPackageResources.ResourceType != nil {
175-
_ = d.Set("resource_type", bandwidthPackageResources.ResourceType)
175+
if bandwidthPackageAttachment.ResourceType != nil {
176+
_ = d.Set("resource_type", bandwidthPackageAttachment.ResourceType)
176177
}
177178

178-
//if bandwidthPackageResources.Protocol != nil {
179-
// _ = d.Set("protocol", bandwidthPackageResources.Protocol)
179+
//if bandwidthPackageAttachment.Protocol != nil {
180+
// _ = d.Set("protocol", bandwidthPackageAttachment.Protocol)
180181
//}
181182

182183
return nil
183184
}
184185

185-
func resourceTencentCloudVpcBandwidthPackageResourcesDelete(d *schema.ResourceData, meta interface{}) error {
186-
defer logElapsed("resource.tencentcloud_vpc_bandwidth_package_resources.delete")()
186+
func resourceTencentCloudVpcBandwidthPackageAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
187+
defer logElapsed("resource.tencentcloud_vpc_bandwidth_package_attachment.delete")()
187188
defer inconsistentCheck(d, meta)()
188189

189190
logId := getLogId(contextNil)
190191
ctx := context.WithValue(context.TODO(), logIdKey, logId)
191192

192193
service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
193194

194-
ids := strings.Split(d.Id(), FILED_SP)
195-
if len(ids) != 3 {
196-
return fmt.Errorf("id is broken, id is %s", d.Id())
195+
idSplit := strings.Split(d.Id(), FILED_SP)
196+
if len(idSplit) != 2 {
197+
return fmt.Errorf("id is broken,%s", d.Id())
197198
}
199+
bandwidthPackageId := idSplit[0]
200+
resourceId := idSplit[1]
198201

199-
bandwidthPackageId := ids[0]
200-
resourceId := ids[1]
201-
resourceType := ids[2]
202-
203-
if err := service.DeleteVpcBandwidthPackageResourcesById(ctx, bandwidthPackageId, resourceId, resourceType); err != nil {
202+
if err := service.DeleteVpcBandwidthPackageAttachmentById(ctx, bandwidthPackageId, resourceId); err != nil {
204203
return err
205204
}
206205

0 commit comments

Comments
 (0)