Skip to content

Commit

Permalink
using elbV2Client to update endpoint in lb resources (#692)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiChangkuo authored Nov 25, 2020
1 parent 09aaefe commit 33952a7
Show file tree
Hide file tree
Showing 20 changed files with 210 additions and 221 deletions.
5 changes: 3 additions & 2 deletions huaweicloud/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ var allServiceCatalog = map[string]ServiceCatalog{
WithOutProjectID: true,
},
"elbv2": ServiceCatalog{
Name: "elb",
Version: "v2.0",
Name: "elb",
Version: "v2.0",
WithOutProjectID: true,
},
"fwv2": ServiceCatalog{
Name: "vpc",
Expand Down
4 changes: 2 additions & 2 deletions huaweicloud/endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ func TestAccServiceEndpoints_Network(t *testing.T) {
}
expectedURL = fmt.Sprintf("https://vpc.%s.%s/v1/%s/", OS_REGION_NAME, config.Cloud, config.TenantID)
actualURL = serviceClient.ResourceBaseURL()
compareURL(expectedURL, actualURL, "vpc", "v1", t)
compareURL(expectedURL, actualURL, "security group", "v1", t)

// test endpoint of elb v1.0
serviceClient, err = nil, nil
Expand All @@ -619,7 +619,7 @@ func TestAccServiceEndpoints_Network(t *testing.T) {
if err != nil {
t.Fatalf("Error creating HuaweiCloud ELB v2.0 client: %s", err)
}
expectedURL = fmt.Sprintf("https://elb.%s.%s/v2.0/%s/", OS_REGION_NAME, config.Cloud, config.TenantID)
expectedURL = fmt.Sprintf("https://elb.%s.%s/v2.0/", OS_REGION_NAME, config.Cloud)
actualURL = serviceClient.ResourceBaseURL()
compareURL(expectedURL, actualURL, "elb", "v2.0", t)

Expand Down
24 changes: 12 additions & 12 deletions huaweicloud/resource_huaweicloud_lb_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ func resourceCertificateV2() *schema.Resource {

func resourceCertificateV2Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.NetworkingV2Client(GetRegion(d, config))
elbClient, err := config.elbV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
return fmt.Errorf("Error creating HuaweiCloud elb client: %s", err)
}

createOpts := certificates.CreateOpts{
Expand All @@ -100,7 +100,7 @@ func resourceCertificateV2Create(d *schema.ResourceData, meta interface{}) error
}

log.Printf("[DEBUG] Create Options: %#v", createOpts)
c, err := certificates.Create(networkingClient, createOpts).Extract()
c, err := certificates.Create(elbClient, createOpts).Extract()
if err != nil {
return fmt.Errorf("Error creating Certificate: %s", err)
}
Expand All @@ -113,12 +113,12 @@ func resourceCertificateV2Create(d *schema.ResourceData, meta interface{}) error

func resourceCertificateV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.NetworkingV2Client(GetRegion(d, config))
elbClient, err := config.elbV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
return fmt.Errorf("Error creating HuaweiCloud elb client: %s", err)
}

c, err := certificates.Get(networkingClient, d.Id()).Extract()
c, err := certificates.Get(elbClient, d.Id()).Extract()
if err != nil {
return CheckDeleted(d, err, "certificate")
}
Expand All @@ -140,9 +140,9 @@ func resourceCertificateV2Read(d *schema.ResourceData, meta interface{}) error {

func resourceCertificateV2Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.NetworkingV2Client(GetRegion(d, config))
elbClient, err := config.elbV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
return fmt.Errorf("Error creating HuaweiCloud elb client: %s", err)
}

var updateOpts certificates.UpdateOpts
Expand All @@ -167,7 +167,7 @@ func resourceCertificateV2Update(d *schema.ResourceData, meta interface{}) error
timeout := d.Timeout(schema.TimeoutUpdate)
//lintignore:R006
err = resource.Retry(timeout, func() *resource.RetryError {
_, err := certificates.Update(networkingClient, d.Id(), updateOpts).Extract()
_, err := certificates.Update(elbClient, d.Id(), updateOpts).Extract()
if err != nil {
return checkForRetryableError(err)
}
Expand All @@ -182,16 +182,16 @@ func resourceCertificateV2Update(d *schema.ResourceData, meta interface{}) error

func resourceCertificateV2Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.NetworkingV2Client(GetRegion(d, config))
elbClient, err := config.elbV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
return fmt.Errorf("Error creating HuaweiCloud elb client: %s", err)
}

log.Printf("[DEBUG] Deleting certificate %s", d.Id())
timeout := d.Timeout(schema.TimeoutDelete)
//lintignore:R006
err = resource.Retry(timeout, func() *resource.RetryError {
err := certificates.Delete(networkingClient, d.Id()).ExtractErr()
err := certificates.Delete(elbClient, d.Id()).ExtractErr()
if err != nil {
return checkForRetryableError(err)
}
Expand Down
12 changes: 6 additions & 6 deletions huaweicloud/resource_huaweicloud_lb_certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ func TestAccLBV2Certificate_client(t *testing.T) {

func testAccCheckLBV2CertificateDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
networkingClient, err := config.NetworkingV2Client(OS_REGION_NAME)
elbClient, err := config.elbV2Client(OS_REGION_NAME)
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
return fmt.Errorf("Error creating HuaweiCloud elb client: %s", err)
}

for _, rs := range s.RootModule().Resources {
if rs.Type != "huaweicloud_lb_certificate" {
continue
}

_, err := certificates.Get(networkingClient, rs.Primary.ID).Extract()
_, err := certificates.Get(elbClient, rs.Primary.ID).Extract()
if err == nil {
return fmt.Errorf("Certificate still exists: %s", rs.Primary.ID)
}
Expand All @@ -95,12 +95,12 @@ func testAccCheckLBV2CertificateExists(
}

config := testAccProvider.Meta().(*Config)
networkingClient, err := config.NetworkingV2Client(OS_REGION_NAME)
elbClient, err := config.elbV2Client(OS_REGION_NAME)
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
return fmt.Errorf("Error creating HuaweiCloud elb client: %s", err)
}

found, err := certificates.Get(networkingClient, rs.Primary.ID).Extract()
found, err := certificates.Get(elbClient, rs.Primary.ID).Extract()
if err != nil {
return err
}
Expand Down
20 changes: 10 additions & 10 deletions huaweicloud/resource_huaweicloud_lb_l7policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ func resourceL7PolicyV2() *schema.Resource {

func resourceL7PolicyV2Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
lbClient, err := config.NetworkingV2Client(GetRegion(d, config))
lbClient, err := config.elbV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
return fmt.Errorf("Error creating HuaweiCloud elb client: %s", err)
}

// Assign some required variables for use in creation.
Expand Down Expand Up @@ -191,9 +191,9 @@ func resourceL7PolicyV2Create(d *schema.ResourceData, meta interface{}) error {

func resourceL7PolicyV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
lbClient, err := config.NetworkingV2Client(GetRegion(d, config))
lbClient, err := config.elbV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
return fmt.Errorf("Error creating HuaweiCloud elb client: %s", err)
}

l7Policy, err := l7policies.Get(lbClient, d.Id()).Extract()
Expand All @@ -218,9 +218,9 @@ func resourceL7PolicyV2Read(d *schema.ResourceData, meta interface{}) error {

func resourceL7PolicyV2Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
lbClient, err := config.NetworkingV2Client(GetRegion(d, config))
lbClient, err := config.elbV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
return fmt.Errorf("Error creating HuaweiCloud elb client: %s", err)
}

// Assign some required variables for use in updating.
Expand Down Expand Up @@ -318,9 +318,9 @@ func resourceL7PolicyV2Update(d *schema.ResourceData, meta interface{}) error {

func resourceL7PolicyV2Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
lbClient, err := config.NetworkingV2Client(GetRegion(d, config))
lbClient, err := config.elbV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
return fmt.Errorf("Error creating HuaweiCloud elb client: %s", err)
}

timeout := d.Timeout(schema.TimeoutDelete)
Expand Down Expand Up @@ -368,9 +368,9 @@ func resourceL7PolicyV2Delete(d *schema.ResourceData, meta interface{}) error {

func resourceL7PolicyV2Import(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)
lbClient, err := config.NetworkingV2Client(GetRegion(d, config))
lbClient, err := config.elbV2Client(GetRegion(d, config))
if err != nil {
return nil, fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
return nil, fmt.Errorf("Error creating HuaweiCloud elb client: %s", err)
}

l7Policy, err := l7policies.Get(lbClient, d.Id()).Extract()
Expand Down
4 changes: 2 additions & 2 deletions huaweicloud/resource_huaweicloud_lb_l7policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestAccLBV2L7Policy_basic(t *testing.T) {

func testAccCheckLBV2L7PolicyDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
lbClient, err := config.NetworkingV2Client(OS_REGION_NAME)
lbClient, err := config.elbV2Client(OS_REGION_NAME)
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud load balancing client: %s", err)
}
Expand Down Expand Up @@ -69,7 +69,7 @@ func testAccCheckLBV2L7PolicyExists(n string, l7Policy *l7policies.L7Policy) res
}

config := testAccProvider.Meta().(*Config)
lbClient, err := config.NetworkingV2Client(OS_REGION_NAME)
lbClient, err := config.elbV2Client(OS_REGION_NAME)
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud load balancing client: %s", err)
}
Expand Down
20 changes: 10 additions & 10 deletions huaweicloud/resource_huaweicloud_lb_l7rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ func resourceL7RuleV2() *schema.Resource {

func resourceL7RuleV2Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
lbClient, err := config.NetworkingV2Client(GetRegion(d, config))
lbClient, err := config.elbV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
return fmt.Errorf("Error creating HuaweiCloud elb client: %s", err)
}

// Assign some required variables for use in creation.
Expand Down Expand Up @@ -191,9 +191,9 @@ func resourceL7RuleV2Create(d *schema.ResourceData, meta interface{}) error {

func resourceL7RuleV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
lbClient, err := config.NetworkingV2Client(GetRegion(d, config))
lbClient, err := config.elbV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
return fmt.Errorf("Error creating HuaweiCloud elb client: %s", err)
}

l7policyID := d.Get("l7policy_id").(string)
Expand All @@ -218,9 +218,9 @@ func resourceL7RuleV2Read(d *schema.ResourceData, meta interface{}) error {

func resourceL7RuleV2Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
lbClient, err := config.NetworkingV2Client(GetRegion(d, config))
lbClient, err := config.elbV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
return fmt.Errorf("Error creating HuaweiCloud elb client: %s", err)
}

// Assign some required variables for use in updating.
Expand Down Expand Up @@ -308,9 +308,9 @@ func resourceL7RuleV2Update(d *schema.ResourceData, meta interface{}) error {

func resourceL7RuleV2Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
lbClient, err := config.NetworkingV2Client(GetRegion(d, config))
lbClient, err := config.elbV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
return fmt.Errorf("Error creating HuaweiCloud elb client: %s", err)
}

timeout := d.Timeout(schema.TimeoutDelete)
Expand Down Expand Up @@ -372,9 +372,9 @@ func resourceL7RuleV2Import(d *schema.ResourceData, meta interface{}) ([]*schema
}

config := meta.(*Config)
lbClient, err := config.NetworkingV2Client(GetRegion(d, config))
lbClient, err := config.elbV2Client(GetRegion(d, config))
if err != nil {
return nil, fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
return nil, fmt.Errorf("Error creating HuaweiCloud elb client: %s", err)
}

listenerID := ""
Expand Down
4 changes: 2 additions & 2 deletions huaweicloud/resource_huaweicloud_lb_l7rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestAccLBV2L7Rule_basic(t *testing.T) {

func testAccCheckLBV2L7RuleDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
lbClient, err := config.NetworkingV2Client(OS_REGION_NAME)
lbClient, err := config.elbV2Client(OS_REGION_NAME)
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud load balancing client: %s", err)
}
Expand Down Expand Up @@ -92,7 +92,7 @@ func testAccCheckLBV2L7RuleExists(n string, l7rule *l7rules.Rule) resource.TestC
}

config := testAccProvider.Meta().(*Config)
lbClient, err := config.NetworkingV2Client(OS_REGION_NAME)
lbClient, err := config.elbV2Client(OS_REGION_NAME)
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud load balancing client: %s", err)
}
Expand Down
43 changes: 14 additions & 29 deletions huaweicloud/resource_huaweicloud_lb_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ func resourceListenerV2() *schema.Resource {

func resourceListenerV2Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
lbClient, err := config.NetworkingV2Client(GetRegion(d, config))
lbClient, err := config.elbV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
return fmt.Errorf("Error creating HuaweiCloud elb client: %s", err)
}

adminStateUp := d.Get("admin_state_up").(bool)
Expand Down Expand Up @@ -182,12 +182,8 @@ func resourceListenerV2Create(d *schema.ResourceData, meta interface{}) error {
//set tags
tagRaw := d.Get("tags").(map[string]interface{})
if len(tagRaw) > 0 {
elbv2Client, err := config.elbV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud elb v2 client: %s", err)
}
taglist := expandResourceTags(tagRaw)
if tagErr := tags.Create(elbv2Client, "listeners", listener.ID, taglist).ExtractErr(); tagErr != nil {
if tagErr := tags.Create(lbClient, "listeners", listener.ID, taglist).ExtractErr(); tagErr != nil {
return fmt.Errorf("Error setting tags of elb listener %s: %s", listener.ID, tagErr)
}
}
Expand All @@ -197,9 +193,9 @@ func resourceListenerV2Create(d *schema.ResourceData, meta interface{}) error {

func resourceListenerV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
lbClient, err := config.NetworkingV2Client(GetRegion(d, config))
lbClient, err := config.elbV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
return fmt.Errorf("Error creating HuaweiCloud elb client: %s", err)
}

listener, err := listeners.Get(lbClient, d.Id()).Extract()
Expand All @@ -222,13 +218,8 @@ func resourceListenerV2Read(d *schema.ResourceData, meta interface{}) error {
d.Set("default_tls_container_ref", listener.DefaultTlsContainerRef)
d.Set("region", GetRegion(d, config))

// set tags
elbv2Client, err := config.elbV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud elb v2 client: %s", err)
}

resourceTags, err := tags.Get(elbv2Client, "listeners", d.Id()).Extract()
// fetch tags
resourceTags, err := tags.Get(lbClient, "listeners", d.Id()).Extract()
if err != nil {
return fmt.Errorf("Error fetching tags of elb listener: %s", err)
}
Expand All @@ -240,14 +231,13 @@ func resourceListenerV2Read(d *schema.ResourceData, meta interface{}) error {

func resourceListenerV2Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
lbClient, err := config.elbV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud elb client: %s", err)
}

if d.HasChanges("name", "description", "admin_state_up", "connection_limit",
"default_tls_container_ref", "sni_container_refs", "http2_enable") {
lbClient, err := config.NetworkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
}

var updateOpts listeners.UpdateOpts
if d.HasChange("name") {
updateOpts.Name = d.Get("name").(string)
Expand Down Expand Up @@ -311,12 +301,7 @@ func resourceListenerV2Update(d *schema.ResourceData, meta interface{}) error {

// update tags
if d.HasChange("tags") {
elbv2Client, err := config.elbV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud elb v2 client: %s", err)
}

tagErr := UpdateResourceTags(elbv2Client, d, "listeners", d.Id())
tagErr := UpdateResourceTags(lbClient, d, "listeners", d.Id())
if tagErr != nil {
return fmt.Errorf("Error updating tags of elb listener:%s, err:%s", d.Id(), tagErr)
}
Expand All @@ -328,9 +313,9 @@ func resourceListenerV2Update(d *schema.ResourceData, meta interface{}) error {

func resourceListenerV2Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
lbClient, err := config.NetworkingV2Client(GetRegion(d, config))
lbClient, err := config.elbV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
return fmt.Errorf("Error creating HuaweiCloud elb client: %s", err)
}

// Wait for LoadBalancer to become active before continuing
Expand Down
Loading

0 comments on commit 33952a7

Please sign in to comment.