Skip to content

Commit

Permalink
Fix creating slb listener in international region failed error
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaozhu36 committed Aug 8, 2018
1 parent f37593a commit c977fba
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 16 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 1.11.0 (Unreleased)

IMPROVEMENTS:


BUG FIXES:

- Fix creating slb listener in international region failed error ([#577](https://github.com/alibaba/terraform-provider/pull/577))

## 1.10.1 (August 3, 2018)

IMPROVEMENTS:
Expand Down
2 changes: 2 additions & 0 deletions alicloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type AliyunClient struct {
//In order to build ots table client, add accesskey and secretkey in aliyunclient temporarily.
AccessKey string
SecretKey string
SecurityToken string
OtsInstanceName string
AccountId string
ecsconn *ecs.Client
Expand Down Expand Up @@ -151,6 +152,7 @@ func (c *Config) Client() (*AliyunClient, error) {
RegionId: c.RegionId,
AccessKey: c.AccessKey,
SecretKey: c.SecretKey,
SecurityToken: c.SecurityToken,
OtsInstanceName: c.OtsInstanceName,
AccountId: c.AccountId,
ecsconn: ecsconn,
Expand Down
4 changes: 2 additions & 2 deletions alicloud/diff_suppress_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ func slbInternetDiffSuppressFunc(k, old, new string, d *schema.ResourceData) boo

func slbInternetChargeTypeDiffSuppressFunc(k, old, new string, d *schema.ResourceData) bool {
// Uniform all internet chare type value and be compatible with previous lower value.
if strings.ToLower(old) != strings.ToLower(string(new)) {
return false
if strings.ToLower(old) == strings.ToLower(new) {
return true
}
return !slbInternetDiffSuppressFunc(k, old, new, d)
}
Expand Down
9 changes: 0 additions & 9 deletions alicloud/extension_slb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package alicloud
import (
"fmt"
"strings"

"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
)

type SchedulerType string
Expand Down Expand Up @@ -108,10 +106,3 @@ func expandBackendServersWithPortToString(items []interface{}) string {
}
return fmt.Sprintf("[%s]", strings.Join(servers, COMMA_SEPARATED))
}

func buildSlbCommonRequest() *requests.CommonRequest {
request := requests.NewCommonRequest()
request.Domain = "slb.aliyuncs.com"
request.Version = ApiVersion20140515
return request
}
8 changes: 4 additions & 4 deletions alicloud/resource_alicloud_slb_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func resourceAliyunSlbListenerCreate(d *schema.ResourceData, meta interface{}) e
lb_id := d.Get("load_balancer_id").(string)
frontend := d.Get("frontend_port").(int)

req := buildListenerCommonArgs(d)
req := buildListenerCommonArgs(d, meta)
req.ApiName = fmt.Sprintf("CreateLoadBalancer%sListener", strings.ToUpper(protocol))

if Protocol(protocol) == Http || Protocol(protocol) == Https {
Expand Down Expand Up @@ -289,7 +289,7 @@ func resourceAliyunSlbListenerUpdate(d *schema.ResourceData, meta interface{}) e

d.Partial(true)

commonArgs := buildListenerCommonArgs(d)
commonArgs := buildListenerCommonArgs(d, meta)
commonArgs.ApiName = fmt.Sprintf("SetLoadBalancer%sListenerAttribute", strings.ToUpper(string(protocol)))

update := false
Expand Down Expand Up @@ -464,8 +464,8 @@ func resourceAliyunSlbListenerDelete(d *schema.ResourceData, meta interface{}) e
})
}

func buildListenerCommonArgs(d *schema.ResourceData) *requests.CommonRequest {
req := buildSlbCommonRequest()
func buildListenerCommonArgs(d *schema.ResourceData, meta interface{}) *requests.CommonRequest {
req := meta.(*AliyunClient).BuildSlbCommonRequest()
req.QueryParams["LoadBalancerId"] = d.Get("load_balancer_id").(string)
req.QueryParams["ListenerPort"] = string(requests.NewInteger(d.Get("frontend_port").(int)))
req.QueryParams["BackendServerPort"] = string(requests.NewInteger(d.Get("backend_port").(int)))
Expand Down
37 changes: 37 additions & 0 deletions alicloud/service_alicloud_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (

"io/ioutil"

"fmt"
"strings"

"github.com/denverdino/aliyungo/common"
"github.com/denverdino/aliyungo/location"
"github.com/mitchellh/go-homedir"
"gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -73,3 +78,35 @@ func loadFileContent(v string) ([]byte, error) {
}
return fileContent, nil
}

func (client *AliyunClient) DescribeEndpointByCode(region string, code ServiceCode) (string, error) {
endpointClient := location.NewClient(client.AccessKey, client.SecretKey)
endpointClient.SetSecurityToken(client.SecurityToken)
args := &location.DescribeEndpointsArgs{
Id: common.Region(region),
ServiceCode: strings.ToLower(string(code)),
Type: "openAPI",
}
invoker := NewInvoker()
var endpoints *location.DescribeEndpointsResponse
if err := invoker.Run(func() error {
es, err := endpointClient.DescribeEndpoints(args)
if err != nil {
return err
}
endpoints = es
return nil
}); err != nil {
return "", fmt.Errorf("Describe %s endpoint using region: %#v got an error: %#v.", code, client.RegionId, err)
}
endpointItem := endpoints.Endpoints.Endpoint
var endpoint string
if endpointItem == nil || len(endpointItem) <= 0 {
log.Printf("Cannot find endpoint in the region: %#v", client.RegionId)
endpoint = ""
} else {
endpoint = endpointItem[0].Endpoint
}

return endpoint, nil
}
16 changes: 15 additions & 1 deletion alicloud/service_alicloud_slb.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ import (
"github.com/aliyun/alibaba-cloud-sdk-go/services/slb"
)

func (client *AliyunClient) BuildSlbCommonRequest() *requests.CommonRequest {
request := requests.NewCommonRequest()
endpoint := LoadEndpoint(client.RegionId, SLBCode)
if endpoint == "" {
endpoint, _ = client.DescribeEndpointByCode(client.RegionId, SLBCode)
}
if endpoint == "" {
endpoint = fmt.Sprintf("slb.%s.aliyuncs.com", client.RegionId)
}
request.Domain = endpoint
request.Version = ApiVersion20140515
request.RegionId = client.RegionId
return request
}
func (client *AliyunClient) DescribeLoadBalancerAttribute(slbId string) (loadBalancer *slb.DescribeLoadBalancerAttributeResponse, err error) {

req := slb.CreateDescribeLoadBalancerAttributeRequest()
Expand Down Expand Up @@ -78,7 +92,7 @@ func (client *AliyunClient) DescribeSlbVServerGroupAttribute(groupId string) (*s
}

func (client *AliyunClient) DescribeLoadBalancerListenerAttribute(loadBalancerId string, port int, protocol Protocol) (listener map[string]interface{}, err error) {
req := buildSlbCommonRequest()
req := client.BuildSlbCommonRequest()
req.ApiName = fmt.Sprintf("DescribeLoadBalancer%sListenerAttribute", strings.ToUpper(string(protocol)))
req.QueryParams["LoadBalancerId"] = loadBalancerId
req.QueryParams["ListenerPort"] = string(requests.NewInteger(port))
Expand Down

0 comments on commit c977fba

Please sign in to comment.