Class: ScalingoBackupsManager::Addon

Inherits:
Object
  • Object
show all
Defined in:
lib/scalingo_backups_manager/addon.rb

Constant Summary collapse

DEFAULT_DATABASE_API_ROOT_URL =
"https://db-api.osc-fr1.scalingo.com"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, addon, config: {}, sftp_config: {}) ⇒ Addon

Returns a new instance of Addon.



17
18
19
20
21
22
23
# File 'lib/scalingo_backups_manager/addon.rb', line 17

def initialize(app, addon, config: {}, sftp_config: {})
  raise "Application must be set" unless app && app.is_a?(ScalingoBackupsManager::Application)
  @application = app
  @addon = addon
  @sftp_config = sftp_config
  @config = config
end

Instance Attribute Details

#addonObject

Returns the value of attribute addon.



8
9
10
# File 'lib/scalingo_backups_manager/addon.rb', line 8

def addon
  @addon
end

#applicationObject

Returns the value of attribute application.



8
9
10
# File 'lib/scalingo_backups_manager/addon.rb', line 8

def application
  @application
end

#configObject

Returns the value of attribute config.



8
9
10
# File 'lib/scalingo_backups_manager/addon.rb', line 8

def config
  @config
end

#sftp_configObject

Returns the value of attribute sftp_config.



8
9
10
# File 'lib/scalingo_backups_manager/addon.rb', line 8

def sftp_config
  @sftp_config
end

Class Method Details

.find(app, id, config: {}, sftp_config: {}) ⇒ Object



12
13
14
15
# File 'lib/scalingo_backups_manager/addon.rb', line 12

def self.find(app, id, config: {}, sftp_config: {})
  addon = Configuration.client.addons.find(app.id, id).data
  self.new(app, addon, config: config, sftp_config: sftp_config)
end

Instance Method Details

#backupsObject



54
55
56
57
58
59
60
61
62
# File 'lib/scalingo_backups_manager/addon.rb', line 54

def backups
  response = client.authenticated_connection.get("#{database_api_url}/backups").body
  return [] unless response[:database_backups] && response[:database_backups].size > 0
  bcks = []
  response[:database_backups].each do |backup|
    bcks.push ScalingoBackupsManager::Backup.new(self, backup[:id])
  end
  bcks
end

#client(options: {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/scalingo_backups_manager/addon.rb', line 31

def client(options: {})
  return @client if @client
  scalingo = Scalingo::Client.new
  scalingo.authenticate_with(access_token: ENV["SCALINGO_API_TOKEN"])
  response = scalingo.addons.token(application.id, id).data
  if response.try(:[], :token)
    bearer_token = response[:token]
  else
    raise "An error occured during addon authentication"
  end

  addon_cli_config = Scalingo::Client.new
  addon_cli_config.token = bearer_token
  @client = Scalingo::API::Client.new(database_api_url(options: options), scalingo: addon_cli_config)
end

#database_api_url(options: {}) ⇒ Object



25
26
27
28
29
# File 'lib/scalingo_backups_manager/addon.rb', line 25

def database_api_url(options: {})
  return @database_api_url if @database_api_url

  @database_api_url = "#{options[:database_api_root_url] || DEFAULT_DATABASE_API_ROOT_URL}/api/databases/#{id}"
end