1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
package elasticache
import (
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/docker/goamz/aws"
)
var (
ErrCacheClusterNotFound = errors.New("Cache cluster not found")
)
type ElastiCache struct {
aws.Auth
aws.Region
}
// DescribeReplicationGroupsResult represents the response
type DescribeReplicationGroupsResult struct {
ReplicationGroups []ReplicationGroup `xml:"DescribeReplicationGroupsResult>ReplicationGroups"`
}
func (repGroup *ReplicationGroup) GetPrimaryNode() (*PrimaryEndpoint, error) {
if repGroup.Status != "available" {
return nil, errors.New("Replication group isn't available")
}
if len(repGroup.NodeGroups) == 0 {
return nil, errors.New("No node groups available")
}
ng := repGroup.NodeGroups[0]
if &ng.PrimaryEndpoint == nil {
return nil, errors.New("No primary node found")
}
return &ng.PrimaryEndpoint, nil
}
// DescribeCacheClustersResult represents the response from a
// DescribeCacheClusters ElastiCache API call
type DescribeCacheClustersResult struct {
CacheClusters []*CacheCluster `xml:"DescribeCacheClustersResult>CacheClusters"`
}
// ReplicationGroup represents a replication group
type ReplicationGroup struct {
Status string `xml:"ReplicationGroup>Status"`
ReplicationGroupId string `xml:"ReplicationGroup>ReplicationGroupId"`
MemberClusters []string `xml:"ReplicationGroup>MemberClusters>ClusterId"`
NodeGroups []NodeGroup `xml:"ReplicationGroup>NodeGroups"`
}
// NodeGroup represents a node group
type NodeGroup struct {
Status string `xml:"NodeGroup>Status"`
PrimaryEndpoint PrimaryEndpoint `xml:"NodeGroup>PrimaryEndpoint"`
NodeGroupMembers []*NodeGroupMember `xml:"NodeGroup>NodeGroupMembers>NodeGroupMember"`
}
// NodeGroupMember represents an individual node
type NodeGroupMember struct {
CurrentRole string `xml:"CurrentRole"`
PreferredAvailabilityZone string `xml:"PreferredAvailabilityZone"`
CacheNodeId string `xml:"CacheNodeId"`
CacheClusterId string `xml:"CacheClusterId"`
ReadEndpoint ReadEndpoint `xml:"ReadEndpoint"`
}
// PrimaryEndpoint represents the primary endpoint
type PrimaryEndpoint struct {
Port int `xml:"Port"`
Address string `xml:"Address"`
}
// ReadEndpoint represents a read endpoint
type ReadEndpoint struct {
Port int `xml:"Port"`
Address string `xml:"Address"`
}
// CacheCluster represents a cache cluster
type CacheCluster struct {
CacheClusterId string `xml:"CacheCluster>CacheClusterId"`
CacheNodes []*CacheNode `xml:"CacheCluster>CacheNodes"`
}
// CacheNode represents a cache node
type CacheNode struct {
Endpoint *Endpoint `xml:"CacheNode>Endpoint"`
}
// Endpoint represents a cache node endpoint
type Endpoint struct {
Host string `xml:"Address"`
Port int `xml:"Port"`
}
// New creates a new ElastiCache instance
func New(auth aws.Auth, region aws.Region) *ElastiCache {
return &ElastiCache{auth, region}
}
// DescribeReplicationGroup returns information about a cache replication group
func (ec *ElastiCache) DescribeReplicationGroup(groupName string) (*ReplicationGroup, error) {
var resp DescribeReplicationGroupsResult
err := ec.query("Action=DescribeReplicationGroups&ReplicationGroupId="+groupName+"&Version=2014-07-15", &resp)
if err != nil {
return nil, err
}
if len(resp.ReplicationGroups) == 0 {
return nil, errors.New("Replication group not found")
}
return &resp.ReplicationGroups[0], nil
}
// DescribeCacheCluster returns information about a cache cluster
func (ec *ElastiCache) DescribeCacheCluster(cluster string) (*CacheCluster, error) {
var resp DescribeCacheClustersResult
err := ec.query("Action=DescribeCacheClusters&CacheClusterId="+cluster+"&ShowCacheNodeInfo=true&Version=2014-07-15", &resp)
if err != nil {
return nil, err
}
if len(resp.CacheClusters) == 0 {
return nil, ErrCacheClusterNotFound
}
return resp.CacheClusters[0], nil
}
func (ec *ElastiCache) query(query string, response interface{}) error {
url := ec.Region.ElastiCacheEndpoint + "/?" + query
hreq, err := http.NewRequest("POST", url, nil)
if err != nil {
return err
}
hreq.Header.Set("Content-Type", "application/x-amz-json-1.0")
hreq.Header.Set("X-Amz-Date", time.Now().UTC().Format(aws.ISO8601BasicFormat))
token := ec.Auth.Token()
if token != "" {
hreq.Header.Set("X-Amz-Security-Token", token)
}
signer := aws.NewV4Signer(ec.Auth, "elasticache", ec.Region)
signer.Sign(hreq)
resp, err := http.DefaultClient.Do(hreq)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return buildError(resp)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return xml.Unmarshal(b, &response)
}
/* Copied from elb/elb.go - might not be entirely accurate */
// Error encapsulates an error returned by EC.
type Error struct {
// HTTP status code
StatusCode int
// AWS error code
Code string
// The human-oriented error message
Message string
}
func (err *Error) Error() string {
if err.Code == "" {
return err.Message
}
return fmt.Sprintf("%s (%s)", err.Message, err.Code)
}
type xmlErrors struct {
Errors []Error `xml:"Error"`
}
func buildError(r *http.Response) error {
var (
err Error
errors xmlErrors
)
xml.NewDecoder(r.Body).Decode(&errors)
if len(errors.Errors) > 0 {
err = errors.Errors[0]
}
err.StatusCode = r.StatusCode
if err.Message == "" {
err.Message = r.Status
}
return &err
}
|