Skip to content

Commit

Permalink
Fix lb listener import (#271)
Browse files Browse the repository at this point in the history
* fix_lb_listener_import

* fix_lb_listener_import

* fix_lb_listener_import

---------

Co-authored-by: Sungminlee <[email protected]>
  • Loading branch information
minosmlee and Sungminlee authored Apr 28, 2023
1 parent 675899a commit 2e9139e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
11 changes: 10 additions & 1 deletion docs/resources/lb_listener.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,13 @@ In addition to all arguments above, the following attributes are exported:

* `id` - The ID of listener.
* `listener_no` - The ID of listener (It is the same result as id).
* `rule_no_list` - The list of listener rule number.
* `rule_no_list` - The list of listener rule number.


## Import

Individual load balancer listener can be imported using LOAD_BALANCER_NO:LOAD_BALANCER_LISTENER_NO. For example, import a listener `69643` in a load balancer `17019658` like this:

``` hcl
$ terraform import ncloud_lb_listener.my_lb_listener 17019658:69643
```
10 changes: 9 additions & 1 deletion docs/resources/route.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ The following arguments are supported:
In addition to all arguments above, the following attributes are exported:

* `is_default` - Whether is default or not by Route table creation.
* `vpc_no` - The ID of the associated VPC.
* `vpc_no` - The ID of the associated VPC.

## Import

Individual routes can be imported using ROUTE_TABLE_NO:DESTINATION_CIDR. For example, import a route in a route table `57039` with an IPv4 destination CIDR `0.0.0.0/0` like this:

``` hcl
$ terraform import ncloud_route.my_route 57039:0.0.0.0/0
```
38 changes: 37 additions & 1 deletion ncloud/resource_ncloud_lb_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package ncloud

import (
"context"
"fmt"
"log"
"strings"

"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/ncloud"
"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/services/vloadbalancer"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand All @@ -26,7 +30,17 @@ func resourceNcloudLbListener() *schema.Resource {
UpdateContext: resourceNcloudLbListenerUpdate,
DeleteContext: resourceNcloudLbListenerDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
idParts := strings.Split(d.Id(), ":")
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
return nil, fmt.Errorf("unexpected format of ID (%q), expected LOAD_BALANCER_NO:LOAD_BALANCER_LISTENER_NO", d.Id())
}
load_balancer_no := idParts[0]
listener_no := idParts[1]
d.SetId(listener_no)
d.Set("load_balancer_no", load_balancer_no)
return []*schema.ResourceData{d}, nil
},
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(DefaultCreateTimeout),
Expand Down Expand Up @@ -241,9 +255,31 @@ func getVpcLoadBalancerListener(config *ProviderConfig, id string, loadBalancerN
SslCertificateNo: l.SslCertificateNo,
TlsMinVersionType: l.TlsMinVersionType.Code,
LoadBalancerRuleNoList: l.LoadBalancerRuleNoList,
TargetGroupNo: getVpcLoadBalancerListenerTargetGroupNo(config, id),
}, nil
}
}

return nil, nil
}

func getVpcLoadBalancerListenerTargetGroupNo(config *ProviderConfig, id string) *string {

reqParams := &vloadbalancer.GetLoadBalancerRuleListRequest{
RegionCode: &config.RegionCode,
LoadBalancerListenerNo: ncloud.String(id),
}

resp, err := config.Client.vloadbalancer.V2Api.GetLoadBalancerRuleList(reqParams)
if err != nil {
return nil
}

for _, l := range resp.LoadBalancerRuleList {
if id == *l.LoadBalancerListenerNo {
return l.LoadBalancerRuleActionList[0].TargetGroupAction.TargetGroupWeightList[0].TargetGroupNo
}
}

return nil
}

0 comments on commit 2e9139e

Please sign in to comment.