From c50801dfa6ecf6554d8eab07a5999c9053084cce Mon Sep 17 00:00:00 2001 From: nevins-b Date: Thu, 19 Feb 2015 14:25:52 -0500 Subject: [PATCH 1/2] adding support for ELB policies --- elb/elb.go | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 91 insertions(+), 1 deletion(-) diff --git a/elb/elb.go b/elb/elb.go index e0d53f38..b99e6243 100644 --- a/elb/elb.go +++ b/elb/elb.go @@ -4,6 +4,7 @@ package elb import ( "encoding/xml" + "fmt" "net/http" "net/url" "strconv" @@ -369,7 +370,96 @@ func (elb *ELB) ModifyLoadBalancerAttributes(options *ModifyLoadBalancerAttribut return } -// ---------------------------------------------------------------------------- +// --------------------------------------------------------------------------- +// Policies +type CreateLoadBalancerPolicy struct { + LoadBalancerName string + PolicyName string + PolicyTypeName string + PolicyAttributes []PolicyAttribute +} + +type PolicyAttribute struct { + AttributeName string + AttributeValue string +} + +func (elb *ELB) CreateLoadBalancerPolicy(options *CreateLoadBalancerPolicy) (resp *SimpleResp, err error) { + params := makeParams("CreateLoadBalancerPolicy") + + params["LoadBalancerName"] = options.LoadBalancerName + params["PolicyName"] = options.PolicyName + params["PolicyTypeName"] = options.PolicyTypeName + for i := 0; i <= len(options.PolicyAttributes); i++ { + attributeName := fmt.Sprintf("PolicyAttributes.member.%d.AttributeName", i+1) + attributeValue := fmt.Sprintf("PolicyAttributes.member.%d.AttributeValue", i+1) + params[attributeName] = options.PolicyAttributes[i].AttributeName + params[attributeValue] = options.PolicyAttributes[i].AttributeValue + } + resp = &SimpleResp{} + + err = elb.query(params, resp) + + if err != nil { + resp = nil + } + + return +} + +type SetLoadBalancerPoliciesOfListener struct { + LoadBalancerName string + LoadBalancerPort int64 + PolicyNames []string +} + +func (elb *ELB) SetLoadBalancerPoliciesOfListener(options *SetLoadBalancerPoliciesOfListener) (resp *SimpleResp, err error) { + params := makeParams("SetLoadBalancerPoliciesOfListener") + + params["LoadBalancerName"] = options.LoadBalancerName + params["LoadBalancerPort"] = strconv.Itoa(int(options.LoadBalancerPort)) + for i := 0; i <= len(options.PolicyNames); i++ { + member := fmt.Sprintf("PolicyNames.member.%d", i+1) + params[member] = options.PolicyNames[i] + } + resp = &SimpleResp{} + + err = elb.query(params, resp) + + if err != nil { + resp = nil + } + + return +} + +type SetLoadBalancerPoliciesForBackendServer struct { + LoadBalancerName string + InstancePort int64 + PolicyNames []string +} + +func (elb *ELB) SetLoadBalancerPoliciesForBackendServer(options *SetLoadBalancerPoliciesForBackendServer) (resp *SimpleResp, err error) { + params := makeParams("SetLoadBalancerPoliciesForBackendServer") + + params["LoadBalancerName"] = options.LoadBalancerName + params["InstancePort"] = strconv.Itoa(int(options.InstancePort)) + for i := 0; i <= len(options.PolicyNames); i++ { + member := fmt.Sprintf("PolicyNames.member.%d", i+1) + params[member] = options.PolicyNames[i] + } + resp = &SimpleResp{} + + err = elb.query(params, resp) + + if err != nil { + resp = nil + } + + return +} + +// --------------------------------------------------------------------------- // Instance Registration / deregistration // The RegisterInstancesWithLoadBalancer request parameters From 19005b6c4afb4d3f4bc4f24b93f3839fdacbdbeb Mon Sep 17 00:00:00 2001 From: nevins-b Date: Thu, 19 Feb 2015 14:30:39 -0500 Subject: [PATCH 2/2] fixing off by one error in attribute loop --- elb/elb.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/elb/elb.go b/elb/elb.go index b99e6243..fcf8f253 100644 --- a/elb/elb.go +++ b/elb/elb.go @@ -390,7 +390,7 @@ func (elb *ELB) CreateLoadBalancerPolicy(options *CreateLoadBalancerPolicy) (res params["LoadBalancerName"] = options.LoadBalancerName params["PolicyName"] = options.PolicyName params["PolicyTypeName"] = options.PolicyTypeName - for i := 0; i <= len(options.PolicyAttributes); i++ { + for i := 0; i < len(options.PolicyAttributes); i++ { attributeName := fmt.Sprintf("PolicyAttributes.member.%d.AttributeName", i+1) attributeValue := fmt.Sprintf("PolicyAttributes.member.%d.AttributeValue", i+1) params[attributeName] = options.PolicyAttributes[i].AttributeName @@ -418,7 +418,7 @@ func (elb *ELB) SetLoadBalancerPoliciesOfListener(options *SetLoadBalancerPolici params["LoadBalancerName"] = options.LoadBalancerName params["LoadBalancerPort"] = strconv.Itoa(int(options.LoadBalancerPort)) - for i := 0; i <= len(options.PolicyNames); i++ { + for i := 0; i < len(options.PolicyNames); i++ { member := fmt.Sprintf("PolicyNames.member.%d", i+1) params[member] = options.PolicyNames[i] }