class Fog::OracleCloud::Database::Real

Public Class Methods

new(options={}) click to toggle source
# File lib/fog/oraclecloud/database.rb, line 48
def initialize(options={})
        @username = options[:oracle_username]
        @password = options[:oracle_password]
        @identity_domain   = options[:oracle_domain]
  region_url = options[:oracle_region] == 'emea' ? 'https://dbcs.emea.oraclecloud.com' : 'https://dbaas.oraclecloud.com'
  Excon.ssl_verify_peer = false
  Fog::Logger.debug("Using region_url #{region_url}")
  @connection = Fog::XML::Connection.new(region_url)
end

Public Instance Methods

auth_header() click to toggle source
# File lib/fog/oraclecloud/database.rb, line 66
def auth_header
        auth_header ||= 'Basic ' + Base64.encode64("#{@username}:#{@password}").gsub("\n",'')
end
backup_instance(service_name) click to toggle source
# File lib/fog/oraclecloud/requests/database/backup_instance.rb, line 6
def backup_instance(service_name)
  # Oracle Cloud requires an empty JSON object in the body
  body_data     = {}

  response = request(
    :method   => 'POST',
    :expects  => 202,
    :path     => "/paas/service/dbcs/api/v1.1/instances/#{@identity_domain}/#{service_name}/backups",
    :body     => Fog::JSON.encode(body_data),
  )
  response.database_id = service_name
  response
end
create_access_rule(service_name, name, description, ports, source, destination, status) click to toggle source
# File lib/fog/oraclecloud/requests/database/create_access_rule.rb, line 6
def create_access_rule(service_name, name, description, ports, source, destination, status)
  body_data     = {
    'ruleName' => name,
    'description' => description,
    'destination' => destination,
    'ports' => ports,
    'source' => source,
    'status' => status
  }
  response = request(
    :method   => 'POST',
    :expects  => 202,
    :path     => "/paas/api/v1.1/instancemgmt/#{@identity_domain}/services/dbaas/instances/#{service_name}/accessrules",
    :body     => Fog::JSON.encode(body_data),
  )
  response
end
create_instance(config, options) click to toggle source
# File lib/fog/oraclecloud/requests/database/create_instance.rb, line 6
def create_instance(config, options)
  parameters = options.select{|key, value| [:admin_password, :backup_destination, :charset, :cloud_storage_container, :cloud_storage_pwd, :cloud_storage_user, :cloud_storage_if_missing, :disaster_recovery, :failover_database, :golden_gate, :is_rac, :ncharset, :pdb_name, :sid, :timezone, :usable_storage, :create_storage_container_if_missing].include?(key)}
  params = {
    'type' => 'db'
  }
  parameters.each { |key, value| 
    if !value.nil? then 
      if key == :cloud_storage_container then 
        if !value.start_with?("Storage-") then
          value = "Storage-#{@identity_domain}/#{value}"
        end
      end
      new_key = key.to_s.split('_').collect(&:capitalize).join
      new_key = new_key[0,1].downcase + new_key[1..-1]
      params[new_key] = value
    end
  }
  body_data  = {
    'serviceName'         => config[:service_name],
    'version'             => config[:version],
    'level'               => config[:level],
    'edition'             => config[:edition],
    'subscriptionType'    => config[:subscription_type],
    'description'         => config[:description],
    'shape'               => config[:shape],
    'vmPublicKeyText'     => config[:ssh_key],
    'parameters'          => [params]
  }
  body_data = body_data.reject {|key, value| value.nil?}

  request(
    :method   => 'POST',
    :expects  => 202,
    :path     => "/paas/service/dbcs/api/v1.1/instances/#{@identity_domain}",
    :body     => Fog::JSON.encode(body_data),
    #:headers  => {
    # 'Content-Type'=>'application/vnd.com.oracle.oracloud.provisioning.Service+json'
    #}
  )
end
create_snapshot(name, description, database_id) click to toggle source
# File lib/fog/oraclecloud/requests/database/create_snapshot.rb, line 6
def create_snapshot(name, description, database_id)
  body_data     = {
    'name'              => name,
    'description'       => description
  }
  body_data = body_data.reject {|key, value| value.nil?}

  response = request(
    :method   => 'POST',
    :expects  => 202,
    :path     => "/paas/api/v1.1/instancemgmt/#{@identity_domain}/services/dbaas/instances/#{database_id}/snapshots",
    :body     => Fog::JSON.encode(body_data),
  )
  # Store the database reference in the model, so that we can use it later
  response.database_id = service_name
  response
end
delete_instance(service_name) click to toggle source
# File lib/fog/oraclecloud/requests/database/delete_instance.rb, line 6
def delete_instance(service_name)
  request(
    :method   => 'DELETE',
    :expects  => 202,
    :path     => "/paas/service/dbcs/api/v1.1/instances/#{@identity_domain}/#{service_name}"
  )
end
delete_snapshot(db_name, snapshot_name) click to toggle source
# File lib/fog/oraclecloud/requests/database/delete_snapshot.rb, line 6
def delete_snapshot(db_name, snapshot_name)
  request(
    :method   => 'DELETE',
    :expects  => 202,
    :path     => "/paas/api/v1.1/instancemgmt/#{@identity_domain}/services/dbaas/instances/#{db_name}/snapshots/#{snapshot_name}"
  )
end
get_access_rule(db_name, rule_name) click to toggle source
# File lib/fog/oraclecloud/requests/database/get_access_rule.rb, line 6
def get_access_rule(db_name, rule_name)
  # There isn't actually an API for this. So just get all of them and find the one they wanted
                                response = request(
    :expects  => 200,
    :method   => 'GET',
    :path     => "/paas/api/v1.1/instancemgmt/#{@identity_domain}/services/dbaas/instances/#{db_name}/accessrules"
  )
  rule = response.body["accessRules"].detect { |r| r['ruleName'] == rule_name}
  if rule.nil? then
    raise Fog::OracleCloud::Database::NotFound.new("Could not find rule (#{rule_name}) attached to #{db_name}")
  end
  response.body = rule
  response
end
get_instance(instance_id) click to toggle source
# File lib/fog/oraclecloud/requests/database/get_instance.rb, line 6
def get_instance(instance_id)
                                response = request(
    :expects  => 200,
    :method   => 'GET',
    :path     => "/paas/service/dbcs/api/v1.1/instances/#{@identity_domain}/#{instance_id}"
  )
  response
end
get_instance_from_job(job_id) click to toggle source
# File lib/fog/oraclecloud/requests/database/get_instance_from_job.rb, line 6
def get_instance_from_job(job_id)
                                response = request(
    :expects  => 200,
    :method   => 'GET',
    :path     => "/paas/service/dbcs/api/v1.1/instances/#{@identity_domain}/status/create/#{job_id}"
  )
  response
end
get_snapshot(db_name, snapshot_name) click to toggle source
# File lib/fog/oraclecloud/requests/database/get_snapshot.rb, line 6
def get_snapshot(db_name, snapshot_name)
                                response = request(
    :expects  => 200,
    :method   => 'GET',
    :path     => "/paas/api/v1.1/instancemgmt/#{@identity_domain}/services/dbaas/instances/#{db_name}/snapshots/#{snapshot_name}"
  )
  response
end
list_access_rules(db_name) click to toggle source
# File lib/fog/oraclecloud/requests/database/list_access_rules.rb, line 5
def list_access_rules(db_name)
  response = request(
    :expects  => 200,
    :method   => 'GET',
    :path     => "/paas/api/v1.1/instancemgmt/#{@identity_domain}/services/dbaas/instances/#{db_name}/accessrules"
  )
  response
end
list_backups(db_name) click to toggle source
# File lib/fog/oraclecloud/requests/database/list_backups.rb, line 5
def list_backups(db_name)
  response = request(
    :expects  => 200,
    :method   => 'GET',
    :path     => "/paas/service/dbcs/api/v1.1/instances/#{@identity_domain}/#{db_name}/backups"
  )
  response
end
list_instances() click to toggle source
# File lib/fog/oraclecloud/requests/database/list_instances.rb, line 5
def list_instances
  response = request(
    :expects  => 200,
    :method   => 'GET',
    :path     => "/paas/service/dbcs/api/v1.1/instances/#{@identity_domain}?outputLevel=verbose"
  )
  response
end
list_patches(db_name) click to toggle source
# File lib/fog/oraclecloud/requests/database/list_patches.rb, line 5
def list_patches(db_name)
  response = request(
    :expects  => 200,
    :method   => 'GET',
    :path     => "/paas/api/v1.1/instancemgmt/#{@identity_domain}/services/dbaas/instances/#{db_name}/patches/available"
  )
  response
end
list_recoveries(db_name) click to toggle source
# File lib/fog/oraclecloud/requests/database/list_recoveries.rb, line 5
def list_recoveries(db_name)
  response = request(
    :expects  => 200,
    :method   => 'GET',
    :path     => "/paas/service/dbcs/api/v1.1/instances/#{@identity_domain}/#{db_name}/backups/recovery/history"
  )
  response
end
list_servers(db_name) click to toggle source
# File lib/fog/oraclecloud/requests/database/list_servers.rb, line 5
def list_servers(db_name)
  response = request(
    :expects  => 200,
    :method   => 'GET',
    :path     => "/paas/service/dbcs/api/v1.1/instances/#{@identity_domain}/#{db_name}/servers"
  )
  response
end
list_snapshots(db_name) click to toggle source
# File lib/fog/oraclecloud/requests/database/list_snapshots.rb, line 5
def list_snapshots(db_name)
  response = request(
    :expects  => 200,
    :method   => 'GET',
    :path     => "/paas/api/v1.1/instancemgmt/#{@identity_domain}/services/dbaas/instances/#{db_name}/snapshots"
  )
  response
end
password() click to toggle source
# File lib/fog/oraclecloud/database.rb, line 62
def password
  @password
end
recover_instance(service_name, type=nil, value=nil) click to toggle source
# File lib/fog/oraclecloud/requests/database/recover_instance.rb, line 6
def recover_instance(service_name, type=nil, value=nil)
  if type == 'latest' then body_data = { 'latest' => true } end
  if type == 'tag' then body_data = { 'tag' => value } end
  if type == 'timestamp' then body_data = { 'timestamp' => value } end
  if type == 'scn' then body_data = { 'scn' => value } end

  response = request(
    :method   => 'POST',
    :expects  => 202,
    :path     => "/paas/service/dbcs/api/v1.1/instances/#{@identity_domain}/#{service_name}/backups/recovery",
    :body     => Fog::JSON.encode(body_data),
  )
  response.database_id = service_name
  response
end
request(params, parse_json = true, &block) click to toggle source
# File lib/fog/oraclecloud/database.rb, line 70
def request(params, parse_json = true, &block)
  begin
    Fog::Logger.debug("Sending #{params[:body].to_s} to (#{params[:method]}):#{params[:path]}")
    response = @connection.request(params.merge!({
      :headers  => {
        'Authorization' => auth_header,
        'X-ID-TENANT-NAME' => @identity_domain,
        'Content-Type' => 'application/json',
        #'Accept'       => 'application/json'
      }.merge!(params[:headers] || {})
    }), &block)
  rescue Excon::Errors::HTTPStatusError => error
    raise case error
    when Excon::Errors::NotFound
      Fog::OracleCloud::Database::NotFound.slurp(error)
    else
      error
    end
  end
  #https://jaas.oraclecloud.com/paas/service/jcs/api/v1.1/instances/agriculture/status/create/job/2781084
  if !response.body.empty? && parse_json
    # The Oracle Cloud doesn't return the Content-Type header as application/json, rather as application/vnd.com.oracle.oracloud.provisioning.Pod+json
    # Should add check here to validate, but not sure if this might change in future
    response.body = Fog::JSON.decode(response.body)
  end
  response
end
scale_instance(name, options={}) click to toggle source
# File lib/fog/oraclecloud/requests/database/scale_instance.rb, line 6
def scale_instance(name, options={})
  body_data     = {
    'shape'             => options[:shape],
    'additionalStorage' => options[:additional_storage],
    'usage'             => options[:usage]
  }
  body_data = body_data.reject {|key, value| value.nil?}

  request(
    :method   => 'PUT',
    :expects  => 202,
    :path     => "/paas/service/dbcs/api/v1.1/instances/#{@identity_domain}/#{name}",
    :body     => Fog::JSON.encode(body_data),
  )
end
username() click to toggle source
# File lib/fog/oraclecloud/database.rb, line 58
def username
  @username
end