fastly 1.00 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,68 @@
1
+ class Fastly
2
+ # An s3 endpoint to stream logs to
3
+ class S3Logging < BelongsToServiceAndVersion
4
+ attr_accessor :service_id, :name, :bucket_name, :access_key, :secret_key, :path, :period, :gzip_level, :format, :response_condition
5
+
6
+ ##
7
+ # :attr: service_id
8
+ #
9
+ # The id of the service this belongs to.
10
+
11
+ ##
12
+ # :attr: version
13
+ #
14
+ # The number of the version this belongs to.
15
+
16
+ ##
17
+ # :attr: name
18
+ #
19
+ # The name for this s3 rule
20
+
21
+ ##
22
+ # :attr: bucket_name
23
+ #
24
+ # The name of the s3 bucket
25
+
26
+ ##
27
+ # :attr: access_key
28
+ #
29
+ # The bucket's s3 account access key
30
+
31
+ ##
32
+ # :attr: secret_key
33
+ #
34
+ # The bucket's s3 account secret key
35
+
36
+ ##
37
+ # :attr: path
38
+ #
39
+ # The path to upload logs to
40
+
41
+ ##
42
+ # :attr: period
43
+ #
44
+ # How frequently the logs should be dumped (in seconds, default 3600)
45
+
46
+ ##
47
+ # :attr: gzip_level
48
+ #
49
+ # What level of gzip compression to have when dumping the logs (default
50
+ # 0, no compression).
51
+
52
+ ##
53
+ # :attr: format
54
+ #
55
+ # Apache style log formatting
56
+
57
+ ##
58
+ # :attr: response_condition
59
+ #
60
+ # When to execute the s3 logging. If empty, always execute.
61
+
62
+ private
63
+ # The path here is a bit non-standard
64
+ def self.path
65
+ "logging/s3"
66
+ end
67
+ end
68
+ end
@@ -59,13 +59,13 @@ class Fastly
59
59
 
60
60
  # Purge all assets from this service.
61
61
  def purge_all
62
- res = client.put(get_path(self.id)+"/purge_all")
62
+ res = fetcher.client.post(Fastly::Service.get_path(self.id)+"/purge_all")
63
63
  end
64
64
 
65
65
 
66
66
  # Purge anything with the specific key from the given service.
67
67
  def purge_by_key(key)
68
- res = client.put(get_path(self.id)+"/purge/#{key}")
68
+ res = fetcher.client.post(Fastly::Service.get_path(self.id)+"/purge/#{key}")
69
69
  end
70
70
 
71
71
  # Set all the versions that this service has had.
@@ -85,7 +85,7 @@ class Fastly
85
85
 
86
86
  # A deep hash of nested details
87
87
  def details(opts={})
88
- client.get(get_path(self.id)+"/details", opts);
88
+ fetcher.client.get(Fastly::Service.get_path(self.id)+"/details", opts);
89
89
  end
90
90
 
91
91
  # Get the Customer object for this Service
@@ -109,4 +109,4 @@ class Fastly
109
109
  return nil if hash.nil?
110
110
  klass.new(hash, self)
111
111
  end
112
- end
112
+ end
@@ -19,7 +19,6 @@ class Fastly
19
19
  #
20
20
  # Return a hash containing key/value pairs of settings
21
21
  #
22
-
23
22
 
24
23
  # :nodoc:
25
24
  def self.get_path(service, number)
@@ -38,12 +37,12 @@ class Fastly
38
37
 
39
38
  # :nodoc:
40
39
  def self.post_path
41
- raise "You can't POST to an invoice"
40
+ raise "You can't POST to an setting"
42
41
  end
43
42
 
44
43
  # :nodoc:
45
44
  def self.delete_path
46
- raise "You can't DELETE to an invoice"
45
+ raise "You can't DELETE to an setting"
47
46
  end
48
47
 
49
48
  # :nodoc:
@@ -59,7 +58,13 @@ class Fastly
59
58
 
60
59
  # Get the Settings object for the specified Version
61
60
  def get_settings(service, number)
62
- get(Fastly::Settings, service, number)
61
+ klass = Fastly::Settings
62
+ hash = client.get(Fastly::Settings.get_path(service, number))
63
+
64
+ return nil if hash.nil?
65
+ hash["settings"] = Hash[["general.default_host", "general.default_ttl"].collect { |var| [var, hash.delete(var)] }]
66
+
67
+ return klass.new(hash, self)
63
68
  end
64
69
 
65
70
  # Update the Settings object for the specified Version
@@ -0,0 +1,7 @@
1
+ # add an underscore method to String, so we can easily convert CamelCase to
2
+ # camel_case for CacheSetting and RequestSetting objects.
3
+ class String
4
+ def underscore
5
+ gsub(/([^A-Z])([A-Z]+)/, '\1_\2').downcase
6
+ end
7
+ end
@@ -1,7 +1,7 @@
1
1
  class Fastly
2
2
  # An endpoint to stream syslogs to
3
3
  class Syslog < BelongsToServiceAndVersion
4
- attr_accessor :service_id, :name, :comment, :ipv4, :ipv6, :hostname, :port, :format
4
+ attr_accessor :service_id, :name, :comment, :ipv4, :ipv6, :hostname, :port, :format, :response_conditions
5
5
 
6
6
  ##
7
7
  # :attr: service_id
@@ -61,5 +61,10 @@ class Fastly
61
61
  # :attr: format
62
62
  #
63
63
  # Format to log like in apache format
64
+
65
+ ##
66
+ # :attr: response_condition
67
+ #
68
+ # name of a response_condition to filter the log on, if empty it always logs
64
69
  end
65
70
  end
@@ -96,6 +96,13 @@ class Fastly
96
96
  return Fastly::Version.new(hash, fetcher)
97
97
  end
98
98
 
99
+ # Create an entirely new version, not cloned from the previous one.
100
+ def self.create_new(fetcher, opts)
101
+ hash = fetcher.client.post(Fastly::Version.post_path(opts))
102
+ return nil if hash.nil?
103
+ return Fastly::Version.new(hash, fetcher)
104
+ end
105
+
99
106
  # Get the generated VCL object for this Version (which must have been activated first)
100
107
  #
101
108
  # Won't return the content of the VCL unless you pass in
@@ -1,10 +1,6 @@
1
- #!/usr/bin/env ruby
1
+ require 'helper'
2
2
 
3
- require 'rubygems'
4
- require 'test/unit'
5
- require File.expand_path(File.dirname(__FILE__) + '/helper')
6
-
7
- class AdminTest < Test::Unit::TestCase
3
+ class AdminTest < Fastly::TestCase
8
4
  def setup
9
5
  opts = login_opts(:full).merge(:use_curb => false)
10
6
  begin
@@ -16,20 +12,20 @@ class AdminTest < Test::Unit::TestCase
16
12
  exit(-1)
17
13
  end
18
14
  end
19
-
15
+
20
16
  def test_creating_and_updating_customer
21
17
  return unless @fastly.current_user.can_do?(:admin)
22
18
  customer = @fastly.create_customer(:name => "fastly-ruby-test-customer-#{get_rand}")
23
19
  email = "fastly-ruby-test-#{get_rand}-new@example.com"
24
20
  user = @fastly.create_user(:login => email, :name => "New User")
25
21
  customer.owner_id = user.id
26
-
22
+
27
23
  tmp = @fastly.update_customer(customer)
28
24
  assert tmp
29
25
  assert_equal customer.id, tmp.id
30
26
  assert_equal customer.owner.id, tmp.owner.id
31
27
  end
32
-
28
+
33
29
  def test_creating_and_updating_customer_with_owner
34
30
  return unless @fastly.current_user.can_do?(:admin)
35
31
  email = "fastly-ruby-test-#{get_rand}-new@example.com"
@@ -37,5 +33,5 @@ class AdminTest < Test::Unit::TestCase
37
33
  assert customer
38
34
  assert_equal customer.owner.login, email
39
35
  end
40
-
41
- end
36
+
37
+ end
@@ -1,11 +1,8 @@
1
- #!/usr/bin/env ruby
2
- require 'rubygems'
3
- require 'test/unit'
4
- require File.expand_path(File.dirname(__FILE__) + '/helper')
1
+ require 'helper'
5
2
 
6
- class ApiKeyTest < Test::Unit::TestCase
3
+ class ApiKeyTest < Fastly::TestCase
7
4
  include CommonTests
8
-
5
+
9
6
  def setup
10
7
  @opts = login_opts(:api_key)
11
8
  begin
@@ -16,21 +13,21 @@ class ApiKeyTest < Test::Unit::TestCase
16
13
  exit(-1)
17
14
  end
18
15
  end
19
-
16
+
20
17
  def test_raw_client
21
18
  user = customer = nil
22
19
  assert_raise(Fastly::Error) {
23
20
  user = @client.get('/current_user')
24
21
  }
25
- assert_equal nil, user
22
+ assert_equal nil, user
26
23
 
27
24
  customer = @client.get('/current_customer')
28
25
  assert customer
29
26
  assert_equal @opts[:customer], customer['name']
30
27
  end
31
-
32
-
33
- def test_current_user_and_customer
28
+
29
+
30
+ def test_current_user_and_customer
34
31
  current_user = current_customer = nil
35
32
  assert_raise(Fastly::FullAuthRequired) {
36
33
  current_user = @fastly.current_user
@@ -40,9 +37,9 @@ class ApiKeyTest < Test::Unit::TestCase
40
37
  customer = @fastly.current_customer
41
38
  assert customer
42
39
  assert_equal @opts[:customer], customer.name
43
- end
44
-
45
-
40
+ end
41
+
42
+
46
43
  def test_purging
47
44
  #assert @fastly.purge('foo')
48
45
  # TODO Won't work until we get fixtures in Heavenly
@@ -22,61 +22,61 @@ module CommonTests
22
22
  assert_equal settings.service_id, service.id
23
23
  assert_equal settings.version.to_s, version.number.to_s
24
24
  assert_equal settings.settings['general.default_ttl'], default_ttl
25
-
25
+
26
26
  settings.settings['general.default_ttl'] = default_ttl = "888888888"
27
27
  settings.save!;
28
28
 
29
29
  settings = version.settings
30
30
  assert_equal settings.settings['general.default_ttl'].to_s, default_ttl;
31
-
31
+
32
32
  services = @fastly.list_services
33
33
  assert !services.empty?
34
34
  assert !services.select { |s| s.name == name }.empty?
35
-
35
+
36
36
  service = @fastly.search_services( :name => name )
37
37
  assert service
38
38
  assert name, service.name
39
-
40
-
39
+
40
+
41
41
  service = @fastly.search_services( :name => name, :version => version.number )
42
42
  assert services
43
43
  assert name, service.name
44
-
44
+
45
45
  version2 = @fastly.create_version(:service_id => service.id)
46
46
  assert version2
47
- assert_equal = version.number.to_i+1, version2.number.to_i
48
-
47
+ assert_equal = version.number.to_i+1, version2.number.to_i
48
+
49
49
  version3 = version2.clone
50
50
  assert version3
51
- assert_equal = version2.number.to_i+1, version3.number.to_i
52
-
51
+ assert_equal = version2.number.to_i+1, version3.number.to_i
52
+
53
53
  number = version3.number.to_i
54
-
54
+
55
55
  backend_name = "fastly-test-backend-#{get_rand}"
56
- backend = begin
57
- @fastly.create_backend(:service_id => service.id, :version => number, :hostname => 'localhost', :name => backend_name)
58
- rescue Fastly::Error
56
+ backend = begin
57
+ @fastly.create_backend(:service_id => service.id, :version => number, :hostname => 'localhost', :name => backend_name)
58
+ rescue Fastly::Error
59
59
  end
60
60
  assert_nil backend
61
-
61
+
62
62
  backend = @fastly.create_backend(:service_id => service.id, :version => number, :address => '74.125.224.146', :name => backend_name)
63
63
  assert backend
64
64
  assert_equal backend.service_id, service.id
65
65
  #assert_equal backend.ipv4, '74.125.224.146'
66
66
  assert_equal backend.address, '74.125.224.146'
67
67
  assert_equal backend.port.to_s, '80'
68
-
68
+
69
69
  backend.address = 'thegestalt.org'
70
70
  backend.port = '9092'
71
71
  @fastly.update_backend(backend)
72
72
  backend = @fastly.get_backend(service.id, number, backend_name)
73
-
73
+
74
74
  assert backend
75
75
  assert_equal backend.address, 'thegestalt.org'
76
76
  #assert_equal backend.hostname, 'thegestalt.org'
77
77
  assert_equal backend.port.to_s, '9092'
78
-
79
-
78
+
79
+
80
80
  domain_name = "fastly-test-domain-#{get_rand}-example.com"
81
81
  domain = @fastly.create_domain(:service_id => service.id, :version => number, :name => domain_name)
82
82
  assert domain
@@ -84,13 +84,13 @@ module CommonTests
84
84
  assert_equal domain.service.id, service.id
85
85
  assert_equal domain.version_number.to_s, number.to_s
86
86
  assert_equal domain.version.number.to_s, number.to_s
87
-
87
+
88
88
  domain.comment = "Flibbety gibbet"
89
89
  domain.save!
90
90
  domain = @fastly.get_domain(service.id, number, domain_name)
91
91
  assert_equal domain.name, domain_name
92
92
  assert_equal domain.comment, "Flibbety gibbet"
93
-
93
+
94
94
  director_name = "fastly-test-director-#{get_rand}"
95
95
  director = @fastly.create_director(:service_id => service.id, :version => number, :name => director_name)
96
96
  assert director
@@ -98,10 +98,10 @@ module CommonTests
98
98
  assert_equal director.service.id, service.id
99
99
  assert_equal director.version_number.to_s, number.to_s
100
100
  assert_equal director.version.number.to_s, number.to_s
101
-
101
+
102
102
  assert director.add_backend(backend)
103
103
  generated2 = version3.generated_vcl
104
-
104
+
105
105
  origin_name = "fastly-test-origin-#{get_rand}"
106
106
  origin = @fastly.create_origin(:service_id => service.id, :version => number, :name => origin_name)
107
107
  assert origin
@@ -109,7 +109,56 @@ module CommonTests
109
109
  assert_equal origin.service.id, service.id
110
110
  assert_equal origin.version_number.to_s, number.to_s
111
111
  # assert_equal origin.version.number.to_s, number.to_s
112
-
112
+
113
+ condition_name = "fastly-test-condition-#{get_rand}"
114
+ condition_statement = 'req.url ~ "^/foo"'
115
+ condition = @fastly.create_condition(:service_id => service.id, :version => number, :name => condition_name, :statement => condition_statement, :type => "REQUEST")
116
+ assert condition
117
+ assert_equal condition_name, condition.name
118
+ assert_equal condition_statement, condition.statement
119
+
120
+ cache_condition_name = "cache-#{condition_name}"
121
+ cache_condition = @fastly.create_condition(:service_id => service.id, :version => number, :name => cache_condition_name, :statement => condition_statement, :type => "CACHE")
122
+ assert cache_condition
123
+ assert_equal cache_condition_name, cache_condition.name
124
+ assert_equal condition_statement, cache_condition.statement
125
+
126
+ cache_setting_name = "fastly-cache-setting-#{get_rand}"
127
+ cache_setting = @fastly.create_cache_setting( :service_id => service.id, :version => number, :name => cache_setting_name, :ttl => 3600, :stale_ttl => 10001, :cache_condition => cache_condition_name)
128
+ assert cache_setting
129
+ assert_equal cache_setting.name, cache_setting_name
130
+ assert_equal cache_setting.ttl.to_s, 3600.to_s
131
+ assert_equal cache_setting.stale_ttl.to_s, 10001.to_s
132
+ assert_equal cache_setting.cache_condition, cache_condition_name
133
+
134
+ gzip_name = "fastly-test-gzip-#{get_rand}"
135
+ gzip = @fastly.create_gzip( :service_id => service.id, :version => number, :name => gzip_name, :extensions => "js css html", :content_types => "text/html")
136
+ assert gzip
137
+ assert_equal gzip_name, gzip.name
138
+ assert_equal "text/html", gzip.content_types
139
+ assert_equal "js css html", gzip.extensions
140
+
141
+ response_obj_name = "fastly-test-response-obj-#{get_rand}"
142
+ response_obj = @fastly.create_response_object( :service_id => service.id, :version => number, :name => response_obj_name, :status => 418, :response => "I'm a teapot", :content_type => "text/plain", :content => "short and stout")
143
+ assert response_obj
144
+ assert_equal response_obj_name, response_obj.name
145
+ assert_equal 418.to_s, response_obj.status
146
+ assert_equal "I'm a teapot", response_obj.response
147
+ assert_equal "text/plain", response_obj.content_type
148
+ assert_equal "short and stout", response_obj.content
149
+
150
+ response_condition_name = "fastly-response-condition-#{get_rand}"
151
+ response_condition = @fastly.create_condition(:service_id => service.id, :version => number, :name => response_condition_name, :statement => condition_statement, :type => "RESPONSE")
152
+ header_name = "fastly-header-test-#{get_rand}"
153
+ header = @fastly.create_header( :service_id => service.id, :version => number, :name => header_name, :response_condition => response_condition.name, :ignore_if_set => 1, :type => "response", :dst => "http.Cache-Control", :src => '"max-age=301"', :priority => 10, :action => "set")
154
+ assert header
155
+ assert_equal header.name, header_name
156
+ assert_equal header.response_condition, response_condition.name
157
+ assert_equal header.ignore_if_set.to_s, 1.to_s
158
+ assert_equal header.dst, "http.Cache-Control"
159
+ assert_equal header.src, '"max-age=301"'
160
+ assert_equal header.action, "set"
161
+
113
162
  assert version3.activate!
114
163
  assert version3.deactivate!
115
164
  assert !@fastly.get_service(version3.service_id).version.active
@@ -117,17 +166,23 @@ module CommonTests
117
166
  assert version3.activate!
118
167
  assert @fastly.get_service(version3.service_id).version.active
119
168
  assert @fastly.get_service(version3.service_id).version.active?
120
-
169
+
121
170
  generated = version3.generated_vcl(:no_content => true)
122
171
  assert generated
123
172
  assert generated.content.nil?
124
173
  generated = version3.generated_vcl
125
174
  assert !generated.content.nil?
126
175
  assert generated.content.match(/\.port = "9092"/ms)
127
-
176
+
128
177
  assert version3.validate
129
178
 
130
- #assert @fastly.deactivate_version(version2)
179
+ version3.deactivate!
180
+
181
+ version4 = Fastly::Version.create_new(service.fetcher, :service_id => service.id)
182
+ assert version4.number != version3.number
183
+ assert @fastly.list_directors(:service_id => service.id, :version => version4.number).length == 0
184
+
185
+ @fastly.delete_service(service)
131
186
  end
132
187
 
133
188
  def test_stats
@@ -139,13 +194,17 @@ module CommonTests
139
194
  assert tmp
140
195
  assert_equal name, tmp.name
141
196
 
142
- stats = service.stats
143
- assert stats
144
-
197
+ begin
198
+ stats = service.stats
199
+ rescue Fastly::Error => e
200
+ end
201
+ assert stats.nil?
202
+
145
203
  stats = service.stats(:all, :year => 2011, :month => 10)
146
- assert stats
204
+ assert stats
205
+ @fastly.delete_service(service)
147
206
  end
148
-
207
+
149
208
  def test_invoices
150
209
  name = "fastly-test-service-#{get_rand}"
151
210
  service = @fastly.create_service(:name => name)
@@ -156,20 +215,21 @@ module CommonTests
156
215
  assert invoice
157
216
  assert invoice.regions
158
217
  assert_equal invoice.service_id, service.id
159
-
218
+
160
219
  invoice = @fastly.get_invoice
161
220
  assert_equal Fastly::Invoice, invoice.class
162
-
221
+
163
222
  year = Time.now.year
164
223
  month = Time.now.month
165
-
224
+
166
225
  invoice = @fastly.get_invoice(year, month)
167
226
  assert_equal Fastly::Invoice, invoice.class
168
227
  assert_equal year, invoice.start.year
169
228
  assert_equal month, invoice.start.month
170
- assert_equal 1, invoice.start.day
229
+ assert_equal 1, invoice.start.day
171
230
  assert_equal year, invoice.end.year
172
231
  assert_equal month, invoice.end.month
173
- end
232
+ @fastly.delete_service(service)
233
+ end
174
234
 
175
- end
235
+ end