Class: Fog::Compute::OracleCloud::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/oraclecloud/compute.rb,
lib/fog/oraclecloud/requests/compute/get_ssh_key.rb,
lib/fog/oraclecloud/requests/compute/get_instance.rb,
lib/fog/oraclecloud/requests/compute/list_ssh_keys.rb,
lib/fog/oraclecloud/requests/compute/create_ssh_key.rb,
lib/fog/oraclecloud/requests/compute/delete_ssh_key.rb,
lib/fog/oraclecloud/requests/compute/list_instances.rb,
lib/fog/oraclecloud/requests/compute/update_ssh_key.rb,
lib/fog/oraclecloud/requests/compute/create_instance.rb,
lib/fog/oraclecloud/requests/compute/delete_instance.rb,
lib/fog/oraclecloud/requests/compute/get_orchestration.rb,
lib/fog/oraclecloud/requests/compute/list_security_rules.rb,
lib/fog/oraclecloud/requests/compute/create_orchestration.rb,
lib/fog/oraclecloud/requests/compute/update_orchestration.rb,
lib/fog/oraclecloud/requests/compute/list_security_applications.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



145
146
147
148
149
150
151
# File 'lib/fog/oraclecloud/compute.rb', line 145

def initialize(options={})
  @username = options[:oracle_username]
  @password = options[:oracle_password]
  @identity_domain   = options[:oracle_domain]
  @api_endpoint   = options[:oracle_compute_api]

end

Class Method Details

.dataObject



153
154
155
156
157
158
159
160
# File 'lib/fog/oraclecloud/compute.rb', line 153

def self.data 
  @data ||= {
    :instances => {},
    :sshkeys => {},
    :orchestrations => {},
    :deleted_at => {}
  }
end

.resetObject



162
163
164
# File 'lib/fog/oraclecloud/compute.rb', line 162

def self.reset
  @data = nil
end

Instance Method Details

#create_instance(name, shape, imagelist, label, sshkeys) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fog/oraclecloud/requests/compute/create_instance.rb', line 32

def create_instance (name, shape, imagelist, label, sshkeys)
  response = Excon::Response.new
  name.sub! "/Compute-#{@identity_domain}/#{@username}/", ''

  self.data[:instances][name] = {
    'name' => "/Compute-#{@identity_domain}/#{@username}/#{name}",
    'shape' => shape,
    'imagelist' => imagelist,
    'label' => label,
    'sshkeys' => sshkeys,
    'state' => 'running'
  }
  response.status = 201
  response.body = {
    'instances' => [self.data[:instances][name]]
  }
  response
end

#create_orchestration(name, oplans, options = {}) ⇒ Object



46
47
48
49
50
51
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
# File 'lib/fog/oraclecloud/requests/compute/create_orchestration.rb', line 46

def create_orchestration (name, oplans, options={})
  response = Excon::Response.new
  # Clean up names in case they haven't provided the fully resolved names
  name.sub! "/Compute-#{@identity_domain}/#{@username}/", ''
  oplans.map do |oplan|
    oplan['objects'].map do |object|
      if oplan['obj_type'] == 'launchplan' then
        object['instances'].map do |instance|
          if !instance['name'].start_with?("/Compute-") then
            instance['name'] = "/Compute-#{@identity_domain}/#{@username}/#{instance['name']}"
          end
        end
      else
        if !object['name'].start_with?("/Compute-") then
          object['name'] = "/Compute-#{@identity_domain}/#{@username}/#{object['name']}"
        end
      end
    end
  end
  self.data[:orchestrations][name] = {
    'name'          => "/Compute-#{@identity_domain}/#{@username}/#{name}",
    'oplans'        => oplans,
    'relationships' => options[:relationships],
    'description'   => options[:description],
    'account'       => options[:account],
    'schedule'      => options[:schedule],
    'status'        => 'stopped',
    'uri'           => "#{@api_endpoint}orchestration/Compute-#{@identity_domain}/#{@username}/#{name}"
  }
  response.status = 201
  response.body = self.data[:orchestrations][name]
  response
end

#create_ssh_key(name, enabled, key) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fog/oraclecloud/requests/compute/create_ssh_key.rb', line 27

def create_ssh_key (name, enabled, key)
  response = Excon::Response.new
  name.sub! "/Compute-#{@identity_domain}/#{@username}/", ''

  data = {
    'name' => "/Compute-#{@identity_domain}/#{@username}/#{name}",
    'enabled' => enabled,
    'key' => key,
    'uri' => "#{@api_endpoint}sshkey/#{name}"
  }
  self.data[:sshkeys][name] = data

  response.status = 201
  response.body = self.data[:sshkeys][name]
  response
end

#dataObject



166
167
168
# File 'lib/fog/oraclecloud/compute.rb', line 166

def data 
  self.class.data
end

#delete_instance(name) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/fog/oraclecloud/requests/compute/delete_instance.rb', line 22

def delete_instance(name)
  response = Excon::Response.new
  clean_name = name.sub "/Compute-#{@identity_domain}/#{@username}/", ''
  self.data[:instances][clean_name]['state'] = 'stopping'
  self.data[:deleted_at][clean_name] = Time.now
  response.status = 204
  response
end

#delete_ssh_key(name) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/fog/oraclecloud/requests/compute/delete_ssh_key.rb', line 22

def delete_ssh_key (name)
  response = Excon::Response.new
  clean_name = name.sub "/Compute-#{@identity_domain}/#{@username}/", ''
  self.data[:sshkeys].delete(clean_name)
  response.status = 204
  response
end

#get_instance(name) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fog/oraclecloud/requests/compute/get_instance.rb', line 24

def get_instance(name)
  response = Excon::Response.new
  clean_name = name.sub "/Compute-#{@identity_domain}/#{@username}/", ''

  if instance = self.data[:instances][clean_name] 
    if instance['state'] == 'stopping'
      if Time.now - self.data[:deleted_at][clean_name] >= Fog::Mock.delay
        self.data[:deleted_at].delete(clean_name)
        self.data[:instances].delete(clean_name)
      end
    end
    response.status = 200
    response.body = instance
    response
  else;
    raise Fog::Compute::OracleCloud::NotFound.new("Instance #{name} does not exist");
  end
end

#get_orchestration(name) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/oraclecloud/requests/compute/get_orchestration.rb', line 24

def get_orchestration(name)
  response = Excon::Response.new
  clean_name = name.sub "/Compute-#{@identity_domain}/#{@username}/", ''

  if instance = self.data[:orchestrations][clean_name] 
    response.status = 200
    response.body = instance
    response
  else;
    raise Fog::Compute::OracleCloud::NotFound.new("Orchestration #{name} does not exist");
  end
end

#get_ssh_key(name) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/oraclecloud/requests/compute/get_ssh_key.rb', line 24

def get_ssh_key(name)
  response = Excon::Response.new
  clean_name = name.sub "/Compute-#{@identity_domain}/#{@username}/", ''

  if sshkey = self.data[:sshkeys][clean_name] 
    response.status = 200
    response.body = sshkey
    response
  else;
    raise Fog::Compute::OracleCloud::NotFound.new("SSHKey #{name} does not exist");
  end
end

#list_instancesObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/oraclecloud/requests/compute/list_instances.rb', line 15

def list_instances
  response = Excon::Response.new

  instances = self.data[:instances].values

  response.body = {
    'result' => instances
  }
  response
end

#list_security_applicationsObject



16
17
# File 'lib/fog/oraclecloud/requests/compute/list_security_rules.rb', line 16

def list_security_applications
end

#list_ssh_keysObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/oraclecloud/requests/compute/list_ssh_keys.rb', line 16

def list_ssh_keys
  response = Excon::Response.new

  sshkeys = self.data[:sshkeys].values

  response.body = {
    'result' => sshkeys
  }
  response
end

#update_orchestration(name, oplans, options = {}) ⇒ Object



49
50
51
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
# File 'lib/fog/oraclecloud/requests/compute/update_orchestration.rb', line 49

def update_orchestration (name, oplans, options={})
  response = Excon::Response.new
  clean_name = name.sub "/Compute-#{@identity_domain}/#{@username}/", ''
  if orchestration = self.data[:orchestrations][clean_name] 
    oplans.map do |oplan|
      oplan['objects'].map do |object|
        if oplan['obj_type'] == 'launchplan' then
          object['instances'].map do |instance|
            if !instance['name'].start_with?("/Compute-") then
              instance['name'] = "/Compute-#{@identity_domain}/#{@username}/#{instance['name']}"
            end
          end
        else
          if !object['name'].start_with?("/Compute-") then
            object['name'] = "/Compute-#{@identity_domain}/#{@username}/#{object['name']}"
          end
        end
      end
    end
    self.data[:orchestrations][clean_name].merge!(options)
    self.data[:orchestrations][clean_name]['oplans'] = oplans
    response.status = 200
    response.body = self.data[:orchestrations][clean_name]
    response
  else;
    raise Fog::Compute::OracleCloud::NotFound.new("Orchestration #{name} does not exist");
  end
end

#update_ssh_key(name, enabled, key) ⇒ Object



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

def update_ssh_key (name, enabled, key)
  response = Excon::Response.new
  clean_name = name.sub "/Compute-#{@identity_domain}/#{@username}/", ''
  if sshkey = self.data[:sshkeys][clean_name] 
    self.data[:sshkeys][clean_name].merge!({
      'name' => "/Compute-#{@identity_domain}/#{@username}/#{clean_name}",
      'enabled' => enabled,
      'key' => key,
      'uri' => "#{@api_endpoint}sshkey/#{clean_name}"              
    })
    response.status = 200
    response.body = self.data[:sshkeys][clean_name]
    response
  else;
    raise Fog::Compute::OracleCloud::NotFound.new("SSHKey #{name} does not exist");
  end
end