Class: DeltacloudVM::Client::Connection
- Inherits:
-
Object
- Object
- DeltacloudVM::Client::Connection
- Includes:
- Helpers::Model, Methods::Address, Methods::Api, Methods::BackwardCompatibility, Methods::Blob, Methods::Bucket, Methods::Common, Methods::Driver, Methods::Firewall, Methods::HardwareProfile, Methods::Image, Methods::Instance, Methods::InstanceState, Methods::Key, Methods::LoadBalancer, Methods::Metric, Methods::Realm, Methods::StorageSnapshot, Methods::StorageVolume
- Defined in:
- lib/deltacloud_vm/client/connection.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#entrypoint ⇒ Object
readonly
Returns the value of attribute entrypoint.
-
#request_driver ⇒ Object
readonly
Returns the value of attribute request_driver.
-
#request_provider ⇒ Object
readonly
Returns the value of attribute request_provider.
Instance Method Summary collapse
-
#cache_entrypoint!(force = false) ⇒ Object
Cache the API entrypoint (/api) for the current connection, so we don’t need to query /api everytime we ask if certain collection/operation is supported.
-
#initialize(opts = {}) ⇒ Connection
constructor
A new instance of Connection.
-
#use(driver_id, api_user, api_password, api_provider = nil) {|new_client| ... } ⇒ Object
Change the current driver and return copy of the client This allows chained calls like: client.driver(:ec2).instances.
-
#use_provider(provider_id) {|new_client| ... } ⇒ Object
Change the API provider but keep the current client credentials.
-
#valid_credentials? ⇒ Boolean
Check if the credentials used are valid for the current @connection.
Methods included from Methods::Metric
Methods included from Methods::LoadBalancer
#create_load_balancer, #destroy_load_balancer, #load_balancer, #load_balancers, #register_instance, #unregister_instance
Methods included from Methods::StorageVolume
#attach_storage_volume, #create_storage_volume, #destroy_storage_volume, #detach_storage_volume, #storage_volume, #storage_volumes
Methods included from Methods::StorageSnapshot
#create_storage_snapshot, #destroy_storage_snapshot, #storage_snapshot, #storage_snapshots
Methods included from Methods::Realm
Methods included from Methods::Key
#create_key, #destroy_key, #key, #keys
Methods included from Methods::InstanceState
#instance_state, #instance_states
Methods included from Methods::Instance
#create_instance, #destroy_instance, #instance, #instances, #reboot_instance, #start_instance, #stop_instance
Methods included from Methods::Image
#create_image, #destroy_image, #image, #images
Methods included from Methods::HardwareProfile
#hardware_profile, #hardware_profiles
Methods included from Methods::Firewall
#add_firewall_rule, #create_firewall, #destroy_firewall, #firewall, #firewalls
Methods included from Methods::Driver
Methods included from Methods::Common
#create_resource, #destroy_resource
Methods included from Methods::Bucket
#bucket, #buckets, #create_bucket, #destroy_bucket
Methods included from Methods::Blob
#blob, #blobs, #create_blob, #destroy_blob
Methods included from Methods::BackwardCompatibility
#api_host, #api_port, #connect, #discovered?, #use_driver, #with_config
Methods included from Methods::Api
#api_uri, #current_driver, #current_provider, #feature?, #features, #must_support!, #path, #support?, #supported_collections, #version
Methods included from Methods::Address
#address, #addresses, #associate_address, #create_address, #destroy_address, #disassociate_address
Methods included from Helpers::Model
#error, #from_collection, #from_resource, #model
Constructor Details
#initialize(opts = {}) ⇒ Connection
Returns a new instance of Connection.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/deltacloud_vm/client/connection.rb', line 45 def initialize(opts={}) @request_driver = opts[:driver] @request_provider = opts[:provider] @connection = Faraday.new(:url => opts[:url]) do |f| # NOTE: The order of this is somehow important for VCR # recording. f.request :url_encoded f.headers = deltacloud_request_headers f.basic_auth opts[:api_user], opts[:api_password] f.use DeltacloudVM::ErrorResponse f.adapter :net_http end cache_entrypoint! @request_driver ||= current_driver @request_provider ||= current_provider end |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
19 20 21 |
# File 'lib/deltacloud_vm/client/connection.rb', line 19 def connection @connection end |
#entrypoint ⇒ Object (readonly)
Returns the value of attribute entrypoint.
22 23 24 |
# File 'lib/deltacloud_vm/client/connection.rb', line 22 def entrypoint @entrypoint end |
#request_driver ⇒ Object (readonly)
Returns the value of attribute request_driver.
20 21 22 |
# File 'lib/deltacloud_vm/client/connection.rb', line 20 def request_driver @request_driver end |
#request_provider ⇒ Object (readonly)
Returns the value of attribute request_provider.
21 22 23 |
# File 'lib/deltacloud_vm/client/connection.rb', line 21 def request_provider @request_provider end |
Instance Method Details
#cache_entrypoint!(force = false) ⇒ Object
Cache the API entrypoint (/api) for the current connection, so we don’t need to query /api everytime we ask if certain collection/operation is supported
-
force -> If ‘true’ force to refresh stored cached entrypoint
107 108 109 110 |
# File 'lib/deltacloud_vm/client/connection.rb', line 107 def cache_entrypoint!(force=false) @entrypoint = nil if force @entrypoint ||= connection.get(path).body end |
#use(driver_id, api_user, api_password, api_provider = nil) {|new_client| ... } ⇒ Object
Change the current driver and return copy of the client This allows chained calls like: client.driver(:ec2).instances
-
driver_id -> The new driver id (:mock, :ec2, :rhevm, …)
-
api_user -> API user name
-
api_password -> API password
-
api_provider -> API provider (aka API_PROVIDER string)
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/deltacloud_vm/client/connection.rb', line 70 def use(driver_id, api_user, api_password, api_provider=nil, &block) new_client = self.class.new( :url => @connection.url_prefix.to_s, :api_user => api_user, :api_password => api_password, :provider => api_provider, :driver => driver_id ) new_client.cache_entrypoint! yield new_client if block_given? new_client end |
#use_provider(provider_id) {|new_client| ... } ⇒ Object
Change the API provider but keep the current client credentials. This allows to change the EC2 region and list instances in that region without need to supply credentials.
client.use_provider(‘eu-west-1’) { |p| p.instances }
-
provider_id -> API provider (aka API_PROVIDER)
91 92 93 94 95 96 97 98 99 |
# File 'lib/deltacloud_vm/client/connection.rb', line 91 def use_provider(provider_id, &block) new_client = self.clone new_connection = @connection.clone new_connection.headers['X-DeltacloudVM-Provider'] = provider_id new_client.connection = new_connection new_client.cache_entrypoint!(true) yield new_client if block_given? new_client end |
#valid_credentials? ⇒ Boolean
Check if the credentials used are valid for the current @connection
114 115 116 117 118 119 120 121 |
# File 'lib/deltacloud_vm/client/connection.rb', line 114 def valid_credentials? begin r = connection.get(path, { :force_auth => 'true' }) r.status == 200 rescue error(:authentication_error) false end end |