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

Constants included from Deltacloud

API_VERSION, CIMI_API_VERSION

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 Deltacloud

[], config, configure, connect, database, default_frontend, drivers, enabled_frontends, frontend_required?, frontends, generate_routes, initialize_database, need_database?, new, require_frontend!

Methods included from CollectionMethods

#collection_exists?, #collection_names, #collections

Methods included from Exceptions

exception_from_status, exceptions, included, logger, #safely

Instance Method Details

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



104
105
106
107
108
109
110
111
# File 'lib/deltacloud/drivers/google/google_driver.rb', line 104

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 -



177
178
179
180
181
182
183
184
# File 'lib/deltacloud/drivers/google/google_driver.rb', line 177

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



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/deltacloud/drivers/google/google_driver.rb', line 133

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 –



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/deltacloud/drivers/google/google_driver.rb', line 85

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 –



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/deltacloud/drivers/google/google_driver.rb', line 36

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 –



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/deltacloud/drivers/google/google_driver.rb', line 116

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’ –



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/deltacloud/drivers/google/google_driver.rb', line 55

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 –



167
168
169
170
171
172
# File 'lib/deltacloud/drivers/google/google_driver.rb', line 167

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 –



75
76
77
78
79
80
# File 'lib/deltacloud/drivers/google/google_driver.rb', line 75

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 -



189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/deltacloud/drivers/google/google_driver.rb', line 189

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

Returns:

  • (Boolean)


202
203
204
205
206
207
208
209
# File 'lib/deltacloud/drivers/google/google_driver.rb', line 202

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