Skip to content

Commit

Permalink
Add database metrics credentials actions (#327)
Browse files Browse the repository at this point in the history
* Add database metrics credentials actions

* Update readme

* Update README.md

Co-authored-by: Ben Tranter <ben@bentranter.io>

* Update rubocop

---------

Co-authored-by: Ben Tranter <ben@bentranter.io>
  • Loading branch information
caiohsramos and bentranter authored Oct 7, 2024
1 parent 34cd850 commit c434949
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 9 deletions.
18 changes: 9 additions & 9 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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.
Expand All @@ -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).
Expand Down Expand Up @@ -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
Expand All @@ -125,7 +125,7 @@ RSpec/LeakyConstantDeclaration:
Exclude:
- 'spec/lib/droplet_kit/client_spec.rb'

# Offense count: 122
# Offense count: 123
RSpec/MultipleExpectations:
Max: 30

Expand All @@ -145,7 +145,7 @@ RSpec/NestedGroups:
RSpec/SpecFilePathFormat:
Enabled: false

# Offense count: 168
# Offense count: 170
# Configuration parameters: AllowedConstants.
Style/Documentation:
Enabled: false
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions lib/droplet_kit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand Down
15 changes: 15 additions & 0 deletions lib/droplet_kit/mappings/database_metrics_credentials_mapping.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions lib/droplet_kit/models/database_metrics_credentials.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

module DropletKit
class DatabaseMetricsCredentials < BaseModel
attribute :basic_auth_username
attribute :basic_auth_password
end
end
9 changes: 9 additions & 0 deletions lib/droplet_kit/resources/database_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions spec/fixtures/databases/get_metrics_credentials_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"credentials": {
"basic_auth_username": "username",
"basic_auth_password": "password"
}
}
27 changes: 27 additions & 0 deletions spec/lib/droplet_kit/resources/database_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c434949

Please sign in to comment.