Class: Fog::Compute::Fifo::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/fifo/compute.rb,
lib/fog/fifo/requests/compute/get_vm.rb,
lib/fog/fifo/requests/compute/stop_vm.rb,
lib/fog/fifo/requests/compute/list_vms.rb,
lib/fog/fifo/requests/compute/start_vm.rb,
lib/fog/fifo/requests/compute/create_vm.rb,
lib/fog/fifo/requests/compute/reboot_vm.rb,
lib/fog/fifo/requests/compute/get_dataset.rb,
lib/fog/fifo/requests/compute/get_iprange.rb,
lib/fog/fifo/requests/compute/get_network.rb,
lib/fog/fifo/requests/compute/get_package.rb,
lib/fog/fifo/requests/compute/list_datasets.rb,
lib/fog/fifo/requests/compute/list_ipranges.rb,
lib/fog/fifo/requests/compute/list_networks.rb,
lib/fog/fifo/requests/compute/list_packages.rb

Overview

Mock

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/fog/fifo/compute.rb', line 81

def initialize(options = {})

  @connection_options = options[:connection_options] || {}
  @persistent = options[:persistent] || false

  @fifo_username = options[:fifo_username]

  unless @fifo_username
    raise ArgumentError, "options[:fifo_username] required"
  end

  @fifo_url = options[:fifo_url]

  unless @fifo_url
    raise ArgumentError, "options[:fifo_url] required"
  end

  @fifo_password = options[:fifo_password]

  unless @fifo_password
    raise ArgumentError, "options[:fifo_password] required"
  end

  @fifo_password = options[:fifo_password]

  @fifo_token = nil

  @fifo_uri = ::URI.parse(@fifo_url)

  @connection = Fog::Connection.new(
    @fifo_url,
    @persistent,
    @connection_options
  )
  authenticate
end

Instance Method Details

#authenticateObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/fog/fifo/compute.rb', line 118

def authenticate
  response = @connection.request({
                                   :expects => [200, 204, 303],
                                   #:host    => @fifo_uri.host,
                                   :method  => 'POST',
                                   :path    => create_path('sessions'),
                                   :headers => {
                                     "Content-Type" => "application/json",
                                     "Accept" => "application/json"
                                   },
                                   :body => Fog::JSON.encode({
                                                               "user" => @fifo_username,
                                                               "password" => @fifo_password
                                                             })
                                 })

  @fifo_token = response.headers["x-snarl-token"] || File.basename(response.headers["location"])
end

#create_vm(params) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/fog/fifo/requests/compute/create_vm.rb', line 19

def create_vm(params)
  pp params
  request(
          :method => "POST",
          :path => "vms",
          :expects => [200],
          :body => params
          )
end

#get_dataset(id) ⇒ Object



18
19
20
21
22
23
# File 'lib/fog/fifo/requests/compute/get_dataset.rb', line 18

def get_dataset(id)
  request(
    :method => "GET",
    :path => "datasets/#{id}"
  )
end

#get_iprange(id) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/fog/fifo/requests/compute/get_iprange.rb', line 19

def get_iprange(id)
  request(
          :method => "GET",
          :path => "ipranges/#{id}",
          :expects => 200
          )
end

#get_network(id) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/fog/fifo/requests/compute/get_network.rb', line 19

def get_network(id)
  request(
          :method => "GET",
          :path => "networks/#{id}",
          :expects => 200
          )
end

#get_package(id) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/fog/fifo/requests/compute/get_package.rb', line 19

def get_package(id)
  request(
    :method => "GET",
    :path => "packages/#{id}",
    :expects => 200
  )
end

#get_vm(id) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/fog/fifo/requests/compute/get_vm.rb', line 19

def get_vm(id)
  request(
          :method => "GET",
          :path => "vms/#{id}",
          :expects => [200]
          )
end

#list_datasetsObject



15
16
17
18
19
20
# File 'lib/fog/fifo/requests/compute/list_datasets.rb', line 15

def list_datasets
  request(
    :method => "GET",
    :path => "datasets"
  )
end

#list_iprangesObject



15
16
17
18
19
20
21
# File 'lib/fog/fifo/requests/compute/list_ipranges.rb', line 15

def list_ipranges
  request(
    :path => "ipranges",
    :method => "GET",
    :expects => 200
  )
end

#list_networksObject



15
16
17
18
19
20
21
# File 'lib/fog/fifo/requests/compute/list_networks.rb', line 15

def list_networks
  request(
    :path => "networks",
    :method => "GET",
    :expects => 200
  )
end

#list_packagesObject



15
16
17
18
19
20
21
# File 'lib/fog/fifo/requests/compute/list_packages.rb', line 15

def list_packages
  request(
    :path => "packages",
    :method => "GET",
    :expects => 200
  )
end

#list_vms(options = {}) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/fog/fifo/requests/compute/list_vms.rb', line 15

def list_vms(options={})
  request(
    :path => "vms",
    :method => "GET",
    :query => options,
    :expects => 200
  )
end

#reboot_vm(id) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/fog/fifo/requests/compute/reboot_vm.rb', line 18

def reboot_vm(id)
  request(
          :method => "PUT",
          :path => "vms/#{id}",
          :expects => [200],
          :body => {"action" => "reboot"},
          )
end

#request(opts = {}) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/fog/fifo/compute.rb', line 137

def request(opts = {})
  opts[:headers] = {
    "X-Api-Version" => @fifo_version,
    "Content-Type" => "application/json",
    "Accept" => "application/json",
    "x-Snarl-Token" => @fifo_token
  }.merge(opts[:headers] || {})

  if opts[:body]
    opts[:body] = Fog::JSON.encode(opts[:body])
  end

  opts[:path] = create_path(opts[:path])
  response = @connection.request(opts)
  if response.headers["Content-Type"] == "application/json"
    response.body = json_decode(response.body)
  end

  #pp response
  response
rescue Excon::Errors::HTTPStatusError => e
  raise_if_error!(e.request, e.response)
end

#start_vm(id) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/fog/fifo/requests/compute/start_vm.rb', line 19

def start_vm(id)
  request(
          :method => "PUT",
          :path => "vms/#{id}",
          :expects => [200],
          :body => {"action" => "start"},
          )
end

#stop_vm(id) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/fog/fifo/requests/compute/stop_vm.rb', line 19

def stop_vm(id)
  request(
          :method => "PUT",
          :path => "vms/#{id}",
          :expects => [200],
          :body => {"action" => "stop"},
          )
end