From c43494916797ddcc1abf64dc7305c096bc0a5ca6 Mon Sep 17 00:00:00 2001 From: Caio Ramos Date: Mon, 7 Oct 2024 13:46:01 -0300 Subject: [PATCH] Add database metrics credentials actions (#327) * Add database metrics credentials actions * Update readme * Update README.md Co-authored-by: Ben Tranter * Update rubocop --------- Co-authored-by: Ben Tranter --- .rubocop_todo.yml | 18 ++++++------- README.md | 2 ++ lib/droplet_kit.rb | 2 ++ .../database_metrics_credentials_mapping.rb | 15 +++++++++++ .../models/database_metrics_credentials.rb | 8 ++++++ .../resources/database_resource.rb | 9 +++++++ .../get_metrics_credentials_response.json | 6 +++++ .../resources/database_resource_spec.rb | 27 +++++++++++++++++++ 8 files changed, 78 insertions(+), 9 deletions(-) create mode 100644 lib/droplet_kit/mappings/database_metrics_credentials_mapping.rb create mode 100644 lib/droplet_kit/models/database_metrics_credentials.rb create mode 100644 spec/fixtures/databases/get_metrics_credentials_response.json diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 3a211422..02fde23d 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2024-10-05 16:27:38 UTC using RuboCop version 1.66.1. +# on 2024-10-07 16:25:55 UTC using RuboCop version 1.66.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -33,7 +33,7 @@ Lint/UnderscorePrefixedVariableName: Exclude: - 'spec/lib/droplet_kit/client_spec.rb' -# Offense count: 36 +# Offense count: 37 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: AutoCorrect, IgnoreEmptyBlocks, AllowUnusedKeywordArguments. Lint/UnusedBlockArgument: @@ -55,12 +55,12 @@ Metrics/AbcSize: # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns, inherit_mode. # AllowedMethods: refine Metrics/BlockLength: - Max: 131 + Max: 138 # Offense count: 1 # Configuration parameters: CountComments, CountAsOne. Metrics/ClassLength: - Max: 137 + Max: 144 # Offense count: 6 # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. @@ -70,7 +70,7 @@ Metrics/MethodLength: # Offense count: 1 # Configuration parameters: CountComments, CountAsOne. Metrics/ModuleLength: - Max: 167 + Max: 169 # Offense count: 1 # This cop supports unsafe autocorrection (--autocorrect-all). @@ -110,7 +110,7 @@ RSpec/ContextWording: - 'spec/lib/droplet_kit/resources/vpc_resource_spec.rb' - 'spec/support/resource_context.rb' -# Offense count: 121 +# Offense count: 123 # Configuration parameters: CountAsOne. RSpec/ExampleLength: Max: 43 @@ -125,7 +125,7 @@ RSpec/LeakyConstantDeclaration: Exclude: - 'spec/lib/droplet_kit/client_spec.rb' -# Offense count: 122 +# Offense count: 123 RSpec/MultipleExpectations: Max: 30 @@ -145,7 +145,7 @@ RSpec/NestedGroups: RSpec/SpecFilePathFormat: Enabled: false -# Offense count: 168 +# Offense count: 170 # Configuration parameters: AllowedConstants. Style/Documentation: Enabled: false @@ -173,7 +173,7 @@ Style/SymbolProc: - 'lib/droplet_kit/resources/kubernetes_cluster_resource.rb' - 'lib/tasks/resource_doc.rake' -# Offense count: 91 +# Offense count: 92 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns. # URISchemes: http, https diff --git a/README.md b/README.md index ec1a3ec7..215b76fa 100644 --- a/README.md +++ b/README.md @@ -200,6 +200,8 @@ Actions supported: * `client.databases.get_eviction_policy(id: 'id')` * `client.databases.set_sql_mode(database_sql_mode, id: 'id')` * `client.databases.get_sql_mode(id: 'id')` +* `client.databases.get_metrics_credentials` +* `client.databases.update_metrics_credentials(database_metrics_credentials)` ## Droplet resource diff --git a/lib/droplet_kit.rb b/lib/droplet_kit.rb index e4e7ff99..b2553a94 100644 --- a/lib/droplet_kit.rb +++ b/lib/droplet_kit.rb @@ -52,6 +52,7 @@ module DropletKit autoload :DatabaseUser, 'droplet_kit/models/database_user' autoload :DatabaseUserResetAuth, 'droplet_kit/models/database_user_reset_auth' autoload :DatabaseUserMySQLSettings, 'droplet_kit/models/database_user_mysql_settings' + autoload :DatabaseMetricsCredentials, 'droplet_kit/models/database_metrics_credentials' autoload :LoadBalancer, 'droplet_kit/models/load_balancer' autoload :StickySession, 'droplet_kit/models/sticky_session' autoload :HealthCheck, 'droplet_kit/models/health_check' @@ -152,6 +153,7 @@ module DropletKit autoload :DatabaseSQLModeMapping, 'droplet_kit/mappings/database_sql_mode_mapping' autoload :DatabaseUserMapping, 'droplet_kit/mappings/database_user_mapping' autoload :DatabaseUserResetAuthMapping, 'droplet_kit/mappings/database_user_reset_auth_mapping' + autoload :DatabaseMetricsCredentialsMapping, 'droplet_kit/mappings/database_metrics_credentials_mapping' autoload :LoadBalancerMapping, 'droplet_kit/mappings/load_balancer_mapping' autoload :StickySessionMapping, 'droplet_kit/mappings/sticky_session_mapping' autoload :HealthCheckMapping, 'droplet_kit/mappings/health_check_mapping' diff --git a/lib/droplet_kit/mappings/database_metrics_credentials_mapping.rb b/lib/droplet_kit/mappings/database_metrics_credentials_mapping.rb new file mode 100644 index 00000000..31cad8d0 --- /dev/null +++ b/lib/droplet_kit/mappings/database_metrics_credentials_mapping.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module DropletKit + class DatabaseMetricsCredentialsMapping + include Kartograph::DSL + + kartograph do + mapping DatabaseMetricsCredentials + root_key singular: 'credentials', scopes: %i[read update] + + property :basic_auth_username, scopes: %i[read update] + property :basic_auth_password, scopes: %i[read update] + end + end +end diff --git a/lib/droplet_kit/models/database_metrics_credentials.rb b/lib/droplet_kit/models/database_metrics_credentials.rb new file mode 100644 index 00000000..d03d78c1 --- /dev/null +++ b/lib/droplet_kit/models/database_metrics_credentials.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +module DropletKit + class DatabaseMetricsCredentials < BaseModel + attribute :basic_auth_username + attribute :basic_auth_password + end +end diff --git a/lib/droplet_kit/resources/database_resource.rb b/lib/droplet_kit/resources/database_resource.rb index 6831422e..5fdca6dc 100644 --- a/lib/droplet_kit/resources/database_resource.rb +++ b/lib/droplet_kit/resources/database_resource.rb @@ -170,6 +170,15 @@ class DatabaseResource < ResourceKit::Resource action :get_sql_mode, 'GET /v2/databases/:id/sql_mode' do handler(200) { |response| DatabaseSQLModeMapping.extract_single(response.body, :read) } end + + action :get_metrics_credentials, 'GET /v2/databases/metrics/credentials' do + handler(200) { |response| DatabaseMetricsCredentialsMapping.extract_single(response.body, :read) } + end + + action :update_metrics_credentials, 'PUT /v2/databases/metrics/credentials' do + body { |object| DatabaseMetricsCredentialsMapping.representation_for(:update, object) } + handler(204) { |response| true } + end end def all(*args) diff --git a/spec/fixtures/databases/get_metrics_credentials_response.json b/spec/fixtures/databases/get_metrics_credentials_response.json new file mode 100644 index 00000000..72500cc8 --- /dev/null +++ b/spec/fixtures/databases/get_metrics_credentials_response.json @@ -0,0 +1,6 @@ +{ + "credentials": { + "basic_auth_username": "username", + "basic_auth_password": "password" + } +} diff --git a/spec/lib/droplet_kit/resources/database_resource_spec.rb b/spec/lib/droplet_kit/resources/database_resource_spec.rb index 07611c99..5bc45360 100644 --- a/spec/lib/droplet_kit/resources/database_resource_spec.rb +++ b/spec/lib/droplet_kit/resources/database_resource_spec.rb @@ -620,4 +620,31 @@ expect(request).to have_been_made end end + + describe '#get_metrics_credentials' do + it 'returns the metrics credentials' do + request = stub_do_api('/v2/databases/metrics/credentials', :get).to_return(body: api_fixture('databases/get_metrics_credentials_response'), status: 200) + metrics_credentials = resource.get_metrics_credentials + + expect(metrics_credentials).to be_a(DropletKit::DatabaseMetricsCredentials) + expect(metrics_credentials.basic_auth_username).to eq('username') + expect(metrics_credentials.basic_auth_password).to eq('password') + expect(request).to have_been_made + end + end + + describe '#update_metrics_credentials' do + it 'updates the metrics credentials' do + metrics_credentials = DropletKit::DatabaseMetricsCredentials.new( + basic_auth_username: 'username', + basic_auth_password: 'password' + ) + + json_body = DropletKit::DatabaseMetricsCredentialsMapping.representation_for(:update, metrics_credentials) + request = stub_do_api('/v2/databases/metrics/credentials', :put).with(body: json_body).to_return(status: 204) + resource.update_metrics_credentials(metrics_credentials) + + expect(request).to have_been_made + end + end end