Skip to content

Commit b6581d1

Browse files
authored
add spt feature (#88)
1 parent 345c698 commit b6581d1

11 files changed

+87
-18
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
## 1.25.0 (Unreleased)
2+
3+
ENHANCEMENTS:
4+
5+
* resource/ucloud_lb: add `listen_type` to the argument. [GH-88]
6+
* datasource/ucloud_subnets: add `vpc_id` to the attributes. [GH-88]
7+
28
## 1.24.1 (2020-11-06)
39

410
BUG FIXES:

ucloud/config.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path/filepath"
99
"runtime"
10+
"time"
1011

1112
"github.com/ucloud/ucloud-sdk-go/external"
1213
"github.com/ucloud/ucloud-sdk-go/private/protocol/http"
@@ -62,7 +63,7 @@ func (c *Config) Client() (*UCloudClient, error) {
6263
// enable auto retry with http/connection error
6364
cfg.MaxRetries = c.MaxRetries
6465
cfg.LogLevel = log.PanicLevel
65-
cfg.UserAgent = "Terraform-UCloud/1.24.1"
66+
cfg.UserAgent = "Terraform-UCloud/1.25.0"
6667
cfg.BaseUrl = c.BaseURL
6768

6869
cred := auth.NewCredential()
@@ -116,20 +117,23 @@ func (c *Config) Client() (*UCloudClient, error) {
116117
}
117118

118119
// initialize client connections
119-
client.uhostconn = uhost.NewClient(&cfg, &cred)
120120
client.unetconn = unet.NewClient(&cfg, &cred)
121121
client.ulbconn = ulb.NewClient(&cfg, &cred)
122122
client.vpcconn = vpc.NewClient(&cfg, &cred)
123123
client.uaccountconn = uaccount.NewClient(&cfg, &cred)
124124
client.udiskconn = udisk.NewClient(&cfg, &cred)
125125
client.udpnconn = udpn.NewClient(&cfg, &cred)
126-
client.udbconn = udb.NewClient(&cfg, &cred)
127126
client.umemconn = umem.NewClient(&cfg, &cred)
128127
client.ipsecvpnClient = ipsecvpn.NewClient(&cfg, &cred)
129128

130129
// initialize client connections for private usage
131130
client.pumemconn = pumem.NewClient(&cfg, &cred)
132131

132+
longtimeCfg := cfg
133+
longtimeCfg.Timeout = 60 * time.Second
134+
client.udbconn = udb.NewClient(&longtimeCfg, &cred)
135+
client.uhostconn = uhost.NewClient(&longtimeCfg, &cred)
136+
133137
if cloudShellCredHandler != nil {
134138
client.uhostconn.AddHttpRequestHandler(cloudShellCredHandler)
135139
client.unetconn.AddHttpRequestHandler(cloudShellCredHandler)

ucloud/data_source_ucloud_subnets.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ func dataSourceUCloudSubnets() *schema.Resource {
5757
Computed: true,
5858
Elem: &schema.Resource{
5959
Schema: map[string]*schema.Schema{
60-
6160
"id": {
6261
Type: schema.TypeString,
6362
Computed: true,
@@ -87,6 +86,11 @@ func dataSourceUCloudSubnets() *schema.Resource {
8786
Type: schema.TypeString,
8887
Computed: true,
8988
},
89+
90+
"vpc_id": {
91+
Type: schema.TypeString,
92+
Computed: true,
93+
},
9094
},
9195
},
9296
},
@@ -158,11 +162,11 @@ func dataSourceUCloudSubnetsRead(d *schema.ResourceData, meta interface{}) error
158162
}
159163

160164
func dataSourceUCloudSubnetsSave(d *schema.ResourceData, subnets []vpc.VPCSubnetInfoSet) error {
161-
ids := []string{}
162-
data := []map[string]interface{}{}
165+
ids := make([]string, 0)
166+
data := make([]map[string]interface{}, 0)
163167

164168
for _, subnet := range subnets {
165-
ids = append(ids, string(subnet.SubnetId))
169+
ids = append(ids, subnet.SubnetId)
166170

167171
data = append(data, map[string]interface{}{
168172
"id": subnet.SubnetId,
@@ -171,6 +175,7 @@ func dataSourceUCloudSubnetsSave(d *schema.ResourceData, subnets []vpc.VPCSubnet
171175
"remark": subnet.Remark,
172176
"tag": subnet.Tag,
173177
"cidr_block": fmt.Sprintf("%s/%s", subnet.Subnet, subnet.Netmask),
178+
"vpc_id": subnet.VPCId,
174179
})
175180
}
176181

@@ -182,7 +187,9 @@ func dataSourceUCloudSubnetsSave(d *schema.ResourceData, subnets []vpc.VPCSubnet
182187
}
183188

184189
if outputFile, ok := d.GetOk("output_file"); ok && outputFile.(string) != "" {
185-
writeToFile(outputFile.(string), data)
190+
if err := writeToFile(outputFile.(string), data); err != nil {
191+
return err
192+
}
186193
}
187194

188195
return nil

ucloud/resource_ucloud_lb.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"time"
66

7+
"github.com/hashicorp/terraform-plugin-sdk/helper/customdiff"
8+
79
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
810
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
911
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
@@ -19,6 +21,9 @@ func resourceUCloudLB() *schema.Resource {
1921
Importer: &schema.ResourceImporter{
2022
State: schema.ImportStatePassthrough,
2123
},
24+
CustomizeDiff: customdiff.All(
25+
diffValidateInternalWithSubnetId,
26+
),
2227

2328
Schema: map[string]*schema.Schema{
2429
"internal": {
@@ -81,6 +86,17 @@ func resourceUCloudLB() *schema.Resource {
8186
Computed: true,
8287
},
8388

89+
"listen_type": {
90+
Type: schema.TypeString,
91+
Optional: true,
92+
ForceNew: true,
93+
Computed: true,
94+
ValidateFunc: validation.StringInSlice([]string{
95+
"request_proxy",
96+
"packets_transmit",
97+
}, false),
98+
},
99+
84100
"ip_set": {
85101
Type: schema.TypeList,
86102
Computed: true,
@@ -124,6 +140,10 @@ func resourceUCloudLBCreate(d *schema.ResourceData, meta interface{}) error {
124140

125141
req := conn.NewCreateULBRequest()
126142

143+
if v, ok := d.GetOk("listen_type"); ok {
144+
req.ListenType = ucloud.String(upperCamelCvt.unconvert(v.(string)))
145+
}
146+
127147
if v, ok := d.GetOk("name"); ok {
128148
req.ULBName = ucloud.String(v.(string))
129149
} else {
@@ -265,6 +285,11 @@ func resourceUCloudLBRead(d *schema.ResourceData, meta interface{}) error {
265285
return fmt.Errorf("error on reading lb %q, %s", d.Id(), err)
266286
}
267287

288+
listenType := upperCamelCvt.convert(lbSet.ListenType)
289+
if listenType == "request_proxy" || listenType == "packets_transmit" {
290+
d.Set("listen_type", listenType)
291+
}
292+
268293
d.Set("name", lbSet.Name)
269294
d.Set("tag", lbSet.Tag)
270295
d.Set("remark", lbSet.Remark)
@@ -351,3 +376,21 @@ func lbWaitForState(client *UCloudClient, id string) *resource.StateChangeConf {
351376
},
352377
}
353378
}
379+
380+
func diffValidateInternalWithSubnetId(diff *schema.ResourceDiff, meta interface{}) error {
381+
var internal bool
382+
var subnetId string
383+
384+
if v, ok := diff.GetOk("internal"); ok {
385+
internal = v.(bool)
386+
}
387+
if v, ok := diff.GetOk("subnet_id"); ok {
388+
subnetId = v.(string)
389+
}
390+
391+
if !internal && subnetId != "" {
392+
return fmt.Errorf("the lb instance cannot set %q, When the %q is true", "subnet_id", "internal")
393+
}
394+
395+
return nil
396+
}

ucloud/resource_ucloud_lb_listener.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,23 @@ func resourceUCloudLBListenerCreate(d *schema.ResourceData, meta interface{}) er
154154

155155
req := conn.NewCreateVServerRequest()
156156
if v, ok := d.GetOk("listen_type"); ok {
157+
if isStringIn(upperCamelCvt.convert(lbSet.ListenType), []string{"request_proxy", "packets_transmit"}) && upperCamelCvt.convert(lbSet.ListenType) != v.(string) {
158+
return fmt.Errorf("the %q of lb listenr must be same as the lb's %q, got %q", "listen_type", upperCamelCvt.convert(lbSet.ListenType), v.(string))
159+
}
157160
err := availableLBChoices.validate(lbSet.ULBType, protocol, v.(string))
158161
if err != nil {
159162
return err
160163
}
161164
req.ListenType = ucloud.String(upperCamelCvt.unconvert(v.(string)))
162165
} else {
163166
if choices := availableLBChoices.availableChoices(lbSet.ULBType, protocol); len(choices) == 0 {
164-
return fmt.Errorf("The protocol can only be one of %q, %q when lb is intranet, got %q", "tcp", "udp", protocol)
167+
return fmt.Errorf("the protocol can only be one of %q, %q when lb is intranet, got %q", "tcp", "udp", protocol)
165168
} else {
166-
req.ListenType = ucloud.String(upperCamelCvt.unconvert(choices[0]))
169+
if isStringIn(upperCamelCvt.convert(lbSet.ListenType), []string{"request_proxy", "packets_transmit"}) {
170+
req.ListenType = ucloud.String(lbSet.ListenType)
171+
} else {
172+
req.ListenType = ucloud.String(upperCamelCvt.unconvert(choices[0]))
173+
}
167174
}
168175
}
169176

ucloud/resource_ucloud_lb_ssl_attachment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func resourceUCloudLBSSLAttachmentCreate(d *schema.ResourceData, meta interface{
6565
sslSet, err := client.describeLBSSLAttachmentById(sslId, lbId, listenerId)
6666
if err != nil {
6767
if isNotFoundError(err) {
68-
return sslSet, statusPending, nil
68+
return nil, statusPending, nil
6969
}
7070
return nil, "", err
7171
}

ucloud/resource_ucloud_memcache_instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ func (c *UCloudClient) waitActiveStandbyMemcacheRunning(id string) error {
311311
}
312312

313313
if resp.State != statusRunning {
314-
return nil, statusPending, nil
314+
return resp, statusPending, nil
315315
}
316316
return resp, statusInitialized, nil
317317
}

ucloud/resource_ucloud_redis_instance.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ func (c *UCloudClient) waitActiveStandbyRedisRunning(id string) error {
581581
}
582582

583583
if resp.State != statusRunning {
584-
return nil, statusPending, nil
584+
return resp, statusPending, nil
585585
}
586586
return resp, statusInitialized, nil
587587
}
@@ -600,7 +600,7 @@ func (c *UCloudClient) waitDistributedRedisRunning(id string) error {
600600
}
601601

602602
if resp.State != statusRunning {
603-
return nil, statusPending, nil
603+
return resp, statusPending, nil
604604
}
605605
return resp, statusInitialized, nil
606606
}

ucloud/service_ucloud_umem.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ func (c *UCloudClient) describeActiveStandbyMemcacheById(id string) (*pumem.UMem
7474

7575
func waitForMemoryInstance(refresh func() (interface{}, string, error)) error {
7676
conf := resource.StateChangeConf{
77-
Timeout: 5 * time.Minute,
78-
Delay: 2 * time.Second,
79-
MinTimeout: 1 * time.Second,
77+
Timeout: 10 * time.Minute,
78+
Delay: 3 * time.Second,
79+
MinTimeout: 2 * time.Second,
8080
Target: []string{statusInitialized},
8181
Pending: []string{statusPending},
8282
Refresh: refresh,

website/docs/d/subnets.html.markdown

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@ The attribute (`subnets`) support the following:
4747
* `cidr_block` - The cidr block of the desired Subnet.
4848
* `create_time` - The time of creation of Subnet, formatted in RFC3339 time string.
4949
* `remark` - The remark of the Subnet.
50-
* `tag` - A tag assigned to Subnet.
50+
* `tag` - A tag assigned to Subnet.
51+
* `vpc_id` - The id of the VPC that the desired Subnet belongs to.

website/docs/r/lb.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ The following arguments are supported:
3131
* `tag` - (Optional) A tag assigned to load balancer, which contains at most 63 characters and only support Chinese, English, numbers, '-', '_', and '.'. If it is not filled in or a empty string is filled in, then default tag will be assigned. (Default: `Default`).
3232
* `remark` - (Optional) The remarks of the load balancer. (Default: `""`).
3333
* `security_group` - (Optional) The ID of the associated security group. The security_group only takes effect for ULB instances of request_proxy mode and extranet mode at present.
34+
* `listen_type` - (Optional, ForceNew) The type of listener. Possible values are `request_proxy` and `packets_transmit`. When `packets_transmit` was specified, you need to config the instances by yourself if the instances attach to the load balancer. You may refer to [configuration instruction](https://docs.ucloud.cn/network/ulb/fast/createulb/vservertype).
3435

3536
## Attributes Reference
3637

0 commit comments

Comments
 (0)