From e23e35d030470b62ca6db8cef2781c276af1080d Mon Sep 17 00:00:00 2001 From: aurelien-reeves-scalingo Date: Tue, 3 Jan 2023 11:13:45 +0100 Subject: [PATCH] feat(databases): Add default region management Fix #45 --- CHANGELOG.md | 1 + README.md | 4 ++++ lib/scalingo/core_client.rb | 7 +++++++ spec/scalingo/core_client_spec.rb | 23 +++++++++++++++++++++++ 4 files changed, 35 insertions(+) create mode 100644 spec/scalingo/core_client_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f118a5..dda4926 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## Unreleased * Bugfix: response of Backups#create was not properly unpacked ([#44](https://github.com/Scalingo/scalingo-ruby-api/issues/44)) +* New: Add default region for database API ([#45](https://github.com/Scalingo/scalingo-ruby-api/issues/44)) ## 3.2.0 - 2022-12-23 diff --git a/README.md b/README.md index 327be83..dff0d55 100644 --- a/README.md +++ b/README.md @@ -174,6 +174,10 @@ scalingo.db_api_osc_fr1.backups.for(addon_id) # get URL to download backup archive scalingo.db_api_osc_fr1.backups.archive(addon_id, backup_id) + +# you can omit the region to use the default one +scalingo.databases.find(addon_id) + ``` ## Development diff --git a/lib/scalingo/core_client.rb b/lib/scalingo/core_client.rb index 2ba3b2a..108de5f 100644 --- a/lib/scalingo/core_client.rb +++ b/lib/scalingo/core_client.rb @@ -41,6 +41,10 @@ def region(name = nil) public_send(name || config.default_region) end + def database_region(name = nil) + public_send(name || "db_api_#{config.default_region}") + end + ## Authentication helpers / Token management def authenticate_with(access_token: nil, bearer_token: nil, expires_at: nil) if !access_token && !bearer_token @@ -102,5 +106,8 @@ def authenticate_with(access_token: nil, bearer_token: nil, expires_at: nil) def_delegator :region, :notifiers def_delegator :region, :operations def_delegator :region, :scm_repo_links + + def_delegator :database_region, :databases + def_delegator :database_region, :backups end end diff --git a/spec/scalingo/core_client_spec.rb b/spec/scalingo/core_client_spec.rb new file mode 100644 index 0000000..818a18d --- /dev/null +++ b/spec/scalingo/core_client_spec.rb @@ -0,0 +1,23 @@ +require "spec_helper" + +RSpec.describe Scalingo::CoreClient do + subject { described_class.new } + + describe "#database_region" do + it "forwards call to the specified region" do + allow(subject).to receive("db_api_osc_secnum_fr1") + + subject.database_region("db_api_osc_secnum_fr1") + + expect(subject).to have_received("db_api_osc_secnum_fr1") + end + + it "forwards call to default db_api region" do + allow(subject).to receive("db_api_#{subject.config.default_region}") + + subject.database_region + + expect(subject).to have_received("db_api_#{subject.config.default_region}") + end + end +end