Class: Deltacloud::Drivers::Google::GoogleDriver

Inherits:
BaseDriver
  • Object
show all
Defined in:
lib/deltacloud/drivers/google/google_driver.rb

Constant Summary

Constants inherited from BaseDriver

BaseDriver::MEMBER_SHOW_METHODS, BaseDriver::STATE_MACHINE_OPTS

Instance Method Summary collapse

Methods inherited from BaseDriver

#address, #api_provider, #blob, #bucket, #catched_exceptions_list, #configured_providers, constraints, define_hardware_profile, define_instance_states, driver_name, feature, features, #filter_hardware_profiles, #filter_on, #find_hardware_profile, #firewall, #hardware_profile, #hardware_profiles, hardware_profiles, #has_capability?, #has_feature?, has_feature?, #image, #instance, #instance_actions_for, #instance_state_machine, instance_state_machine, #key, #name, #realm, #storage_snapshot, #storage_volume, #supported_collections

Methods included from Exceptions

exception_from_status, exceptions, included, logger, #safely

Instance Method Details

#blob_data(credentials, bucket_id, blob_id, opts = {}) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/deltacloud/drivers/google/google_driver.rb', line 98

def blob_data(credentials, bucket_id, blob_id, opts={})
  google_client = new_client(credentials)
  safely do
    google_client.get_object(bucket_id, blob_id) do |chunk|
      yield chunk
    end
  end
end

#blob_metadata(credentials, opts = {}) ⇒ Object

- Blob Metadada -



171
172
173
174
175
176
177
178
# File 'lib/deltacloud/drivers/google/google_driver.rb', line 171

def (credentials, opts = {})
  google_client = new_client(credentials)
  safely do
    google_blob = google_client.head_object(opts['bucket'], opts[:id]).headers
    meta_hash = google_blob.inject({}){|result, (k,v)| result[k]=v if k=~/^x-goog-meta-/i ; result}
    meta_hash.gsub_keys("x-goog-meta-", "")
  end
end

#blob_stream_connection(params) ⇒ Object

params: :user,:password,:bucket,:blob,:content_type,:content_length,:metadata



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
# File 'lib/deltacloud/drivers/google/google_driver.rb', line 127

def blob_stream_connection(params)
  client = Fog::Storage.new({:provider => :google, :google_storage_access_key_id => params[:user],
                             :google_storage_secret_access_key => params[:password]})
  google_request_uri = "https://#{client.instance_variable_get(:@host)}"
  uri = URI.parse(google_request_uri)
  conn_params = {} # build hash for the Fog signature method
  conn_params[:headers] = {} #put the metadata here
  conn_params[:host] = uri.host
  conn_params[:path] = "#{params[:bucket]}/#{CGI.escape(params[:blob])}"
  conn_params[:method] = "PUT"
  timestamp = Fog::Time.now.to_date_header
  conn_params[:headers]['Date'] = timestamp
  conn_params[:headers]['Content-Type'] = params[:content_type]
   = params[:metadata] || {}
  BlobHelper::(, 'x-goog-meta-')
  .each{|k,v| conn_params[:headers][k]=v}
  auth_string = "GOOG1 #{params[:user]}:#{client.signature(conn_params)}"

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = Net::HTTP::Put.new("/#{conn_params[:path]}")
  request['Host'] = conn_params[:host]
  request['Date'] = conn_params[:headers]['Date']
  request['Content-Type'] = conn_params[:headers]['Content-Type']
  request['Content-Length'] = params[:content_length]
  request['Authorization'] = auth_string
  .each{|k,v| request[k] = v}
  return http, request
end

#blobs(credentials, opts = {}) ⇒ Object

– Blobs –



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/deltacloud/drivers/google/google_driver.rb', line 79

def blobs(credentials, opts={})
  blobs = []
  google_client = new_client(credentials)
  safely do
    google_blob = google_client.head_object(opts['bucket'], opts[:id]).headers
    meta_hash = google_blob.inject({}){|result, (k,v)| result[k]=v if k=~/^x-goog-meta-/i ; result}
    meta_hash.gsub_keys("x-goog-meta-", "")
    blobs << Blob.new({   :id => opts[:id],
               :bucket => opts['bucket'],
               :content_length => google_blob['Content-Length'],
               :content_type => google_blob['Content-Type'],
               :last_modified => google_blob['Last-Modified'],
               :user_metadata => meta_hash
            })

  end
  blobs
end

#buckets(credentials, opts = {}) ⇒ Object

– Buckets –



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/deltacloud/drivers/google/google_driver.rb', line 30

def buckets(credentials, opts={})
  buckets = []
  google_client = new_client(credentials)
  safely do
    if opts[:id]
      bucket = google_client.get_bucket(opts[:id])
      buckets << convert_bucket(bucket.body)
    else
      google_client.get_service.body['Buckets'].each do |bucket|
        buckets << Bucket.new({:name => bucket['Name'], :id => bucket['Name']})
      end
    end
  end
  buckets = filter_on(buckets, :id, opts)
end

#create_blob(credentials, bucket_id, blob_id, blob_data, opts = {}) ⇒ Object

– Create Blob –



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/deltacloud/drivers/google/google_driver.rb', line 110

def create_blob(credentials, bucket_id, blob_id, blob_data, opts={})
  google_client = new_client(credentials)
  safely do
     = BlobHelper::(opts)
    BlobHelper::(opts, 'x-goog-meta-')
    opts['Content-Type'] = blob_data[:type]
    google_client.put_object(bucket_id, blob_id, blob_data[:tempfile], opts)
    Blob.new({ :id => blob_id,
               :bucket => bucket_id,
               :content_length => File.size(blob_data[:tempfile]).to_s,
               :content_type => blob_data[:type],
               :last_modified => "",
               :user_metadata =>   })
  end
end

#create_bucket(credentials, name, opts = {}) ⇒ Object

– Create bucket - valid values for location ‘EU’,‘US’ –



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/deltacloud/drivers/google/google_driver.rb', line 49

def create_bucket(credentials, name, opts={})
  google_client = new_client(credentials)
  safely do
    bucket_location = opts['location']
    if (bucket_location && bucket_location.size > 0)
      res = google_client.put_bucket(name, {"LocationConstraint" => opts['location']})
    else
      google_client.put_bucket(name)
    end
    #res.status should be eql 200 - but fog will explode if not all ok...
    Bucket.new({ :id => name,
                 :name => name,
                 :size => 0,
                 :blob_list => [] })
  end
end

#delete_blob(credentials, bucket_id, blob_id, opts = {}) ⇒ Object

– Delete Blob –



161
162
163
164
165
166
# File 'lib/deltacloud/drivers/google/google_driver.rb', line 161

def delete_blob(credentials, bucket_id, blob_id, opts={})
  google_client = new_client(credentials)
  safely do
    google_client.delete_object(bucket_id, blob_id)
  end
end

#delete_bucket(credentials, name, opts = {}) ⇒ Object

– Delete bucket –



69
70
71
72
73
74
# File 'lib/deltacloud/drivers/google/google_driver.rb', line 69

def delete_bucket(credentials, name, opts={})
  google_client = new_client(credentials)
  safely do
    google_client.delete_bucket(name)
  end
end

#update_blob_metadata(credentials, opts = {}) ⇒ Object

- Update Blob Metadata -



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/deltacloud/drivers/google/google_driver.rb', line 183

def (credentials, opts={})
  google_client = new_client(credentials)
  safely do
    meta_hash = BlobHelper::(opts['meta_hash'], 'x-goog-meta-')
    options = {'x-goog-metadata-directive'=>'REPLACE'}
    options.merge!(meta_hash)
    bucket = opts['bucket']
    blob = opts[:id]
    google_client.copy_object(bucket, blob, bucket, blob, options) #source,source,target,target,options
    meta_hash.gsub_keys("x-goog-meta-", "")
  end
end

#valid_credentials?(credentials) ⇒ Boolean



196
197
198
199
200
201
202
203
# File 'lib/deltacloud/drivers/google/google_driver.rb', line 196

def valid_credentials?(credentials)
  begin
    new_client(credentials).get_service && true
  rescue
    return false
  end
  return true
end