Class: Kytoon::Providers::CloudServersVPC::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/kytoon/providers/cloud_servers_vpc/connection.rb

Constant Summary collapse

MULTI_PART_BOUNDARY =
"jtZ!pZ1973um"
@@http =
nil
@@auth_user =
nil
@@auth_password =
nil

Class Method Summary collapse

Class Method Details

.delete(url_path) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/kytoon/providers/cloud_servers_vpc/connection.rb', line 129

def self.delete(url_path)
  init_connection if @@http.nil?
  req = Net::HTTP::Delete.new(url_path)
  req.basic_auth @@auth_user, @@auth_password if @@auth_user and @@auth_password
  response = @@http.request(req)
  case response
  when Net::HTTPSuccess
    return response.body
  else
    response.error!
  end
end

.file_upload(url_path, file_data = {}, post_data = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/kytoon/providers/cloud_servers_vpc/connection.rb', line 57

def self.file_upload(url_path, file_data={}, post_data={})
  init_connection if @@http.nil?
  req = Net::HTTP::Post.new(url_path)

  post_arr=[]
  post_data.each_pair do |key, value|
    post_arr << "--#{MULTI_PART_BOUNDARY}\r\n"
    post_arr << "Content-Disposition: form-data; name=\"#{key}\"\r\n"
    post_arr << "\r\n"
    post_arr << value
    post_arr << "\r\n"
  end

  file_data.each_pair do |name, file|
    post_arr << "--#{MULTI_PART_BOUNDARY}\r\n"
    post_arr << "Content-Disposition: form-data; name=\"#{name}\"; filename=\"#{File.basename(file)}\"\r\n"
    post_arr << "Content-Type: text/plain\r\n"
    post_arr << "\r\n"
    post_arr << File.read(file)
    post_arr << "\r\n--#{MULTI_PART_BOUNDARY}--\r\n"
  end
  post_arr << "--#{MULTI_PART_BOUNDARY}--\r\n\r\n"

  req.body=post_arr.join

  req.basic_auth @@auth_user, @@auth_password if @@auth_user and @@auth_password
  req["Content-Type"] = "multipart/form-data, boundary=#{MULTI_PART_BOUNDARY}"

  response = @@http.request(req)
  case response
  when Net::HTTPSuccess
    return response.body
  else
    puts response.body
    response.error!
  end
end

.get(url_path) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/kytoon/providers/cloud_servers_vpc/connection.rb', line 116

def self.get(url_path)
  init_connection if @@http.nil?
  req = Net::HTTP::Get.new(url_path)
  req.basic_auth @@auth_user, @@auth_password if @@auth_user and @@auth_password
  response = @@http.request(req)
  case response
  when Net::HTTPSuccess
    return response.body
  else
    response.error!
  end
end

.init_connectionObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/kytoon/providers/cloud_servers_vpc/connection.rb', line 20

def self.init_connection

  configs=Util.load_configs

  base_url = configs["cloud_servers_vpc_url"]
  @@auth_user = configs["cloud_servers_vpc_username"]
  @@auth_password = configs["cloud_servers_vpc_password"]

  ssl_key = configs["ssl_key"]
  ssl_cert = configs["ssl_cert"]
  ssl_ca_cert = configs["ssl_ca_cert"]

  url=URI.parse(base_url)
  @@http = Net::HTTP.new(url.host,url.port)

  if base_url =~ /^https/
    @@http.use_ssl = true
    if ssl_ca_cert then
      @@http.verify_mode = OpenSSL::SSL::VERIFY_PEER
    else
      @@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end

    if ssl_key then
      pkey_data=IO.read(ssl_key)
      if pkey_data =~ /^-----BEGIN RSA PRIVATE KEY-----/
        @@http.key=OpenSSL::PKey::RSA.new(pkey_data)
      else
        @@http.key=OpenSSL::PKey::DSA.new(pkey_data)
      end
    end
    @@http.cert=OpenSSL::X509::Certificate.new(IO.read(ssl_cert)) if ssl_cert
    @@http.ca_file=ssl_ca_cert if ssl_ca_cert
  end

end

.post(url_path, post_data) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/kytoon/providers/cloud_servers_vpc/connection.rb', line 95

def self.post(url_path, post_data)
  init_connection if @@http.nil?
  req = Net::HTTP::Post.new(url_path)
  if post_data.kind_of?(String) then
    req.body=post_data
  elsif post_data.kind_of?(Hash) then
    req.form_data=post_data
  else
    raise "Invalid post data type."
  end
  req.basic_auth @@auth_user, @@auth_password if @@auth_user and @@auth_password
  response = @@http.request(req)
  case response
  when Net::HTTPSuccess
    return response.body
  else
    puts response.body
    response.error!
  end
end