Class: Fog::OracleCloud::Database::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/oraclecloud/database.rb,
lib/fog/oraclecloud/requests/database/get_instance.rb,
lib/fog/oraclecloud/requests/database/get_snapshot.rb,
lib/fog/oraclecloud/requests/database/list_backups.rb,
lib/fog/oraclecloud/requests/database/list_patches.rb,
lib/fog/oraclecloud/requests/database/list_servers.rb,
lib/fog/oraclecloud/requests/database/list_instances.rb,
lib/fog/oraclecloud/requests/database/list_snapshots.rb,
lib/fog/oraclecloud/requests/database/scale_instance.rb,
lib/fog/oraclecloud/requests/database/backup_instance.rb,
lib/fog/oraclecloud/requests/database/create_instance.rb,
lib/fog/oraclecloud/requests/database/create_snapshot.rb,
lib/fog/oraclecloud/requests/database/delete_instance.rb,
lib/fog/oraclecloud/requests/database/delete_snapshot.rb,
lib/fog/oraclecloud/requests/database/get_access_rule.rb,
lib/fog/oraclecloud/requests/database/list_recoveries.rb,
lib/fog/oraclecloud/requests/database/recover_instance.rb,
lib/fog/oraclecloud/requests/database/list_access_rules.rb,
lib/fog/oraclecloud/requests/database/create_access_rule.rb,
lib/fog/oraclecloud/requests/database/get_instance_from_job.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock



100
101
102
103
104
# File 'lib/fog/oraclecloud/database.rb', line 100

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

Class Method Details

.dataObject



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fog/oraclecloud/database.rb', line 114

def self.data 
  @data ||= {
    :instances  => {},
    :snapshots  => {},
    :servers    => {},
    :backups    => {},
    :access_rules => {},
    :recoveries => {},
    :deleted_at => {},
    :created_at => {},
    :maintenance_at => {}
  }
end

.resetObject



128
129
130
# File 'lib/fog/oraclecloud/database.rb', line 128

def self.reset
  @data = nil
end

Instance Method Details

#backup_instance(service_name) ⇒ Object



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

def backup_instance(service_name)
  response = Excon::Response.new

   if !self.data[:backups][service_name].is_a? Array 
     self.data[:backups][service_name] = []
   end
   self.data[:backups][service_name].push({
     'backupCompleteDate'=>Time.now.strftime('%d-%b-%Y %H:%M:%S UTC'),
     'dbTag'=>'TAG' + Time.now.strftime('%Y%m%dT%H%M%S'),
     'status'=>'IN PROGRESS',
     'database_id'=>service_name
   })
   if !self.data[:created_at][:backups] 
     self.data[:created_at][:backups] = {}
     self.data[:created_at][:backups][service_name] = []
   end
   self.data[:created_at][:backups][service_name].push(Time.now)
   response.status = 202
   response
end

#create_access_rule(service_name, name, description, ports, source, destination, status) ⇒ Object



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

def create_access_rule(service_name, name, description, ports, source, destination, status)
  response = Excon::Response.new

  data = {
    'ruleName' => name,
    'description' => description,
    'status' => status,
    'source' => source,
    'destination'=>destination,
    'ports'=>ports,
    'ruleType'=>'USER',
    'database_id'=>service_name
  }
  if !self.data[:access_rules].key?(service_name) then self.data[:access_rules][service_name] = [] end
  self.data[:access_rules][service_name] << data
  response.status = 202
  response
end

#create_instance(config, options) ⇒ Object



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
79
80
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
# File 'lib/fog/oraclecloud/requests/database/create_instance.rb', line 50

def create_instance(config, options)
  response = Excon::Response.new
  job_id = rand(10000).to_s
  data = {
    'serviceName' => config[:service_name],
    'shape' => config[:shape],
    'edition' => config[:edition],
    'version' => config[:version],
    'status' => 'Starting Provisioning', # Not a valid status, but we use it to simulate the time that the Oracle Cloud takes to add this to the list of instances
    'charset' => 'AL32UTF8',
    'ncharset' => 'AL16UTF16',
    'pdbName' => 'pdb1', # Note this is only valid for 12c instances. Too hard to fix for mocking
    'timezone' => 'UTC',
    'creation_job_id' => job_id,
    'totalSharedStorage' => options[:usable_storage],
    'domainName' => @identity_domain,
    'creation_date'=>Time.now.strftime('%Y-%b-%dT%H:%M:%S'),
    'serviceType'=>'DBaaS',
    'creator'=>@username
  }
    .merge(config.select {|key, value| [:description, :level, :subscription_type].include?(key) })
    .merge(options.select {|key, value| [:backup_destination, :failover_database, :cloud_storage_container, :is_rac, :ncharset, :pdb_name, :sid, :timezone].include?(key) })

  self.data[:instances][config[:service_name]] = data
  self.data[:created_at][config[:service_name]] = Time.now

  # Also create some compute nodes 
  node = {
    "status"=>"Running",
    "creation_job_id"=>"5495118",
    "creation_time"=>"Tue Jun 28 23:52:45 UTC 2016",
    "created_by"=>"dbaasadmin",
    "shape"=>"oc4",
    "sid"=>"ORCL1",
    "pdbName"=>"PDB1",
    "listenerPort"=> 1521,
    "connect_descriptor"=>"(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=db12c-xp-rac2)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=db12c-xp-rac1)(PORT=1521))(LOAD_BALANCE=ON)(FAILOVER=ON))(CONNECT_DATA=(SERVICE_NAME=PDB1.usexample.oraclecloud.internal)))",
    "connect_descriptor_with_public_ip"=>"(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=129.144.23.176)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=129.144.23.112)(PORT=1521))(LOAD_BALANCE=ON)(FAILOVER=ON))(CONNECT_DATA=(SERVICE_NAME=PDB1.usexample.oraclecloud.internal)))",
    "initialPrimary"=> true,
    "storageAllocated"=> 97280,
    "reservedIP"=>"129.144.23.112",
    "hostname"=>"db12c-xp-rac1"
  }
  self.data[:servers][config[:service_name]] = [node]
  
  # And some access rules
  if self.data[:access_rules][config[:service_name]].nil? then self.data[:access_rules][config[:service_name]] = [] end
  self.data[:access_rules][config[:service_name]] << {
    "ruleName"=>"ora_p2_ssh",
    "destination"=>"DB",
    "ports"=>22,
    "source"=>"PUBLIC-INTERNET",
    "status"=>"enabled",
    "database_id"=>config[:service_name]
  }

  response.headers['Location'] = "https://dbaas.oraclecloud.com:443/paas/service/dbcs/api/v1.1/instances/#{@identity_domain}/status/create/job/#{job_id}"
  response.status = 202
  response
end

#create_snapshot(name, description, database_id) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fog/oraclecloud/requests/database/create_snapshot.rb', line 26

def create_snapshot(name, description, database_id)
  response = Excon::Response.new

   data = {
     'name' => name,
     'description' => description,
     'creationTime' => Time.now,
     'clonedServicesSize' => 1,
     'status'=>'In Progress',
     'database_id'=>database_id,
     'clonedServices'=>[{
       'clonedServiceName'=>'ClonedService1',
       'clonedServiceCreationTime'=> Time.now
       }]
   }
   if !self.data[:snapshots].key?(database_id) then self.data[:snapshots][database_id] = {} end
   self.data[:snapshots][database_id][name] = data
   self.data[:created_at][name] = Time.now
   response.status = 202
   response
end

#dataObject



132
133
134
# File 'lib/fog/oraclecloud/database.rb', line 132

def data 
  self.class.data
end

#delete_instance(name) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/fog/oraclecloud/requests/database/delete_instance.rb', line 16

def delete_instance(name)
  response = Excon::Response.new
  self.data[:instances][name]['status'] = 'Terminating'
  self.data[:deleted_at][name] = Time.now
  response.status = 204
  response
end

#delete_snapshot(db_name, snapshot_name) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/fog/oraclecloud/requests/database/delete_snapshot.rb', line 16

def delete_snapshot(db_name, snapshot_name)
  response = Excon::Response.new
  self.data[:snapshots][db_name][snapshot_name]['status'] = 'Terminating'
  self.data[:deleted_at][snapshot_name] = Time.now
  response.status = 204
  response
end

#get_access_rule(db_name, rule_name) ⇒ Object



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

def get_access_rule(db_name, rule_name)
  response = Excon::Response.new

  rule = self.data[:access_rules][db_name].detect { |r| r['ruleName'] == rule_name}

  if !rule.nil? then
    response.status = 200
    response.body = rule
    response
  else
    raise Fog::OracleCloud::Database::NotFound.new("Rule #{rule_name} for #{db_name} does not exist");
  end
end

#get_instance(name) ⇒ Object



17
18
19
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
56
57
58
59
# File 'lib/fog/oraclecloud/requests/database/get_instance.rb', line 17

def get_instance(name)
  response = Excon::Response.new

  if instance = self.data[:instances][name]
    case instance['status']
    when 'Starting Provisioning'
      self.data[:instances][name]['status'] = 'In Progress'
      # This simulates the few seconds the Oracle Cloud takes to add this instance to the GET request after creating
      raise(
        Excon::Errors.status_error(
          { :expects => 200 },
          Excon::Response.new({
            :status => 404,
            :body => 'No such service exists, check domain and service name'
          })
        )
      )
    when 'Terminating'
      if Time.now - self.data[:deleted_at][name] >= Fog::Mock.delay
        self.data[:deleted_at].delete(name)
        self.data[:instances].delete(name)
      end
    when 'In Progress'
      if Time.now - self.data[:created_at][name] >= Fog::Mock.delay
        self.data[:instances][name]['status'] = 'Running'
        instance = self.data[:instances][name]
        self.data[:created_at].delete(name)
      end
    when 'Maintenance'
      info = self.data[:maintenance_at][name]
      if Time.now - info['time'] >= Fog::Mock.delay
        self.data[:instances][name]['status'] = 'Running'
        self.data[:instances][name][info['attribute']] = info['value']
        self.data[:maintenance_at].delete(name)
      end
    end
    response.status = 200
    response.body = instance
    response
  else
    raise Fog::OracleCloud::Database::NotFound.new("Database #{name} does not exist");
  end
end

#get_instance_from_job(job_id) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fog/oraclecloud/requests/database/get_instance_from_job.rb', line 17

def get_instance_from_job(job_id)
  response = Excon::Response.new

  instance = self.data[:instances].select { |k, v| v['creation_job_id'] == job_id }
  if instance
    response.status = 200
    response.body = instance
    response
  else
    raise Fog::OracleCloud::Database::NotFound.new("Database #{job_id} does not exist");
  end
end

#get_snapshot(db_name, snapshot_name) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fog/oraclecloud/requests/database/get_snapshot.rb', line 17

def get_snapshot(db_name, snapshot_name)
  response = Excon::Response.new

  if snapshot = self.data[:snapshots][db_name][snapshot_name]
    case snapshot['status']
    when 'Terminating'
      if Time.now - self.data[:deleted_at][snapshot_name] >= Fog::Mock.delay
        self.data[:deleted_at].delete(snapshot_name)
        self.data[:snapshots][db_name].delete(snapshot_name)
      end
    when 'In Progress'
      if Time.now - self.data[:created_at][snapshot_name] >= Fog::Mock.delay
        self.data[:snapshots][db_name][snapshot_name]['status'] = 'Succeeded'
        snapshot = self.data[:snapshots][db_name][snapshot_name]
        self.data[:created_at].delete(snapshot_name)
      end
    end
    response.status = 200
    response.body = snapshot
    response
  else
    raise Fog::OracleCloud::Database::NotFound.new("Snapshot #{snapshot_name} for #{db_name} does not exist");
  end
end

#list_access_rules(db_name) ⇒ Object



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

def list_access_rules(db_name)
  response = Excon::Response.new

  if !self.data[:access_rules][db_name] then self.data[:access_rules][db_name] = [] end
  access_rules = self.data[:access_rules][db_name]

  response.body = {
    'accessRules' => access_rules
  }
  response
end

#list_backups(db_name) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/oraclecloud/requests/database/list_backups.rb', line 16

def list_backups(db_name)
  response = Excon::Response.new

  if !self.data[:backups][db_name] then self.data[:backups][db_name] = [] end
  backups = self.data[:backups][db_name]

  backups.each_with_index { |b, i| 
    if b['status'] = 'IN PROGRESS' then
      if Time.now - self.data[:created_at][:backups][db_name][i] >= Fog::Mock.delay
        self.data[:created_at][:backups][db_name].delete(i)
        self.data[:backups][db_name][i]['status'] = 'COMPLETED'
        b = self.data[:backups][db_name][i]
      end
    end
  }
  response.body = {
    'backupList' => backups
  }
  response
end

#list_instancesObject



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

def list_instances
  response = Excon::Response.new

  instances = self.data[:instances].values

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

#list_patches(db_name) ⇒ Object



16
17
18
19
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
# File 'lib/fog/oraclecloud/requests/database/list_patches.rb', line 16

def list_patches(db_name)
  response = Excon::Response.new

  # Fake a patch
  response.body = {
    'availablePatches' => [{
      "patchId"=>"23054246-SE",
      "patchNumber"=>"Patch_12.1.0.2.160719_SE",
      "patchCategory"=>"DB",
      "patchSeverity"=>"Normal",
      "includesConfigUpgrade"=>false,
      "patchDescription"=>"DB 12.1.0.2.160719 Jul 2016 PSU Standard Edition image",
      "patchReleaseUrl"=>"https://support.oracle.com/epmos/faces/PatchDetail?patchId=23054246",
      "serviceType"=>"DBaaS",
      "serviceVersion"=>"12.1.0.2",
      "releaseDate"=>"2016-07-16T01:40:00.000+0000",
      "entryDate"=>"2016-10-07T20:57:33.121+0000",
      "entryUserId"=>"OCLOUD9_SM_PLATFORM_APPID",
      "patchType"=>"PSU",
      "requiresRestart"=> true,
      "serviceTypeVersions"=>"ANY",
      "isDeleted"=> false,
      "isCustomerVisible"=> false,
      "isAutoApply"=> false,
      "induceDownTime"=> false,
      "displayName"=>"12.1.0.2.160719",
      "releaseVersion"=>"12.1.0.2.160719",
      "serviceEditions"=>"SE",
    }]
  }
  response
end

#list_recoveries(db_name) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fog/oraclecloud/requests/database/list_recoveries.rb', line 16

def list_recoveries(db_name)
  response = Excon::Response.new

  if !self.data[:recoveries][db_name] then self.data[:recoveries][db_name] = [] end
  recoveries = self.data[:recoveries][db_name]

  recoveries.each_with_index { |r, i| 
    if r['status'] = 'IN PROGRESS' then
      if Time.now - self.data[:created_at][:recoveries][db_name][i] >= Fog::Mock.delay
        self.data[:created_at][:recoveries][db_name].delete(i)
        self.data[:recoveries][db_name][i]['status'] = 'COMPLETED'
        self.data[:recoveries][db_name][i]['recoveryCompleteDate'] = Time.now.strftime('%d-%b-%Y %H:%M:%S UTC')
        b = self.data[:backups][db_name][i]
      end
    end
  }
  response.body = {
    'recoveryList' => recoveries
  }
  response
end

#list_servers(db_name) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/fog/oraclecloud/requests/database/list_servers.rb', line 16

def list_servers(db_name)
  response = Excon::Response.new

  servers = self.data[:servers][db_name]

  response.body = servers
  response
end

#list_snapshots(db_name) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/fog/oraclecloud/requests/database/list_snapshots.rb', line 16

def list_snapshots(db_name)
  response = Excon::Response.new

  snapshots = self.data[:snapshots][db_name].values

  response.body = snapshots
  response
end

#passwordObject



110
111
112
# File 'lib/fog/oraclecloud/database.rb', line 110

def password
  @password
end

#recover_instance(service_name, type = nil, value = nil) ⇒ Object



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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/fog/oraclecloud/requests/database/recover_instance.rb', line 24

def recover_instance(service_name, type=nil, value=nil)
  response = Excon::Response.new

   if !self.data[:recoveries][service_name].is_a? Array then 
     self.data[:recoveries][service_name] = []
   end

   if !self.data[:created_at][:recoveries] 
     self.data[:created_at][:recoveries] = {}
     self.data[:created_at][:recoveries][service_name] = []
   end

   # Find the backup first
   backups = self.data[:backups][service_name]
   backup = nil
   if type == 'tag' then
     backup = backups.find { |b| b['dbTag'] = value }
   elsif type == 'timestamp' then
     # Too hard to do this logic in mock. Just return the latest
     backup = backups.last
   elsif type.nil? then
     # Default to searching for the latest
     backup = backups.last
   end
   if backup.nil? then
     response.status = 500
   else
     if type == 'tag' then
       self.data[:recoveries][service_name].push({
         'recoveryStartDate'=>Time.now.strftime('%d-%b-%Y %H:%M:%S UTC'),
         'status'=>'IN PROGRESS',
         'dbTag'=>value,
         'database_id' => service_name
       })
       self.data[:created_at][:recoveries][service_name].push(Time.now)
     elsif type == 'timestamp' then
       self.data[:recoveries][service_name].push({
         'recoveryStartDate'=>Time.now.strftime('%d-%b-%Y %H:%M:%S UTC'),
         'status'=>'IN PROGRESS',
         'timestamp'=>value.strftime('%d-%b-%Y %H:%M:%S'),
         'database_id' => service_name
       })
       self.data[:created_at][:recoveries][service_name].push(Time.now)
     elsif type.nil? then
       self.data[:recoveries][service_name].push({
         'recoveryStartDate'=>Time.now.strftime('%d-%b-%Y %H:%M:%S UTC'),
         'status'=>'IN PROGRESS',
         'latest'=>true,
         'database_id' => service_name
       })
       self.data[:created_at][:recoveries][service_name].push(Time.now)
     end
     response.status = 202
   end
   response
end

#scale_instance(name, options = {}) ⇒ Object



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

def scale_instance(name, options={})
  response = Excon::Response.new

   self.data[:instances][name]['status'] = 'Maintenance'
   info = { 'time'=> Time.now }
   if (options[:shape]) then
     info['attribute'] = 'shape'
     info['value'] = options[:shape]
   end
   self.data[:maintenance_at][name] = info
   response.status = 202
   response
end

#usernameObject



106
107
108
# File 'lib/fog/oraclecloud/database.rb', line 106

def username
  @username
end