DropletKit 是 DigitalOcean 为 Ruby 官方提供的 API 客户端。
Deprecated: DropletKit is no longer actively maintained. New applications should use the DigitalOcean API directly or a supported SDK (TypeScript, Go, or Python). There is no official Ruby SDK. DropletKit is a Ruby client for the DigitalOcean V2 API. It provides resource-oriented access to API endpoints. Existing apps may continue to use published gem versions; new API features will not be added here.
Add this line to your application's Gemfile:
gem 'droplet_kit'And then execute:
$ bundleOr install it yourself as:
$ gem install droplet_kitYou'll need to generate an access token in DigitalOcean's control panel at https://cloud.digitalocean.com/settings/applications
Using your access token, retrieve a client instance.
require 'droplet_kit'
client = DropletKit::Client.new(access_token: 'YOUR_TOKEN')You may also set timeout and time to first byte options on the client.
require 'droplet_kit'
client = DropletKit::Client.new(
access_token: 'YOUR_TOKEN',
open_timeout: 60, # time to first byte in seconds
timeout: 120, # response timeout in seconds
)If you would like to include a custom User-Agent header beyond what DropletKit uses, you can pass one in at the client initialization like so:
require 'droplet_kit'
client = DropletKit::Client.new(access_token: 'YOUR_TOKEN', user_agent: 'custom')By default, DropletKit will handle requests that are rate limited by the DigitalOcean API's burst limit. When the burst rate limit is reached, DropletKit will wait according to the value of the API response's Retry-After header. Typically the wait time is less than one minute. When the hourly rate limit is hit, an error is raised.
By default, DropletKit will retry a rate limited request three times before returning an error. If you would like to disable the retry behavior altogether, and instead raise an error when any rate limit is reached, you can set the retry_max config value to zero.
DropletKit will also wait zero seconds until retrying a request after the Retry-After time has elapsed by default. To change this, set the retry_wait_min to a different value.
require 'droplet_kit'
client = DropletKit::Client.new(access_token: 'YOUR_TOKEN', retry_max: 3, retry_wait_min: 1)DropletKit follows a strict design of resources as methods on your client. For examples, for droplets, you will call your client like this:
client = DropletKit::Client.new(access_token: 'YOUR_TOKEN')
client.droplets #=> DropletsResourceDropletKit will return Plain Old Ruby objects(tm) that contain the information provided by the API. For actions that return multiple objects, the request won't be executed until the result is accessed. For example:
client = DropletKit::Client.new(access_token: 'YOUR_TOKEN')
# Returns an instance of a Paginated Resource.
client.droplets.all
# => #
# Returns the first Droplet.
client.droplets.all.first
# => 1, :@name=>"mydroplet", ...}>
# Returns an array of Droplet IDs.
client.droplets.all.map(&:id)
# => [1]When you'd like to save objects, it's your responsibility to instantiate the objects and persist them using the resource objects. Let's use creating a Droplet as an example:
client = DropletKit::Client.new(access_token: 'YOUR_TOKEN')
droplet = DropletKit::Droplet.new(name: 'mysite.com', region: 'nyc2', image: 'ubuntu-14-04-x64', size: 's-1vcpu-1gb')
created = client.droplets.create(droplet)
# => DropletKit::Droplet(id: 1231, name: 'something.com', ...)To retrieve objects, you can perform this type of action on the resource (if the API supports it):
client = DropletKit::Client.new(access_token: 'YOUR_TOKEN')
droplet = client.droplets.find(id: 123)
# => DropletKit::Droplet(id: 1231, name: 'something.com', ...)client = DropletKit::Client.new(access_token: 'TOKEN')
client.cdns #=> DropletKit::CertificateResource
cdn = DropletKit::CDN.new(
origin: 'myspace.nyc3.digitaloceanspaces.com',
ttl: 1800,
custom_domain: 'www.myacme.xyz',
certificate_id: 'a6689b98-2bb9-40be-8638-fb8426aabd26'
)Actions supported:
client.cdns.find(id: 'id')client.cdns.all()client.cdns.create(cdn)client.cdns.update_ttl(id: 'id', ttl: 3600)client.cdns.update_custom_domain(id: 'id', custom_domain: 'www.myacme.xyz', certificate_id: 'a6689b98-2bb9-40be-8638-fb8426aabd26')client.cdns.flush_cache(id: 'id', files: ['*', 'path/to/css/*'])client.cdns.delete(id: 'id')client = DropletKit::Client.new(access_token: 'TOKEN')
client.certificates #=> DropletKit::CertificateResourceActions supported:
client.certificates.find(id: 'id')client.certificates.all()client.certificates.create(certificate)client.certificates.delete(id: 'id')client = DropletKit::Client.new(access_token: 'TOKEN')
client.databases #=> DropletKit::DatabaseResource
database_cluster = DropletKit::DatabaseCluster.new(
name: 'backend',
engine: 'pg',
version: '10',
region: 'nyc3',
size: 'db-s-2vcpu-4gb',
num_nodes: 2,
tags: ['production']
)Actions supported:
client.databases.find_cluster(id: 'id')client.databases.all_clusters()client.databases.create_cluster(database_cluster)client.databases.resize_cluster(database_cluster, id: 'id')client.databases.migrate_cluster(database_cluster, id: 'id')client.databases.set_maintenance_window(database_maintenance_window, id: 'id')client.databases.update_maintenance_window(database_maintenance_window, id: 'id')client.databases.list_backups(id: 'id')client.databases.restore_from_backup(database_backup)client.databases.delete_cluster(id: 'id')client.databases.create_db(database, id: 'id')client.databases.find_db(id: 'id', name: 'name')client.databases.all_dbs(id: 'id')client.databases.delete_db(id: 'id', name: 'name')client.databases.list_firewall_rules(id: 'id')client.databases.set_firewall_rules(database_firewall_rules, id: 'id')client.databases.create_read_only_replica(database_read_only_replica, id: 'id')client.databases.find_read_only_replica(id: 'id', name: 'name')client.databases.list_read_only_replicas(id: 'id')client.databases.delete_read_only_replica(id: 'id', name: 'name')client.databases.create_database_user(database_user, id: 'id')client.databases.find_database_user(id: 'id', name: 'name')client.databases.list_database_users(id: 'id')client.databases.reset_database_user_auth(reset_auth, id: 'id', name: 'name')client.databases.delete_database_user(id: 'id', name: 'name')client.databases.create_connection_pool(database_connection_pool, id: 'id')client.databases.find_connection_pool(id: 'id', name: 'name')client.databases.list_connection_pools(id: 'id')client.databases.update_connection_pool(database_connection_pool, id: 'id', name: 'name')client.databases.delete_connection_pool(id: 'id', name: 'name')client.databases.set_eviction_policy(database_eviction_policy, id: 'id')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_credentialsclient.databases.update_metrics_credentials(database_metrics_credentials)client.databases.get_mongo_config(id: 'id')client.databases.get_postgres_config(id: 'id')client.databases.get_mysql_config(id: 'id')client.databases.get_redis_config(id: 'id')client.databases.get_kafka_config(id: 'id')client.databases.get_opensearch_config(id: 'id')client.databases.update_mongo_config(mongo_config, id: 'id')client.databases.update_postgres_config(postgres_config, id: 'id')client.databases.update_mysql_config(mysql_config, id: 'id')client.databases.update_redis_config(redis_config, id: 'id')client.databases.update_kafka_config(kafka_config, id: 'id')client.databases.update_opensearch_config(opensearch_config, id: 'id')client = DropletKit::Client.new(access_token: 'TOKEN')
client.droplets #=> DropletKit::DropletResourceActions supported:
client.droplets.all()client.droplets.all(tag_name: 'tag_name')client.droplets.find(id: 'id')client.droplets.create(droplet)client.droplets.create_multiple(droplet)client.droplets.delete(id: 'id')client.droplets.delete_for_tag(tag_name: 'tag_name')client.droplets.kernels(id: 'id')client.droplets.snapshots(id: 'id')client.droplets.backups(id: 'id')client.droplets.actions(id: 'id')client = DropletKit::Client.new(access_token: 'TOKEN')
client.droplet_actions #=> DropletKit::DropletActionActions supported:
client.droplet_actions.reboot(droplet_id: droplet.id)client.droplet_actions.power_cycle(droplet_id: droplet.id)client.droplet_actions.power_cycle_for_tag(tag_name: 'tag_name')client.droplet_actions.shutdown(droplet_id: droplet.id)client.droplet_actions.shutdown_for_tag(tag_name: 'tag_name')client.droplet_actions.power_off(droplet_id: droplet.id)client.droplet_actions.power_off_for_tag(tag_name: 'tag_name')client.droplet_actions.power_on(droplet_id: droplet.id)client.droplet_actions.power_on_for_tag(tag_name: 'tag_name')client.droplet_actions.password_reset(droplet_id: droplet.id)client.droplet_actions.enable_ipv6(droplet_id: droplet.id)client.droplet_actions.enable_ipv6_for_tag(tag_name: 'tag_name')client.droplet_actions.enable_backups(droplet_id: droplet.id)client.droplet_actions.enable_backups_for_tag(tag_name: 'tag_name')client.droplet_actions.disable_backups(droplet_id: droplet.id)client.droplet_actions.disable_backups_for_tag(tag_name: 'tag_name')client.droplet_actions.upgrade(droplet_id: droplet.id)client.droplet_actions.enable_private_networking(droplet_id: droplet.id)client.droplet_actions.enable_private_networking_for_tag(tag_name: 'tag_name')client.droplet_actions.snapshot(droplet_id: droplet.id, name: 'Snapshot Name')client.droplet_actions.snapshot_for_tag(tag_name: 'tag_name', name: 'Snapshot Name')client.droplet_actions.change_kernel(droplet_id: droplet.id, kernel: 'kernel_id')client.droplet_actions.rename(droplet_id: droplet.id, name: 'New-Droplet-Name')client.droplet_actions.rebuild(droplet_id: droplet.id, image: 'image_id')client.droplet_actions.restore(droplet_id: droplet.id, image: 'image_id')client.droplet_actions.resize(droplet_id: droplet.id, size: 's-1vcpu-1gb')client.droplet_actions.find(droplet_id: droplet.id, id: action.id)client.droplet_actions.action_for_id(droplet_id: droplet.id, type: 'event_name', param: 'value')client.droplet_actions.action_for_tag(tag_name: 'tag_name', type: 'event_name', param: 'value')client = DropletKit::Clien暂无开放 Issues,或尚未同步最近议题。