~juju-qa/ubuntu/yakkety/juju/2.0-rc3-again

« back to all changes in this revision

Viewing changes to src/launchpad.net/goamz/aws/aws.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-04-24 22:34:47 UTC
  • Revision ID: package-import@ubuntu.com-20130424223447-f0qdji7ubnyo0s71
Tags: upstream-1.10.0.1
ImportĀ upstreamĀ versionĀ 1.10.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// goamz - Go packages to interact with the Amazon Web Services.
 
3
//
 
4
//   https://wiki.ubuntu.com/goamz
 
5
//
 
6
// Copyright (c) 2011 Canonical Ltd.
 
7
//
 
8
// Written by Gustavo Niemeyer <gustavo.niemeyer@canonical.com>
 
9
//
 
10
package aws
 
11
 
 
12
import (
 
13
        "errors"
 
14
        "os"
 
15
)
 
16
 
 
17
// Region defines the URLs where AWS services may be accessed.
 
18
//
 
19
// See http://goo.gl/d8BP1 for more details.
 
20
type Region struct {
 
21
        Name                 string // the canonical name of this region.
 
22
        EC2Endpoint          string
 
23
        S3Endpoint           string
 
24
        S3BucketEndpoint     string // Not needed by AWS S3. Use ${bucket} for bucket name.
 
25
        S3LocationConstraint bool   // true if this region requires a LocationConstraint declaration.
 
26
        S3LowercaseBucket    bool   // true if the region requires bucket names to be lower case.
 
27
        SDBEndpoint          string
 
28
        SNSEndpoint          string
 
29
        SQSEndpoint          string
 
30
        IAMEndpoint          string
 
31
}
 
32
 
 
33
var USEast = Region{
 
34
        "us-east-1",
 
35
        "https://ec2.us-east-1.amazonaws.com",
 
36
        "https://s3.amazonaws.com",
 
37
        "",
 
38
        false,
 
39
        false,
 
40
        "https://sdb.amazonaws.com",
 
41
        "https://sns.us-east-1.amazonaws.com",
 
42
        "https://sqs.us-east-1.amazonaws.com",
 
43
        "https://iam.amazonaws.com",
 
44
}
 
45
 
 
46
var USWest = Region{
 
47
        "us-west-1",
 
48
        "https://ec2.us-west-1.amazonaws.com",
 
49
        "https://s3-us-west-1.amazonaws.com",
 
50
        "",
 
51
        true,
 
52
        true,
 
53
        "https://sdb.us-west-1.amazonaws.com",
 
54
        "https://sns.us-west-1.amazonaws.com",
 
55
        "https://sqs.us-west-1.amazonaws.com",
 
56
        "https://iam.amazonaws.com",
 
57
}
 
58
 
 
59
var USWest2 = Region{
 
60
        "us-west-2",
 
61
        "https://ec2.us-west-2.amazonaws.com",
 
62
        "https://s3-us-west-2.amazonaws.com",
 
63
        "",
 
64
        true,
 
65
        true,
 
66
        "https://sdb.us-west-2.amazonaws.com",
 
67
        "https://sns.us-west-2.amazonaws.com",
 
68
        "https://sqs.us-west-2.amazonaws.com",
 
69
        "https://iam.amazonaws.com",
 
70
}
 
71
 
 
72
var EUWest = Region{
 
73
        "eu-west-1",
 
74
        "https://ec2.eu-west-1.amazonaws.com",
 
75
        "https://s3-eu-west-1.amazonaws.com",
 
76
        "",
 
77
        true,
 
78
        true,
 
79
        "https://sdb.eu-west-1.amazonaws.com",
 
80
        "https://sns.eu-west-1.amazonaws.com",
 
81
        "https://sqs.eu-west-1.amazonaws.com",
 
82
        "https://iam.amazonaws.com",
 
83
}
 
84
 
 
85
var APSoutheast = Region{
 
86
        "ap-southeast-1",
 
87
        "https://ec2.ap-southeast-1.amazonaws.com",
 
88
        "https://s3-ap-southeast-1.amazonaws.com",
 
89
        "",
 
90
        true,
 
91
        true,
 
92
        "https://sdb.ap-southeast-1.amazonaws.com",
 
93
        "https://sns.ap-southeast-1.amazonaws.com",
 
94
        "https://sqs.ap-southeast-1.amazonaws.com",
 
95
        "https://iam.amazonaws.com",
 
96
}
 
97
 
 
98
var APSoutheast2 = Region{
 
99
        "ap-southeast-2",
 
100
        "https://ec2.ap-southeast-2.amazonaws.com",
 
101
        "https://s3-ap-southeast-2.amazonaws.com",
 
102
        "",
 
103
        true,
 
104
        true,
 
105
        "https://sdb.ap-southeast-2.amazonaws.com",
 
106
        "https://sns.ap-southeast-2.amazonaws.com",
 
107
        "https://sqs.ap-southeast-2.amazonaws.com",
 
108
        "https://iam.amazonaws.com",
 
109
}
 
110
 
 
111
var APNortheast = Region{
 
112
        "ap-northeast-1",
 
113
        "https://ec2.ap-northeast-1.amazonaws.com",
 
114
        "https://s3-ap-northeast-1.amazonaws.com",
 
115
        "",
 
116
        true,
 
117
        true,
 
118
        "https://sdb.ap-northeast-1.amazonaws.com",
 
119
        "https://sns.ap-northeast-1.amazonaws.com",
 
120
        "https://sqs.ap-northeast-1.amazonaws.com",
 
121
        "https://iam.amazonaws.com",
 
122
}
 
123
 
 
124
var SAEast = Region{
 
125
        "sa-east-1",
 
126
        "https://ec2.sa-east-1.amazonaws.com",
 
127
        "https://s3-sa-east-1.amazonaws.com",
 
128
        "",
 
129
        true,
 
130
        true,
 
131
        "https://sdb.sa-east-1.amazonaws.com",
 
132
        "https://sns.sa-east-1.amazonaws.com",
 
133
        "https://sqs.sa-east-1.amazonaws.com",
 
134
        "https://iam.amazonaws.com",
 
135
}
 
136
 
 
137
var Regions = map[string]Region{
 
138
        APNortheast.Name:  APNortheast,
 
139
        APSoutheast.Name:  APSoutheast,
 
140
        APSoutheast2.Name: APSoutheast2,
 
141
        EUWest.Name:       EUWest,
 
142
        USEast.Name:       USEast,
 
143
        USWest.Name:       USWest,
 
144
        USWest2.Name:      USWest2,
 
145
        SAEast.Name:       SAEast,
 
146
}
 
147
 
 
148
type Auth struct {
 
149
        AccessKey, SecretKey string
 
150
}
 
151
 
 
152
var unreserved = make([]bool, 128)
 
153
var hex = "0123456789ABCDEF"
 
154
 
 
155
func init() {
 
156
        // RFC3986
 
157
        u := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890-_.~"
 
158
        for _, c := range u {
 
159
                unreserved[c] = true
 
160
        }
 
161
}
 
162
 
 
163
// EnvAuth creates an Auth based on environment information.
 
164
// The AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment
 
165
// variables are used.
 
166
func EnvAuth() (auth Auth, err error) {
 
167
        auth.AccessKey = os.Getenv("AWS_ACCESS_KEY_ID")
 
168
        auth.SecretKey = os.Getenv("AWS_SECRET_ACCESS_KEY")
 
169
        if auth.AccessKey == "" {
 
170
                err = errors.New("AWS_ACCESS_KEY_ID not found in environment")
 
171
        }
 
172
        if auth.SecretKey == "" {
 
173
                err = errors.New("AWS_SECRET_ACCESS_KEY not found in environment")
 
174
        }
 
175
        return
 
176
}
 
177
 
 
178
// Encode takes a string and URI-encodes it in a way suitable
 
179
// to be used in AWS signatures.
 
180
func Encode(s string) string {
 
181
        encode := false
 
182
        for i := 0; i != len(s); i++ {
 
183
                c := s[i]
 
184
                if c > 127 || !unreserved[c] {
 
185
                        encode = true
 
186
                        break
 
187
                }
 
188
        }
 
189
        if !encode {
 
190
                return s
 
191
        }
 
192
        e := make([]byte, len(s)*3)
 
193
        ei := 0
 
194
        for i := 0; i != len(s); i++ {
 
195
                c := s[i]
 
196
                if c > 127 || !unreserved[c] {
 
197
                        e[ei] = '%'
 
198
                        e[ei+1] = hex[c>>4]
 
199
                        e[ei+2] = hex[c&0xF]
 
200
                        ei += 3
 
201
                } else {
 
202
                        e[ei] = c
 
203
                        ei += 1
 
204
                }
 
205
        }
 
206
        return string(e[:ei])
 
207
}