Skip to content

Commit 2a977aa

Browse files
authored
add resource type (#110)
1 parent fbffb74 commit 2a977aa

8 files changed

+75
-54
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
## 1.31.0 (Unreleased)
2+
3+
ENHANCEMENTS:
4+
5+
* resource/ucloud_eip_association: add `resource_type` to argument. [GH-110]
6+
* resource/ucloud_lb_attachment: add `resource_type` to argument. [GH-110]
7+
8+
29
## 1.30.0 (2021-10-26)
310

411
FEATURES:

ucloud/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (c *Config) Client() (*UCloudClient, error) {
6868
// enable auto retry with http/connection error
6969
cfg.MaxRetries = c.MaxRetries
7070
cfg.LogLevel = log.PanicLevel
71-
cfg.UserAgent = "Terraform-UCloud/1.30.0"
71+
cfg.UserAgent = "Terraform-UCloud/1.31.0"
7272
cfg.BaseUrl = c.BaseURL
7373

7474
cred := auth.NewCredential()

ucloud/consts.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ const (
9292
)
9393

9494
const (
95+
resourceTypeInstance = "instance"
96+
resourceTypeLb = "lb"
9597
lbResourceTypeUHost = "UHost"
9698
lbMatchTypePath = "Path"
9799
lbMatchTypeDomain = "Domain"
@@ -127,6 +129,11 @@ var lowerCaseProdCvt = newStringConverter(map[string]string{
127129
"lb": "ulb",
128130
})
129131

132+
// lbBackendCaseProdCvt is used to covert one backend string to another backend string
133+
var lbBackendCaseProdCvt = newStringConverter(map[string]string{
134+
"instance": "UHost",
135+
})
136+
130137
// titleCaseProdCvt is used to covert one lower string to another string begin with uppercase letters
131138
var titleCaseProdCvt = newStringConverter(map[string]string{
132139
"instance": "UHost",

ucloud/resource_ucloud_eip_association.go

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,17 @@ func resourceUCloudEIPAssociation() *schema.Resource {
2727
},
2828

2929
"resource_type": {
30-
Type: schema.TypeString,
31-
Optional: true,
32-
ForceNew: true,
33-
Deprecated: "attribute `resource_type` is deprecated for optimizing parameters",
34-
//ValidateFunc: validation.StringInSlice([]string{"instance", "lb"}, false),
30+
Type: schema.TypeString,
31+
Optional: true,
32+
ForceNew: true,
33+
Computed: true,
34+
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
35+
if (isStringIn(old, []string{resourceTypeInstance, eipResourceTypeUHost}) && isStringIn(new, []string{resourceTypeInstance, eipResourceTypeUHost})) ||
36+
(isStringIn(old, []string{resourceTypeLb, eipResourceTypeULB}) && isStringIn(new, []string{resourceTypeLb, eipResourceTypeULB})) {
37+
return true
38+
}
39+
return false
40+
},
3541
},
3642

3743
"resource_id": {
@@ -49,42 +55,17 @@ func resourceUCloudEIPAssociationCreate(d *schema.ResourceData, meta interface{}
4955

5056
eipId := d.Get("eip_id").(string)
5157
resourceId := d.Get("resource_id").(string)
52-
//resourceType := eipResourceTypeUHost
53-
//if strings.HasPrefix(resourceId, "ulb-") {
54-
// resourceType = eipResourceTypeULB
55-
//}
56-
//
57-
//if strings.HasPrefix(resourceId, "cube-") {
58-
// resourceType = eipResourceTypeCube
59-
//}
60-
//
61-
//if strings.HasPrefix(resourceId, "natgw-") {
62-
// resourceType = eipResourceTypeNatGateway
63-
//}
64-
6558
var resourceType string
66-
if strings.HasPrefix(resourceId, "uhost-") {
67-
resourceType = eipResourceTypeUHost
68-
}
69-
70-
if strings.HasPrefix(resourceId, "ulb-") {
71-
resourceType = eipResourceTypeULB
72-
}
73-
74-
if strings.HasPrefix(resourceId, "cube-") {
75-
resourceType = eipResourceTypeCube
76-
}
77-
78-
if strings.HasPrefix(resourceId, "natgw-") {
79-
resourceType = eipResourceTypeNatGateway
80-
}
81-
82-
if resourceType == "" && len(strings.Split(resourceId, "-")) > 0 {
59+
if len(strings.Split(resourceId, "-")) > 0 {
8360
resourceType = strings.Split(resourceId, "-")[0]
8461
}
8562

8663
if v, ok := d.GetOk("resource_type"); ok {
87-
resourceType = v.(string)
64+
resourceType = lowerCaseProdCvt.convert(v.(string))
65+
}
66+
67+
if len(resourceType) == 0 {
68+
return fmt.Errorf("must set `resource_type` when creating eip association")
8869
}
8970

9071
req := conn.NewBindEIPRequest()
@@ -132,7 +113,7 @@ func resourceUCloudEIPAssociationRead(d *schema.ResourceData, meta interface{})
132113
client := meta.(*UCloudClient)
133114

134115
p := strings.Split(d.Id(), ":")
135-
resource, err := client.describeEIPResourceById(p[0], p[1])
116+
res, err := client.describeEIPResourceById(p[0], p[1])
136117
if err != nil {
137118
if isNotFoundError(err) {
138119
d.SetId("")
@@ -143,8 +124,12 @@ func resourceUCloudEIPAssociationRead(d *schema.ResourceData, meta interface{})
143124

144125
// remote api has not returned eip
145126
d.Set("eip_id", d.Get("eip_id"))
146-
d.Set("resource_id", resource.ResourceID)
147-
//d.Set("resource_type", lowerCaseProdCvt.unconvert(resource.ResourceType))
127+
d.Set("resource_id", res.ResourceID)
128+
if v, ok := d.GetOk("resource_type"); ok && isStringIn(v.(string), []string{resourceTypeInstance, resourceTypeLb}) {
129+
d.Set("resource_type", lowerCaseProdCvt.unconvert(res.ResourceType))
130+
} else {
131+
d.Set("resource_type", res.ResourceType)
132+
}
148133

149134
return nil
150135
}
@@ -158,8 +143,12 @@ func resourceUCloudEIPAssociationDelete(d *schema.ResourceData, meta interface{}
158143
req.EIPId = ucloud.String(p[0])
159144
req.ResourceId = ucloud.String(p[1])
160145
resourceType := eipResourceTypeUHost
161-
if strings.HasPrefix(p[1], "ulb-") {
162-
resourceType = eipResourceTypeULB
146+
if len(strings.Split(p[1], "-")) > 0 {
147+
resourceType = strings.Split(p[1], "-")[0]
148+
}
149+
150+
if v, ok := d.GetOk("resource_type"); ok {
151+
resourceType = lowerCaseProdCvt.convert(v.(string))
163152
}
164153
req.ResourceType = ucloud.String(resourceType)
165154

ucloud/resource_ucloud_lb_attachment.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ucloud
22

33
import (
44
"fmt"
5+
"strings"
56
"time"
67

78
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
@@ -36,12 +37,16 @@ func resourceUCloudLBAttachment() *schema.Resource {
3637
},
3738

3839
"resource_type": {
39-
Type: schema.TypeString,
40-
Optional: true,
41-
ForceNew: true,
42-
Computed: true,
43-
Deprecated: "attribute `resource_type` is deprecated for optimizing parameters",
44-
ValidateFunc: validation.StringInSlice([]string{"instance"}, false),
40+
Type: schema.TypeString,
41+
Optional: true,
42+
ForceNew: true,
43+
Computed: true,
44+
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
45+
if isStringIn(old, []string{resourceTypeInstance, lbResourceTypeUHost}) && isStringIn(new, []string{resourceTypeInstance, lbResourceTypeUHost}) {
46+
return true
47+
}
48+
return false
49+
},
4550
},
4651

4752
"resource_id": {
@@ -80,9 +85,17 @@ func resourceUCloudLBAttachmentCreate(d *schema.ResourceData, meta interface{})
8085
req := conn.NewAllocateBackendRequest()
8186
req.ULBId = ucloud.String(lbId)
8287
req.VServerId = ucloud.String(listenerId)
83-
req.ResourceType = ucloud.String(lbResourceTypeUHost)
84-
req.ResourceId = ucloud.String(d.Get("resource_id").(string))
88+
resourceId := d.Get("resource_id").(string)
89+
req.ResourceId = ucloud.String(resourceId)
8590
req.Port = ucloud.Int(d.Get("port").(int))
91+
resourceType := lbResourceTypeUHost
92+
if v, ok := d.GetOk("resource_type"); ok {
93+
resourceType = lbBackendCaseProdCvt.convert(v.(string))
94+
} else if len(strings.Split(resourceId, "-")) > 0 && strings.Split(resourceId, "-")[0] != eipResourceTypeUHost {
95+
return fmt.Errorf("must set `resource_type` when creating lb attachment")
96+
}
97+
98+
req.ResourceType = ucloud.String(resourceType)
8699

87100
resp, err := conn.AllocateBackend(req)
88101
if err != nil {
@@ -162,11 +175,16 @@ func resourceUCloudLBAttachmentRead(d *schema.ResourceData, meta interface{}) er
162175
}
163176

164177
d.Set("resource_id", backendSet.ResourceId)
165-
d.Set("resource_type", titleCaseProdCvt.unconvert(backendSet.ResourceType))
166178
d.Set("port", backendSet.Port)
167179
d.Set("private_ip", backendSet.PrivateIP)
168180
d.Set("status", lbAttachmentStatusCvt.convert(backendSet.Status))
169181

182+
if v, ok := d.GetOk("resource_type"); ok && isStringIn(v.(string), []string{resourceTypeInstance}) {
183+
d.Set("resource_type", lbBackendCaseProdCvt.unconvert(backendSet.ResourceType))
184+
} else {
185+
d.Set("resource_type", backendSet.ResourceType)
186+
}
187+
170188
return nil
171189
}
172190

website/docs/r/cube_pod.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ description: |-
88

99
# ucloud_cube_pod
1010

11-
Provides a CubePod resource.
11+
Provides a CubePod resource(**Deprecated**, this resource has been offline).
1212

1313
## Example Usage
1414

website/docs/r/eip_association.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,4 @@ The following arguments are supported:
7272

7373
* `eip_id` - (Required, ForceNew) The ID of EIP.
7474
* `resource_id` - (Required, ForceNew) The ID of resource with EIP attached.
75-
* `resource_type` - (**Deprecated**, ForceNew), attribute `resource_type` is deprecated for optimizing parameters.
75+
* `resource_type` - (Optional, ForceNew) The type of resource with EIP attached, possible values are `instance` or `uhost` as instance, `lb` or `ulb` as load balancer, `natgw` as NAT GateWay host, `udb` as database, `vpngw` as ipsec vpn host.

website/docs/r/lb_attachment.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ The following arguments are supported:
6363

6464
- - -
6565

66-
* `resource_type` - (**Deprecated**, ForceNew), attribute `resource_type` is deprecated for optimizing parameters.
66+
* `resource_type` - (Optional, ForceNew) The types of backend servers, possible values are: `instance` or `UHost` as UHost instance.
6767
* `port` - (Optional) The listening port of the backend server, range: 1-65535, (Default: `80`). Backend server port have the following restrictions: If the LB listener type is `request_proxy`, the backend serve can add different ports to implement different service instances of the same IP. Else if LB listener type is `packets_transmit`, the port of the backend server must be consistent with the LB listening port.
6868

6969
## Attributes Reference

0 commit comments

Comments
 (0)