Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit cfd8254

Browse files
committed
Check for existing but unassigned LB policies
If an AWS Cookie Stickiness Policy is removed from the listener in the AWS console the policy isn't deleted. Cross reference the policy name with those assigned to the listener to determine if the policy is actually being used.
1 parent 5fdcf5d commit cfd8254

4 files changed

Lines changed: 158 additions & 1 deletion

builtin/providers/aws/resource_aws_app_cookie_stickiness_policy.go

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package aws
22

33
import (
44
"fmt"
5+
"log"
56
"regexp"
7+
"strconv"
68
"strings"
79

810
"github.com/aws/aws-sdk-go/aws"
@@ -106,11 +108,22 @@ func resourceAwsAppCookieStickinessPolicyRead(d *schema.ResourceData, meta inter
106108
}
107109
return fmt.Errorf("Error retrieving policy: %s", err)
108110
}
109-
110111
if len(getResp.PolicyDescriptions) != 1 {
111112
return fmt.Errorf("Unable to find policy %#v", getResp.PolicyDescriptions)
112113
}
113114

115+
// we know the policy exists now, but we have to check if it's assigned to a listener
116+
assigned, err := resourceAwsELBSticknessPolicyAssigned(policyName, lbName, lbPort, elbconn)
117+
if err != nil {
118+
return err
119+
}
120+
if !assigned {
121+
// policy exists, but isn't assigned to a listener
122+
log.Printf("[DEBUG] policy '%s' exists, but isn't assigned to a listener", policyName)
123+
d.SetId("")
124+
return nil
125+
}
126+
114127
// We can get away with this because there's only one attribute, the
115128
// cookie expiration, in these descriptions.
116129
policyDesc := getResp.PolicyDescriptions[0]
@@ -127,6 +140,43 @@ func resourceAwsAppCookieStickinessPolicyRead(d *schema.ResourceData, meta inter
127140
return nil
128141
}
129142

143+
// Determine if a particular policy is assigned to an ELB listener
144+
func resourceAwsELBSticknessPolicyAssigned(policyName, lbName, lbPort string, elbconn *elb.ELB) (bool, error) {
145+
describeElbOpts := &elb.DescribeLoadBalancersInput{
146+
LoadBalancerNames: []*string{aws.String(lbName)},
147+
}
148+
describeResp, err := elbconn.DescribeLoadBalancers(describeElbOpts)
149+
if err != nil {
150+
if ec2err, ok := err.(awserr.Error); ok {
151+
if ec2err.Code() == "LoadBalancerNotFound" {
152+
return false, nil
153+
}
154+
}
155+
return false, fmt.Errorf("Error retrieving ELB description: %s", err)
156+
}
157+
158+
if len(describeResp.LoadBalancerDescriptions) != 1 {
159+
return false, fmt.Errorf("Unable to find ELB: %#v", describeResp.LoadBalancerDescriptions)
160+
}
161+
162+
lb := describeResp.LoadBalancerDescriptions[0]
163+
assigned := false
164+
for _, listener := range lb.ListenerDescriptions {
165+
if lbPort != strconv.Itoa(int(*listener.Listener.LoadBalancerPort)) {
166+
continue
167+
}
168+
169+
for _, name := range listener.PolicyNames {
170+
if policyName == *name {
171+
assigned = true
172+
break
173+
}
174+
}
175+
}
176+
177+
return assigned, nil
178+
}
179+
130180
func resourceAwsAppCookieStickinessPolicyDelete(d *schema.ResourceData, meta interface{}) error {
131181
elbconn := meta.(*AWSClient).elbconn
132182

builtin/providers/aws/resource_aws_app_cookie_stickiness_policy_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,54 @@ func testAccCheckAppCookieStickinessPolicy(elbResource string, policyResource st
139139
}
140140
}
141141

142+
// ensure the policy is re-added is it goes missing
143+
func TestAccAWSAppCookieStickinessPolicy_drift(t *testing.T) {
144+
lbName := fmt.Sprintf("tf-test-lb-%s", acctest.RandString(5))
145+
146+
// We only want to remove the reference to the policy from the listner,
147+
// beacause that's all that can be done via the console.
148+
removePolicy := func() {
149+
conn := testAccProvider.Meta().(*AWSClient).elbconn
150+
151+
setLoadBalancerOpts := &elb.SetLoadBalancerPoliciesOfListenerInput{
152+
LoadBalancerName: aws.String(lbName),
153+
LoadBalancerPort: aws.Int64(80),
154+
PolicyNames: []*string{},
155+
}
156+
157+
if _, err := conn.SetLoadBalancerPoliciesOfListener(setLoadBalancerOpts); err != nil {
158+
t.Fatalf("Error removing AppCookieStickinessPolicy: %s", err)
159+
}
160+
}
161+
162+
resource.Test(t, resource.TestCase{
163+
PreCheck: func() { testAccPreCheck(t) },
164+
Providers: testAccProviders,
165+
CheckDestroy: testAccCheckAppCookieStickinessPolicyDestroy,
166+
Steps: []resource.TestStep{
167+
resource.TestStep{
168+
Config: testAccAppCookieStickinessPolicyConfig(lbName),
169+
Check: resource.ComposeTestCheckFunc(
170+
testAccCheckAppCookieStickinessPolicy(
171+
"aws_elb.lb",
172+
"aws_app_cookie_stickiness_policy.foo",
173+
),
174+
),
175+
},
176+
resource.TestStep{
177+
PreConfig: removePolicy,
178+
Config: testAccAppCookieStickinessPolicyConfig(lbName),
179+
Check: resource.ComposeTestCheckFunc(
180+
testAccCheckAppCookieStickinessPolicy(
181+
"aws_elb.lb",
182+
"aws_app_cookie_stickiness_policy.foo",
183+
),
184+
),
185+
},
186+
},
187+
})
188+
}
189+
142190
func testAccAppCookieStickinessPolicyConfig(rName string) string {
143191
return fmt.Sprintf(`
144192
resource "aws_elb" "lb" {

builtin/providers/aws/resource_aws_lb_cookie_stickiness_policy.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,18 @@ func resourceAwsLBCookieStickinessPolicyRead(d *schema.ResourceData, meta interf
115115
return fmt.Errorf("Unable to find policy %#v", getResp.PolicyDescriptions)
116116
}
117117

118+
// we know the policy exists now, but we have to check if it's assigned to a listener
119+
assigned, err := resourceAwsELBSticknessPolicyAssigned(policyName, lbName, lbPort, elbconn)
120+
if err != nil {
121+
return err
122+
}
123+
if !assigned {
124+
// policy exists, but isn't assigned to a listener
125+
log.Printf("[DEBUG] policy '%s' exists, but isn't assigned to a listener", policyName)
126+
d.SetId("")
127+
return nil
128+
}
129+
118130
// We can get away with this because there's only one attribute, the
119131
// cookie expiration, in these descriptions.
120132
policyDesc := getResp.PolicyDescriptions[0]

builtin/providers/aws/resource_aws_lb_cookie_stickiness_policy_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,53 @@ func testAccCheckLBCookieStickinessPolicy(elbResource string, policyResource str
102102
}
103103
}
104104

105+
func TestAccCheckLBCookieStickinessPolicy_drift(t *testing.T) {
106+
lbName := fmt.Sprintf("tf-test-lb-%s", acctest.RandString(5))
107+
108+
// We only want to remove the reference to the policy from the listner,
109+
// beacause that's all that can be done via the console.
110+
removePolicy := func() {
111+
conn := testAccProvider.Meta().(*AWSClient).elbconn
112+
113+
setLoadBalancerOpts := &elb.SetLoadBalancerPoliciesOfListenerInput{
114+
LoadBalancerName: aws.String(lbName),
115+
LoadBalancerPort: aws.Int64(80),
116+
PolicyNames: []*string{},
117+
}
118+
119+
if _, err := conn.SetLoadBalancerPoliciesOfListener(setLoadBalancerOpts); err != nil {
120+
t.Fatalf("Error removing LBCookieStickinessPolicy: %s", err)
121+
}
122+
}
123+
124+
resource.Test(t, resource.TestCase{
125+
PreCheck: func() { testAccPreCheck(t) },
126+
Providers: testAccProviders,
127+
CheckDestroy: testAccCheckLBCookieStickinessPolicyDestroy,
128+
Steps: []resource.TestStep{
129+
resource.TestStep{
130+
Config: testAccLBCookieStickinessPolicyConfig(lbName),
131+
Check: resource.ComposeTestCheckFunc(
132+
testAccCheckLBCookieStickinessPolicy(
133+
"aws_elb.lb",
134+
"aws_lb_cookie_stickiness_policy.foo",
135+
),
136+
),
137+
},
138+
resource.TestStep{
139+
PreConfig: removePolicy,
140+
Config: testAccLBCookieStickinessPolicyConfig(lbName),
141+
Check: resource.ComposeTestCheckFunc(
142+
testAccCheckLBCookieStickinessPolicy(
143+
"aws_elb.lb",
144+
"aws_lb_cookie_stickiness_policy.foo",
145+
),
146+
),
147+
},
148+
},
149+
})
150+
}
151+
105152
func testAccLBCookieStickinessPolicyConfig(rName string) string {
106153
return fmt.Sprintf(`
107154
resource "aws_elb" "lb" {

0 commit comments

Comments
 (0)