Class: Fog::Compute::UKCloud::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/ukcloud/compute.rb,
lib/fog/ukcloud/requests/compute/get_ping.rb,
lib/fog/ukcloud/requests/compute/get_my_vm.rb,
lib/fog/ukcloud/requests/compute/post_login_session.rb

Constant Summary collapse

UKCLOUD_DEFAULT_ENDPOINT =
'portal.skyscapecloud.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fog/ukcloud/compute.rb', line 29

def initialize(options={})
  @ukcloud_username = options[:ukcloud_username]
  @ukcloud_password = options[:ukcloud_password]
  @ukcloud_endpoint = options[:ukcloud_endpoint] || UKCLOUD_DEFAULT_ENDPOINT
  @path = 'api'
  @connection_options = options[:connection_options] || {}
  @connection_options[:omit_default_port] = true unless @connection_options[:omit_default_port]
  @cookies = ''

  scheme = 'https'
  port = '443'
  url = sprintf('%s://%s:%s', scheme, @ukcloud_endpoint, port)

  @connection = Fog::XML::Connection.new(url, false, @connection_options)
end

Instance Attribute Details

#cookiesObject (readonly)

Returns the value of attribute cookies.



27
28
29
# File 'lib/fog/ukcloud/compute.rb', line 27

def cookies
  @cookies
end

#ukcloud_endpointObject (readonly)

Returns the value of attribute ukcloud_endpoint.



27
28
29
# File 'lib/fog/ukcloud/compute.rb', line 27

def ukcloud_endpoint
  @ukcloud_endpoint
end

#ukcloud_usernameObject (readonly)

Returns the value of attribute ukcloud_username.



27
28
29
# File 'lib/fog/ukcloud/compute.rb', line 27

def ukcloud_username
  @ukcloud_username
end

Instance Method Details

#get_cookiesObject



46
47
48
49
# File 'lib/fog/ukcloud/compute.rb', line 46

def get_cookies
   if @cookies.empty?
  @cookies
end

#get_my_vmObject



5
6
7
8
9
10
11
12
13
14
# File 'lib/fog/ukcloud/requests/compute/get_my_vm.rb', line 5

def get_my_vm

  request(
    :expects => 200,
    :method  => 'GET',
    :parser  => Fog::ToHashDocument.new,
    :path    => "#{@path}/my_vm"
  )

end

#get_pingObject



5
6
7
8
9
10
11
12
13
14
# File 'lib/fog/ukcloud/requests/compute/get_ping.rb', line 5

def get_ping

  request(
    :expects => 200,
    :method  => 'GET',
    #:parser  => Fog::ToHashDocument.new,
    :path    => "#{@path}/ping"
  )

end

#loginObject



87
88
89
90
91
92
93
94
95
# File 'lib/fog/ukcloud/compute.rb', line 87

def 
  response = 
  @cookies = response.headers['Set-Cookie']
  if response[:cookies].empty? && response.status == 201 then
    err =  RuntimeError.new("No Cookie In Response")
    raise Fog::UKCloud::Errors::ServiceError.slurp(err)
  end

end

#post_login_sessionObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/ukcloud/requests/compute/post_login_session.rb', line 7

def 

  body = {
    :email => @ukcloud_username,
    :password => @ukcloud_password
  }

  begin

    @connection.request(
      :headers => {'Accept' => 'application/json'},
      :expects => 201,
      :method  => 'POST',
      :body => Fog::JSON.encode(body),
      :path    => "#{@path}/authenticate"
    )
  rescue Excon::Errors::HTTPStatusError => error
    raise case error
      when Excon::Errors::BadRequest   then Fog::UKCloud::Errors::BadRequest.slurp(error);
      when Excon::Errors::Unauthorized then Fog::UKCloud::Errors::Unauthorised.slurp(error);
      when Excon::Errors::Forbidden    then Fog::UKCloud::Errors::Forbidden.slurp(error);
      when Excon::Errors::Conflict     then Fog::UKCloud::Errors::Conflict.slurp(error);
    else
      Fog::UKCloud::Errors::ServiceError.slurp(error)
    end
  end


end

#request(params) ⇒ Object



52
53
54
55
56
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
# File 'lib/fog/ukcloud/compute.rb', line 52

def request(params)
  path = params[:path]
  params[:body] = Fog::JSON.encode(params[:body]) if params[:body]
  begin


    headers = {
      'Accept' => 'application/json',
      'Content-Type' => 'application/json',
      'Cookie' => get_cookies
    }

    @connection.request({
      :body       => params[:body],
      :expects    => params[:expects],
      :headers    => headers.merge!(params[:headers] || {}),
      :idempotent => params[:idempotent],
      :method     => params[:method],
      :parser     => params[:parser],
      :path       => path,
      :query      => params[:query]
    })
  rescue Excon::Errors::HTTPStatusError => error
    raise case error
      when Excon::Errors::BadRequest   then Fog::UKCloud::Errors::BadRequest.slurp(error);
      when Excon::Errors::Unauthorized then Fog::UKCloud::Errors::Unauthorised.slurp(error);
      when Excon::Errors::Forbidden    then Fog::UKCloud::Errors::Forbidden.slurp(error);
      when Excon::Errors::Conflict     then Fog::UKCloud::Errors::Conflict.slurp(error);
    else
      Fog::UKCloud::Errors::ServiceError.slurp(error)
    end
  end
end