fastly 0.98 → 0.99

Sign up to get free protection for your applications and to get access to all the features.
data/Changes CHANGED
@@ -1,3 +1,8 @@
1
+ 2012-05-01 v0.99
2
+
3
+ Fix some SSL issues
4
+ Allow some admin functionality
5
+
1
6
  2012-02-02 v0.98
2
7
 
3
8
  Make deactivate! work
data/README.md CHANGED
@@ -2,13 +2,13 @@ Fastly - client library for interacting with the Fastly web acceleration service
2
2
 
3
3
  # Example
4
4
 
5
- fastly = Fastly.new(login_opts);
5
+ fastly = Fastly.new(login_opts)
6
6
 
7
- current_user = fastly.current_user;
8
- current_customer = fastly.current_customer;
7
+ current_user = fastly.current_user
8
+ current_customer = fastly.current_customer
9
9
 
10
- user = fastly.get_user(current_user.id);
11
- customer = fastly.get_customer(current_customer.id);
10
+ user = fastly.get_user(current_user.id)
11
+ customer = fastly.get_customer(current_customer.id)
12
12
 
13
13
  puts "Name: #{user.name}"
14
14
  puts "Works for #{user.customer.name}"
@@ -16,35 +16,41 @@ Fastly - client library for interacting with the Fastly web acceleration service
16
16
  puts "Which has the owner #{customer.owner.name}"
17
17
 
18
18
  # Let's see which services we have defined
19
- customer.list_services.each do |service|
20
- puts service.id
21
- puts service.name
22
- service.versions.each do |version
23
- puts "\t#{version.number}"
19
+ fastly.list_services.each do |service|
20
+ puts "Service ID: #{service.id}"
21
+ puts "Service Name: #{service.name}"
22
+ puts "Service Versions:"
23
+ service.versions.each do |version|
24
+ puts "\t#{version.number}"
25
+ end
24
26
  end
25
27
 
26
- service = fastly.create_service(:name => "MyFirstService");
27
- latest_version = service.version;
28
+ service = fastly.create_service(:name => "MyFirstService")
29
+ latest_version = service.version
28
30
 
29
31
  # Create a domain and a backend for the service ...
30
- domain = fastly.create_domain(:service_id => service.id, :version => latest_version.number, :name => "www.example.com");
31
- backend = fastly.create_backend(:service_id => service.id, :version => latest_version.number, :ipv4 => "127.0.0.1", :port => 80);
32
+ domain = fastly.create_domain(:service_id => service.id, :version => latest_version.number, :name => "www.example.com")
33
+ backend = fastly.create_backend(:service_id => service.id, :version => latest_version.number, :name => "Backend 1", :ipv4 => "192.0.43.10", :port => 80)
32
34
 
33
35
  # ... and activate it. You're now hosted on Fastly.
34
36
  latest_version.activate
35
37
 
36
38
  # Let's take a peek at the VCL that Fastly generated for us
37
- vcl = latest_version.generated_vcl;
39
+ vcl = latest_version.generated_vcl
38
40
  puts "Generated VCL file is:\n#{vcl.content}"
39
41
 
40
42
  # Now let's create a new version ...
41
- new_version = latest_version.clone;
43
+ new_version = latest_version.clone
42
44
  # ... add a new backend ...
43
- new_backend = fastly.create_backend(:service_id => service.id, :version => new_version.number, :ipv4 => "192.168.0.1", :port => 8080);
44
- # ... and upload some custome vcl (presuming we have permissions)
45
- new_version.upload_vcl(vcl_name, File.read(vcl_file));
45
+ new_backend = fastly.create_backend(:service_id => service.id, :version => new_version.number, :name => "Backend 2", :ipv4 => "74.125.224.136", :port => 8080)
46
+ # ... add a director to switch between them
47
+ director = fastly.create_director(:service_id => service.id, :version => new_version.number, :name => "My Director")
48
+ director.add_backend(backend)
49
+ director.add_backend(new_backend)
50
+ # ... and upload some custom vcl (presuming we have permissions)
51
+ new_version.upload_vcl(vcl_name, File.read(vcl_file))
46
52
 
47
- new_version.activate;
53
+ new_version.activate
48
54
 
49
55
  # Copyright
50
56
 
File without changes
data/lib/fastly.rb CHANGED
@@ -5,7 +5,7 @@
5
5
  # A client library for interacting with the Fastly web acceleration service
6
6
  class Fastly
7
7
  # The current version of the library
8
- VERSION = "0.98"
8
+ VERSION = "0.99"
9
9
 
10
10
  require 'fastly/fetcher'
11
11
  require 'fastly/client'
@@ -67,6 +67,15 @@ class Fastly
67
67
  @current_user ||= get(User)
68
68
  end
69
69
 
70
+ # Set the current customer to act as.
71
+ # NOTE: this will only work if you're an admin
72
+ def set_customer(id)
73
+ raise Fastly::FullAuthRequired "You must be fully authed to set the customer" unless self.fully_authed?;
74
+ raise Fastly::AdminRequired "You must be an admin to set the customer" unless self.current_user.can_do?(:admin);
75
+ @current_customer = nil
76
+ client.set_customer(id);
77
+ end
78
+
70
79
  # Return a hash representing all commands available.
71
80
  #
72
81
  # Useful for information.
data/lib/fastly/client.rb CHANGED
@@ -15,18 +15,19 @@ class Fastly
15
15
  CURB_FU=false
16
16
  end
17
17
 
18
- attr_accessor :http, :api_key, :user, :password, :cookie
18
+ attr_accessor :http, :api_key, :user, :password, :cookie, :customer
19
19
 
20
20
  def initialize(opts)
21
21
  [:api_key, :user, :password].each do |key|
22
22
  self.send("#{key}=", opts[key]) if opts.has_key?(key)
23
23
  end
24
- base = opts[:base_url] || "https://api.fastly.com"
25
- port = opts[:base_port] || 80
26
- uri = URI.parse(base)
27
- scheme = uri.scheme
28
- host = uri.host
29
- curb = opts.has_key?(:use_curb) ? !!opts[:use_curb] && CURB_FU : CURB_FU
24
+ base = opts[:base_url] || "https://api.fastly.com"
25
+ customer = opts[:customer]
26
+ uri = URI.parse(base)
27
+ scheme = uri.scheme
28
+ host = uri.host
29
+ curb = opts.has_key?(:use_curb) ? !!opts[:use_curb] && CURB_FU : CURB_FU
30
+ port = opts.has_key?(:base_port) ? opts[:base_port] : (scheme == "https") ? 443 : 80
30
31
  self.http = curb ? Fastly::Client::Curl.new(host, port) : Net::HTTP.new(host, port)
31
32
  self.http.use_ssl = (scheme == "https")
32
33
  return self unless fully_authed?
@@ -49,6 +50,10 @@ class Fastly
49
50
  def fully_authed?
50
51
  !(user.nil? || password.nil?)
51
52
  end
53
+
54
+ def set_customer(id)
55
+
56
+ end
52
57
 
53
58
  def get(path, params={})
54
59
  path += "?"+make_params(params) unless params.empty?
@@ -81,7 +86,9 @@ class Fastly
81
86
  end
82
87
 
83
88
  def headers
84
- (fully_authed? ? { 'Cookie' => cookie } : { 'X-Fastly-Key' => api_key }).merge( 'Content-Accept' => 'application/json')
89
+ headers = fully_authed? ? { 'Cookie' => cookie } : { 'X-Fastly-Key' => api_key }
90
+ headers.merge( 'Fastly-Explicit-Customer' => customer ) if customer
91
+ headers.merge( 'Content-Accept' => 'application/json')
85
92
  end
86
93
 
87
94
  def make_params(params)
@@ -102,7 +109,7 @@ class Fastly
102
109
  class Curl
103
110
  attr_accessor :host, :port, :protocol
104
111
 
105
- def initialize(host, port=80)
112
+ def initialize(host, port=443)
106
113
  self.host = host
107
114
  self.port = port
108
115
  self.protocol = 'https'
@@ -7,13 +7,13 @@ class Fastly
7
7
  class Error < RuntimeError; end
8
8
  # :nodoc:
9
9
  class Unauthorized < AuthRequired; end
10
+ # :nodoc:
11
+ class AdminRequired < AuthRequired; end
10
12
 
11
13
  module Fetcher # :nodoc: all
12
14
 
13
15
  # Get the current Fastly::Client
14
16
  def client(opts={})
15
- opts[:base_url] ||= 'api.fastly.com'
16
- opts[:base_port] ||= 80
17
17
  @client ||= Fastly::Client.new(opts)
18
18
  end
19
19
 
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fastly
3
3
  version: !ruby/object:Gem::Version
4
- hash: 207
4
+ hash: 205
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 98
9
- version: "0.98"
8
+ - 99
9
+ version: "0.99"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Fastly Inc
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2012-02-16 00:00:00 Z
17
+ date: 2012-05-01 00:00:00 Z
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: json