From 120070ecd7c9ae8efc6237a4873bd3ce2ed6317e Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Sat, 14 Aug 2021 00:11:07 -0500 Subject: [PATCH 01/50] WIP --- Gemfile | 12 +- Rakefile | 6 +- lib/vultr.rb | 924 +---------------------- lib/vultr/client.rb | 42 ++ lib/vultr/collection.rb | 22 + lib/vultr/error.rb | 4 + lib/vultr/object.rb | 14 + lib/vultr/objects/account.rb | 4 + lib/vultr/objects/application.rb | 4 + lib/vultr/objects/instance.rb | 4 + lib/vultr/objects/user.rb | 4 + lib/vultr/resource.rb | 54 ++ lib/vultr/resources/account.rb | 7 + lib/vultr/resources/applications.rb | 8 + lib/vultr/resources/backups.rb | 8 + lib/vultr/resources/bare_metal.rb | 8 + lib/vultr/resources/block_storage.rb | 8 + lib/vultr/resources/dns.rb | 8 + lib/vultr/resources/firewall.rb | 8 + lib/vultr/resources/instances.rb | 145 ++++ lib/vultr/resources/iso.rb | 8 + lib/vultr/resources/kubernetes.rb | 8 + lib/vultr/resources/load_balancers.rb | 8 + lib/vultr/resources/object_storage.rb | 8 + lib/vultr/resources/operating_systems.rb | 8 + lib/vultr/resources/plans.rb | 12 + lib/vultr/resources/private_networks.rb | 8 + lib/vultr/resources/regions.rb | 12 + lib/vultr/resources/reserved_ips.rb | 8 + lib/vultr/resources/snapshots.rb | 8 + lib/vultr/resources/ssh_keys.rb | 8 + lib/vultr/resources/startup_scripts.rb | 8 + lib/vultr/resources/users.rb | 24 + test/test_helper.rb | 3 - test/vultr/client_test.rb | 8 + test/vultr/config_test.rb | 9 + test/vultr/object_test.rb | 11 + test/vultr/resources/account_test.rb | 10 + test/vultr/vultr_account_test.rb | 29 - test/vultr/vultr_app_test.rb | 26 - test/vultr/vultr_backup_test.rb | 22 - test/vultr/vultr_iso_test.rb | 27 - test/vultr/vultr_os_test.rb | 21 - test/vultr/vultr_plan_test.rb | 22 - test/vultr/vultr_region_test.rb | 33 - test/vultr/vultr_snapshot_test.rb | 30 - test/vultr/vultr_sshkey_test.rb | 50 -- test/vultr/vultr_startupscript_test.rb | 53 -- vultr.gemspec | 4 +- 49 files changed, 569 insertions(+), 1211 deletions(-) create mode 100644 lib/vultr/client.rb create mode 100644 lib/vultr/collection.rb create mode 100644 lib/vultr/error.rb create mode 100644 lib/vultr/object.rb create mode 100644 lib/vultr/objects/account.rb create mode 100644 lib/vultr/objects/application.rb create mode 100644 lib/vultr/objects/instance.rb create mode 100644 lib/vultr/objects/user.rb create mode 100644 lib/vultr/resource.rb create mode 100644 lib/vultr/resources/account.rb create mode 100644 lib/vultr/resources/applications.rb create mode 100644 lib/vultr/resources/backups.rb create mode 100644 lib/vultr/resources/bare_metal.rb create mode 100644 lib/vultr/resources/block_storage.rb create mode 100644 lib/vultr/resources/dns.rb create mode 100644 lib/vultr/resources/firewall.rb create mode 100644 lib/vultr/resources/instances.rb create mode 100644 lib/vultr/resources/iso.rb create mode 100644 lib/vultr/resources/kubernetes.rb create mode 100644 lib/vultr/resources/load_balancers.rb create mode 100644 lib/vultr/resources/object_storage.rb create mode 100644 lib/vultr/resources/operating_systems.rb create mode 100644 lib/vultr/resources/plans.rb create mode 100644 lib/vultr/resources/private_networks.rb create mode 100644 lib/vultr/resources/regions.rb create mode 100644 lib/vultr/resources/reserved_ips.rb create mode 100644 lib/vultr/resources/snapshots.rb create mode 100644 lib/vultr/resources/ssh_keys.rb create mode 100644 lib/vultr/resources/startup_scripts.rb create mode 100644 lib/vultr/resources/users.rb create mode 100644 test/vultr/client_test.rb create mode 100644 test/vultr/config_test.rb create mode 100644 test/vultr/object_test.rb create mode 100644 test/vultr/resources/account_test.rb delete mode 100644 test/vultr/vultr_account_test.rb delete mode 100644 test/vultr/vultr_app_test.rb delete mode 100644 test/vultr/vultr_backup_test.rb delete mode 100644 test/vultr/vultr_iso_test.rb delete mode 100644 test/vultr/vultr_os_test.rb delete mode 100644 test/vultr/vultr_plan_test.rb delete mode 100644 test/vultr/vultr_region_test.rb delete mode 100644 test/vultr/vultr_snapshot_test.rb delete mode 100644 test/vultr/vultr_sshkey_test.rb delete mode 100644 test/vultr/vultr_startupscript_test.rb diff --git a/Gemfile b/Gemfile index efcd6af..baa72e9 100644 --- a/Gemfile +++ b/Gemfile @@ -1,12 +1,6 @@ source 'https://rubygems.org' -gem 'rake', '~> 10.5.0' if RUBY_VERSION < '1.9.3' -gem 'rake' if RUBY_VERSION >= '1.9.3' - -group :test do - gem 'coveralls', :require => false - gem 'minitest', '>= 5.0.5' - gem 'simplecov' -end - gemspec + +gem 'rake' +gem 'minitest', '>= 5.0.5' diff --git a/Rakefile b/Rakefile index 121155c..83d2add 100644 --- a/Rakefile +++ b/Rakefile @@ -7,4 +7,8 @@ Rake::TestTask.new(:test) do |t| t.test_files = FileList['test/**/*_test.rb'] end -task :default => :test +task default: :test + +task :console do + exec 'irb -I lib -r vultr.rb' +end diff --git a/lib/vultr.rb b/lib/vultr.rb index acc9047..05682cc 100644 --- a/lib/vultr.rb +++ b/lib/vultr.rb @@ -2,887 +2,45 @@ require 'faraday_middleware' require 'vultr/version' -module Vultr extend self - - DEFINITIONS = { - # Account - Account: { - info: { - :method => :get, - :path => '/v1/account/info', - :apikey_required => true - } - }, - # Application - App: { - list: { - :method => :get, - :path => '/v1/app/list', - :apikey_required => false - } - }, - # API Key - Auth: { - info: { - :method => :get, - :path => '/v1/auth/info', - :apikey_required => true - } - }, - # Backup - Backup: { - list: { - :method => :get, - :path => '/v1/backup/list', - :params => %w(SUBID BACKUPID), - :apikey_required => true - } - }, - # Bare Metal - BareMetal: { - app_change: { - :method => :post, - :path => '/v1/baremetal/app_change', - :params => %w(SUBID APPID), - :apikey_required => true - }, - app_change_list: { - :method => :get, - :path => '/v1/baremetal/app_change_list', - :params => %w(SUBID), - :apikey_required => true - }, - bandwidth: { - :method => :get, - :path => '/v1/baremetal/bandwidth', - :apikey_required => true - }, - create: { - :method => :post, - :path => '/v1/baremetal/create', - :params => %w(DCID METALPLANID OSID SCRIPTID SNAPSHOTID enable_ipv6 label SSHKEYID APPID userdata notify_activate hostname tag), - :apikey_required => true - }, - destroy: { - :method => :post, - :path => '/v1/baremetal/destroy', - :params => %w(SUBID), - :apikey_required => true - }, - get_app_info: { - :method => :get, - :path => '/v1/baremetal/get_app_info', - :params => %w(SUBID), - :apikey_required => true - }, - get_user_data: { - :method => :get, - :path => '/v1/baremetal/get_user_data', - :params => %w(SUBID), - :apikey_required => true - }, - halt: { - :method => :post, - :path => '/v1/baremetal/halt', - :params => %w(SUBID), - :apikey_required => true - }, - ipv6_enable: { - :method => :post, - :path => '/v1/baremetal/ipv6_enable', - :params => %w(SUBID), - :apikey_required => true - }, - label_set: { - :method => :post, - :path => '/v1/baremetal/label_set', - :params => %w(SUBID label), - :apikey_required => true - }, - list: { - :method => :get, - :path => '/v1/baremetal/list', - :params => %w(SUBID tag label main_ip), - :apikey_required => true - }, - list_ipv4: { - :method => :get, - :path => '/v1/baremetal/list_ipv4', - :params => %w(SUBID), - :apikey_required => true - }, - list_ipv6: { - :method => :get, - :path => '/v1/baremetal/list_ipv6', - :params => %w(SUBID), - :apikey_required => true - }, - os_change: { - :method => :post, - :path => '/v1/baremetal/os_change', - :params => %w(SUBID OSID), - :apikey_required => true - }, - os_change_list: { - :method => :get, - :path => '/v1/baremetal/os_change_list', - :params => %w(SUBID), - :apikey_required => true - }, - reboot: { - :method => :post, - :path => '/v1/baremetal/reboot', - :params => %w(SUBID), - :apikey_required => true - }, - reinstall: { - :method => :post, - :path => '/v1/baremetal/reinstall', - :params => %w(SUBID), - :apikey_required => true - }, - set_user_data: { - :method => :post, - :path => '/v1/baremetal/set_user_data', - :params => %w(SUBID userdata), - :apikey_required => true - }, - tag_set: { - :method => :post, - :path => '/v1/baremetal/tag_set', - :params => %w(SUBID tag), - :apikey_required => true - } - }, - # Block Storage - Block: { - attach: { - :method => :post, - :path => '/v1/block/attach?api_key=[api_key]', - :params => %w(SUBID attach_to_SUBID), - :apikey_required => true - }, - create: { - :method => :post, - :path => '/v1/block/create', - :params => %w(DCID size_gb label), - :apikey_required => true - }, - delete: { - :method => :post, - :path => '/v1/block/delete', - :params => %w(SUBID), - :apikey_required => true - }, - detach: { - :method => :post, - :path => '/v1/block/detach', - :params => %w(SUBID), - :apikey_required => true - }, - label_set: { - :method => :post, - :path => '/v1/block/label_set', - :params => %w(SUBID label), - :apikey_required => true - }, - list: { - :method => :get, - :path => '/v1/block/list', - :params => %w(SUBID), - :apikey_required => true - }, - resize: { - :method => :post, - :path => '/v1/block/resize', - :params => %w(SUBID size_gb), - :apikey_required => true - } - }, - # DNS - DNS: { - create_domain: { - :method => :post, - :path => '/v1/dns/create_domain', - :params => %w(domain serverip), - :apikey_required => true - }, - create_record: { - :method => :post, - :path => '/v1/dns/create_record', - :params => %w(domain name type data ttl priority), - :apikey_required => true - }, - delete_domain: { - :method => :post, - :path => '/v1/dns/delete_domain', - :params => %w(domain), - :apikey_required => true - }, - delete_record: { - :method => :post, - :path => '/v1/dns/delete_record', - :params => %w(domain RECORDID), - :apikey_required => true - }, - dnssec_enable: { - :method => :post, - :path => '/v1/dns/dnssec_enable', - :params => %w(domain enable), - :apikey_required => true - }, - dnssec_info: { - :method => :post, - :path => '/v1/dns/dnssec_info', - :params => %w(domain), - :apikey_required => true - }, - list: { - :method => :get, - :path => '/v1/dns/list', - :apikey_required => true - }, - records: { - :method => :get, - :path => '/v1/dns/records', - :params => %w(domain), - :apikey_required => true - }, - soa_info: { - :method => :get, - :path => '/v1/dns/records', - :params => %w(domain), - :apikey_required => true - }, - soa_update: { - :method => :get, - :path => '/v1/dns/records', - :params => %w(domain), - :apikey_required => true - }, - update_record: { - :method => :post, - :path => '/v1/dns/update_record', - :params => %w(domain RECORDID name type data ttl priority), - :apikey_required => true - } - }, - # Firewall - Firewall: { - group_create: { - :method => :post, - :path => '/v1/firewall/group_create', - :params => %w(description), - :apikey_required => true - }, - group_delete: { - :method => :post, - :path => '/v1/firewall/group_delete', - :params => %w(FIREWALLGROUPID), - :apikey_required => true - }, - group_list: { - :method => :get, - :path => '/v1/firewall/group_list', - :params => %w(FIREWALLGROUPID), - :apikey_required => true - }, - group_set_description: { - :method => :post, - :path => '/v1/firewall/group_set_description', - :params => %w(FIREWALLGROUPID description), - :apikey_required => true - }, - rule_create: { - :method => :post, - :path => '/v1/firewall/rule_create', - :params => %w(FIREWALLGROUPID direction ip_type protocol subnet subnet_size port notes), - :apikey_required => true - }, - rule_delete: { - :method => :post, - :path => '/v1/firewall/rule_delete', - :params => %w(FIREWALLGROUPID rulenumber), - :apikey_required => true - }, - rule_list: { - :method => :get, - :path => '/v1/firewall/rule_list', - :params => %w(FIREWALLGROUPID direction ip_type), - :apikey_required => true - } - }, - # ISO Image - ISO: { - create_from_url: { - :method => :post, - :path => '/v1/iso/create_from_url', - :params => %w(url), - :apikey_required => true - }, - list: { - :method => :get, - :path => '/v1/iso/list', - :apikey_required => true - }, - list_public: { - :method => :get, - :path => '/v1/iso/list_public', - :apikey_required => true - } - }, - # Network - Network: { - create: { - :method => :post, - :path => '/v1/network/create', - :params => %w(DCID description v4_subnet v4_subnet_mask), - :apikey_required => true - }, - destroy: { - :method => :post, - :path => '/v1/network/destroy', - :params => %w(NETWORKID), - :apikey_required => true - }, - list: { - :method => :get, - :path => '/v1/network/list', - :apikey_required => true - } - }, - # Operating System - OS: { - list: { - :method => :get, - :path => '/v1/os/list', - :apikey_required => false - } - }, - # Plans - Plans: { - list: { - :method => :get, - :path => '/v1/plans/list', - :params => %w(type), - :apikey_required => true - }, - list_baremetal: { - :method => :get, - :path => '/v1/plans/list_baremetal', - :apikey_required => true - }, - list_vc2: { - :method => :get, - :path => '/v1/plans/list_vc2', - :apikey_required => false - }, - list_vdc2: { - :method => :get, - :path => '/v1/plans/list_vdc2', - :apikey_required => false - } - }, - # Regions - Regions: { - availability: { - :method => :get, - :path => '/v1/regions/availability', - :params => %w(DCID type), - :apikey_required => false - }, - availability_baremetal: { - :method => :get, - :path => '/v1/regions/availability_baremetal', - :params => %w(DCID), - :apikey_required => true - }, - availability_vc2: { - :method => :get, - :path => '/v1/regions/availability_vc2', - :params => %w(DCID), - :apikey_required => false - }, - availability_vdc2: { - :method => :get, - :path => '/v1/regions/availability_vdc2', - :params => %w(DCID), - :apikey_required => false - }, - list: { - :method => :get, - :path => '/v1/regions/list', - :params => %w(availability), - :apikey_required => false - } - }, - # Reserved IP - ReservedIP: { - attach: { - :method => :post, - :path => '/v1/reservedip/attach', - :params => %w(ip_address attach_SUBID), - :apikey_required => true - }, - convert: { - :method => :post, - :path => '/v1/reservedip/convert', - :params => %w(SUBID ip_address label), - :apikey_required => true - }, - create: { - :method => :post, - :path => '/v1/reservedip/create', - :params => %w(DCID ip_type label), - :apikey_required => true - }, - destroy: { - :method => :post, - :path => '/v1/reservedip/destroy', - :params => %w(ip_address), - :apikey_required => true - }, - detach: { - :method => :post, - :path => '/v1/reservedip/detach', - :params => %w(ip_address detach_SUBID), - :apikey_required => true - }, - list: { - :method => :get, - :path => '/v1/reservedip/list', - :apikey_required => true - } - }, - # Server - Server: { - app_change: { - :method => :post, - :path => '/v1/server/app_change', - :params => %w(SUBID APPID), - :apikey_required => true - }, - app_change_list: { - :method => :get, - :path => '/v1/server/app_change_list', - :params => ['SUBID'], - :apikey_required => true - }, - backup_disable: { - :method => :post, - :path => '/v1/server/backup_disable', - :params => %w(SUBID), - :apikey_required => true - }, - backup_enable: { - :method => :post, - :path => '/v1/server/backup_enable', - :params => %w(SUBID), - :apikey_required => true - }, - backup_get_schedule: { - :method => :post, - :path => '/v1/server/backup_get_schedule', - :params => %w(SUBID), - :apikey_required => true - }, - backup_set_schedule: { - :method => :post, - :path => '/v1/server/backup_set_schedule', - :params => %w(SUBID cron_type hour dow dom), - :apikey_required => true - }, - bandwidth: { - :method => :get, - :path => '/v1/server/bandwidth', - :params => %w(SUBID), - :apikey_required => true - }, - create: { - :method => :post, - :path => '/v1/server/create', - :params => %w(DCID VPSPLANID OSID ipxe_chain_url ISOID SCRIPTID SNAPSHOTID enable_ipv6 enable_private_network NETWORKID label SSHKEYID auto_backups APPID userdata notify_activate ddos_protection reserved_ip_v4 hostname tag FIREWALLGROUPID), - :apikey_required => true - }, - create_ipv4: { - :method => :post, - :path => '/v1/server/create_ipv4', - :params => %w(SUBID reboot), - :apikey_required => true - }, - destroy: { - :method => :post, - :path => '/v1/server/destroy', - :params => %w(SUBID), - :apikey_required => true - }, - destroy_ipv4: { - :method => :post, - :path => '/v1/server/destroy_ipv4', - :params => %w(SUBID ip), - :apikey_required => true - }, - firewall_group_set: { - :method => :post, - :path => '/v1/server/firewall_group_set', - :params => %w(SUBID FIREWALLGROUPID), - :apikey_required => true - }, - get_app_info: { - :method => :get, - :path => '/v1/server/get_app_info', - :params => %w(SUBID), - :apikey_required => true - }, - get_user_data: { - :method => :get, - :path => '/v1/server/set_user_data', - :params => %w(SUBID), - :apikey_required => true - }, - halt: { - :method => :post, - :path => '/v1/server/halt', - :params => %w('SUBID'), - :apikey_required => true - }, - ipv6_enable: { - :method => :post, - :path => '/v1/server/ipv6_enable', - :params => %w(SUBID), - :apikey_required => true - }, - iso_attach: { - :method => :post, - :path => '/v1/server/iso_detach', - :params => %w(SUBID, ISOID), - :apikey_required => true - }, - iso_detach: { - :method => :post, - :path => '/v1/server/iso_detach', - :params => %w(SUBID), - :apikey_required => true - }, - iso_status: { - :method => :get, - :path => '/v1/server/iso_status', - :params => %w(SUBID), - :apikey_required => true - }, - label_set: { - :method => :post, - :path => '/v1/server/label_set', - :params => %w(SUBID label), - :apikey_required => true - }, - list: { - :method => :get, - :path => '/v1/server/list', - :params => %w(SUBID tag label main_ip), - :apikey_required => true - }, - list_ipv4: { - :method => :get, - :path => '/v1/server/list_ipv4', - :params => %w(SUBID), - :apikey_required => true - }, - list_ipv6: { - :method => :get, - :path => '/v1/server/list_ipv6', - :params => %w(SUBID), - :apikey_required => true - }, - neighbors: { - :method => :get, - :path => '/v1/server/neighbors', - :params => %w(SUBID), - :apikey_required => true - }, - os_change: { - :method => :post, - :path => '/v1/server/os_change', - :params => %w(SUBID OSID), - :apikey_required => true - }, - os_change_list: { - :method => :get, - :path => '/v1/server/os_change_list', - :params => %w(SUBID), - :apikey_required => true - }, - private_network_disable: { - :method => :post, - :path => '/v1/server/private_network_disable', - :params => %w(SUBID NETWORKID), - :apikey_required => true - }, - private_network_enable: { - :method => :post, - :path => '/v1/server/private_network_enable', - :params => %w(SUBID NETWORKID), - :apikey_required => true - }, - private_networks: { - :method => :get, - :path => '/v1/server/private_networks', - :params => %w(SUBID), - :apikey_required => true - }, - reboot: { - :method => :post, - :path => '/v1/server/reboot' 'SUBID', - :params => %w(SUBID), - :apikey_required => true - }, - reinstall: { - :method => :post, - :path => '/v1/server/reinstall', - :params => %w(SUBID hostname), - :apikey_required => true - }, - restore_backup: { - :method => :post, - :path => '/v1/server/restore_backup', - :params => %w(SUBID BACKUPID), - :apikey_required => true - }, - restore_snapshot: { - :method => :post, - :path => '/v1/server/restore_snapshot', - :params => %w(SUBID SNAPSHOTID), - :apikey_required => true - }, - reverse_default_ipv4: { - :method => :post, - :path => '/v1/server/reverse_default_ipv4', - :params => %w(SUBID ip), - :apikey_required => true - }, - reverse_delete_ipv6: { - :method => :post, - :path => '/v1/server/reverse_delete_ipv6', - :params => %w(SUBID ip), - :apikey_required => true - }, - reverse_list_ipv6: { - :method => :get, - :path => '/v1/server/reverse_list_ipv6', - :params => %w(SUBID), - :apikey_required => true - }, - reverse_set_ipv4: { - :method => :post, - :path => '/v1/server/reverse_set_ipv4', - :params => %w(SUBID ip entry), - :apikey_required => true - }, - reverse_set_ipv6: { - :method => :post, - :path => '/v1/server/reverse_set_ipv6', - :params => %w(SUBID ip entry), - :apikey_required => true - }, - set_user_data: { - :method => :post, - :path => '/v1/server/set_user_data', - :params => %w(SUBID userdata), - :apikey_required => true - }, - start: { - :method => :post, - :path => '/v1/server/start', - :params => %w(SUBID), - :apikey_required => true - }, - tag_set: { - :method => :post, - :path => '/v1/server/tag_set', - :params => %w(SUBID tag), - :apikey_required => true - }, - upgrade_plan: { - :method => :post, - :path => '/v1/server/upgrade_plan', - :params => %w(SUBID VPSPLANID), - :apikey_required => true - }, - upgrade_plan_list: { - :method => :get, - :path => '/v1/server/upgrade_plan_list', - :params => %w(SUBID), - :apikey_required => true - } - }, - # Snapshot - Snapshot: { - create: { - :method => :post, - :path => '/v1/snapshot/create', - :params => %w(SUBID), - :apikey_required => true - }, - destroy: { - :method => :post, - :path => '/v1/snapshot/destroy', - :params => %w(SNAPSHOTID), - :apikey_required => true - }, - list: { - :method => :get, - :path => '/v1/snapshot/list', - :apikey_required => true - } - }, - # SSH Key - SSHKey: { - create: { - :method => :post, - :path => '/v1/sshkey/create', - :params => %w(name ssh_key), - :apikey_required => true - }, - destroy: { - :method => :post, - :path => '/v1/sshkey/destroy', - :params => %w(SSHKEYID), - :apikey_required => true - }, - list: { - :method => :get, - :path => '/v1/sshkey/list', - :apikey_required => true - }, - update: { - :method => :post, - :path => '/v1/sshkey/update', - :params => %w(SSHKEYID name ssh_key), - :apikey_required => true - } - }, - # Startup Script - StartupScript: { - create: { - :method => :post, - :path => '/v1/startupscript/create', - :params => %w(name script type), - :apikey_required => true - }, - destroy: { - :method => :post, - :path => '/v1/startupscript/destroy', - :params => %w(SCRIPTID), - :apikey_required => true - }, - list: { - :method => :get, - :path => '/v1/startupscript/list', - :apikey_required => true - }, - update: { - :method => :post, - :path => '/v1/startupscript/update', - :params => %w(SCRIPTID name script), - :apikey_required => true - } - }, - # User Management - User: { - create: { - :method => :post, - :path => '/v1/user/create', - :params => %w(email name password api_enabled acls), - :apikey_required => true - }, - delete: { - :method => :post, - :path => '/v1/user/delete', - :params => %w(USERID), - :apikey_required => true - }, - list: { - :method => :get, - :path => '/v1/user/list', - :apikey_required => true - }, - update: { - :method => :post, - :path => '/v1/user/update', - :params => %w(USERID email name password api_enabled acls), - :apikey_required => true - } - } - } - - DEFINITIONS.each do |resource_name, actions| - resource_class = Class.new(Object) do - actions.each do |action, config| - method = config.fetch(:method) - path = config.fetch(:path) - params = config.fetch(:params, nil) - apikey_required = config.fetch(:apikey_required, false) - - define_singleton_method action do |*args| - headers = apikey_required ? {'API-Key' => Vultr::APIKey} : {} - body = nil - if method == :get - extra_params = Hash.new - hash = args[-1] - if hash.is_a?(Hash) - hash.each do |key, value| - extra_params[key] = value if params.include? key.to_s - end - end - url = Vultr.request.build_url(path, extra_params) - elsif method == :post - body = Hash.new - hash = args[-1] - if hash.is_a?(Hash) - hash.each do |key, value| - body[key] = value if params.include? key.to_s - end - end - url = Vultr.request.build_url(path) - end - - resp = Vultr.request.run_request(method, url, body, headers) - {status: resp.status, result: resp.body} - end - end - end - - Vultr.const_set(resource_name, resource_class) - end - - def request=(request) - @request = request - end - - def request - @request - end - - def api_key=(api_key) - Vultr.const_set('APIKey', api_key) - setup_request! - - Vultr::APIKey - end - - def api_key - return Vultr::APIKey if Vultr::APIKey - 'API Key is required' - end - - private - - def setup_request! - Vultr.request = Faraday.new do |faraday| - faraday.url_prefix = 'https://api.vultr.com' - faraday.request :url_encoded - faraday.response :json, :content_type => 'application/json' - faraday.adapter :net_http - end - end +module Vultr + autoload :Client, "vultr/client" + autoload :Collection, "vultr/collection" + autoload :Error, "vultr/error" + autoload :Resource, "vultr/resource" + autoload :Object, "vultr/object" + + autoload :AccountResource, "vultr/resources/account" + autoload :ApplicationsResource, "vultr/resources/applications" + autoload :InstancesResource, "vultr/resources/instances" + autoload :BackupsResource, "vultr/resources/backups" + autoload :BareMetalResource, "vultr/resources/bare_metal" + autoload :BlockStorageResource, "vultr/resources/block_storage" + autoload :DnsResource, "vultr/resources/dns" + autoload :FirewallResource, "vultr/resources/firewall" + autoload :IsoResource, "vultr/resources/iso" + autoload :KubernetesResource, "vultr/resources/kubernetes" + autoload :LoadBalancersResource, "vultr/resources/load_balancers" + autoload :ObjectStorageResource, "vultr/resources/object_storage" + autoload :OperatingSystemsResource, "vultr/resources/operating_systems" + autoload :PlansResource, "vultr/resources/plans" + autoload :PrivateNetworksResource, "vultr/resources/private_networks" + autoload :RegionsResource, "vultr/resources/regions" + autoload :ReservedIpsResource, "vultr/resources/reserved_ips" + autoload :SnapshotsResource, "vultr/resources/snapshots" + autoload :SshKeysResource, "vultr/resources/ssh_keys" + autoload :StartupScriptsResource, "vultr/resources/startup_scripts" + autoload :UserResource, "vultr/resources/users" + + autoload :Account, "vultr/objects/account" + autoload :Application, "vultr/objects/application" + autoload :Instance, "vultr/objects/instance" + autoload :User, "vultr/objects/user" + + def self.api_key=(key) + @api_key = key + end + + def self.api_key + @api_key + end end diff --git a/lib/vultr/client.rb b/lib/vultr/client.rb new file mode 100644 index 0000000..e480952 --- /dev/null +++ b/lib/vultr/client.rb @@ -0,0 +1,42 @@ +module Vultr + class Client + BASE_URL = "https://api.vultr.com/v2" + + attr_reader :api_key, :adapter + + def initialize(api_key: Vultr.api_key, adapter: Faraday.default_adapter) + #raise Error, "Please set Vultr.api_key = 'xxx' to make a request" unless api_key + @api_key = api_key + @adapter = adapter + end + + def account + AccountResource.new(self) + end + + def applications + ApplicationsResource.new(self) + end + + def backups + BackupResource.new(self) + end + + def instances + InstancesResource.new(self) + end + + def users + UserResource.new(self) + end + + def connection + @connection ||= Faraday.new do |conn| + conn.url_prefix = BASE_URL + conn.request :json + conn.response :json, content_type: 'application/json' + conn.adapter adapter + end + end + end +end diff --git a/lib/vultr/collection.rb b/lib/vultr/collection.rb new file mode 100644 index 0000000..331618b --- /dev/null +++ b/lib/vultr/collection.rb @@ -0,0 +1,22 @@ +module Vultr + class Collection + attr_reader :data, :total, :next_cursor, :prev_cursor + + def self.from_response(response, key:, type:) + body = response.body + new( + data: body[key].map{ |attrs| type.new(attrs) }, + total: body.dig("meta", "total"), + next_cursor: body.dig("meta", "links", "next"), + prev_cursor: body.dig("meta", "links", "prev") + ) + end + + def initialize(data:, total:, next_cursor:, prev_cursor:) + @data = data + @total = total + @next_cursor = next_cursor.empty? ? nil : next_cursor + @prev_cursor = prev_cursor.empty? ? nil : prev_cursor + end + end +end diff --git a/lib/vultr/error.rb b/lib/vultr/error.rb new file mode 100644 index 0000000..8a72d5b --- /dev/null +++ b/lib/vultr/error.rb @@ -0,0 +1,4 @@ +module Vultr + class Error < StandardError + end +end diff --git a/lib/vultr/object.rb b/lib/vultr/object.rb new file mode 100644 index 0000000..e2d6ad4 --- /dev/null +++ b/lib/vultr/object.rb @@ -0,0 +1,14 @@ +require "ostruct" + +module Vultr + class Object + def initialize(attributes) + @attributes = OpenStruct.new(attributes) + end + + def method_missing(method, *args, &block) + attribute = @attributes.send(method, *args, &block) + attribute.is_a?(Hash) ? Object.new(attribute) : attribute + end + end +end diff --git a/lib/vultr/objects/account.rb b/lib/vultr/objects/account.rb new file mode 100644 index 0000000..e703427 --- /dev/null +++ b/lib/vultr/objects/account.rb @@ -0,0 +1,4 @@ +module Vultr + class Account < Object + end +end diff --git a/lib/vultr/objects/application.rb b/lib/vultr/objects/application.rb new file mode 100644 index 0000000..be14d01 --- /dev/null +++ b/lib/vultr/objects/application.rb @@ -0,0 +1,4 @@ +module Vultr + class Application < Object + end +end diff --git a/lib/vultr/objects/instance.rb b/lib/vultr/objects/instance.rb new file mode 100644 index 0000000..6dfe385 --- /dev/null +++ b/lib/vultr/objects/instance.rb @@ -0,0 +1,4 @@ +module Vultr + class Instance < Object + end +end diff --git a/lib/vultr/objects/user.rb b/lib/vultr/objects/user.rb new file mode 100644 index 0000000..b5f8f71 --- /dev/null +++ b/lib/vultr/objects/user.rb @@ -0,0 +1,4 @@ +module Vultr + class User < Object + end +end diff --git a/lib/vultr/resource.rb b/lib/vultr/resource.rb new file mode 100644 index 0000000..f709af2 --- /dev/null +++ b/lib/vultr/resource.rb @@ -0,0 +1,54 @@ +module Vultr + class Resource + attr_reader :client + + def initialize(client) + @client = client + end + + private + + def get_request(url, params: {}, headers: {}) + handle_response client.connection.get(url, params, default_headers.merge(headers)) + end + + def post_request(url, body:, headers: {}) + handle_response client.connection.post(url, body, default_headers.merge(headers)) + end + + def patch_request(url, body:, headers: {}) + handle_response client.connection.patch(url, body, default_headers.merge(headers)) + end + + def put_request(url, body:, headers: {}) + handle_response client.connection.put(url, body, default_headers.merge(headers)) + end + + def delete_request(url, params: {}, headers: {}) + handle_response client.connection.delete(url, params, default_headers.merge(headers)) + end + + def default_headers + {Authorization: "Bearer #{client.api_key}"} + end + + def handle_response(response) + case response.status + when 400 + raise Error, "Your request was malformed. #{response.body["error"]}" + when 401 + raise Error, "You did not supply valid authentication credentials. #{response.body["error"]}" + when 403 + raise Error, "You are not allowed to perform that action. #{response.body["error"]}" + when 404 + raise Error, "No results were found for your request. #{response.body["error"]}" + when 429 + raise Error, "Your request exceeded the API rate limit. #{response.body["error"]}" + when 500 + raise Error, "We were unable to perform the request due to server-side problems. #{response.body["error"]}" + end + + response + end + end +end diff --git a/lib/vultr/resources/account.rb b/lib/vultr/resources/account.rb new file mode 100644 index 0000000..1479b86 --- /dev/null +++ b/lib/vultr/resources/account.rb @@ -0,0 +1,7 @@ +module Vultr + class AccountResource < Resource + def info + Account.new get("account").body + end + end +end diff --git a/lib/vultr/resources/applications.rb b/lib/vultr/resources/applications.rb new file mode 100644 index 0000000..af30ab8 --- /dev/null +++ b/lib/vultr/resources/applications.rb @@ -0,0 +1,8 @@ +module Vultr + class ApplicationsResource < Resource + def list + Application.new get_request("applications").body + end + end +end + diff --git a/lib/vultr/resources/backups.rb b/lib/vultr/resources/backups.rb new file mode 100644 index 0000000..5c4deae --- /dev/null +++ b/lib/vultr/resources/backups.rb @@ -0,0 +1,8 @@ +module Vultr + class BackupsResource < Resource + def list + Backup.new get_request("backups").body + end + end +end + diff --git a/lib/vultr/resources/bare_metal.rb b/lib/vultr/resources/bare_metal.rb new file mode 100644 index 0000000..dec1016 --- /dev/null +++ b/lib/vultr/resources/bare_metal.rb @@ -0,0 +1,8 @@ +module Vultr + class BareMetalResource < Resource + def list + BareMetal.new get_request("bare-metals").body + end + end +end + diff --git a/lib/vultr/resources/block_storage.rb b/lib/vultr/resources/block_storage.rb new file mode 100644 index 0000000..9040b79 --- /dev/null +++ b/lib/vultr/resources/block_storage.rb @@ -0,0 +1,8 @@ +module Vultr + class BlockStorageResource < Resource + def list + BlockStorage.new get_request("blocks").body + end + end +end + diff --git a/lib/vultr/resources/dns.rb b/lib/vultr/resources/dns.rb new file mode 100644 index 0000000..2d658ce --- /dev/null +++ b/lib/vultr/resources/dns.rb @@ -0,0 +1,8 @@ +module Vultr + class DnsResource < Resource + def list + Domain.new get_request("domains").body + end + end +end + diff --git a/lib/vultr/resources/firewall.rb b/lib/vultr/resources/firewall.rb new file mode 100644 index 0000000..c23d0f1 --- /dev/null +++ b/lib/vultr/resources/firewall.rb @@ -0,0 +1,8 @@ +module Vultr + class FirewallResource < Resource + def list + FirewallGroup.new get_request("firewalls").body + end + end +end + diff --git a/lib/vultr/resources/instances.rb b/lib/vultr/resources/instances.rb new file mode 100644 index 0000000..8ffaa59 --- /dev/null +++ b/lib/vultr/resources/instances.rb @@ -0,0 +1,145 @@ +module Vultr + class InstancesResource < Resource + def list + response = get_request("instances") + Collection.from_response(response, key: "instances", type: Instance) + end + + def retrieve(id) + Instance.new get_request("instances/#{id}").body + end + + def create(**attributes) + Instance.new post_request("instances", body: attributes).body.dig("instance") + end + + def update(id, **attributes) + patch_request("instances/#{id}", body: attributes) + end + + def delete(id) + delete_request("instances/#{id}") + end + + def start(id) + post_request("instances/#{id}/start") + end + + def reboot(id) + post_request("instances/#{id}/reboot") + end + + def reinstall(id) + Instance.new post_request("instances/#{id}/reinstall").body.dig("instance") + end + + def restore(id, **attributes) + Object.new post_request("instances/#{id}/restore", attributes).body.dig("status") + end + + def halt(id) + post_request("instances/#{id}/halt") + end + + def bandwidth(id) + Object.new get_request("instances/#{id}/bandwidth").body.dig("bandwidth") + end + + def neighbors(id) + Object.new get_request("instances/#{id}/neighbors").body.dig("neighbors") + end + + def user_data(id) + Object.new get_request("instances/#{id}/user-data").body.dig("user_data") + end + + def upgrades(id, **params) + Object.new get_request("instances/#{id}/upgrades", params).body.dig("upgrades") + end + + def ipv4(id, **params) + response = get_request("instances/#{id}/ipv4", params) + Collection.from_response(response, key: "ipv4s", type: Object) + end + + def create_ipv4(id, **params) + Object.new post_request("instances/#{id}/ipv4", params).body.dig("ipv4") + end + + def delete_ipv4(id, ipv4:) + delete_request("instances/#{id}/ipv4/#{ipv4}") + end + + def create_ipv4_reverse(id, **params) + post_request("instances/#{id}/ipv4/reverse", params) + end + + def set_default_reverse_dns_entry(id, **params) + post_request("instances/#{id}/ipv4/reverse/default", params) + end + + def ipv6(id, **params) + response = get_request("instances/#{id}/ipv6", params) + Collection.from_response(response, key: "ipv6s", type: Object) + end + + def ipv6_reverse(id) + response = get_request("instances/#{id}/ipv6") + Collection.from_response(response, key: "reverse_ipv6s", type: Object) + end + + def create_ipv6_reverse(id, **params) + post_request("instances/#{id}/ipv6/reverse", params) + end + + def delete_ipv6_reverse(id, ipv6:) + delete_request("instances/#{id}/ipv6/reverse/#{ipv6}") + end + + def private_networks(id) + response = get_request("instances/#{id}/private-networks") + Collection.from_response(response, key: "private_networks", type: PrivateNetwork) + end + + def attach_private_network(id, network_id:) + post_request("instances/#{id}/private-networks/attach", { network_id: network_id }) + end + + def detach_private_network(id, network_id:) + post_request("instances/#{id}/private-networks/detach", { network_id: network_id }) + end + + def iso(id) + Object.new get_request("instances/#{id}/iso").body.dig("iso_status") + end + + def attach_iso(id, iso_id:) + Object.new post_request("instances/#{id}/iso/attach", { iso_id: iso_id }).body.dig("iso_status") + end + + def detach_iso(id, iso_id:) + Object.new post_request("instances/#{id}/iso/detach", { iso_id: iso_id }).body.dig("iso_status") + end + + def get_backup_schedule(id) + Object.new get_request("instances/#{id}/backup-schedule").body.dig("backup_schedule") + end + + def set_backup_schedule(id, **attributes) + post_request("instances/#{id}/backup-schedule", attributes) + end + + # Bulk operations + def halt_instances(ids) + post_request("instances/halt", { instance_ids: Array(ids) }) + end + + def reboot_instances(ids) + post_request("instances/reboot", { instance_ids: Array(ids) }) + end + + def start_instances(ids) + post_request("instances/start", { instance_ids: Array(ids) }) + end + end +end diff --git a/lib/vultr/resources/iso.rb b/lib/vultr/resources/iso.rb new file mode 100644 index 0000000..3bc6cf0 --- /dev/null +++ b/lib/vultr/resources/iso.rb @@ -0,0 +1,8 @@ +module Vultr + class IsoResource < Resource + def + Iso.new get_request("iso").body + end + end +end + diff --git a/lib/vultr/resources/kubernetes.rb b/lib/vultr/resources/kubernetes.rb new file mode 100644 index 0000000..1ad9b7d --- /dev/null +++ b/lib/vultr/resources/kubernetes.rb @@ -0,0 +1,8 @@ +module Vultr + class KubernetesResource < Resource + def + Kubernetes.new get_request("kubernetes/clusters").body + end + end +end + diff --git a/lib/vultr/resources/load_balancers.rb b/lib/vultr/resources/load_balancers.rb new file mode 100644 index 0000000..4691ff3 --- /dev/null +++ b/lib/vultr/resources/load_balancers.rb @@ -0,0 +1,8 @@ +module Vultr + class LoadBalancersResource < Resource + def + LoadBalancer.new get_request("load-balancers").body + end + end +end + diff --git a/lib/vultr/resources/object_storage.rb b/lib/vultr/resources/object_storage.rb new file mode 100644 index 0000000..8720928 --- /dev/null +++ b/lib/vultr/resources/object_storage.rb @@ -0,0 +1,8 @@ +module Vultr + class ObjectStorageResource < Resource + def + ObjectStorage.new get_request("object-storage").body + end + end +end + diff --git a/lib/vultr/resources/operating_systems.rb b/lib/vultr/resources/operating_systems.rb new file mode 100644 index 0000000..53e83f4 --- /dev/null +++ b/lib/vultr/resources/operating_systems.rb @@ -0,0 +1,8 @@ +module Vultr + class OperatingSystemsResource < Resource + def + OperatingSystem.new get_request("os").body + end + end +end + diff --git a/lib/vultr/resources/plans.rb b/lib/vultr/resources/plans.rb new file mode 100644 index 0000000..fc391ee --- /dev/null +++ b/lib/vultr/resources/plans.rb @@ -0,0 +1,12 @@ +module Vultr + class PlansResource < Resource + def list + Plan.new get_request("plans").body + end + + def list_metal + Plan.new get_request("plans-metal").body + end + end +end + diff --git a/lib/vultr/resources/private_networks.rb b/lib/vultr/resources/private_networks.rb new file mode 100644 index 0000000..2226a29 --- /dev/null +++ b/lib/vultr/resources/private_networks.rb @@ -0,0 +1,8 @@ +module Vultr + class PrivateNetworksResource < Resource + def + PrivateNetwork.new get_request("private-networks").body + end + end +end + diff --git a/lib/vultr/resources/regions.rb b/lib/vultr/resources/regions.rb new file mode 100644 index 0000000..1b3ca36 --- /dev/null +++ b/lib/vultr/resources/regions.rb @@ -0,0 +1,12 @@ +module Vultr + class RegionsResource < Resource + def list + Region.new get_request("regions").body + end + + def list_availability(id) + Region.new get_request("regions/#{id}/availability") + end + end +end + diff --git a/lib/vultr/resources/reserved_ips.rb b/lib/vultr/resources/reserved_ips.rb new file mode 100644 index 0000000..d4c47da --- /dev/null +++ b/lib/vultr/resources/reserved_ips.rb @@ -0,0 +1,8 @@ +module Vultr + class ReservedIpsResource < Resource + def list + ReservedIps.new get_request("reserved-ips").body + end + end +end + diff --git a/lib/vultr/resources/snapshots.rb b/lib/vultr/resources/snapshots.rb new file mode 100644 index 0000000..563cf0d --- /dev/null +++ b/lib/vultr/resources/snapshots.rb @@ -0,0 +1,8 @@ +module Vultr + class SnapshotsResource < Resource + def list + Snapshot.new get_request("snapshots").body + end + end +end + diff --git a/lib/vultr/resources/ssh_keys.rb b/lib/vultr/resources/ssh_keys.rb new file mode 100644 index 0000000..575cebf --- /dev/null +++ b/lib/vultr/resources/ssh_keys.rb @@ -0,0 +1,8 @@ +module Vultr + class SshKeysResource < Resource + def list + SshKey.new get_request("startup-scripts").body + end + end +end + diff --git a/lib/vultr/resources/startup_scripts.rb b/lib/vultr/resources/startup_scripts.rb new file mode 100644 index 0000000..eaa8ac8 --- /dev/null +++ b/lib/vultr/resources/startup_scripts.rb @@ -0,0 +1,8 @@ +module Vultr + class StartupScriptsResource < Resource + def + StartupScript.new get("").body + end + end +end + diff --git a/lib/vultr/resources/users.rb b/lib/vultr/resources/users.rb new file mode 100644 index 0000000..1e37097 --- /dev/null +++ b/lib/vultr/resources/users.rb @@ -0,0 +1,24 @@ +module Vultr + class UserResource < Resource + def list(params: {}) + response = get_request("users", params: params) + Collection.from_response(response, key: "users", type: User) + end + + def retrieve(id) + User.new get_request("users/#{id}").body + end + + def create(**attributes) + User.new post_request("users", body: attributes).body.dig("user") + end + + def update(id, **attributes) + patch_request("users/#{id}", body: attributes) + end + + def delete(id) + delete_request("users/#{id}") + end + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 3f8f183..abd3458 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,6 +1,3 @@ -require 'coveralls' -Coveralls.wear! - $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) require 'vultr' require 'minitest/autorun' diff --git a/test/vultr/client_test.rb b/test/vultr/client_test.rb new file mode 100644 index 0000000..6cbd9cb --- /dev/null +++ b/test/vultr/client_test.rb @@ -0,0 +1,8 @@ +require "test_helper" + +class ClientTest < Minitest::Test + def test_api_key + client = Vultr::Client.new api_key: "test" + assert_equal "test", client.api_key + end +end diff --git a/test/vultr/config_test.rb b/test/vultr/config_test.rb new file mode 100644 index 0000000..0f5df40 --- /dev/null +++ b/test/vultr/config_test.rb @@ -0,0 +1,9 @@ +require "test_helper" + +class ConfigTest < Minitest::Test + def test_api_key + assert_nil Vultr.api_key + Vultr.api_key = "test" + assert_equal "test", Vultr.api_key + end +end diff --git a/test/vultr/object_test.rb b/test/vultr/object_test.rb new file mode 100644 index 0000000..6911655 --- /dev/null +++ b/test/vultr/object_test.rb @@ -0,0 +1,11 @@ +require "test_helper" + +class ObjectTest < Minitest::Test + def test_creating_object_from_hash + assert_equal :bar, Vultr::Object.new(foo: :bar).foo + end + + def test_nested_hash + assert_equal :foobar, Vultr::Object.new(foo: { bar: { baz: :foobar } }).foo.bar.baz + end +end diff --git a/test/vultr/resources/account_test.rb b/test/vultr/resources/account_test.rb new file mode 100644 index 0000000..a87fee2 --- /dev/null +++ b/test/vultr/resources/account_test.rb @@ -0,0 +1,10 @@ +require "test_helper" + +class AccountResourceTest < Minitest::Test + def test_info + end + + def test_retrieve + end +end + diff --git a/test/vultr/vultr_account_test.rb b/test/vultr/vultr_account_test.rb deleted file mode 100644 index 0c36c2c..0000000 --- a/test/vultr/vultr_account_test.rb +++ /dev/null @@ -1,29 +0,0 @@ -require 'test_helper' - -class VultrAccountTest < Minitest::Test - - # Called before every test method runs. Can be used - # to set up fixture information. - def setup - Vultr.api_key = ENV['VULTR_API_KEY'] - end - - def test_account_info_response - r = Vultr::Account.info - - assert r.has_key? :status - assert_equal r[:status], 200 - - assert r.has_key? :result - assert r[:result].has_key? 'balance' - assert r[:result].has_key? 'pending_charges' - assert r[:result].has_key? 'last_payment_date' - assert r[:result].has_key? 'last_payment_amount' - end - - # Called after every test method runs. Can be used to tear - # down fixture information. - def teardown - # Do nothing - end -end diff --git a/test/vultr/vultr_app_test.rb b/test/vultr/vultr_app_test.rb deleted file mode 100644 index e4377cd..0000000 --- a/test/vultr/vultr_app_test.rb +++ /dev/null @@ -1,26 +0,0 @@ -require 'test_helper' - -class VultrAppTest < Minitest::Test - - # Called before every test method runs. Can be used - # to set up fixture information. - def setup - Vultr.api_key = ENV['VULTR_API_KEY'] - end - - def test_app_list_response - r = Vultr::App.list - - assert r.has_key? :status - assert_equal r[:status], 200 - - assert r.has_key? :result - assert_instance_of Hash, r[:result] - end - - # Called after every test method runs. Can be used to tear - # down fixture information. - def teardown - # Do nothing - end -end diff --git a/test/vultr/vultr_backup_test.rb b/test/vultr/vultr_backup_test.rb deleted file mode 100644 index 077791b..0000000 --- a/test/vultr/vultr_backup_test.rb +++ /dev/null @@ -1,22 +0,0 @@ -require 'test_helper' - -class VultrBackupTest < Minitest::Test - - def setup - Vultr.api_key = ENV['VULTR_API_KEY'] - end - - def test_backup_list_response - r = Vultr::Backup.list - - assert r.has_key? :status - assert_equal r[:status], 200 - - assert r.has_key? :result - assert_instance_of Array, r[:result] - end - - def teardown - # Do nothing - end -end diff --git a/test/vultr/vultr_iso_test.rb b/test/vultr/vultr_iso_test.rb deleted file mode 100644 index 87e0cb8..0000000 --- a/test/vultr/vultr_iso_test.rb +++ /dev/null @@ -1,27 +0,0 @@ -require 'test_helper' - -class VultrIsoTest < Minitest::Test - - def setup - Vultr.api_key = ENV['VULTR_API_KEY'] - end - - def test_iso_list_response - r = Vultr::ISO.list - - assert r.has_key? :status - assert r[:status], 200 - - assert r.has_key? :result - - if r[:result].empty? - assert_instance_of Array, r[:result] - else - assert_instance_of Hash, r[:result] - end - end - - def teardown - # Do nothing - end -end diff --git a/test/vultr/vultr_os_test.rb b/test/vultr/vultr_os_test.rb deleted file mode 100644 index fe3c8b2..0000000 --- a/test/vultr/vultr_os_test.rb +++ /dev/null @@ -1,21 +0,0 @@ -require 'test_helper' - -class VultrOsTest < Minitest::Test - def setup - Vultr.api_key = ENV['VULTR_API_KEY'] - end - - def test_os_list_response - r = Vultr::OS.list - - assert r.has_key? :status - assert_equal r[:status], 200 - - assert r.has_key? :result - assert_instance_of Hash, r[:result] - end - - def teardown - # Do nothing - end -end diff --git a/test/vultr/vultr_plan_test.rb b/test/vultr/vultr_plan_test.rb deleted file mode 100644 index d05028b..0000000 --- a/test/vultr/vultr_plan_test.rb +++ /dev/null @@ -1,22 +0,0 @@ -require 'test_helper' - -class VultrPlanTest < Minitest::Test - - def setup - Vultr.api_key = ENV['VULTR_API_KEY'] - end - - def test_plan_list_response - r = Vultr::Plans.list - - assert r.has_key? :status - assert_equal r[:status], 200 - - assert r.has_key? :result - assert_instance_of Hash, r[:result] - end - - def teardown - # Do nothing - end -end diff --git a/test/vultr/vultr_region_test.rb b/test/vultr/vultr_region_test.rb deleted file mode 100644 index 15f0b01..0000000 --- a/test/vultr/vultr_region_test.rb +++ /dev/null @@ -1,33 +0,0 @@ -require 'test_helper' - -class VultrRegionTest < Minitest::Test - - def setup - Vultr.api_key = ENV['VULTR_API_KEY'] - end - - def test_region_responses - r = Vultr::Regions.list - - assert r.has_key? :status - assert_equal r[:status], 200 - - assert r.has_key? :result - assert_instance_of Hash, r[:result] - - k, v = r[:result].first - dc_id = k.to_i - - r = Vultr::Regions.availability(DCID: dc_id) - - assert r.has_key? :status - assert_equal r[:status], 200 - - assert r.has_key? :result - assert_instance_of Array, r[:result] - end - - def teardown - # Do nothing - end -end diff --git a/test/vultr/vultr_snapshot_test.rb b/test/vultr/vultr_snapshot_test.rb deleted file mode 100644 index 05af1d5..0000000 --- a/test/vultr/vultr_snapshot_test.rb +++ /dev/null @@ -1,30 +0,0 @@ -require 'test_helper' - -class VultrSnapshotTest < Minitest::Test - - def setup - Vultr.api_key = ENV['VULTR_API_KEY'] - end - - def test_snapshot_list_response - r = Vultr::Snapshot.list - - assert r.has_key? :status - assert_equal r[:status], 200 - - assert r.has_key? :result - assert_instance_of Array, r[:result] - end - - def test_snapshot_create_response - # TODO - end - - def test_snapshot_destroy_response - # TODO - end - - def teardown - # Do nothing - end -end diff --git a/test/vultr/vultr_sshkey_test.rb b/test/vultr/vultr_sshkey_test.rb deleted file mode 100644 index ba0f7eb..0000000 --- a/test/vultr/vultr_sshkey_test.rb +++ /dev/null @@ -1,50 +0,0 @@ -require 'test_helper' - -class VultrSshkeyTest < Minitest::Test - - def setup - Vultr.api_key = ENV['VULTR_API_KEY'] - end - - def test_sshkey_responses - r = Vultr::SSHKey.create(name: 'test_ssh_key', - ssh_key: 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDJMDEZ80TRG4FMi3lz4zonNnUlSUd92UfOEh2OtS6Rtl9X/2vqqnbxh4tbCNUH6JGEHhjXV6JKoj3G1Tui+7keTLov9vGWJsgDCIndU5r/wUp6G99oAMpoldsuUXjh1xKYT6XrF6ud1Rd3WIFuzZAgv/dTBD+tUWr53YngqN1hJ7Ux4pxu4whMpdiaeom8cVp+u8pI9n06ESgMClPe2eloFOd1agsOmhLL4YrTveGf7DHLED39rkcEvRBSalM2q5frY0x5gx2QTDv85cHHM2gsHgJB4CwRajPUYnqjA+YH/QZIq629z29YJP3odcyDVIiFQuqCtxYPoakfTzoSP3zx tolbkni@gmail.com') - - assert r.has_key? :status - assert_equal r[:status], 200 - - assert r.has_key? :result - assert_instance_of Hash, r[:result] - - sshkey_id = r[:result]['SSHKEYID'] - - r = Vultr::SSHKey.update(SSHKEYID: sshkey_id, - name: 'test_ssh_key1', - ssh_key: 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDJMDEZ80TRG4FMi3lz4zonNnUlSUd92UfOEh2OtS6Rtl9X/2vqqnbxh4tbCNUH6JGEHhjXV6JKoj3G1Tui+7keTLov9vGWJsgDCIndU5r/wUp6G99oAMpoldsuUXjh1xKYT6XrF6ud1Rd3WIFuzZAgv/dTBD+tUWr53YngqN1hJ7Ux4pxu4whMpdiaeom8cVp+u8pI9n06ESgMClPe2eloFOd1agsOmhLL4YrTveGf7DHLED39rkcEvRBSalM2q5frY0x5gx2QTDv85cHHM2gsHgJB4CwRajPUYnqjA+YH/QZIq629z29YJP3odcyDVIiFQuqCtxYPoakfTzoSP3zx tolbkni@gmail.com') - - assert r.has_key? :status - assert_equal r[:status], 200 - - assert r.has_key? :result - assert_empty r[:result] - - r = Vultr::SSHKey.list - - assert r.has_key? :status - assert_equal r[:status], 200 - - assert r.has_key? :result - assert_instance_of Hash, r[:result] - - r = Vultr::SSHKey.destroy(SSHKEYID: sshkey_id) - assert r.has_key? :status - assert_equal r[:status], 200 - - assert r.has_key? :result - assert_empty r[:result] - end - - def teardown - # Do nothing - end -end diff --git a/test/vultr/vultr_startupscript_test.rb b/test/vultr/vultr_startupscript_test.rb deleted file mode 100644 index 1ec6d9f..0000000 --- a/test/vultr/vultr_startupscript_test.rb +++ /dev/null @@ -1,53 +0,0 @@ -require 'test_helper' - -class VultrStartupscriptTest < Minitest::Test - - def setup - Vultr.api_key = ENV['VULTR_API_KEY'] - end - - def test_startupscript_responses - r = Vultr::StartupScript.create(name: 'test_startup_script', - script: 'echo \'test\'', - type: 'boot') - - assert r.has_key? :status - assert_equal r[:status], 200 - - assert r.has_key? :result - assert_instance_of Hash, r[:result] - - script_id = r[:result]['SCRIPTID'] - - r = Vultr::StartupScript.update(SCRIPTID: script_id, - name: 'test_startup_script1', - script: 'echo \'test1\'') - - assert r.has_key? :status - assert_equal r[:status], 200 - - assert r.has_key? :result - assert_empty r[:result] - - r = Vultr::StartupScript.list - - assert r.has_key? :status - assert_equal r[:status], 200 - - assert r.has_key? :result - assert_instance_of Hash, r[:result] - - - r = Vultr::StartupScript.destroy(SCRIPTID: script_id) - - assert r.has_key? :status - assert_equal r[:status], 200 - - assert r.has_key? :result - assert_empty r[:result] - end - - def teardown - # Do nothing - end -end diff --git a/vultr.gemspec b/vultr.gemspec index cd8d39d..987dfc5 100644 --- a/vultr.gemspec +++ b/vultr.gemspec @@ -21,6 +21,6 @@ Gem::Specification.new do |s| s.required_ruby_version = '>= 1.9' - s.add_dependency 'faraday', '>= 0.12.0', '< 1.0' - s.add_dependency 'faraday_middleware', '>= 0.12.0', '< 1.0' + s.add_dependency 'faraday', '~> 1.7' + #s.add_dependency 'faraday_middleware', '~> 1.1' end From c745069df3de28cb7394a793742914946b30fc02 Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Sat, 14 Aug 2021 22:51:43 -0500 Subject: [PATCH 02/50] Add more resources --- lib/vultr.rb | 4 ++ lib/vultr/objects/backup.rb | 4 ++ lib/vultr/objects/bare_metal.rb | 4 ++ lib/vultr/objects/block_storage.rb | 4 ++ lib/vultr/objects/domain.rb | 4 ++ lib/vultr/resources/applications.rb | 5 +- lib/vultr/resources/backups.rb | 10 ++-- lib/vultr/resources/bare_metal.rb | 76 +++++++++++++++++++++++++++- lib/vultr/resources/block_storage.rb | 30 +++++++++-- lib/vultr/resources/dns.rb | 54 +++++++++++++++++++- lib/vultr/resources/instances.rb | 38 +++++++------- 11 files changed, 202 insertions(+), 31 deletions(-) create mode 100644 lib/vultr/objects/backup.rb create mode 100644 lib/vultr/objects/bare_metal.rb create mode 100644 lib/vultr/objects/block_storage.rb create mode 100644 lib/vultr/objects/domain.rb diff --git a/lib/vultr.rb b/lib/vultr.rb index 05682cc..53878d1 100644 --- a/lib/vultr.rb +++ b/lib/vultr.rb @@ -33,6 +33,10 @@ module Vultr autoload :Account, "vultr/objects/account" autoload :Application, "vultr/objects/application" + autoload :Backup, "vultr/objects/backup" + autoload :BareMetal, "vultr/objects/bare_metal" + autoload :BlockStorage, "vultr/objects/block_storage" + autoload :Domain, "vultr/objects/domain" autoload :Instance, "vultr/objects/instance" autoload :User, "vultr/objects/user" diff --git a/lib/vultr/objects/backup.rb b/lib/vultr/objects/backup.rb new file mode 100644 index 0000000..c544d32 --- /dev/null +++ b/lib/vultr/objects/backup.rb @@ -0,0 +1,4 @@ +module Vultr + class Backup < Object + end +end diff --git a/lib/vultr/objects/bare_metal.rb b/lib/vultr/objects/bare_metal.rb new file mode 100644 index 0000000..320f870 --- /dev/null +++ b/lib/vultr/objects/bare_metal.rb @@ -0,0 +1,4 @@ +module Vultr + class BareMetal < Object + end +end diff --git a/lib/vultr/objects/block_storage.rb b/lib/vultr/objects/block_storage.rb new file mode 100644 index 0000000..c797879 --- /dev/null +++ b/lib/vultr/objects/block_storage.rb @@ -0,0 +1,4 @@ +module Vultr + class BlockStorage < Object + end +end diff --git a/lib/vultr/objects/domain.rb b/lib/vultr/objects/domain.rb new file mode 100644 index 0000000..df9ab23 --- /dev/null +++ b/lib/vultr/objects/domain.rb @@ -0,0 +1,4 @@ +module Vultr + class Domain < Object + end +end diff --git a/lib/vultr/resources/applications.rb b/lib/vultr/resources/applications.rb index af30ab8..1c2ad1b 100644 --- a/lib/vultr/resources/applications.rb +++ b/lib/vultr/resources/applications.rb @@ -1,7 +1,8 @@ module Vultr class ApplicationsResource < Resource - def list - Application.new get_request("applications").body + def list(**params) + response = get_request("applications", params: params) + Collection.from_response(response, key: "applications", type: Application) end end end diff --git a/lib/vultr/resources/backups.rb b/lib/vultr/resources/backups.rb index 5c4deae..b4c51bc 100644 --- a/lib/vultr/resources/backups.rb +++ b/lib/vultr/resources/backups.rb @@ -1,8 +1,12 @@ module Vultr class BackupsResource < Resource - def list - Backup.new get_request("backups").body + def list(**params + response = get_request("backups", params: params) + Collection.from_response(response, key: "backups", type: Backup) + end + + def retrieve(id) + Backup.new get_request("backups/#{id}").body end end end - diff --git a/lib/vultr/resources/bare_metal.rb b/lib/vultr/resources/bare_metal.rb index dec1016..f7e2cb1 100644 --- a/lib/vultr/resources/bare_metal.rb +++ b/lib/vultr/resources/bare_metal.rb @@ -1,7 +1,79 @@ module Vultr class BareMetalResource < Resource - def list - BareMetal.new get_request("bare-metals").body + def list(**params) + response = get_request("bare-metals", params: params) + Collection.from_response(response, key: "bare_metals", type: BareMetal) + end + + def retrieve(id) + BareMetal.new get_request("bare-metals/#{id}") + end + + def create(**attributes) + BareMetal.new post_request("bare-metals", body: attributes).body.dig("bare_metal") + end + + def update(id, **attributes) + BareMetal.new patch_request("bare-metals/#{id}", body: attributes).body.dig("bare_metal") + end + + def delete(id) + delete_request("bare-metals/#{id}") + end + + def start(id) + post_request("bare-metals/#{id}/start") + end + + def reboot(id) + post_request("bare-metals/#{id}/reboot") + end + + def reinstall(id) + BareMetal.new post_request("bare-metals/#{id}/reinstall").body.dig("bare_metal") + end + + def halt(id) + post_request("bare-metals/#{id}/halt") + end + + def bandwidth(id) + Object.new get_request("bare-metals/#{id}/bandwidth").body.dig("bandwidth") + end + + def user_data(id) + Object.new get_request("bare-metals/#{id}/user-data").body.dig("user_data") + end + + def upgrades(id, **params) + Object.new get_request("bare-metals/#{id}/upgrades", params: params).body.dig("upgrades") + end + + def vnc(id) + Object.new get_request("bare-metals/#{id}/vnc", params: params).body.dig("vnc") + end + + def ipv4(id, **params) + response = get_request("bare-metals/#{id}/ipv4", params: params) + Collection.from_response(response, key: "ipv4s", type: Object) + end + + def ipv6(id, **params) + response = get_request("bare-metals/#{id}/ipv6", params: params) + Collection.from_response(response, key: "ipv6s", type: Object) + end + + # Bulk operations + def halt_instances(ids) + post_request("bare-metals/halt", body: { baremetal_ids: Array(ids) }) + end + + def reboot_instances(ids) + post_request("bare-metals/reboot", body: { baremetal_ids: Array(ids) }) + end + + def start_instances(ids) + post_request("bare-metals/start", body: { baremetal_ids: Array(ids) }) end end end diff --git a/lib/vultr/resources/block_storage.rb b/lib/vultr/resources/block_storage.rb index 9040b79..201e2c5 100644 --- a/lib/vultr/resources/block_storage.rb +++ b/lib/vultr/resources/block_storage.rb @@ -1,8 +1,32 @@ module Vultr class BlockStorageResource < Resource - def list - BlockStorage.new get_request("blocks").body + def list(**params) + response = get_request("blocks", params: params) + Collection.from_response(response, key: "blocks", type: BlockStorage) + end + + def retrieve(id) + BlockStorage.new get_request("blocks/#{id}") + end + + def create(**attributes) + BlockStorage.new post_request("blocks", body: attributes).body.dig("block") + end + + def update(id, **attributes) + patch_request("blocks/#{id}", body: attributes) + end + + def delete(id) + delete_request("blocks/#{id}") + end + + def attach(id, **params) + post_request("blocks/#{id}/attach", body: params) + end + + def detach(id) + post_request("blocks/#{id}/detach", body: params) end end end - diff --git a/lib/vultr/resources/dns.rb b/lib/vultr/resources/dns.rb index 2d658ce..68189af 100644 --- a/lib/vultr/resources/dns.rb +++ b/lib/vultr/resources/dns.rb @@ -1,7 +1,57 @@ module Vultr class DnsResource < Resource - def list - Domain.new get_request("domains").body + def list(**params) + response = get_request("domains", params: params) + Collection.from_response(response, key: "domains", type: Domain) + end + + def retrieve(id) + Domain.new get_request("domains/#{id}") + end + + def create(**attributes) + Domain.new post_request("domains", body: attributes).body.dig("domain") + end + + def update(id, **attributes) + patch_request("domains/#{id}", body: attributes) + end + + def delete(id) + delete_request("domains/#{id}") + end + + def soa(id) + Object.new get_request("domains/#{id}/soa").body.dig("dns_soa") + end + + def update_soa(id, **attributes) + post_request("domains/#{id}/soa", body: attributes) + end + + def dnssec(id) + Object.new get_request("domains/#{id}/dnssec").body.dig("dns_sec") + end + + def records(id, **params) + response = get_request("domains/#{id}/records", params: params) + Collection.from_response(response, key: "records", type: Object) + end + + def retrieve_record(id, record_id:) + Object.new get_request("domains/#{id}/records/#{record_id}").body.dig("record") + end + + def create_record(id, **attributes) + Object.new post_request("domains/#{id}/records", body: attributes).body.dig("record") + end + + def update_record(id, record_id:, **attributes) + patch_request("domains/#{id}/records/#{record_id}", body: attributes) + end + + def delete_record(id, record_id:) + delete_request("domains/#{id}/records/#{record_id}") end end end diff --git a/lib/vultr/resources/instances.rb b/lib/vultr/resources/instances.rb index 8ffaa59..cac2560 100644 --- a/lib/vultr/resources/instances.rb +++ b/lib/vultr/resources/instances.rb @@ -1,7 +1,7 @@ module Vultr class InstancesResource < Resource - def list - response = get_request("instances") + def list(**params) + response = get_request("instances", params: params) Collection.from_response(response, key: "instances", type: Instance) end @@ -54,16 +54,16 @@ def user_data(id) end def upgrades(id, **params) - Object.new get_request("instances/#{id}/upgrades", params).body.dig("upgrades") + Object.new get_request("instances/#{id}/upgrades", params: params).body.dig("upgrades") end def ipv4(id, **params) - response = get_request("instances/#{id}/ipv4", params) + response = get_request("instances/#{id}/ipv4", params: params) Collection.from_response(response, key: "ipv4s", type: Object) end def create_ipv4(id, **params) - Object.new post_request("instances/#{id}/ipv4", params).body.dig("ipv4") + Object.new post_request("instances/#{id}/ipv4", body: params).body.dig("ipv4") end def delete_ipv4(id, ipv4:) @@ -71,15 +71,15 @@ def delete_ipv4(id, ipv4:) end def create_ipv4_reverse(id, **params) - post_request("instances/#{id}/ipv4/reverse", params) + post_request("instances/#{id}/ipv4/reverse", body: params) end def set_default_reverse_dns_entry(id, **params) - post_request("instances/#{id}/ipv4/reverse/default", params) + post_request("instances/#{id}/ipv4/reverse/default", body: params) end def ipv6(id, **params) - response = get_request("instances/#{id}/ipv6", params) + response = get_request("instances/#{id}/ipv6", params: params) Collection.from_response(response, key: "ipv6s", type: Object) end @@ -89,24 +89,24 @@ def ipv6_reverse(id) end def create_ipv6_reverse(id, **params) - post_request("instances/#{id}/ipv6/reverse", params) + post_request("instances/#{id}/ipv6/reverse", body: params) end def delete_ipv6_reverse(id, ipv6:) delete_request("instances/#{id}/ipv6/reverse/#{ipv6}") end - def private_networks(id) - response = get_request("instances/#{id}/private-networks") + def private_networks(id, **params) + response = get_request("instances/#{id}/private-networks", params: params) Collection.from_response(response, key: "private_networks", type: PrivateNetwork) end def attach_private_network(id, network_id:) - post_request("instances/#{id}/private-networks/attach", { network_id: network_id }) + post_request("instances/#{id}/private-networks/attach", body: { network_id: network_id }) end def detach_private_network(id, network_id:) - post_request("instances/#{id}/private-networks/detach", { network_id: network_id }) + post_request("instances/#{id}/private-networks/detach", body: { network_id: network_id }) end def iso(id) @@ -114,11 +114,11 @@ def iso(id) end def attach_iso(id, iso_id:) - Object.new post_request("instances/#{id}/iso/attach", { iso_id: iso_id }).body.dig("iso_status") + Object.new post_request("instances/#{id}/iso/attach", body: { iso_id: iso_id }).body.dig("iso_status") end def detach_iso(id, iso_id:) - Object.new post_request("instances/#{id}/iso/detach", { iso_id: iso_id }).body.dig("iso_status") + Object.new post_request("instances/#{id}/iso/detach", body: { iso_id: iso_id }).body.dig("iso_status") end def get_backup_schedule(id) @@ -126,20 +126,20 @@ def get_backup_schedule(id) end def set_backup_schedule(id, **attributes) - post_request("instances/#{id}/backup-schedule", attributes) + post_request("instances/#{id}/backup-schedule", body: attributes) end # Bulk operations def halt_instances(ids) - post_request("instances/halt", { instance_ids: Array(ids) }) + post_request("instances/halt", body: { instance_ids: Array(ids) }) end def reboot_instances(ids) - post_request("instances/reboot", { instance_ids: Array(ids) }) + post_request("instances/reboot", body: { instance_ids: Array(ids) }) end def start_instances(ids) - post_request("instances/start", { instance_ids: Array(ids) }) + post_request("instances/start", body: { instance_ids: Array(ids) }) end end end From 995a8e8878432e9f1849afacb268a3180baafe0f Mon Sep 17 00:00:00 2001 From: Andrea Fomera Date: Sat, 14 Aug 2021 23:46:05 -0600 Subject: [PATCH 03/50] Update more of the API endpoints and that should bring things to be mostly functional Co-authored-by: Chris Oliver --- lib/vultr.rb | 15 ++++++ lib/vultr/client.rb | 66 +++++++++++++++++++++++- lib/vultr/objects/firewall_group.rb | 4 ++ lib/vultr/objects/iso.rb | 4 ++ lib/vultr/objects/kubernetes_cluster.rb | 4 ++ lib/vultr/objects/load_balancer.rb | 4 ++ lib/vultr/objects/object_storage.rb | 4 ++ lib/vultr/objects/operating_system.rb | 4 ++ lib/vultr/objects/plan.rb | 4 ++ lib/vultr/objects/private_network.rb | 4 ++ lib/vultr/objects/region.rb | 4 ++ lib/vultr/objects/reserved_ip.rb | 4 ++ lib/vultr/objects/snapshot.rb | 4 ++ lib/vultr/objects/ssh_key.rb | 4 ++ lib/vultr/objects/startup_script.rb | 4 ++ lib/vultr/resources/account.rb | 2 +- lib/vultr/resources/applications.rb | 1 - lib/vultr/resources/backups.rb | 4 +- lib/vultr/resources/bare_metal.rb | 7 ++- lib/vultr/resources/block_storage.rb | 2 +- lib/vultr/resources/dns.rb | 5 +- lib/vultr/resources/firewall.rb | 40 ++++++++++++-- lib/vultr/resources/instances.rb | 6 +-- lib/vultr/resources/iso.rb | 23 +++++++-- lib/vultr/resources/kubernetes.rb | 60 +++++++++++++++++++-- lib/vultr/resources/load_balancers.rb | 48 +++++++++++++++-- lib/vultr/resources/object_storage.rb | 31 +++++++++-- lib/vultr/resources/operating_systems.rb | 6 +-- lib/vultr/resources/plans.rb | 11 ++-- lib/vultr/resources/private_networks.rb | 22 ++++++-- lib/vultr/resources/regions.rb | 8 +-- lib/vultr/resources/reserved_ips.rb | 30 +++++++++-- lib/vultr/resources/snapshots.rb | 26 ++++++++-- lib/vultr/resources/ssh_keys.rb | 22 ++++++-- lib/vultr/resources/startup_scripts.rb | 22 ++++++-- lib/vultr/resources/users.rb | 4 +- 36 files changed, 453 insertions(+), 60 deletions(-) create mode 100644 lib/vultr/objects/firewall_group.rb create mode 100644 lib/vultr/objects/iso.rb create mode 100644 lib/vultr/objects/kubernetes_cluster.rb create mode 100644 lib/vultr/objects/load_balancer.rb create mode 100644 lib/vultr/objects/object_storage.rb create mode 100644 lib/vultr/objects/operating_system.rb create mode 100644 lib/vultr/objects/plan.rb create mode 100644 lib/vultr/objects/private_network.rb create mode 100644 lib/vultr/objects/region.rb create mode 100644 lib/vultr/objects/reserved_ip.rb create mode 100644 lib/vultr/objects/snapshot.rb create mode 100644 lib/vultr/objects/ssh_key.rb create mode 100644 lib/vultr/objects/startup_script.rb diff --git a/lib/vultr.rb b/lib/vultr.rb index 53878d1..170dd12 100644 --- a/lib/vultr.rb +++ b/lib/vultr.rb @@ -9,6 +9,7 @@ module Vultr autoload :Resource, "vultr/resource" autoload :Object, "vultr/object" + # High-level categories of Vultr API calls autoload :AccountResource, "vultr/resources/account" autoload :ApplicationsResource, "vultr/resources/applications" autoload :InstancesResource, "vultr/resources/instances" @@ -31,13 +32,27 @@ module Vultr autoload :StartupScriptsResource, "vultr/resources/startup_scripts" autoload :UserResource, "vultr/resources/users" + # Classes used to return a nicer object wrapping the response data autoload :Account, "vultr/objects/account" autoload :Application, "vultr/objects/application" autoload :Backup, "vultr/objects/backup" autoload :BareMetal, "vultr/objects/bare_metal" autoload :BlockStorage, "vultr/objects/block_storage" autoload :Domain, "vultr/objects/domain" + autoload :FirewallGroup, "vultr/objects/firewall_group" autoload :Instance, "vultr/objects/instance" + autoload :Iso, "vultr/objects/iso" + autoload :KubernetesCluster, "vultr/objects/kubernetes_cluster" + autoload :LoadBalancer, "vultr/objects/load_balancer" + autoload :ObjectStorage, "vultr/objects/object_storage" + autoload :OperatingSystem, "vultr/objects/operating_system" + autoload :Plan, "vultr/objects/plan" + autoload :PrivateNetwork, "vultr/objects/private_network" + autoload :Region, "vultr/objects/region" + autoload :ReservedIp, "vultr/objects/reserved_ip" + autoload :Snapshot, "vultr/objects/snapshot" + autoload :SshKey, "vultr/objects/ssh_key" + autoload :StartupScript, "vultr/objects/startup_script" autoload :User, "vultr/objects/user" def self.api_key=(key) diff --git a/lib/vultr/client.rb b/lib/vultr/client.rb index e480952..caddccc 100644 --- a/lib/vultr/client.rb +++ b/lib/vultr/client.rb @@ -19,13 +19,77 @@ def applications end def backups - BackupResource.new(self) + BackupsResource.new(self) + end + + def bare_metal + BareMetalResource.new(self) + end + + def block_storage + BlockStorageResource.new(self) + end + + def dns + DnsResource.new(self) + end + + def firewall + FirewallResource.new(self) end def instances InstancesResource.new(self) end + def iso + IsoResource.new(self) + end + + def kubernetes + KubernetesResource.new(self) + end + + def load_balancers + LoadBalancersResource.new(self) + end + + def object_storage + ObjectStorageResource.new(self) + end + + def operating_systems + OperatingSystemsResource.new(self) + end + + def plans + PlansResource.new(self) + end + + def private_networks + PrivateNetworksResource.new(self) + end + + def reserved_ips + ReservedIpsResource.new(self) + end + + def regions + RegionsResource.new(self) + end + + def snapshots + SnapshotsResource.new(self) + end + + def ssh_keys + SshKeysResource.new(self) + end + + def startup_scripts + StartupScriptsResource.new(self) + end + def users UserResource.new(self) end diff --git a/lib/vultr/objects/firewall_group.rb b/lib/vultr/objects/firewall_group.rb new file mode 100644 index 0000000..ac0b285 --- /dev/null +++ b/lib/vultr/objects/firewall_group.rb @@ -0,0 +1,4 @@ +module Vultr + class FirewallGroup < Object + end +end diff --git a/lib/vultr/objects/iso.rb b/lib/vultr/objects/iso.rb new file mode 100644 index 0000000..67fdeb4 --- /dev/null +++ b/lib/vultr/objects/iso.rb @@ -0,0 +1,4 @@ +module Vultr + class Iso < Object + end +end diff --git a/lib/vultr/objects/kubernetes_cluster.rb b/lib/vultr/objects/kubernetes_cluster.rb new file mode 100644 index 0000000..4426085 --- /dev/null +++ b/lib/vultr/objects/kubernetes_cluster.rb @@ -0,0 +1,4 @@ +module Vultr + class KubernetesCluster < Object + end +end diff --git a/lib/vultr/objects/load_balancer.rb b/lib/vultr/objects/load_balancer.rb new file mode 100644 index 0000000..f596449 --- /dev/null +++ b/lib/vultr/objects/load_balancer.rb @@ -0,0 +1,4 @@ +module Vultr + class LoadBalancer < Object + end +end diff --git a/lib/vultr/objects/object_storage.rb b/lib/vultr/objects/object_storage.rb new file mode 100644 index 0000000..dde95ea --- /dev/null +++ b/lib/vultr/objects/object_storage.rb @@ -0,0 +1,4 @@ +module Vultr + class ObjectStorage < Object + end +end diff --git a/lib/vultr/objects/operating_system.rb b/lib/vultr/objects/operating_system.rb new file mode 100644 index 0000000..4bbcf27 --- /dev/null +++ b/lib/vultr/objects/operating_system.rb @@ -0,0 +1,4 @@ +module Vultr + class OperatingSystem < Object + end +end diff --git a/lib/vultr/objects/plan.rb b/lib/vultr/objects/plan.rb new file mode 100644 index 0000000..b4ca652 --- /dev/null +++ b/lib/vultr/objects/plan.rb @@ -0,0 +1,4 @@ +module Vultr + class Plan < Object + end +end diff --git a/lib/vultr/objects/private_network.rb b/lib/vultr/objects/private_network.rb new file mode 100644 index 0000000..ec57c7a --- /dev/null +++ b/lib/vultr/objects/private_network.rb @@ -0,0 +1,4 @@ +module Vultr + class PrivateNetwork < Object + end +end diff --git a/lib/vultr/objects/region.rb b/lib/vultr/objects/region.rb new file mode 100644 index 0000000..28f8527 --- /dev/null +++ b/lib/vultr/objects/region.rb @@ -0,0 +1,4 @@ +module Vultr + class Region < Object + end +end diff --git a/lib/vultr/objects/reserved_ip.rb b/lib/vultr/objects/reserved_ip.rb new file mode 100644 index 0000000..9f1c1dc --- /dev/null +++ b/lib/vultr/objects/reserved_ip.rb @@ -0,0 +1,4 @@ +module Vultr + class ReservedIp < Object + end +end diff --git a/lib/vultr/objects/snapshot.rb b/lib/vultr/objects/snapshot.rb new file mode 100644 index 0000000..ca5c783 --- /dev/null +++ b/lib/vultr/objects/snapshot.rb @@ -0,0 +1,4 @@ +module Vultr + class Snapshot < Object + end +end diff --git a/lib/vultr/objects/ssh_key.rb b/lib/vultr/objects/ssh_key.rb new file mode 100644 index 0000000..a47970e --- /dev/null +++ b/lib/vultr/objects/ssh_key.rb @@ -0,0 +1,4 @@ +module Vultr + class SshKey < Object + end +end diff --git a/lib/vultr/objects/startup_script.rb b/lib/vultr/objects/startup_script.rb new file mode 100644 index 0000000..85899b9 --- /dev/null +++ b/lib/vultr/objects/startup_script.rb @@ -0,0 +1,4 @@ +module Vultr + class StartupScript < Object + end +end diff --git a/lib/vultr/resources/account.rb b/lib/vultr/resources/account.rb index 1479b86..50d9cab 100644 --- a/lib/vultr/resources/account.rb +++ b/lib/vultr/resources/account.rb @@ -1,7 +1,7 @@ module Vultr class AccountResource < Resource def info - Account.new get("account").body + Account.new get_request("account").body.dig("account") end end end diff --git a/lib/vultr/resources/applications.rb b/lib/vultr/resources/applications.rb index 1c2ad1b..cfac9b5 100644 --- a/lib/vultr/resources/applications.rb +++ b/lib/vultr/resources/applications.rb @@ -6,4 +6,3 @@ def list(**params) end end end - diff --git a/lib/vultr/resources/backups.rb b/lib/vultr/resources/backups.rb index b4c51bc..b40afea 100644 --- a/lib/vultr/resources/backups.rb +++ b/lib/vultr/resources/backups.rb @@ -1,12 +1,12 @@ module Vultr class BackupsResource < Resource - def list(**params + def list(**params) response = get_request("backups", params: params) Collection.from_response(response, key: "backups", type: Backup) end def retrieve(id) - Backup.new get_request("backups/#{id}").body + Backup.new get_request("backups/#{id}").body.dig("backup") end end end diff --git a/lib/vultr/resources/bare_metal.rb b/lib/vultr/resources/bare_metal.rb index f7e2cb1..c1c0c0b 100644 --- a/lib/vultr/resources/bare_metal.rb +++ b/lib/vultr/resources/bare_metal.rb @@ -6,7 +6,7 @@ def list(**params) end def retrieve(id) - BareMetal.new get_request("bare-metals/#{id}") + BareMetal.new get_request("bare-metals/#{id}").body.dig("bare_metal") end def create(**attributes) @@ -53,12 +53,12 @@ def vnc(id) Object.new get_request("bare-metals/#{id}/vnc", params: params).body.dig("vnc") end - def ipv4(id, **params) + def list_ipv4(id, **params) response = get_request("bare-metals/#{id}/ipv4", params: params) Collection.from_response(response, key: "ipv4s", type: Object) end - def ipv6(id, **params) + def list_ipv6(id, **params) response = get_request("bare-metals/#{id}/ipv6", params: params) Collection.from_response(response, key: "ipv6s", type: Object) end @@ -77,4 +77,3 @@ def start_instances(ids) end end end - diff --git a/lib/vultr/resources/block_storage.rb b/lib/vultr/resources/block_storage.rb index 201e2c5..6eb7718 100644 --- a/lib/vultr/resources/block_storage.rb +++ b/lib/vultr/resources/block_storage.rb @@ -6,7 +6,7 @@ def list(**params) end def retrieve(id) - BlockStorage.new get_request("blocks/#{id}") + BlockStorage.new get_request("blocks/#{id}").body.dig("block") end def create(**attributes) diff --git a/lib/vultr/resources/dns.rb b/lib/vultr/resources/dns.rb index 68189af..dc7a149 100644 --- a/lib/vultr/resources/dns.rb +++ b/lib/vultr/resources/dns.rb @@ -6,7 +6,7 @@ def list(**params) end def retrieve(id) - Domain.new get_request("domains/#{id}") + Domain.new get_request("domains/#{id}").body.dig("domain") end def create(**attributes) @@ -33,7 +33,7 @@ def dnssec(id) Object.new get_request("domains/#{id}/dnssec").body.dig("dns_sec") end - def records(id, **params) + def list_records(id, **params) response = get_request("domains/#{id}/records", params: params) Collection.from_response(response, key: "records", type: Object) end @@ -55,4 +55,3 @@ def delete_record(id, record_id:) end end end - diff --git a/lib/vultr/resources/firewall.rb b/lib/vultr/resources/firewall.rb index c23d0f1..47e2518 100644 --- a/lib/vultr/resources/firewall.rb +++ b/lib/vultr/resources/firewall.rb @@ -1,8 +1,42 @@ module Vultr class FirewallResource < Resource - def list - FirewallGroup.new get_request("firewalls").body + def list(**params) + response = get_request("firewalls", params: params) + Collection.from_response(response: response, key: "firewall_groups", type: FirewallGroup) + end + + def retrieve(id) + FirewallGroup.new get_request("firewalls/#{id}").body.dig("firewall_group") + end + + def create(**attributes) + FirewallGroup.new post_request("firewalls", body: attributes).body.dig("firewall_group") + end + + def update(id, **attributes) + put_request("firewalls/#{id}", body: attributes) + end + + def delete(id) + delete_request("firewalls/#{id}") + end + + # Firewall Rules + def list_rules(id, **params) + response = get_request("firewalls/#{id}/rules", params: params) + Collection.from_response(response: response, key: "firewall_rules", type: Object) + end + + def retrieve_rule(id, rule_id:) + Object.new get_request("firewalls/#{id}/rules/#{rule_id}").body.dig("firewall_rule") + end + + def create_rule(id, **attributes) + Object.new post_request("firewalls/#{id}/rules", body: attributes).body.dig("firewall_rule") + end + + def delete_rule(id, rule_id:) + delete_request("firewalls/#{id}/rules/#{rule_id}") end end end - diff --git a/lib/vultr/resources/instances.rb b/lib/vultr/resources/instances.rb index cac2560..89a7b61 100644 --- a/lib/vultr/resources/instances.rb +++ b/lib/vultr/resources/instances.rb @@ -57,7 +57,7 @@ def upgrades(id, **params) Object.new get_request("instances/#{id}/upgrades", params: params).body.dig("upgrades") end - def ipv4(id, **params) + def list_ipv4(id, **params) response = get_request("instances/#{id}/ipv4", params: params) Collection.from_response(response, key: "ipv4s", type: Object) end @@ -78,7 +78,7 @@ def set_default_reverse_dns_entry(id, **params) post_request("instances/#{id}/ipv4/reverse/default", body: params) end - def ipv6(id, **params) + def list_ipv6(id, **params) response = get_request("instances/#{id}/ipv6", params: params) Collection.from_response(response, key: "ipv6s", type: Object) end @@ -96,7 +96,7 @@ def delete_ipv6_reverse(id, ipv6:) delete_request("instances/#{id}/ipv6/reverse/#{ipv6}") end - def private_networks(id, **params) + def list_private_networks(id, **params) response = get_request("instances/#{id}/private-networks", params: params) Collection.from_response(response, key: "private_networks", type: PrivateNetwork) end diff --git a/lib/vultr/resources/iso.rb b/lib/vultr/resources/iso.rb index 3bc6cf0..7ca448e 100644 --- a/lib/vultr/resources/iso.rb +++ b/lib/vultr/resources/iso.rb @@ -1,8 +1,25 @@ module Vultr class IsoResource < Resource - def - Iso.new get_request("iso").body + def list(**params) + response = get_request("iso", params: params) + Collection.from_response(response, key: "isos", type: Iso) + end + + def retrieve(id) + Iso.new get_request("isos/#{id}").body.dig("iso") + end + + def create(**attributes) + Iso.new post_request("isos", body: attributes).body.dig("iso") + end + + def delete(id) + delete_request("isos/#{id}") + end + + def list_public(**params) + response = get_request("iso-public", params: params) + Collection.from_response(response, key: "public_isos", type: Iso) end end end - diff --git a/lib/vultr/resources/kubernetes.rb b/lib/vultr/resources/kubernetes.rb index 1ad9b7d..43b1bee 100644 --- a/lib/vultr/resources/kubernetes.rb +++ b/lib/vultr/resources/kubernetes.rb @@ -1,8 +1,62 @@ module Vultr class KubernetesResource < Resource - def - Kubernetes.new get_request("kubernetes/clusters").body + def list(**params) + response = get_request("kubernetes/clusters", params: params) + Collection.from_response(response, key: "vke_clusters", type: KubernetesCluster) + end + + def retrieve(id) + KubernetesCluster.new get_request("kubernetes/clusters/#{id}").body.dig("vke_cluster") + end + + def create(**attributes) + KubernetesCluster.new post_request("kubernetes/clusters", body: attributes).body.dig("vke_cluster") + end + + def update(id, **attributes) + put_request("kubernetes/clusters/#{id}", body: attributes) + end + + def delete(id) + delete_request("kubernetes/clusters/#{id}") + end + + def config(id) + Object.new get_request("kubernetes/clusters/#{id}/config").body + end + + def list_resources(id, **params) + response = get_request("kubernetes/clusters/#{id}/resources", params: params) + Collection.from_response(response, key: "resources", type: Object) + end + + def list_node_pools(id, **params) + response = get_request("kubernetes/clusters/#{id}/node-pools", params: params) + Collection.from_response(response, key: "node_pools", type: Object) + end + + def retrieve_node_pool(id, nodepool_id:) + Object.new get_request("kubernetes/clusters/#{id}/node-pools/#{nodepool_id}").body.dig("node_pool") + end + + def create_node_pool(id, **attributes) + Object.new post_request("kubernetes/clusters/#{id}/node-pools", body: attributes).body.dig("node_pool") + end + + def update_node_pool(id, nodepool_id:, **attributes) + Object.new patch_request("kubernetes/clusters/#{id}/node-pools/#{nodepool_id}", body: attributes).body.dig("node_pool") + end + + def delete_node_pool(id, nodepool_id:) + delete_request("kubernetes/clusters/#{id}/node-pools/#{nodepool_id}") + end + + def delete_node_pool_instance(id, nodepool_id:, node_id:) + delete_request("kubernetes/clusters/#{id}/node-pools/#{nodepool_id}/nodes/#{node_id}") + end + + def recycle_node_pool_instance(id, nodepool_id:, node_id:) + post_request("kubernetes/clusters/#{id}/node-pools/#{nodepool_id}/nodes/#{node_id}/recycle") end end end - diff --git a/lib/vultr/resources/load_balancers.rb b/lib/vultr/resources/load_balancers.rb index 4691ff3..56c6596 100644 --- a/lib/vultr/resources/load_balancers.rb +++ b/lib/vultr/resources/load_balancers.rb @@ -1,8 +1,50 @@ module Vultr class LoadBalancersResource < Resource - def - LoadBalancer.new get_request("load-balancers").body + def list(**params) + response = get_request("load-balancers", params: params) + Collection.from_response(response: response, key: "load_balancers", type: LoadBalancer) + end + + def retrieve(id) + LoadBalancer.new get_request("load-balancers/#{id}").body.dig("load_balancer") + end + + def create(**attributes) + LoadBalancer.new post_request("load-balancers", body: attributes).body.dig("load_balancer") + end + + def update(id, **attributes) + LoadBalancer.new patch_request("load-balancers/#{id}", body: attributes).body.dig("load_balancer") + end + + def delete(id) + delete_request("load-balancers/#{id}") + end + + def list_forwarding_rules(id, **params) + response = get_request("load-balancers/#{id}/forwarding-rules", params: params) + Collection.from_response(response: response, key: "forwarding_rules", type: Object) + end + + def retrieve_forwarding_rule(id, rule_id:) + Object.new get_request("load-balancers/#{id}/forwarding-rules/#{rule_id}").body.dig("forwarding_rule") + end + + def create_forwarding_rule(id, **attributes) + Object.new post_request("load-balancers/#{id}/forwarding-rules", body: attributes).body.dig("forwarding_rule") + end + + def delete_forwarding_rule(id, rule_id:) + delete_request("load-balancers/#{id}/forwarding-rules/#{rule_id}") + end + + def list_firewall_rules(id, **params) + response = get_request("load-balancers/#{id}/firewall-rules", params: params) + Collection.from_response(response: response, key: "firewall_rules", type: Object) + end + + def retrieve_firewall_rule(id, rule_id:) + Object.new get_request("load-balancers/#{id}/firewall-rules/#{rule_id}").body.dig("firewall_rule") end end end - diff --git a/lib/vultr/resources/object_storage.rb b/lib/vultr/resources/object_storage.rb index 8720928..75b4e80 100644 --- a/lib/vultr/resources/object_storage.rb +++ b/lib/vultr/resources/object_storage.rb @@ -1,8 +1,33 @@ module Vultr class ObjectStorageResource < Resource - def - ObjectStorage.new get_request("object-storage").body + def list(**params) + response = get_request("object-storage", params: params) + Collection.from_response(response: response, key: "object_storages", type: ObjectStorage) + end + + def retrieve(id) + ObjectStorage.new get_request("object-storage/#{id}").body.dig("object_storage") + end + + def create(**attributes) + ObjectStorage.new post_request("object-storage", body: attributes).body.dig("object_storage") + end + + def update(id, **attributes) + put_request("object-storage/#{id}", body: attributes) + end + + def delete(id) + delete_request("object-storage/#{id}") + end + + def regenerate_keys(id) + Object.new post_request("object-storage/#{id}/regenerate-keys").body + end + + def list_clusters(**params) + response = get_request("object-storage/clusters", params: params) + Collection.from_response(response: response, key: "clusters", type: Object) end end end - diff --git a/lib/vultr/resources/operating_systems.rb b/lib/vultr/resources/operating_systems.rb index 53e83f4..b33effb 100644 --- a/lib/vultr/resources/operating_systems.rb +++ b/lib/vultr/resources/operating_systems.rb @@ -1,8 +1,8 @@ module Vultr class OperatingSystemsResource < Resource - def - OperatingSystem.new get_request("os").body + def list(**params) + response = get_request("os", params: params) + Collection.from_response(response, key: "os", type: OperatingSystem) end end end - diff --git a/lib/vultr/resources/plans.rb b/lib/vultr/resources/plans.rb index fc391ee..1f518a8 100644 --- a/lib/vultr/resources/plans.rb +++ b/lib/vultr/resources/plans.rb @@ -1,12 +1,13 @@ module Vultr class PlansResource < Resource - def list - Plan.new get_request("plans").body + def list(**params) + response = get_request("plans", params: params) + Collection.from_response(response, key: "plans", type: Plan) end - def list_metal - Plan.new get_request("plans-metal").body + def list_metal(**params) + response = get_request("plans-metal", params: params) + Collection.from_response(response, key: "plans_metal", type: Plan) end end end - diff --git a/lib/vultr/resources/private_networks.rb b/lib/vultr/resources/private_networks.rb index 2226a29..ac3ca7d 100644 --- a/lib/vultr/resources/private_networks.rb +++ b/lib/vultr/resources/private_networks.rb @@ -1,8 +1,24 @@ module Vultr class PrivateNetworksResource < Resource - def - PrivateNetwork.new get_request("private-networks").body + def list(**params) + response = get_request("private-networks", params: params) + Collection.from_response(response, key: "networks", type: PrivateNetwork) + end + + def retrieve(id) + PrivateNetwork.new get_request("private-networks/#{id}").body.dig("network") + end + + def create(**attributes) + PrivateNetwork.new post_request("private-networks", body: attributes).body.dig("network") + end + + def update(id, **attributes) + put_request("private-networks/#{id}", body: attributes) + end + + def delete(id) + delete_request("private-networks/#{id}") end end end - diff --git a/lib/vultr/resources/regions.rb b/lib/vultr/resources/regions.rb index 1b3ca36..776fa83 100644 --- a/lib/vultr/resources/regions.rb +++ b/lib/vultr/resources/regions.rb @@ -1,12 +1,12 @@ module Vultr class RegionsResource < Resource - def list - Region.new get_request("regions").body + def list(**params) + response = get_request("regions", params: params) + Collection.from_response(response, key: "regions", type: Region) end def list_availability(id) - Region.new get_request("regions/#{id}/availability") + Object.new get_request("regions/#{id}/availability").body end end end - diff --git a/lib/vultr/resources/reserved_ips.rb b/lib/vultr/resources/reserved_ips.rb index d4c47da..b522e8c 100644 --- a/lib/vultr/resources/reserved_ips.rb +++ b/lib/vultr/resources/reserved_ips.rb @@ -1,8 +1,32 @@ module Vultr class ReservedIpsResource < Resource - def list - ReservedIps.new get_request("reserved-ips").body + def list(**params) + response = get_request("reserved-ips", params: params) + Collection.from_response(response, key: "reserved_ips", type: ReservedIp) + end + + def retrieve(id) + ReservedIp.new get_request("reserved-ips/#{id}").body.dig("reserved_ip") + end + + def create(**attributes) + ReservedIp.new post_request("reserved-ips", body: attributes).body.dig("reserved_ip") + end + + def delete(id) + delete_request("reserved-ips/#{id}") + end + + def attach(id, reserved_ip:) + post_request("reserved-ips/#{id}/attach", body: { reserved_ip: reserved_ip }) + end + + def detach(id) + post_request("reserved-ips/#{id}/detach") + end + + def convert(**attributes) + ReservedIp.new post_request("reserved-ips/convert", body: attributes).body.dig("reserved_ip") end end end - diff --git a/lib/vultr/resources/snapshots.rb b/lib/vultr/resources/snapshots.rb index 563cf0d..996a8be 100644 --- a/lib/vultr/resources/snapshots.rb +++ b/lib/vultr/resources/snapshots.rb @@ -1,8 +1,28 @@ module Vultr class SnapshotsResource < Resource - def list - Snapshot.new get_request("snapshots").body + def list(**params) + response = get_request("snapshots", params: params) + Collection.from_response(response, key: "snapshots", type: Snapshot) + end + + def retrieve(id) + Snapshot.new get_request("snapshots/#{id}").body.dig("snapshot") + end + + def create(**attributes) + Snapshot.new post_request("snapshots", body: attributes).body.dig("snapshot") + end + + def create_from_url(url:) + Snapshot.new post_request("snapshots/create-from-url", body: { url: url }).body.dig("snapshot") + end + + def update(id, **attributes) + put_request("snapshots/#{id}", body: attributes) + end + + def delete(id) + delete_request("snapshots/#{id}") end end end - diff --git a/lib/vultr/resources/ssh_keys.rb b/lib/vultr/resources/ssh_keys.rb index 575cebf..6bb1062 100644 --- a/lib/vultr/resources/ssh_keys.rb +++ b/lib/vultr/resources/ssh_keys.rb @@ -1,8 +1,24 @@ module Vultr class SshKeysResource < Resource - def list - SshKey.new get_request("startup-scripts").body + def list(**params) + response = get_request("ssh-keys", params: params) + Collection.from_response(response: response, key: "ssh_keys", type: SshKey) + end + + def retrieve(id) + SshKey.new get_request("ssh-keys/#{id}").body.dig("ssh_key") + end + + def create(**attributes) + SshKey.new post_request("ssh-keys", body: attributes).body.dig("ssh_key") + end + + def update(id, **attributes) + patch_request("ssh-keys/#{id}", body: attributes) + end + + def delete(id) + delete_request("ssh-keys/#{id}") end end end - diff --git a/lib/vultr/resources/startup_scripts.rb b/lib/vultr/resources/startup_scripts.rb index eaa8ac8..6021701 100644 --- a/lib/vultr/resources/startup_scripts.rb +++ b/lib/vultr/resources/startup_scripts.rb @@ -1,8 +1,24 @@ module Vultr class StartupScriptsResource < Resource - def - StartupScript.new get("").body + def list(**params) + response = get_request("startup-scripts", params: params) + Collection.from_response(response: response, key: "startup_scripts", type: StartupScript) + end + + def retrieve(id) + StartupScript.new get_request("startup-scripts/#{id}").body.dig("startup_script") + end + + def create(**attributes) + StartupScript.new post_request("startup-scripts", body: attributes).body.dig("startup_script") + end + + def update(id, **attributes) + patch_request("startup-scripts/#{id}", body: attributes) + end + + def delete(id) + delete_request("startup-scripts/#{id}") end end end - diff --git a/lib/vultr/resources/users.rb b/lib/vultr/resources/users.rb index 1e37097..10ef939 100644 --- a/lib/vultr/resources/users.rb +++ b/lib/vultr/resources/users.rb @@ -1,12 +1,12 @@ module Vultr class UserResource < Resource - def list(params: {}) + def list(**params) response = get_request("users", params: params) Collection.from_response(response, key: "users", type: User) end def retrieve(id) - User.new get_request("users/#{id}").body + User.new get_request("users/#{id}").body.dig("user") end def create(**attributes) From 66926a9ef7d85b28d17f557a88068f57cccf1570 Mon Sep 17 00:00:00 2001 From: Andrea Fomera Date: Sat, 14 Aug 2021 23:49:22 -0600 Subject: [PATCH 04/50] Install and run Standardrb Co-authored-by: Chris Oliver --- Gemfile | 7 +++--- Rakefile | 12 +++++----- lib/vultr.rb | 6 ++--- lib/vultr/client.rb | 4 ++-- lib/vultr/collection.rb | 2 +- lib/vultr/resources/bare_metal.rb | 6 ++--- lib/vultr/resources/instances.rb | 14 ++++++------ lib/vultr/resources/reserved_ips.rb | 2 +- lib/vultr/resources/snapshots.rb | 2 +- lib/vultr/version.rb | 6 ++--- test/test_helper.rb | 6 ++--- test/vultr/object_test.rb | 2 +- test/vultr/resources/account_test.rb | 1 - vultr.gemspec | 34 ++++++++++++++-------------- 14 files changed, 52 insertions(+), 52 deletions(-) diff --git a/Gemfile b/Gemfile index baa72e9..76d84c8 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,7 @@ -source 'https://rubygems.org' +source "https://rubygems.org" gemspec -gem 'rake' -gem 'minitest', '>= 5.0.5' +gem "rake" +gem "minitest", ">= 5.0.5" +gem "standard" diff --git a/Rakefile b/Rakefile index 83d2add..c30a350 100644 --- a/Rakefile +++ b/Rakefile @@ -1,14 +1,14 @@ -require 'bundler/gem_tasks' -require 'rake/testtask' +require "bundler/gem_tasks" +require "rake/testtask" Rake::TestTask.new(:test) do |t| - t.libs << 'test' - t.libs << 'lib' - t.test_files = FileList['test/**/*_test.rb'] + t.libs << "test" + t.libs << "lib" + t.test_files = FileList["test/**/*_test.rb"] end task default: :test task :console do - exec 'irb -I lib -r vultr.rb' + exec "irb -I lib -r vultr.rb" end diff --git a/lib/vultr.rb b/lib/vultr.rb index 170dd12..2e41421 100644 --- a/lib/vultr.rb +++ b/lib/vultr.rb @@ -1,6 +1,6 @@ -require 'faraday' -require 'faraday_middleware' -require 'vultr/version' +require "faraday" +require "faraday_middleware" +require "vultr/version" module Vultr autoload :Client, "vultr/client" diff --git a/lib/vultr/client.rb b/lib/vultr/client.rb index caddccc..1eb8c2f 100644 --- a/lib/vultr/client.rb +++ b/lib/vultr/client.rb @@ -5,7 +5,7 @@ class Client attr_reader :api_key, :adapter def initialize(api_key: Vultr.api_key, adapter: Faraday.default_adapter) - #raise Error, "Please set Vultr.api_key = 'xxx' to make a request" unless api_key + # raise Error, "Please set Vultr.api_key = 'xxx' to make a request" unless api_key @api_key = api_key @adapter = adapter end @@ -98,7 +98,7 @@ def connection @connection ||= Faraday.new do |conn| conn.url_prefix = BASE_URL conn.request :json - conn.response :json, content_type: 'application/json' + conn.response :json, content_type: "application/json" conn.adapter adapter end end diff --git a/lib/vultr/collection.rb b/lib/vultr/collection.rb index 331618b..5146cd9 100644 --- a/lib/vultr/collection.rb +++ b/lib/vultr/collection.rb @@ -5,7 +5,7 @@ class Collection def self.from_response(response, key:, type:) body = response.body new( - data: body[key].map{ |attrs| type.new(attrs) }, + data: body[key].map { |attrs| type.new(attrs) }, total: body.dig("meta", "total"), next_cursor: body.dig("meta", "links", "next"), prev_cursor: body.dig("meta", "links", "prev") diff --git a/lib/vultr/resources/bare_metal.rb b/lib/vultr/resources/bare_metal.rb index c1c0c0b..5560981 100644 --- a/lib/vultr/resources/bare_metal.rb +++ b/lib/vultr/resources/bare_metal.rb @@ -65,15 +65,15 @@ def list_ipv6(id, **params) # Bulk operations def halt_instances(ids) - post_request("bare-metals/halt", body: { baremetal_ids: Array(ids) }) + post_request("bare-metals/halt", body: {baremetal_ids: Array(ids)}) end def reboot_instances(ids) - post_request("bare-metals/reboot", body: { baremetal_ids: Array(ids) }) + post_request("bare-metals/reboot", body: {baremetal_ids: Array(ids)}) end def start_instances(ids) - post_request("bare-metals/start", body: { baremetal_ids: Array(ids) }) + post_request("bare-metals/start", body: {baremetal_ids: Array(ids)}) end end end diff --git a/lib/vultr/resources/instances.rb b/lib/vultr/resources/instances.rb index 89a7b61..37ec3e5 100644 --- a/lib/vultr/resources/instances.rb +++ b/lib/vultr/resources/instances.rb @@ -102,11 +102,11 @@ def list_private_networks(id, **params) end def attach_private_network(id, network_id:) - post_request("instances/#{id}/private-networks/attach", body: { network_id: network_id }) + post_request("instances/#{id}/private-networks/attach", body: {network_id: network_id}) end def detach_private_network(id, network_id:) - post_request("instances/#{id}/private-networks/detach", body: { network_id: network_id }) + post_request("instances/#{id}/private-networks/detach", body: {network_id: network_id}) end def iso(id) @@ -114,11 +114,11 @@ def iso(id) end def attach_iso(id, iso_id:) - Object.new post_request("instances/#{id}/iso/attach", body: { iso_id: iso_id }).body.dig("iso_status") + Object.new post_request("instances/#{id}/iso/attach", body: {iso_id: iso_id}).body.dig("iso_status") end def detach_iso(id, iso_id:) - Object.new post_request("instances/#{id}/iso/detach", body: { iso_id: iso_id }).body.dig("iso_status") + Object.new post_request("instances/#{id}/iso/detach", body: {iso_id: iso_id}).body.dig("iso_status") end def get_backup_schedule(id) @@ -131,15 +131,15 @@ def set_backup_schedule(id, **attributes) # Bulk operations def halt_instances(ids) - post_request("instances/halt", body: { instance_ids: Array(ids) }) + post_request("instances/halt", body: {instance_ids: Array(ids)}) end def reboot_instances(ids) - post_request("instances/reboot", body: { instance_ids: Array(ids) }) + post_request("instances/reboot", body: {instance_ids: Array(ids)}) end def start_instances(ids) - post_request("instances/start", body: { instance_ids: Array(ids) }) + post_request("instances/start", body: {instance_ids: Array(ids)}) end end end diff --git a/lib/vultr/resources/reserved_ips.rb b/lib/vultr/resources/reserved_ips.rb index b522e8c..77bf27c 100644 --- a/lib/vultr/resources/reserved_ips.rb +++ b/lib/vultr/resources/reserved_ips.rb @@ -18,7 +18,7 @@ def delete(id) end def attach(id, reserved_ip:) - post_request("reserved-ips/#{id}/attach", body: { reserved_ip: reserved_ip }) + post_request("reserved-ips/#{id}/attach", body: {reserved_ip: reserved_ip}) end def detach(id) diff --git a/lib/vultr/resources/snapshots.rb b/lib/vultr/resources/snapshots.rb index 996a8be..5c752d9 100644 --- a/lib/vultr/resources/snapshots.rb +++ b/lib/vultr/resources/snapshots.rb @@ -14,7 +14,7 @@ def create(**attributes) end def create_from_url(url:) - Snapshot.new post_request("snapshots/create-from-url", body: { url: url }).body.dig("snapshot") + Snapshot.new post_request("snapshots/create-from-url", body: {url: url}).body.dig("snapshot") end def update(id, **attributes) diff --git a/lib/vultr/version.rb b/lib/vultr/version.rb index 1e6a400..73b91c8 100644 --- a/lib/vultr/version.rb +++ b/lib/vultr/version.rb @@ -2,9 +2,9 @@ module Vultr module VERSION MAJOR = 0 MINOR = 4 - TINY = 3 - PRE = nil + TINY = 3 + PRE = nil - STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.') + STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".") end end diff --git a/test/test_helper.rb b/test/test_helper.rb index abd3458..c656465 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,3 +1,3 @@ -$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) -require 'vultr' -require 'minitest/autorun' +$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__) +require "vultr" +require "minitest/autorun" diff --git a/test/vultr/object_test.rb b/test/vultr/object_test.rb index 6911655..dd68e2d 100644 --- a/test/vultr/object_test.rb +++ b/test/vultr/object_test.rb @@ -6,6 +6,6 @@ def test_creating_object_from_hash end def test_nested_hash - assert_equal :foobar, Vultr::Object.new(foo: { bar: { baz: :foobar } }).foo.bar.baz + assert_equal :foobar, Vultr::Object.new(foo: {bar: {baz: :foobar}}).foo.bar.baz end end diff --git a/test/vultr/resources/account_test.rb b/test/vultr/resources/account_test.rb index a87fee2..afabc32 100644 --- a/test/vultr/resources/account_test.rb +++ b/test/vultr/resources/account_test.rb @@ -7,4 +7,3 @@ def test_info def test_retrieve end end - diff --git a/vultr.gemspec b/vultr.gemspec index 987dfc5..5f7e27e 100644 --- a/vultr.gemspec +++ b/vultr.gemspec @@ -1,26 +1,26 @@ # Run `rake vultr.gemspec` to update the gemspec. -lib = File.expand_path('../lib', __FILE__) +lib = File.expand_path("../lib", __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'vultr/version' +require "vultr/version" Gem::Specification.new do |s| - s.name = 'vultr' - s.version = Vultr::VERSION::STRING - s.authors = ['Gao Jiangmiao'] - s.email = ['tolbkni@gmail.com'] - s.summary = %q{Ruby bindings for VULTR API.} - s.description = %q{Ruby bindings for VULTR API. VULTR APIs can be found here: https://www.vultr.com/api/} - s.homepage = 'https://github.com/tolbkni/vultr.rb' - s.license = 'MIT' + s.name = "vultr" + s.version = Vultr::VERSION::STRING + s.authors = ["Gao Jiangmiao"] + s.email = ["tolbkni@gmail.com"] + s.summary = "Ruby bindings for VULTR API." + s.description = "Ruby bindings for VULTR API. VULTR APIs can be found here: https://www.vultr.com/api/" + s.homepage = "https://github.com/tolbkni/vultr.rb" + s.license = "MIT" - s.files = `git ls-files -z`.split("\x0") - s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) } - s.test_files = s.files.grep(%r{^(test|spec|features)/}) - s.require_paths = ['lib'] + s.files = `git ls-files -z`.split("\x0") + s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) } + s.test_files = s.files.grep(%r{^(test|spec|features)/}) + s.require_paths = ["lib"] - s.required_ruby_version = '>= 1.9' + s.required_ruby_version = ">= 1.9" - s.add_dependency 'faraday', '~> 1.7' - #s.add_dependency 'faraday_middleware', '~> 1.1' + s.add_dependency "faraday", "~> 1.7" + # s.add_dependency 'faraday_middleware', '~> 1.1' end From d267d7c828df85c2add64034e5e41d92d6d91931 Mon Sep 17 00:00:00 2001 From: Andrea Fomera Date: Sat, 14 Aug 2021 23:54:59 -0600 Subject: [PATCH 05/50] Add CI workflow and bump version num to 1.0 Co-authored-by: Chris Oliver --- .github/workflows/ci.yml | 32 ++++++++++++++++++++++++++++++++ lib/vultr/version.rb | 6 +++--- vultr.gemspec | 2 +- 3 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..e87a409 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,32 @@ +name: Tests + +on: + pull_request: + branches: + - "*" + push: + branches: + - master +jobs: + tests: + runs-on: ubuntu-latest + strategy: + matrix: + ruby: ["2.7", "3.0"] + + steps: + - uses: actions/checkout@master + + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + bundler: default + bundler-cache: true + + - name: StandardRb check + run: bundle exec standardrb + + - name: Run tests + run: | + bundle exec rake test diff --git a/lib/vultr/version.rb b/lib/vultr/version.rb index 73b91c8..408c231 100644 --- a/lib/vultr/version.rb +++ b/lib/vultr/version.rb @@ -1,8 +1,8 @@ module Vultr module VERSION - MAJOR = 0 - MINOR = 4 - TINY = 3 + MAJOR = 1 + MINOR = 0 + TINY = 0 PRE = nil STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".") diff --git a/vultr.gemspec b/vultr.gemspec index 5f7e27e..0f6f827 100644 --- a/vultr.gemspec +++ b/vultr.gemspec @@ -22,5 +22,5 @@ Gem::Specification.new do |s| s.required_ruby_version = ">= 1.9" s.add_dependency "faraday", "~> 1.7" - # s.add_dependency 'faraday_middleware', '~> 1.1' + s.add_dependency 'faraday_middleware', '~> 1.1' end From b37d6f521f472b6d7fde2f62054eeed240184716 Mon Sep 17 00:00:00 2001 From: Andrea Fomera Date: Sat, 14 Aug 2021 23:57:20 -0600 Subject: [PATCH 06/50] Lint and define respond_to_missing? Co-authored-by: Chris Oliver --- lib/vultr/object.rb | 4 ++++ vultr.gemspec | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/vultr/object.rb b/lib/vultr/object.rb index e2d6ad4..54f40a2 100644 --- a/lib/vultr/object.rb +++ b/lib/vultr/object.rb @@ -10,5 +10,9 @@ def method_missing(method, *args, &block) attribute = @attributes.send(method, *args, &block) attribute.is_a?(Hash) ? Object.new(attribute) : attribute end + + def respond_to_missing?(method, include_private = false) + true + end end end diff --git a/vultr.gemspec b/vultr.gemspec index 0f6f827..6e475f8 100644 --- a/vultr.gemspec +++ b/vultr.gemspec @@ -22,5 +22,5 @@ Gem::Specification.new do |s| s.required_ruby_version = ">= 1.9" s.add_dependency "faraday", "~> 1.7" - s.add_dependency 'faraday_middleware', '~> 1.1' + s.add_dependency "faraday_middleware", "~> 1.1" end From 17bb42c86b93130e9b2c67a48047f9c0b72e482c Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Sun, 15 Aug 2021 17:04:19 -0500 Subject: [PATCH 07/50] Add all endpoints to docs --- CHANGELOG.md | 4 + README.md | 437 +++++++++++++++++++++---------- lib/vultr/resources/snapshots.rb | 2 +- 3 files changed, 309 insertions(+), 134 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1db8564..0cf0aa0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +== 1.0.0 + +* Vultr API v2 support + == 0.4.3 / 2019-10-8 * Relaxed Faraday dependencies diff --git a/README.md b/README.md index b74ea3e..531fb4f 100644 --- a/README.md +++ b/README.md @@ -3,22 +3,7 @@ [![Build Status](https://img.shields.io/travis/tolbkni/vultr.rb.svg)](https://travis-ci.org/tolbkni/vultr.rb) [![Gem Version](https://badge.fury.io/rb/vultr.svg)](https://badge.fury.io/rb/vultr) - -The easiest and most complete rubygem for [VULTR](https://www.vultr.com). - -```ruby -Vultr.api_key = "your_api_key" -r = Vultr::Server.list -# => { -# :status=>200, -# :result=>[40, 11, 45, 29, 41, 61, 46, 35, 12, 30, 42, 3, 39, 13, 60, 36, 37, 43, 27, 28, 38] -# } - -r[:status] -r[:result] -``` - -You can find your keys at [https://my.vultr.com/settings/#API](https://my.vultr.com/settings/#API) +The easiest and most complete rubygem for [VULTR](https://www.vultr.com) [API v2](https://www.vultr.com/api/v2). ## Installation @@ -36,123 +21,309 @@ Or install it yourself as: ## Usage -### List Servers - -```ruby -Vultr::Server.list -``` - -### Create Server - -```ruby -Vultr::Server.create({DCID: dc_id, VPSPLANID: vps_plan_id, OSID: os_id}) -``` - -## Available Commands - -```ruby -Vultr::Account.info - -Vultr::App.list - -Vultr::Auth.info - -Vultr::Backup.list - -Vultr::Block.attach({SUBID: sub_id, attach_to_SUBID: attach_to_sub_id}) -Vultr::Block.create({DCID: dc_id, size_gb: size_gb, label: label}) -Vultr::Block.delete(SUBID: sub_id) -Vultr::Block.detach(SUBID: sub_id) -Vultr::Block.label_set({SUBID: sub_id, label: label}) -Vultr::Block.list(SUBID: sub_id) -Vultr::Block.resize({SUBID: sub_id, size_gb: size_gb}) - -Vultr::DNS.create_domain({domain: domain, serverip: server_ip}) -Vultr::DNS.create_record({domain: domain, name: name, type: type, data: data, ttl: ttl, priority: priority}) -Vultr::DNS.delete_domain(domain: domain) -Vultr::DNS.delete_record({domain: domain, RECORDID: record_id}) -Vultr::DNS.list -Vultr::DNS.records(domain: domain) -Vultr::DNS.update_record({domain: domain, RECORDID: record_id, name: name, type: type, data: data, ttl: ttl, priority: priority}) - -Vultr::ISO.list - -Vultr::OS.list - -Vultr::Plans.list -Vultr::Plans.list_vc2 -Vultr::Plans.list_vdc2 - -Vultr::Regions.list -Vultr::Regions.availability(DCID: dc_id) - -Vultr::ReservedIP.attach({ip_address: ip_address, attach_SUBID: attach_sub_id}) -Vultr::ReservedIP.convert({SUBID: sub_id, ip_address: ip_address, label: label}) -Vultr::ReservedIP.create({DCID: dc_id, ip_type: ip_type, label: label}) -Vultr::ReservedIP.destroy(ip_address: ip_address) -Vultr::ReservedIP.detach({ip_address: ip_address, detach_SUBID: detach_sub_id}) -Vultr::ReservedIP.list - -Vultr::Server.app_change({SUBID: sub_id, APPID: app_id}) -Vultr::Server.app_change_list(SUBID: sub_id) -Vultr::Server.backup_disable(SUBID: sub_id) -Vultr::Server.backup_enable(SUBID: sub_id) -Vultr::Server.backup_get_schedule(SUBID: sub_id) -Vultr::Server.backup_set_schedule({SUBID: sub_id, cron_type: cron_type, hour: hour, dow: dow, dom: dom}) -Vultr::Server.bandwidth(SUBID: sub_id) -Vultr::Server.create({DCID: dc_id, VPSPLANID: vps_plan_id, OSID: os_id, ipxe_chain_url: ipxe_chain_url, ISOID: iso_id, - SCRIPTID: script_id, SNAPSHOTID: snapshot_id, enable_ipv6: enable_ipv6, enable_private_network: enable_private_network, - private_network: private_network, label: label, SSHKEYID: sshkey_id, auto_backups: auto_backups, APPID: app_id, - userdata: userdata, notify_activate: notify_activate, ddos_protection: ddos_protection, reserved_ip_v4: reserved_ipv4, - hostname: hostname, tag: tag}) -Vultr::Server.create_ipv4({SUBID: sub_id, reboot: reboot}) -Vultr::Server.destroy(SUBID: sub_id) -Vultr::Server.destroy_ipv4({SUBID: sub_id, ip: ip}) -Vultr::Server.get_app_inf(SUBID: sub_id) -Vultr::Server.get_user_data(SUBID: sub_id) -Vultr::Server.halt(SUBID: sub_id) -Vultr::Server.iso_attach({SUBID: sub_id, ISOID: iso_id}) -Vultr::Server.iso_detach(SUBID: sub_id) -Vultr::Server.iso_status(SUBID: sub_id) -Vultr::Server.label_set({SUBID: sub_id, label: label}) -Vultr::Server.list({SUBID: sub_id, tag: tag}) -Vultr::Server.list_ipv4(SUBID: sub_id) -Vultr::Server.list_ipv6(SUBID: sub_id) -Vultr::Server.neighbors(SUBID: sub_id) -Vultr::Server.os_change({SUBID: sub_id, OSID: os_id}) -Vultr::Server.os_change_list(SUBID: sub_id) -Vultr::Server.reboot(SUBID: sub_id) -Vultr::Server.reinstall({SUBID: sub_id, hostname: hostname}) -Vultr::Server.restore_backup({SUBID: sub_id, BACKUPID: backup_id}) -Vultr::Server.restore_snapshot({SUBID: sub_id, SNAPSHOTID: snapshot_id}) -Vultr::Server.reverse_default_ipv4({SUBID: sub_id, ip: ip}) -Vultr::Server.reverse_delete_ipv6({SUBID: sub_id, ip: ip}) -Vultr::Server.reverse_list_ipv6(SUBID: sub_id) -Vultr::Server.reverse_set_ipv4({SUBID: sub_id, ip: ip, entry: entry}) -Vultr::Server.reverse_set_ipv6({SUBID: sub_id, ip: ip, entry: entry}) -Vultr::Server.set_user_data({SUBID: sub_id, userdata: userdata}) -Vultr::Server.start(SUBID: sub_id) -Vultr::Server.upgrade_plan({SUBID: sub_id, VPSPLANID: vpsplan_id}) -Vultr::Server.upgrade_plan_list(SUBID: sub_id) - -Vultr::Snapshot.create(SUBID: sub_id) -Vultr::Snapshot.destroy(SNAPSHOTID: snapshot_id) -Vultr::Snapshot.list - -Vultr::SSHKey.create({name: name, ssh_key: ssh_key}) -Vultr::SSHKey.destroy(SSHKEYID: sshkey_id) -Vultr::SSHKey.list -Vultr::SSHKey.update({SSHKEYID: sshkey_id, name: name, ssh_key: ssh_key}) - -Vultr::StartupScript.create({name: name, script: script, type: type}) -Vultr::StartupScript.destroy(SCRIPTID: script_id) -Vultr::StartupScript.list -Vultr::StartupScript.update({SCRIPTID: script_id, name: name, script: script}) - -Vultr::User.create({email: email, name: name, password: password, api_enabled: api_enabled, acls: acls}) -Vultr::User.delete(USERID: user_id) -Vultr::User.list -Vultr::User.update({USERID: user_id, email: email, name: name, password: password, api_enabled: api_enabled, acls: acls}) +To access the API, you'll need to create a `Vultr::Client` and pass in your API key. You can find your API key at [https://my.vultr.com/settings](https://my.vultr.com/settings/#settingsapi) + +```ruby +client = Vultr::Client.new(api_key: ENV["VULTR_API_KEY"]) +``` + +The client then gives you access to each of the resources. + +## Resources + +The gem maps as closely as we can to the Vultr API so you can easily convert API examples to gem code. + +Responses are created as objects like `Vultr::Account`. Having types like `Vultr::User` is handy for understanding what type of object you're working with. They're built using OpenStruct so you can easily access data in a Ruby-ish way. + +##### Pagination + + `list` endpoints return pages of results. The result object will have a `data` key to access the results, as well as metadata like `next_cursor` and `prev_cursor` for retrieving the next and previous pages. You may also specify the + +```ruby +results = client.applications.list(per_page: 5) +#=> Vultr::Collection + +results.total +#=> 48 + +results.data +#=> [#, #] + +results.next_cursor +#=> "bmV4dF9fMTU=" + +# Retrieve the next page +client.applications.list(per_page: 5, cursor: results.next_cursor) +#=> Vultr::Collection +``` + +### Account + +```ruby +client.account.info +``` + +### Applications + +```ruby +client.applications.list +``` + +### Backups + +```ruby +client.backups.list +client.backups.get("id") +``` + +### Bare Metal + +```ruby +client.bare_metal.list +client.bare_metal.retrieve("id") +client.bare_metal.create({}) +client.bare_metal.update(id, {}) +client.bare_metal.delete(id) +client.bare_metal.start(id) +client.bare_metal.reboot(id) +client.bare_metal.reinstall(id) +client.bare_metal.halt(id) +client.bare_metal.bandwidth(id) +client.bare_metal.user_data(id) +client.bare_metal.upgrades(id) +client.bare_metal.vnc(id) +client.bare_metal.list_ipv4(id) +client.bare_metal.list_ipv6(id) + +# Bulk operations +client.bare_metal.start_instances([id1, id2]) +client.bare_metal.halt_instances([id1, id2]) +client.bare_metal.reboot_instances([id1, id2]) +``` + +### Block Storage + +```ruby +client.block_storage.list +client.block_storage.retrieve(id) +client.block_storage.create({}) +client.block_storage.update(id, {}) +client.block_storage.delete(id) +client.block_storage.attach(id, {}) +client.block_storage.detach(id) +``` + +### DNS (Domains) + +```ruby +client.dns.list +client.dns.retrieve(id) +client.dns.create({}) +client.dns.update(id, {}) +client.dns.delete(id) +client.dns.soa(id) +client.dns.update_soa(id, {}) +client.dns.dnssec(id) +client.dns.list_records(id) +client.dns.retrieve_record(id, record_id: "id") +client.dns.create_record(id, {}) +client.dns.update_record(id, record_id: "id", {}) +client.dns.delete_record(id, record_id: "id") +``` + +### Firewall + +```ruby +client.firewall.list +client.firewall.retrieve(id) +client.firewall.create({}) +client.firewall.update(id, {}) +client.firewall.delete(id) +client.firewall.list_rules(id) +client.firewall.retrieve_rule(id, rule_id: "id") +client.firewall.create_rule(id, {}) +client.firewall.delete_rule(id, rule_id: "id") +``` + +### Instances + +```ruby +client.instances.list +client.instances.retrieve("id") +client.instances.create({}) +client.instances.update(id, {}) +client.instances.delete(id) +client.instances.start(id) +client.instances.reboot(id) +client.instances.reinstall(id) +client.instances.restore(id, {}) +client.instances.halt(id) +client.instances.bandwidth(id) +client.instances.neighbors(id) +client.instances.user_data(id) +client.instances.upgrades(id) +client.instances.list_ipv4(id) +client.instances.create_ipv4(id, {}) +client.instances.delete_ipv4(id, ipv4: "id") +client.instances.create_ipv4_reverse(id, {}) +client.instances.set_default_reverse_dns_entry(id, {}) +client.instances.list_ipv6(id) +client.instances.ipv6_reverse(id) +client.instances.create_ipv6_reverse(id, {}) +client.instances.delete_ipv6_reverse(id, ipv6: "id") +client.instances.list_private_networks(id) +client.instances.attach_private_network(id, network_id: "id") +client.instances.detach_private_network(id, network_id: "id") +client.instances.iso(id) +client.instances.attach_iso(id, iso_id: "id") +client.instances.detach_iso(id, iso_id: "id") +client.instances.backup_schedule(id) +client.instances.set_backup_schedule(id, {}) + +# Bulk operations +client.instances.start_instances([id1, id2]) +client.instances.halt_instances([id1, id2]) +client.instances.reboot_instances([id1, id2]) +``` + +### ISO + +```ruby +client.iso.list +client.iso.retrieve(id) +client.iso.create({}) +client.iso.delete(id) +client.iso.list_public +``` + +### Kubernetes + +```ruby +client.kubernetes.list +client.kubernetes.retrieve(id) +client.kubernetes.create({}) +client.kubernetes.update(id, {}) +client.kubernetes.delete(id) +client.kubernetes.config(id) +client.kubernetes.list_resources(id) +client.kubernetes.list_node_pools(id) +client.kubernetes.retrieve_node_pool(id, nodepool_id: "id") +client.kubernetes.create_node_pool(id, {}) +client.kubernetes.update_node_pool(id, nodepool_id: "id", {}) +client.kubernetes.delete_node_pool(id, nodepool_id: "id") +client.kubernetes.delete_node_pool_instance(id, nodepool_id: "id", node_id: "id") +client.kubernetes.recycle_node_pool_instance(id, nodepool_id: "id", node_id: "id") +``` + +### Load Balancers + +```ruby +client.load_balancers.list +client.load_balancers.retrieve(id) +client.load_balancers.create({}) +client.load_balancers.update(id, {}) +client.load_balancers.delete(id) +client.load_balancers.list_forwarding_rules(id) +client.load_balancers.retrieve_forwarding_rule(id, rule_id: "id") +client.load_balancers.create_forwarding_rule(id, {}) +client.load_balancers.delete_forwarding_rule(id, rule_id: "id") +client.load_balancers.list_firewall_rules(id) +client.load_balancers.retrieve_firewall_rule(id) +``` + +### Object Storage + +```ruby +client.object_storage.list +client.object_storage.retrieve(id) +client.object_storage.create({}) +client.object_storage.update(id, {}) +client.object_storage.delete(id) +client.object_storage.regenerate_keys(id) +client.object_storage.list_clusters() +``` + +### Operating Systems + +```ruby +client.operating_systems.list +``` + +### Plans + +```ruby +client.plans.list +client.plans.list_metal +``` + +### Private Networks + +```ruby +client.private_networks.list +client.private_networks.retrieve(id) +client.private_networks.create({}) +client.private_networks.update(id, {}) +client.private_networks.delete(id) +``` + +### Regions + +```ruby +client.regions.list +client.regions.list_availability("region_id") +``` + +### Reserved IPs + +```ruby +client.reserved_ips.list +client.reserved_ips.retrieve(id) +client.reserved_ips.create({}) +client.reserved_ips.delete(id) +client.reserved_ips.attach(id, reserved_ip: "") +client.reserved_ips.detach(id) +client.reserved_ips.convert({}) +``` + +### Snapshots + +```ruby +client.snapshots.list +client.snapshots.retrieve(id) +client.snapshots.create({}) +client.snapshots.create_from_url("https://...") +client.snapshots.update(id, {}) +client.snapshots.delete(id) +``` + +### SSH Keys + +```ruby +client.ssh_keys.list +client.ssh_keys.retrieve(id) +client.ssh_keys.create({}) +client.ssh_keys.update(id, {}) +client.ssh_keys.delete(id) +``` + +### Startup Scripts + +```ruby +client.startup_scripts.list +client.startup_scripts.retrieve(id) +client.startup_scripts.create({}) +client.startup_scripts.update(id, {}) +client.startup_scripts.delete(id) +``` + +### Users + +```ruby +client.users.list +client.users.retrieve(id) +client.users.create({}) +client.users.update(id, {}) +client.users.delete(id) ``` ## Contributing diff --git a/lib/vultr/resources/snapshots.rb b/lib/vultr/resources/snapshots.rb index 5c752d9..54ac3f9 100644 --- a/lib/vultr/resources/snapshots.rb +++ b/lib/vultr/resources/snapshots.rb @@ -13,7 +13,7 @@ def create(**attributes) Snapshot.new post_request("snapshots", body: attributes).body.dig("snapshot") end - def create_from_url(url:) + def create_from_url(url) Snapshot.new post_request("snapshots/create-from-url", body: {url: url}).body.dig("snapshot") end From 15be0c48a63d7419245432b724f8b51e66026d04 Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Sun, 15 Aug 2021 17:09:45 -0500 Subject: [PATCH 08/50] Rename backup schedule --- lib/vultr/resources/instances.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vultr/resources/instances.rb b/lib/vultr/resources/instances.rb index 37ec3e5..f59546f 100644 --- a/lib/vultr/resources/instances.rb +++ b/lib/vultr/resources/instances.rb @@ -121,7 +121,7 @@ def detach_iso(id, iso_id:) Object.new post_request("instances/#{id}/iso/detach", body: {iso_id: iso_id}).body.dig("iso_status") end - def get_backup_schedule(id) + def backup_schedule(id) Object.new get_request("instances/#{id}/backup-schedule").body.dig("backup_schedule") end From 17a09ac7a1a679e5b8a5d00fe54e3296278cfad1 Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Tue, 17 Aug 2021 19:41:57 -0500 Subject: [PATCH 09/50] Add bin scripts --- bin/console | 21 +++++++++++++++++++++ bin/setup | 8 ++++++++ bin/test | 6 ++++++ lib/vultr.rb | 8 -------- lib/vultr/client.rb | 3 +-- test/vultr/config_test.rb | 9 --------- 6 files changed, 36 insertions(+), 19 deletions(-) create mode 100755 bin/console create mode 100755 bin/setup create mode 100755 bin/test delete mode 100644 test/vultr/config_test.rb diff --git a/bin/console b/bin/console new file mode 100755 index 0000000..f94463c --- /dev/null +++ b/bin/console @@ -0,0 +1,21 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require "bundler/setup" +require "vultr" + +# You can add fixtures and/or initialization code here to make experimenting +# with your gem easier. You can also use a different console, if you like. + +# (If you use this, don't forget to add pry to your Gemfile!) +# require "pry" +# Pry.start + +client = Vultr::Client.new(api_key: ENV["VULTR_API_KEY"]) + +puts +puts "We've created a 'client' variable as a Vultr::Client using ENV['VULTR_API_KEY'] as the API key." +puts + +require "irb" +IRB.start(__FILE__) diff --git a/bin/setup b/bin/setup new file mode 100755 index 0000000..dce67d8 --- /dev/null +++ b/bin/setup @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -euo pipefail +IFS=$'\n\t' +set -vx + +bundle install + +# Do any other automated setup that you need to do here diff --git a/bin/test b/bin/test new file mode 100755 index 0000000..4f83f09 --- /dev/null +++ b/bin/test @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +set -euo pipefail +IFS=$'\n\t' +set -vx + +bundle exec rake test diff --git a/lib/vultr.rb b/lib/vultr.rb index 2e41421..36c25c5 100644 --- a/lib/vultr.rb +++ b/lib/vultr.rb @@ -54,12 +54,4 @@ module Vultr autoload :SshKey, "vultr/objects/ssh_key" autoload :StartupScript, "vultr/objects/startup_script" autoload :User, "vultr/objects/user" - - def self.api_key=(key) - @api_key = key - end - - def self.api_key - @api_key - end end diff --git a/lib/vultr/client.rb b/lib/vultr/client.rb index 1eb8c2f..97f5a40 100644 --- a/lib/vultr/client.rb +++ b/lib/vultr/client.rb @@ -4,8 +4,7 @@ class Client attr_reader :api_key, :adapter - def initialize(api_key: Vultr.api_key, adapter: Faraday.default_adapter) - # raise Error, "Please set Vultr.api_key = 'xxx' to make a request" unless api_key + def initialize(api_key:, adapter: Faraday.default_adapter) @api_key = api_key @adapter = adapter end diff --git a/test/vultr/config_test.rb b/test/vultr/config_test.rb deleted file mode 100644 index 0f5df40..0000000 --- a/test/vultr/config_test.rb +++ /dev/null @@ -1,9 +0,0 @@ -require "test_helper" - -class ConfigTest < Minitest::Test - def test_api_key - assert_nil Vultr.api_key - Vultr.api_key = "test" - assert_equal "test", Vultr.api_key - end -end From 3e833b3fbf4052aedde0b148c5fca84cc2fc670d Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Tue, 17 Aug 2021 21:55:30 -0500 Subject: [PATCH 10/50] Okay, we'll use the keyword args @afomera. Fine. --- README.md | 240 ++++++++++++------------ lib/vultr/resources/backups.rb | 4 +- lib/vultr/resources/bare_metal.rb | 68 +++---- lib/vultr/resources/block_storage.rb | 24 +-- lib/vultr/resources/dns.rb | 48 ++--- lib/vultr/resources/firewall.rb | 32 ++-- lib/vultr/resources/instances.rb | 132 ++++++------- lib/vultr/resources/iso.rb | 12 +- lib/vultr/resources/kubernetes.rb | 52 ++--- lib/vultr/resources/load_balancers.rb | 40 ++-- lib/vultr/resources/object_storage.rb | 20 +- lib/vultr/resources/private_networks.rb | 16 +- lib/vultr/resources/regions.rb | 4 +- lib/vultr/resources/reserved_ips.rb | 20 +- lib/vultr/resources/snapshots.rb | 16 +- lib/vultr/resources/ssh_keys.rb | 16 +- lib/vultr/resources/startup_scripts.rb | 16 +- lib/vultr/resources/users.rb | 16 +- 18 files changed, 388 insertions(+), 388 deletions(-) diff --git a/README.md b/README.md index 531fb4f..9b6abdb 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ Responses are created as objects like `Vultr::Account`. Having types like `Vultr ##### Pagination - `list` endpoints return pages of results. The result object will have a `data` key to access the results, as well as metadata like `next_cursor` and `prev_cursor` for retrieving the next and previous pages. You may also specify the + `list` endpoints return pages of results. The result object will have a `data` key to access the results, as well as metadata like `next_cursor` and `prev_cursor` for retrieving the next and previous pages. You may also specify the ```ruby results = client.applications.list(per_page: 5) @@ -73,126 +73,126 @@ client.applications.list ```ruby client.backups.list -client.backups.get("id") +client.backups.get(backup_id: "id") ``` ### Bare Metal ```ruby client.bare_metal.list -client.bare_metal.retrieve("id") client.bare_metal.create({}) -client.bare_metal.update(id, {}) -client.bare_metal.delete(id) -client.bare_metal.start(id) -client.bare_metal.reboot(id) -client.bare_metal.reinstall(id) -client.bare_metal.halt(id) -client.bare_metal.bandwidth(id) -client.bare_metal.user_data(id) -client.bare_metal.upgrades(id) -client.bare_metal.vnc(id) -client.bare_metal.list_ipv4(id) -client.bare_metal.list_ipv6(id) +client.bare_metal.retrieve(baremetal_id: "id") +client.bare_metal.update(baremetal_id: "id", {}) +client.bare_metal.delete(baremetal_id: "id") +client.bare_metal.start(baremetal_id: "id") +client.bare_metal.reboot(baremetal_id: "id") +client.bare_metal.reinstall(baremetal_id: "id") +client.bare_metal.halt(baremetal_id: "id") +client.bare_metal.bandwidth(baremetal_id: "id") +client.bare_metal.user_data(baremetal_id: "id") +client.bare_metal.upgrades(baremetal_id: "id") +client.bare_metal.vnc(baremetal_id: "id") +client.bare_metal.list_ipv4(baremetal_id: "id") +client.bare_metal.list_ipv6(baremetal_id: "id") # Bulk operations -client.bare_metal.start_instances([id1, id2]) -client.bare_metal.halt_instances([id1, id2]) -client.bare_metal.reboot_instances([id1, id2]) +client.bare_metal.start_instances(baremetal_ids: [id1, id2]) +client.bare_metal.halt_instances(baremetal_ids: [id1, id2]) +client.bare_metal.reboot_instances(baremetal_ids: [id1, id2]) ``` ### Block Storage ```ruby client.block_storage.list -client.block_storage.retrieve(id) client.block_storage.create({}) -client.block_storage.update(id, {}) -client.block_storage.delete(id) -client.block_storage.attach(id, {}) -client.block_storage.detach(id) +client.block_storage.retrieve(block_id: "id") +client.block_storage.update(block_id: "id", {}) +client.block_storage.delete(block_id: "id") +client.block_storage.attach(block_id: "id", {}) +client.block_storage.detach(block_id: "id") ``` ### DNS (Domains) ```ruby client.dns.list -client.dns.retrieve(id) client.dns.create({}) -client.dns.update(id, {}) -client.dns.delete(id) -client.dns.soa(id) -client.dns.update_soa(id, {}) -client.dns.dnssec(id) -client.dns.list_records(id) -client.dns.retrieve_record(id, record_id: "id") -client.dns.create_record(id, {}) -client.dns.update_record(id, record_id: "id", {}) -client.dns.delete_record(id, record_id: "id") +client.dns.retrieve(dns_domain: "id") +client.dns.update(dns_domain: "id", {}) +client.dns.delete(dns_domain: "id") +client.dns.soa(dns_domain: "id") +client.dns.update_soa(dns_domain: "id", {}) +client.dns.dnssec(dns_domain: "id") +client.dns.list_records(dns_domain: "id") +client.dns.retrieve_record(dns_domain: "id", record_id: "id") +client.dns.create_record(dns_domain: "id", {}) +client.dns.update_record(dns_domain: "id", record_id: "id", {}) +client.dns.delete_record(dns_domain: "id", record_id: "id") ``` ### Firewall ```ruby client.firewall.list -client.firewall.retrieve(id) client.firewall.create({}) -client.firewall.update(id, {}) -client.firewall.delete(id) -client.firewall.list_rules(id) -client.firewall.retrieve_rule(id, rule_id: "id") -client.firewall.create_rule(id, {}) -client.firewall.delete_rule(id, rule_id: "id") +client.firewall.retrieve(firewall_group_id: "id") +client.firewall.update(firewall_group_id: "id", {}) +client.firewall.delete(firewall_group_id: "id") +client.firewall.list_rules(firewall_group_id: "id") +client.firewall.create_rule(firewall_group_id: "id", {}) +client.firewall.retrieve_rule(firewall_group_id: "id", firewall_rule_id: "id") +client.firewall.delete_rule(firewall_group_id: "id", firewall_rule_id: "id") ``` ### Instances ```ruby client.instances.list -client.instances.retrieve("id") client.instances.create({}) -client.instances.update(id, {}) -client.instances.delete(id) -client.instances.start(id) -client.instances.reboot(id) -client.instances.reinstall(id) -client.instances.restore(id, {}) -client.instances.halt(id) -client.instances.bandwidth(id) -client.instances.neighbors(id) -client.instances.user_data(id) -client.instances.upgrades(id) -client.instances.list_ipv4(id) -client.instances.create_ipv4(id, {}) -client.instances.delete_ipv4(id, ipv4: "id") -client.instances.create_ipv4_reverse(id, {}) -client.instances.set_default_reverse_dns_entry(id, {}) -client.instances.list_ipv6(id) -client.instances.ipv6_reverse(id) -client.instances.create_ipv6_reverse(id, {}) -client.instances.delete_ipv6_reverse(id, ipv6: "id") -client.instances.list_private_networks(id) -client.instances.attach_private_network(id, network_id: "id") -client.instances.detach_private_network(id, network_id: "id") -client.instances.iso(id) -client.instances.attach_iso(id, iso_id: "id") -client.instances.detach_iso(id, iso_id: "id") -client.instances.backup_schedule(id) -client.instances.set_backup_schedule(id, {}) +client.instances.retrieve(instance_id: "id") +client.instances.update(instance_id: "id", {}) +client.instances.delete(instance_id: "id") +client.instances.start(instance_id: "id") +client.instances.reboot(instance_id: "id") +client.instances.reinstall(instance_id: "id") +client.instances.restore(instance_id: "id", {}) +client.instances.halt(instance_id: "id") +client.instances.bandwidth(instance_id: "id") +client.instances.neighbors(instance_id: "id") +client.instances.user_data(instance_id: "id") +client.instances.upgrades(instance_id: "id") +client.instances.list_ipv4(instance_id: "id") +client.instances.create_ipv4(instance_id: "id", {}) +client.instances.delete_ipv4(instance_id: "id", ipv4: "id") +client.instances.create_ipv4_reverse(instance_id: "id", {}) +client.instances.set_default_reverse_dns_entry(instance_id: "id", {}) +client.instances.list_ipv6(instance_id: "id") +client.instances.ipv6_reverse(instance_id: "id") +client.instances.create_ipv6_reverse(instance_id: "id", {}) +client.instances.delete_ipv6_reverse(instance_id: "id", ipv6: "id") +client.instances.list_private_networks(instance_id: "id") +client.instances.attach_private_network(instance_id: "id", network_id: "id") +client.instances.detach_private_network(instance_id: "id", network_id: "id") +client.instances.iso(instance_id: "id") +client.instances.attach_iso(instance_id: "id", iso_id: "id") +client.instances.detach_iso(instance_id: "id", iso_id: "id") +client.instances.backup_schedule(instance_id: "id") +client.instances.set_backup_schedule(instance_id: "id", {}) # Bulk operations -client.instances.start_instances([id1, id2]) -client.instances.halt_instances([id1, id2]) -client.instances.reboot_instances([id1, id2]) +client.instances.start_instances(instance_ids: [id1, id2]) +client.instances.halt_instances(instance_ids: [id1, id2]) +client.instances.reboot_instances(instance_ids: [id1, id2]) ``` ### ISO ```ruby client.iso.list -client.iso.retrieve(id) client.iso.create({}) -client.iso.delete(id) +client.iso.retrieve(iso_id: "id") +client.iso.delete(iso_id: "id") client.iso.list_public ``` @@ -200,46 +200,46 @@ client.iso.list_public ```ruby client.kubernetes.list -client.kubernetes.retrieve(id) client.kubernetes.create({}) -client.kubernetes.update(id, {}) -client.kubernetes.delete(id) -client.kubernetes.config(id) -client.kubernetes.list_resources(id) -client.kubernetes.list_node_pools(id) -client.kubernetes.retrieve_node_pool(id, nodepool_id: "id") -client.kubernetes.create_node_pool(id, {}) -client.kubernetes.update_node_pool(id, nodepool_id: "id", {}) -client.kubernetes.delete_node_pool(id, nodepool_id: "id") -client.kubernetes.delete_node_pool_instance(id, nodepool_id: "id", node_id: "id") -client.kubernetes.recycle_node_pool_instance(id, nodepool_id: "id", node_id: "id") +client.kubernetes.retrieve(vke_id: "id") +client.kubernetes.update(vke_id: "id", {}) +client.kubernetes.delete(vke_id: "id") +client.kubernetes.config(vke_id: "id") +client.kubernetes.list_resources(vke_id: "id") +client.kubernetes.list_node_pools(vke_id: "id") +client.kubernetes.retrieve_node_pool(vke_id: "id", nodepool_id: "id") +client.kubernetes.create_node_pool(vke_id: "id", {}) +client.kubernetes.update_node_pool(vke_id: "id", nodepool_id: "id", {}) +client.kubernetes.delete_node_pool(vke_id: "id", nodepool_id: "id") +client.kubernetes.delete_node_pool_instance(vke_id: "id", nodepool_id: "id", node_id: "id") +client.kubernetes.recycle_node_pool_instance(vke_id: "id", nodepool_id: "id", node_id: "id") ``` ### Load Balancers ```ruby client.load_balancers.list -client.load_balancers.retrieve(id) client.load_balancers.create({}) -client.load_balancers.update(id, {}) -client.load_balancers.delete(id) -client.load_balancers.list_forwarding_rules(id) -client.load_balancers.retrieve_forwarding_rule(id, rule_id: "id") -client.load_balancers.create_forwarding_rule(id, {}) -client.load_balancers.delete_forwarding_rule(id, rule_id: "id") -client.load_balancers.list_firewall_rules(id) -client.load_balancers.retrieve_firewall_rule(id) +client.load_balancers.retrieve(load_balancer_id: "id") +client.load_balancers.update(load_balancer_id: "id", {}) +client.load_balancers.delete(load_balancer_id: "id") +client.load_balancers.list_forwarding_rules(load_balancer_id: "id") +client.load_balancers.create_forwarding_rule(load_balancer_id: "id", {}) +client.load_balancers.retrieve_forwarding_rule(load_balancer_id: "id", forwarding_rule_id: "id") +client.load_balancers.delete_forwarding_rule(load_balancer_id: "id", forwarding_rule_id: "id") +client.load_balancers.list_firewall_rules(load_balancer_id: "id") +client.load_balancers.retrieve_firewall_rule(load_balancer_id: "id", firewall_rule_id: "id") ``` ### Object Storage ```ruby client.object_storage.list -client.object_storage.retrieve(id) client.object_storage.create({}) -client.object_storage.update(id, {}) -client.object_storage.delete(id) -client.object_storage.regenerate_keys(id) +client.object_storage.retrieve(object_storage_id: "id") +client.object_storage.update(object_storage_id: "id", {}) +client.object_storage.delete(object_storage_id: "id") +client.object_storage.regenerate_keys(object_storage_id: "id") client.object_storage.list_clusters() ``` @@ -260,28 +260,28 @@ client.plans.list_metal ```ruby client.private_networks.list -client.private_networks.retrieve(id) client.private_networks.create({}) -client.private_networks.update(id, {}) -client.private_networks.delete(id) +client.private_networks.retrieve(network_id: "id") +client.private_networks.update(network_id: "id", {}) +client.private_networks.delete(network_id: "id") ``` ### Regions ```ruby client.regions.list -client.regions.list_availability("region_id") +client.regions.list_availability(region_id: "id") ``` ### Reserved IPs ```ruby client.reserved_ips.list -client.reserved_ips.retrieve(id) client.reserved_ips.create({}) -client.reserved_ips.delete(id) -client.reserved_ips.attach(id, reserved_ip: "") -client.reserved_ips.detach(id) +client.reserved_ips.retrieve(reserved_ip: "id") +client.reserved_ips.delete(reserved_ip: "id") +client.reserved_ips.attach(reserved_ip: "id", instance_id: "id") +client.reserved_ips.detach(reserved_ip: "id") client.reserved_ips.convert({}) ``` @@ -289,41 +289,41 @@ client.reserved_ips.convert({}) ```ruby client.snapshots.list -client.snapshots.retrieve(id) client.snapshots.create({}) +client.snapshots.retrieve(snapshot_id: "id") client.snapshots.create_from_url("https://...") -client.snapshots.update(id, {}) -client.snapshots.delete(id) +client.snapshots.update(snapshot_id: "id", {}) +client.snapshots.delete(snapshot_id: "id") ``` ### SSH Keys ```ruby client.ssh_keys.list -client.ssh_keys.retrieve(id) client.ssh_keys.create({}) -client.ssh_keys.update(id, {}) -client.ssh_keys.delete(id) +client.ssh_keys.retrieve(ssh_key_id: "id") +client.ssh_keys.update(ssh_key_id: "id", {}) +client.ssh_keys.delete(ssh_key_id: "id") ``` ### Startup Scripts ```ruby client.startup_scripts.list -client.startup_scripts.retrieve(id) client.startup_scripts.create({}) -client.startup_scripts.update(id, {}) -client.startup_scripts.delete(id) +client.startup_scripts.retrieve(startup_script_id: "id") +client.startup_scripts.update(startup_script_id: "id", {}) +client.startup_scripts.delete(startup_script_id: "id") ``` ### Users ```ruby client.users.list -client.users.retrieve(id) client.users.create({}) -client.users.update(id, {}) -client.users.delete(id) +client.users.retrieve(user_id: "id") +client.users.update(user_id: "id", {}) +client.users.delete(user_id: "id") ``` ## Contributing diff --git a/lib/vultr/resources/backups.rb b/lib/vultr/resources/backups.rb index b40afea..a06fb24 100644 --- a/lib/vultr/resources/backups.rb +++ b/lib/vultr/resources/backups.rb @@ -5,8 +5,8 @@ def list(**params) Collection.from_response(response, key: "backups", type: Backup) end - def retrieve(id) - Backup.new get_request("backups/#{id}").body.dig("backup") + def retrieve(backup_id:) + Backup.new get_request("backups/#{backup_id}").body.dig("backup") end end end diff --git a/lib/vultr/resources/bare_metal.rb b/lib/vultr/resources/bare_metal.rb index 5560981..b794fc2 100644 --- a/lib/vultr/resources/bare_metal.rb +++ b/lib/vultr/resources/bare_metal.rb @@ -5,75 +5,75 @@ def list(**params) Collection.from_response(response, key: "bare_metals", type: BareMetal) end - def retrieve(id) - BareMetal.new get_request("bare-metals/#{id}").body.dig("bare_metal") - end - def create(**attributes) BareMetal.new post_request("bare-metals", body: attributes).body.dig("bare_metal") end - def update(id, **attributes) - BareMetal.new patch_request("bare-metals/#{id}", body: attributes).body.dig("bare_metal") + def retrieve(baremetal_id:) + BareMetal.new get_request("bare-metals/#{baremetal_id}").body.dig("bare_metal") + end + + def update(baremetal_id:, **attributes) + BareMetal.new patch_request("bare-metals/#{baremetal_id}", body: attributes).body.dig("bare_metal") end - def delete(id) - delete_request("bare-metals/#{id}") + def delete(baremetal_id:) + delete_request("bare-metals/#{baremetal_id}") end - def start(id) - post_request("bare-metals/#{id}/start") + def start(baremetal_id:) + post_request("bare-metals/#{baremetal_id}/start") end - def reboot(id) - post_request("bare-metals/#{id}/reboot") + def reboot(baremetal_id:) + post_request("bare-metals/#{baremetal_id}/reboot") end - def reinstall(id) - BareMetal.new post_request("bare-metals/#{id}/reinstall").body.dig("bare_metal") + def reinstall(baremetal_id:) + BareMetal.new post_request("bare-metals/#{baremetal_id}/reinstall").body.dig("bare_metal") end - def halt(id) - post_request("bare-metals/#{id}/halt") + def halt(baremetal_id:) + post_request("bare-metals/#{baremetal_id}/halt") end - def bandwidth(id) - Object.new get_request("bare-metals/#{id}/bandwidth").body.dig("bandwidth") + def bandwidth(baremetal_id:) + Object.new get_request("bare-metals/#{baremetal_id}/bandwidth").body.dig("bandwidth") end - def user_data(id) - Object.new get_request("bare-metals/#{id}/user-data").body.dig("user_data") + def user_data(baremetal_id:) + Object.new get_request("bare-metals/#{baremetal_id}/user-data").body.dig("user_data") end - def upgrades(id, **params) - Object.new get_request("bare-metals/#{id}/upgrades", params: params).body.dig("upgrades") + def upgrades(baremetal_id:, **params) + Object.new get_request("bare-metals/#{baremetal_id}/upgrades", params: params).body.dig("upgrades") end - def vnc(id) - Object.new get_request("bare-metals/#{id}/vnc", params: params).body.dig("vnc") + def vnc(baremetal_id:) + Object.new get_request("bare-metals/#{baremetal_id}/vnc", params: params).body.dig("vnc") end - def list_ipv4(id, **params) - response = get_request("bare-metals/#{id}/ipv4", params: params) + def list_ipv4(baremetal_id:, **params) + response = get_request("bare-metals/#{baremetal_id}/ipv4", params: params) Collection.from_response(response, key: "ipv4s", type: Object) end - def list_ipv6(id, **params) - response = get_request("bare-metals/#{id}/ipv6", params: params) + def list_ipv6(baremetal_id:, **params) + response = get_request("bare-metals/#{baremetal_id}/ipv6", params: params) Collection.from_response(response, key: "ipv6s", type: Object) end # Bulk operations - def halt_instances(ids) - post_request("bare-metals/halt", body: {baremetal_ids: Array(ids)}) + def halt_instances(baremetal_ids:) + post_request("bare-metals/halt", body: {baremetal_ids: Array(baremetal_ids)}) end - def reboot_instances(ids) - post_request("bare-metals/reboot", body: {baremetal_ids: Array(ids)}) + def reboot_instances(baremetal_ids:) + post_request("bare-metals/reboot", body: {baremetal_ids: Array(baremetal_ids)}) end - def start_instances(ids) - post_request("bare-metals/start", body: {baremetal_ids: Array(ids)}) + def start_instances(baremetal_ids:) + post_request("bare-metals/start", body: {baremetal_ids: Array(baremetal_ids)}) end end end diff --git a/lib/vultr/resources/block_storage.rb b/lib/vultr/resources/block_storage.rb index 6eb7718..020a6a6 100644 --- a/lib/vultr/resources/block_storage.rb +++ b/lib/vultr/resources/block_storage.rb @@ -5,28 +5,28 @@ def list(**params) Collection.from_response(response, key: "blocks", type: BlockStorage) end - def retrieve(id) - BlockStorage.new get_request("blocks/#{id}").body.dig("block") - end - def create(**attributes) BlockStorage.new post_request("blocks", body: attributes).body.dig("block") end - def update(id, **attributes) - patch_request("blocks/#{id}", body: attributes) + def retrieve(block_id:) + BlockStorage.new get_request("blocks/#{block_id}").body.dig("block") + end + + def update(block_id:, **attributes) + patch_request("blocks/#{block_id}", body: attributes) end - def delete(id) - delete_request("blocks/#{id}") + def delete(block_id:) + delete_request("blocks/#{block_id}") end - def attach(id, **params) - post_request("blocks/#{id}/attach", body: params) + def attach(block_id:, **params) + post_request("blocks/#{block_id}/attach", body: params) end - def detach(id) - post_request("blocks/#{id}/detach", body: params) + def detach(block_id:) + post_request("blocks/#{block_id}/detach", body: params) end end end diff --git a/lib/vultr/resources/dns.rb b/lib/vultr/resources/dns.rb index dc7a149..a45581f 100644 --- a/lib/vultr/resources/dns.rb +++ b/lib/vultr/resources/dns.rb @@ -5,53 +5,53 @@ def list(**params) Collection.from_response(response, key: "domains", type: Domain) end - def retrieve(id) - Domain.new get_request("domains/#{id}").body.dig("domain") - end - def create(**attributes) Domain.new post_request("domains", body: attributes).body.dig("domain") end - def update(id, **attributes) - patch_request("domains/#{id}", body: attributes) + def retrieve(dns_domain:) + Domain.new get_request("domains/#{dns_domain}").body.dig("domain") + end + + def update(dns_domain:, **attributes) + patch_request("domains/#{dns_domain}", body: attributes) end - def delete(id) - delete_request("domains/#{id}") + def delete(dns_domain:) + delete_request("domains/#{dns_domain}") end - def soa(id) - Object.new get_request("domains/#{id}/soa").body.dig("dns_soa") + def soa(dns_domain:) + Object.new get_request("domains/#{dns_domain}/soa").body.dig("dns_soa") end - def update_soa(id, **attributes) - post_request("domains/#{id}/soa", body: attributes) + def update_soa(dns_domain:, **attributes) + post_request("domains/#{dns_domain}/soa", body: attributes) end - def dnssec(id) - Object.new get_request("domains/#{id}/dnssec").body.dig("dns_sec") + def dnssec(dns_domain:) + Object.new get_request("domains/#{dns_domain}/dnssec").body.dig("dns_sec") end - def list_records(id, **params) - response = get_request("domains/#{id}/records", params: params) + def list_records(dns_domain:, **params) + response = get_request("domains/#{dns_domain}/records", params: params) Collection.from_response(response, key: "records", type: Object) end - def retrieve_record(id, record_id:) - Object.new get_request("domains/#{id}/records/#{record_id}").body.dig("record") + def create_record(dns_domain:, **attributes) + Object.new post_request("domains/#{dns_domain}/records", body: attributes).body.dig("record") end - def create_record(id, **attributes) - Object.new post_request("domains/#{id}/records", body: attributes).body.dig("record") + def retrieve_record(dns_domain:, record_id:) + Object.new get_request("domains/#{dns_domain}/records/#{record_id}").body.dig("record") end - def update_record(id, record_id:, **attributes) - patch_request("domains/#{id}/records/#{record_id}", body: attributes) + def update_record(dns_domain:, record_id:, **attributes) + patch_request("domains/#{dns_domain}/records/#{record_id}", body: attributes) end - def delete_record(id, record_id:) - delete_request("domains/#{id}/records/#{record_id}") + def delete_record(dns_domain:, record_id:) + delete_request("domains/#{dns_domain}/records/#{record_id}") end end end diff --git a/lib/vultr/resources/firewall.rb b/lib/vultr/resources/firewall.rb index 47e2518..a89b3e8 100644 --- a/lib/vultr/resources/firewall.rb +++ b/lib/vultr/resources/firewall.rb @@ -5,38 +5,38 @@ def list(**params) Collection.from_response(response: response, key: "firewall_groups", type: FirewallGroup) end - def retrieve(id) - FirewallGroup.new get_request("firewalls/#{id}").body.dig("firewall_group") - end - def create(**attributes) FirewallGroup.new post_request("firewalls", body: attributes).body.dig("firewall_group") end - def update(id, **attributes) - put_request("firewalls/#{id}", body: attributes) + def retrieve(firewall_group_id:) + FirewallGroup.new get_request("firewalls/#{firewall_group_id}").body.dig("firewall_group") + end + + def update(firewall_group_id:, **attributes) + put_request("firewalls/#{firewall_group_id}", body: attributes) end - def delete(id) - delete_request("firewalls/#{id}") + def delete(firewall_group_id:) + delete_request("firewalls/#{firewall_group_id}") end # Firewall Rules - def list_rules(id, **params) - response = get_request("firewalls/#{id}/rules", params: params) + def list_rules(firewall_group_id:, **params) + response = get_request("firewalls/#{firewall_group_id}/rules", params: params) Collection.from_response(response: response, key: "firewall_rules", type: Object) end - def retrieve_rule(id, rule_id:) - Object.new get_request("firewalls/#{id}/rules/#{rule_id}").body.dig("firewall_rule") + def create_rule(firewall_group_id:, **attributes) + Object.new post_request("firewalls/#{firewall_group_id}/rules", body: attributes).body.dig("firewall_rule") end - def create_rule(id, **attributes) - Object.new post_request("firewalls/#{id}/rules", body: attributes).body.dig("firewall_rule") + def retrieve_rule(firewall_group_id:, firewall_rule_id:) + Object.new get_request("firewalls/#{firewall_group_id}/rules/#{firewall_rule_id}").body.dig("firewall_rule") end - def delete_rule(id, rule_id:) - delete_request("firewalls/#{id}/rules/#{rule_id}") + def delete_rule(firewall_group_id:, firewall_rule_id:) + delete_request("firewalls/#{firewall_group_id}/rules/#{firewall_rule_id}") end end end diff --git a/lib/vultr/resources/instances.rb b/lib/vultr/resources/instances.rb index f59546f..b6f7ee8 100644 --- a/lib/vultr/resources/instances.rb +++ b/lib/vultr/resources/instances.rb @@ -5,141 +5,141 @@ def list(**params) Collection.from_response(response, key: "instances", type: Instance) end - def retrieve(id) - Instance.new get_request("instances/#{id}").body - end - def create(**attributes) Instance.new post_request("instances", body: attributes).body.dig("instance") end - def update(id, **attributes) - patch_request("instances/#{id}", body: attributes) + def retrieve(instance_id:) + Instance.new get_request("instances/#{instance_id}").body + end + + def update(instance_id:, **attributes) + patch_request("instances/#{instance_id}", body: attributes) end - def delete(id) - delete_request("instances/#{id}") + def delete(instance_id:) + delete_request("instances/#{instance_id}") end - def start(id) - post_request("instances/#{id}/start") + def start(instance_id:) + post_request("instances/#{instance_id}/start") end - def reboot(id) - post_request("instances/#{id}/reboot") + def reboot(instance_id:) + post_request("instances/#{instance_id}/reboot") end - def reinstall(id) - Instance.new post_request("instances/#{id}/reinstall").body.dig("instance") + def reinstall(instance_id:) + Instance.new post_request("instances/#{instance_id}/reinstall").body.dig("instance") end - def restore(id, **attributes) - Object.new post_request("instances/#{id}/restore", attributes).body.dig("status") + def restore(instance_id:, **attributes) + Object.new post_request("instances/#{instance_id}/restore", attributes).body.dig("status") end - def halt(id) - post_request("instances/#{id}/halt") + def halt(instance_id:) + post_request("instances/#{instance_id}/halt") end - def bandwidth(id) - Object.new get_request("instances/#{id}/bandwidth").body.dig("bandwidth") + def bandwidth(instance_id:) + Object.new get_request("instances/#{instance_id}/bandwidth").body.dig("bandwidth") end - def neighbors(id) - Object.new get_request("instances/#{id}/neighbors").body.dig("neighbors") + def neighbors(instance_id:) + Object.new get_request("instances/#{instance_id}/neighbors").body.dig("neighbors") end - def user_data(id) - Object.new get_request("instances/#{id}/user-data").body.dig("user_data") + def user_data(instance_id:) + Object.new get_request("instances/#{instance_id}/user-data").body.dig("user_data") end - def upgrades(id, **params) - Object.new get_request("instances/#{id}/upgrades", params: params).body.dig("upgrades") + def upgrades(instance_id:, **params) + Object.new get_request("instances/#{instance_id}/upgrades", params: params).body.dig("upgrades") end - def list_ipv4(id, **params) - response = get_request("instances/#{id}/ipv4", params: params) + def list_ipv4(instance_id:, **params) + response = get_request("instances/#{instance_id}/ipv4", params: params) Collection.from_response(response, key: "ipv4s", type: Object) end - def create_ipv4(id, **params) - Object.new post_request("instances/#{id}/ipv4", body: params).body.dig("ipv4") + def create_ipv4(instance_id:, **params) + Object.new post_request("instances/#{instance_id}/ipv4", body: params).body.dig("ipv4") end - def delete_ipv4(id, ipv4:) - delete_request("instances/#{id}/ipv4/#{ipv4}") + def delete_ipv4(instance_id:, ipv4:) + delete_request("instances/#{instance_id}/ipv4/#{ipv4}") end - def create_ipv4_reverse(id, **params) - post_request("instances/#{id}/ipv4/reverse", body: params) + def create_ipv4_reverse(instance_id:, **params) + post_request("instances/#{instance_id}/ipv4/reverse", body: params) end - def set_default_reverse_dns_entry(id, **params) - post_request("instances/#{id}/ipv4/reverse/default", body: params) + def set_default_reverse_dns_entry(instance_id:, **params) + post_request("instances/#{instance_id}/ipv4/reverse/default", body: params) end - def list_ipv6(id, **params) - response = get_request("instances/#{id}/ipv6", params: params) + def list_ipv6(instance_id:, **params) + response = get_request("instances/#{instance_id}/ipv6", params: params) Collection.from_response(response, key: "ipv6s", type: Object) end - def ipv6_reverse(id) - response = get_request("instances/#{id}/ipv6") + def ipv6_reverse(instance_id:) + response = get_request("instances/#{instance_id}/ipv6") Collection.from_response(response, key: "reverse_ipv6s", type: Object) end - def create_ipv6_reverse(id, **params) - post_request("instances/#{id}/ipv6/reverse", body: params) + def create_ipv6_reverse(instance_id:, **params) + post_request("instances/#{instance_id}/ipv6/reverse", body: params) end - def delete_ipv6_reverse(id, ipv6:) - delete_request("instances/#{id}/ipv6/reverse/#{ipv6}") + def delete_ipv6_reverse(instance_id:, ipv6:) + delete_request("instances/#{instance_id}/ipv6/reverse/#{ipv6}") end - def list_private_networks(id, **params) - response = get_request("instances/#{id}/private-networks", params: params) + def list_private_networks(instance_id:, **params) + response = get_request("instances/#{instance_id}/private-networks", params: params) Collection.from_response(response, key: "private_networks", type: PrivateNetwork) end - def attach_private_network(id, network_id:) - post_request("instances/#{id}/private-networks/attach", body: {network_id: network_id}) + def attach_private_network(instance_id:, network_id:) + post_request("instances/#{instance_id}/private-networks/attach", body: {network_id: network_id}) end - def detach_private_network(id, network_id:) - post_request("instances/#{id}/private-networks/detach", body: {network_id: network_id}) + def detach_private_network(instance_id:, network_id:) + post_request("instances/#{instance_id}/private-networks/detach", body: {network_id: network_id}) end - def iso(id) - Object.new get_request("instances/#{id}/iso").body.dig("iso_status") + def iso(instance_id:) + Object.new get_request("instances/#{instance_id}/iso").body.dig("iso_status") end - def attach_iso(id, iso_id:) - Object.new post_request("instances/#{id}/iso/attach", body: {iso_id: iso_id}).body.dig("iso_status") + def attach_iso(instance_id:, iso_id:) + Object.new post_request("instances/#{instance_id}/iso/attach", body: {iso_id: iso_id}).body.dig("iso_status") end - def detach_iso(id, iso_id:) - Object.new post_request("instances/#{id}/iso/detach", body: {iso_id: iso_id}).body.dig("iso_status") + def detach_iso(instance_id:, iso_id:) + Object.new post_request("instances/#{instance_id}/iso/detach", body: {iso_id: iso_id}).body.dig("iso_status") end - def backup_schedule(id) - Object.new get_request("instances/#{id}/backup-schedule").body.dig("backup_schedule") + def backup_schedule(instance_id:) + Object.new get_request("instances/#{instance_id}/backup-schedule").body.dig("backup_schedule") end - def set_backup_schedule(id, **attributes) - post_request("instances/#{id}/backup-schedule", body: attributes) + def set_backup_schedule(instance_id:, **attributes) + post_request("instances/#{instance_id}/backup-schedule", body: attributes) end # Bulk operations - def halt_instances(ids) - post_request("instances/halt", body: {instance_ids: Array(ids)}) + def halt_instances(instance_ids:) + post_request("instances/halt", body: {instance_ids: Array(instance_ids)}) end - def reboot_instances(ids) - post_request("instances/reboot", body: {instance_ids: Array(ids)}) + def reboot_instances(instance_ids:) + post_request("instances/reboot", body: {instance_ids: Array(instance_ids)}) end - def start_instances(ids) - post_request("instances/start", body: {instance_ids: Array(ids)}) + def start_instances(instance_ids:) + post_request("instances/start", body: {instance_ids: Array(instance_ids)}) end end end diff --git a/lib/vultr/resources/iso.rb b/lib/vultr/resources/iso.rb index 7ca448e..0ca19b3 100644 --- a/lib/vultr/resources/iso.rb +++ b/lib/vultr/resources/iso.rb @@ -5,16 +5,16 @@ def list(**params) Collection.from_response(response, key: "isos", type: Iso) end - def retrieve(id) - Iso.new get_request("isos/#{id}").body.dig("iso") - end - def create(**attributes) Iso.new post_request("isos", body: attributes).body.dig("iso") end - def delete(id) - delete_request("isos/#{id}") + def retrieve(iso_id:) + Iso.new get_request("isos/#{iso_id}").body.dig("iso") + end + + def delete(iso_id:) + delete_request("isos/#{iso_id}") end def list_public(**params) diff --git a/lib/vultr/resources/kubernetes.rb b/lib/vultr/resources/kubernetes.rb index 43b1bee..779a80c 100644 --- a/lib/vultr/resources/kubernetes.rb +++ b/lib/vultr/resources/kubernetes.rb @@ -5,58 +5,58 @@ def list(**params) Collection.from_response(response, key: "vke_clusters", type: KubernetesCluster) end - def retrieve(id) - KubernetesCluster.new get_request("kubernetes/clusters/#{id}").body.dig("vke_cluster") - end - def create(**attributes) KubernetesCluster.new post_request("kubernetes/clusters", body: attributes).body.dig("vke_cluster") end - def update(id, **attributes) - put_request("kubernetes/clusters/#{id}", body: attributes) + def retrieve(vke_id:) + KubernetesCluster.new get_request("kubernetes/clusters/#{vke_id}").body.dig("vke_cluster") + end + + def update(vke_id:, **attributes) + put_request("kubernetes/clusters/#{vke_id}", body: attributes) end - def delete(id) - delete_request("kubernetes/clusters/#{id}") + def delete(vke_id:) + delete_request("kubernetes/clusters/#{vke_id}") end - def config(id) - Object.new get_request("kubernetes/clusters/#{id}/config").body + def config(vke_id:) + Object.new get_request("kubernetes/clusters/#{vke_id}/config").body end - def list_resources(id, **params) - response = get_request("kubernetes/clusters/#{id}/resources", params: params) + def list_resources(vke_id:, **params) + response = get_request("kubernetes/clusters/#{vke_id}/resources", params: params) Collection.from_response(response, key: "resources", type: Object) end - def list_node_pools(id, **params) - response = get_request("kubernetes/clusters/#{id}/node-pools", params: params) + def list_node_pools(vke_id:, **params) + response = get_request("kubernetes/clusters/#{vke_id}/node-pools", params: params) Collection.from_response(response, key: "node_pools", type: Object) end - def retrieve_node_pool(id, nodepool_id:) - Object.new get_request("kubernetes/clusters/#{id}/node-pools/#{nodepool_id}").body.dig("node_pool") + def retrieve_node_pool(vke_id:, nodepool_id:) + Object.new get_request("kubernetes/clusters/#{vke_id}/node-pools/#{nodepool_id}").body.dig("node_pool") end - def create_node_pool(id, **attributes) - Object.new post_request("kubernetes/clusters/#{id}/node-pools", body: attributes).body.dig("node_pool") + def create_node_pool(vke_id:, **attributes) + Object.new post_request("kubernetes/clusters/#{vke_id}/node-pools", body: attributes).body.dig("node_pool") end - def update_node_pool(id, nodepool_id:, **attributes) - Object.new patch_request("kubernetes/clusters/#{id}/node-pools/#{nodepool_id}", body: attributes).body.dig("node_pool") + def update_node_pool(vke_id:, nodepool_id:, **attributes) + Object.new patch_request("kubernetes/clusters/#{vke_id}/node-pools/#{nodepool_id}", body: attributes).body.dig("node_pool") end - def delete_node_pool(id, nodepool_id:) - delete_request("kubernetes/clusters/#{id}/node-pools/#{nodepool_id}") + def delete_node_pool(vke_id:, nodepool_id:) + delete_request("kubernetes/clusters/#{vke_id}/node-pools/#{nodepool_id}") end - def delete_node_pool_instance(id, nodepool_id:, node_id:) - delete_request("kubernetes/clusters/#{id}/node-pools/#{nodepool_id}/nodes/#{node_id}") + def delete_node_pool_instance(vke_id:, nodepool_id:, node_id:) + delete_request("kubernetes/clusters/#{vke_id}/node-pools/#{nodepool_id}/nodes/#{node_id}") end - def recycle_node_pool_instance(id, nodepool_id:, node_id:) - post_request("kubernetes/clusters/#{id}/node-pools/#{nodepool_id}/nodes/#{node_id}/recycle") + def recycle_node_pool_instance(vke_id:, nodepool_id:, node_id:) + post_request("kubernetes/clusters/#{vke_id}/node-pools/#{nodepool_id}/nodes/#{node_id}/recycle") end end end diff --git a/lib/vultr/resources/load_balancers.rb b/lib/vultr/resources/load_balancers.rb index 56c6596..7312978 100644 --- a/lib/vultr/resources/load_balancers.rb +++ b/lib/vultr/resources/load_balancers.rb @@ -5,46 +5,46 @@ def list(**params) Collection.from_response(response: response, key: "load_balancers", type: LoadBalancer) end - def retrieve(id) - LoadBalancer.new get_request("load-balancers/#{id}").body.dig("load_balancer") - end - def create(**attributes) LoadBalancer.new post_request("load-balancers", body: attributes).body.dig("load_balancer") end - def update(id, **attributes) - LoadBalancer.new patch_request("load-balancers/#{id}", body: attributes).body.dig("load_balancer") + def retrieve(load_balancer_id:) + LoadBalancer.new get_request("load-balancers/#{load_balancer_id}").body.dig("load_balancer") + end + + def update(load_balancer_id:, **attributes) + LoadBalancer.new patch_request("load-balancers/#{load_balancer_id}", body: attributes).body.dig("load_balancer") end - def delete(id) - delete_request("load-balancers/#{id}") + def delete(load_balancer_id:) + delete_request("load-balancers/#{load_balancer_id}") end - def list_forwarding_rules(id, **params) - response = get_request("load-balancers/#{id}/forwarding-rules", params: params) + def list_forwarding_rules(load_balancer_id:, **params) + response = get_request("load-balancers/#{load_balancer_id}/forwarding-rules", params: params) Collection.from_response(response: response, key: "forwarding_rules", type: Object) end - def retrieve_forwarding_rule(id, rule_id:) - Object.new get_request("load-balancers/#{id}/forwarding-rules/#{rule_id}").body.dig("forwarding_rule") + def create_forwarding_rule(load_balancer_id:, **attributes) + Object.new post_request("load-balancers/#{load_balancer_id}/forwarding-rules", body: attributes).body.dig("forwarding_rule") end - def create_forwarding_rule(id, **attributes) - Object.new post_request("load-balancers/#{id}/forwarding-rules", body: attributes).body.dig("forwarding_rule") + def retrieve_forwarding_rule(load_balancer_id:, forwarding_rule_id:) + Object.new get_request("load-balancers/#{load_balancer_id}/forwarding-rules/#{forwarding_rule_id}").body.dig("forwarding_rule") end - def delete_forwarding_rule(id, rule_id:) - delete_request("load-balancers/#{id}/forwarding-rules/#{rule_id}") + def delete_forwarding_rule(load_balancer_id:, forwarding_rule_id:) + delete_request("load-balancers/#{load_balancer_id}/forwarding-rules/#{forwarding_rule_id}") end - def list_firewall_rules(id, **params) - response = get_request("load-balancers/#{id}/firewall-rules", params: params) + def list_firewall_rules(load_balancer_id:, **params) + response = get_request("load-balancers/#{load_balancer_id}/firewall-rules", params: params) Collection.from_response(response: response, key: "firewall_rules", type: Object) end - def retrieve_firewall_rule(id, rule_id:) - Object.new get_request("load-balancers/#{id}/firewall-rules/#{rule_id}").body.dig("firewall_rule") + def retrieve_firewall_rule(load_balancer_id:, firewall_rule_id:) + Object.new get_request("load-balancers/#{load_balancer_id}/firewall-rules/#{firewall_rule_id}").body.dig("firewall_rule") end end end diff --git a/lib/vultr/resources/object_storage.rb b/lib/vultr/resources/object_storage.rb index 75b4e80..7620a77 100644 --- a/lib/vultr/resources/object_storage.rb +++ b/lib/vultr/resources/object_storage.rb @@ -5,24 +5,24 @@ def list(**params) Collection.from_response(response: response, key: "object_storages", type: ObjectStorage) end - def retrieve(id) - ObjectStorage.new get_request("object-storage/#{id}").body.dig("object_storage") - end - def create(**attributes) ObjectStorage.new post_request("object-storage", body: attributes).body.dig("object_storage") end - def update(id, **attributes) - put_request("object-storage/#{id}", body: attributes) + def retrieve(object_storage_id:) + ObjectStorage.new get_request("object-storage/#{object_storage_id}").body.dig("object_storage") + end + + def update(object_storage_id:, **attributes) + put_request("object-storage/#{object_storage_id}", body: attributes) end - def delete(id) - delete_request("object-storage/#{id}") + def delete(object_storage_id:) + delete_request("object-storage/#{object_storage_id}") end - def regenerate_keys(id) - Object.new post_request("object-storage/#{id}/regenerate-keys").body + def regenerate_keys(object_storage_id:) + Object.new post_request("object-storage/#{object_storage_id}/regenerate-keys").body end def list_clusters(**params) diff --git a/lib/vultr/resources/private_networks.rb b/lib/vultr/resources/private_networks.rb index ac3ca7d..647fe55 100644 --- a/lib/vultr/resources/private_networks.rb +++ b/lib/vultr/resources/private_networks.rb @@ -5,20 +5,20 @@ def list(**params) Collection.from_response(response, key: "networks", type: PrivateNetwork) end - def retrieve(id) - PrivateNetwork.new get_request("private-networks/#{id}").body.dig("network") - end - def create(**attributes) PrivateNetwork.new post_request("private-networks", body: attributes).body.dig("network") end - def update(id, **attributes) - put_request("private-networks/#{id}", body: attributes) + def retrieve(network_id:) + PrivateNetwork.new get_request("private-networks/#{network_id}").body.dig("network") + end + + def update(network_id:, **attributes) + put_request("private-networks/#{network_id}", body: attributes) end - def delete(id) - delete_request("private-networks/#{id}") + def delete(network_id:) + delete_request("private-networks/#{network_id}") end end end diff --git a/lib/vultr/resources/regions.rb b/lib/vultr/resources/regions.rb index 776fa83..294dcbd 100644 --- a/lib/vultr/resources/regions.rb +++ b/lib/vultr/resources/regions.rb @@ -5,8 +5,8 @@ def list(**params) Collection.from_response(response, key: "regions", type: Region) end - def list_availability(id) - Object.new get_request("regions/#{id}/availability").body + def list_availability(region_id:) + Object.new get_request("regions/#{region_id}/availability").body end end end diff --git a/lib/vultr/resources/reserved_ips.rb b/lib/vultr/resources/reserved_ips.rb index 77bf27c..6f365bb 100644 --- a/lib/vultr/resources/reserved_ips.rb +++ b/lib/vultr/resources/reserved_ips.rb @@ -5,24 +5,24 @@ def list(**params) Collection.from_response(response, key: "reserved_ips", type: ReservedIp) end - def retrieve(id) - ReservedIp.new get_request("reserved-ips/#{id}").body.dig("reserved_ip") - end - def create(**attributes) ReservedIp.new post_request("reserved-ips", body: attributes).body.dig("reserved_ip") end - def delete(id) - delete_request("reserved-ips/#{id}") + def retrieve(reserved_ip:) + ReservedIp.new get_request("reserved-ips/#{reserved_ip}").body.dig("reserved_ip") + end + + def delete(reserved_ip:) + delete_request("reserved-ips/#{reserved_ip}") end - def attach(id, reserved_ip:) - post_request("reserved-ips/#{id}/attach", body: {reserved_ip: reserved_ip}) + def attach(reserved_ip:, instance_id:) + post_request("reserved-ips/#{reserved_ip}/attach", body: {instance_id: instance_id}) end - def detach(id) - post_request("reserved-ips/#{id}/detach") + def detach(reserved_ip:) + post_request("reserved-ips/#{reserved_ip}/detach") end def convert(**attributes) diff --git a/lib/vultr/resources/snapshots.rb b/lib/vultr/resources/snapshots.rb index 54ac3f9..21a7975 100644 --- a/lib/vultr/resources/snapshots.rb +++ b/lib/vultr/resources/snapshots.rb @@ -5,24 +5,24 @@ def list(**params) Collection.from_response(response, key: "snapshots", type: Snapshot) end - def retrieve(id) - Snapshot.new get_request("snapshots/#{id}").body.dig("snapshot") - end - def create(**attributes) Snapshot.new post_request("snapshots", body: attributes).body.dig("snapshot") end + def retrieve(snapshot_id:) + Snapshot.new get_request("snapshots/#{snapshot_id}").body.dig("snapshot") + end + def create_from_url(url) Snapshot.new post_request("snapshots/create-from-url", body: {url: url}).body.dig("snapshot") end - def update(id, **attributes) - put_request("snapshots/#{id}", body: attributes) + def update(snapshot_id:, **attributes) + put_request("snapshots/#{snapshot_id}", body: attributes) end - def delete(id) - delete_request("snapshots/#{id}") + def delete(snapshot_id:) + delete_request("snapshots/#{snapshot_id}") end end end diff --git a/lib/vultr/resources/ssh_keys.rb b/lib/vultr/resources/ssh_keys.rb index 6bb1062..c3e23fb 100644 --- a/lib/vultr/resources/ssh_keys.rb +++ b/lib/vultr/resources/ssh_keys.rb @@ -5,20 +5,20 @@ def list(**params) Collection.from_response(response: response, key: "ssh_keys", type: SshKey) end - def retrieve(id) - SshKey.new get_request("ssh-keys/#{id}").body.dig("ssh_key") - end - def create(**attributes) SshKey.new post_request("ssh-keys", body: attributes).body.dig("ssh_key") end - def update(id, **attributes) - patch_request("ssh-keys/#{id}", body: attributes) + def retrieve(ssh_key_id:) + SshKey.new get_request("ssh-keys/#{ssh_key_id}").body.dig("ssh_key") + end + + def update(ssh_key_id:, **attributes) + patch_request("ssh-keys/#{ssh_key_id}", body: attributes) end - def delete(id) - delete_request("ssh-keys/#{id}") + def delete(ssh_key_id:) + delete_request("ssh-keys/#{ssh_key_id}") end end end diff --git a/lib/vultr/resources/startup_scripts.rb b/lib/vultr/resources/startup_scripts.rb index 6021701..36ef19e 100644 --- a/lib/vultr/resources/startup_scripts.rb +++ b/lib/vultr/resources/startup_scripts.rb @@ -5,20 +5,20 @@ def list(**params) Collection.from_response(response: response, key: "startup_scripts", type: StartupScript) end - def retrieve(id) - StartupScript.new get_request("startup-scripts/#{id}").body.dig("startup_script") - end - def create(**attributes) StartupScript.new post_request("startup-scripts", body: attributes).body.dig("startup_script") end - def update(id, **attributes) - patch_request("startup-scripts/#{id}", body: attributes) + def retrieve(startup_script_id:) + StartupScript.new get_request("startup-scripts/#{startup_script_id}").body.dig("startup_script") + end + + def update(startup_script_id:, **attributes) + patch_request("startup-scripts/#{startup_script_id}", body: attributes) end - def delete(id) - delete_request("startup-scripts/#{id}") + def delete(startup_script_id:) + delete_request("startup-scripts/#{startup_script_id}") end end end diff --git a/lib/vultr/resources/users.rb b/lib/vultr/resources/users.rb index 10ef939..1c9bd77 100644 --- a/lib/vultr/resources/users.rb +++ b/lib/vultr/resources/users.rb @@ -5,20 +5,20 @@ def list(**params) Collection.from_response(response, key: "users", type: User) end - def retrieve(id) - User.new get_request("users/#{id}").body.dig("user") - end - def create(**attributes) User.new post_request("users", body: attributes).body.dig("user") end - def update(id, **attributes) - patch_request("users/#{id}", body: attributes) + def retrieve(user_id:) + User.new get_request("users/#{user_id}").body.dig("user") + end + + def update(user_id:, **attributes) + patch_request("users/#{user_id}", body: attributes) end - def delete(id) - delete_request("users/#{id}") + def delete(user_id:) + delete_request("users/#{user_id}") end end end From 3825bfd9676cbd960cd6912397c56c0289d47e6f Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Tue, 17 Aug 2021 22:02:16 -0500 Subject: [PATCH 11/50] Version bump --- lib/vultr/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vultr/version.rb b/lib/vultr/version.rb index 408c231..070c215 100644 --- a/lib/vultr/version.rb +++ b/lib/vultr/version.rb @@ -1,6 +1,6 @@ module Vultr module VERSION - MAJOR = 1 + MAJOR = 2 MINOR = 0 TINY = 0 PRE = nil From b225f23cb82db83ddaeed0e6c3b37a95bd7b1623 Mon Sep 17 00:00:00 2001 From: Bilal Budhani Date: Thu, 19 Aug 2021 19:25:18 +0530 Subject: [PATCH 12/50] Updates Vultr::Object to support marshalling --- lib/vultr/object.rb | 15 ++++----------- test/vultr/object_test.rb | 4 ++-- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/lib/vultr/object.rb b/lib/vultr/object.rb index 54f40a2..33b7036 100644 --- a/lib/vultr/object.rb +++ b/lib/vultr/object.rb @@ -1,18 +1,11 @@ require "ostruct" +require "delegate" +require "json" module Vultr - class Object + class Object < SimpleDelegator def initialize(attributes) - @attributes = OpenStruct.new(attributes) - end - - def method_missing(method, *args, &block) - attribute = @attributes.send(method, *args, &block) - attribute.is_a?(Hash) ? Object.new(attribute) : attribute - end - - def respond_to_missing?(method, include_private = false) - true + super(JSON.parse(attributes.to_json, object_class: OpenStruct)) end end end diff --git a/test/vultr/object_test.rb b/test/vultr/object_test.rb index dd68e2d..5a20f90 100644 --- a/test/vultr/object_test.rb +++ b/test/vultr/object_test.rb @@ -2,10 +2,10 @@ class ObjectTest < Minitest::Test def test_creating_object_from_hash - assert_equal :bar, Vultr::Object.new(foo: :bar).foo + assert_equal "bar", Vultr::Object.new(foo: :bar).foo end def test_nested_hash - assert_equal :foobar, Vultr::Object.new(foo: {bar: {baz: :foobar}}).foo.bar.baz + assert_equal "foobar", Vultr::Object.new(foo: {bar: {baz: :foobar}}).foo.bar.baz end end From f09be50428c428f8a81812faab12401fddf17ac2 Mon Sep 17 00:00:00 2001 From: Bilal Budhani Date: Thu, 19 Aug 2021 19:37:00 +0530 Subject: [PATCH 13/50] Adds test case for number --- test/vultr/object_test.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/vultr/object_test.rb b/test/vultr/object_test.rb index 5a20f90..caff32f 100644 --- a/test/vultr/object_test.rb +++ b/test/vultr/object_test.rb @@ -8,4 +8,8 @@ def test_creating_object_from_hash def test_nested_hash assert_equal "foobar", Vultr::Object.new(foo: {bar: {baz: :foobar}}).foo.bar.baz end + + def test_nested_number + assert_equal 1, Vultr::Object.new(foo: {bar: 1}).foo.bar + end end From 4dd1a5742324cc301d711b4ec56b0b95dc702a3e Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Thu, 19 Aug 2021 17:45:30 -0500 Subject: [PATCH 14/50] Add test stubs --- lib/vultr/client.rb | 7 ++-- test/fixtures/account/info.json | 11 ++++++ test/fixtures/users/create.json | 9 +++++ test/fixtures/users/delete.json | 1 + test/fixtures/users/list.json | 29 ++++++++++++++++ test/fixtures/users/retrieve.json | 20 +++++++++++ test/fixtures/users/update.json | 1 + test/test_helper.rb | 16 +++++++++ test/vultr/resources/account_test.rb | 7 ++-- test/vultr/resources/users_test.rb | 50 ++++++++++++++++++++++++++++ 10 files changed, 147 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/account/info.json create mode 100644 test/fixtures/users/create.json create mode 100644 test/fixtures/users/delete.json create mode 100644 test/fixtures/users/list.json create mode 100644 test/fixtures/users/retrieve.json create mode 100644 test/fixtures/users/update.json create mode 100644 test/vultr/resources/users_test.rb diff --git a/lib/vultr/client.rb b/lib/vultr/client.rb index 97f5a40..0f2fca9 100644 --- a/lib/vultr/client.rb +++ b/lib/vultr/client.rb @@ -4,9 +4,12 @@ class Client attr_reader :api_key, :adapter - def initialize(api_key:, adapter: Faraday.default_adapter) + def initialize(api_key:, adapter: Faraday.default_adapter, stubs: nil) @api_key = api_key @adapter = adapter + + # Test stubs for requests + @stubs = stubs end def account @@ -98,7 +101,7 @@ def connection conn.url_prefix = BASE_URL conn.request :json conn.response :json, content_type: "application/json" - conn.adapter adapter + conn.adapter adapter, @stubs end end end diff --git a/test/fixtures/account/info.json b/test/fixtures/account/info.json new file mode 100644 index 0000000..9b25d2b --- /dev/null +++ b/test/fixtures/account/info.json @@ -0,0 +1,11 @@ +{ + "account": { + "name": "Example Account", + "email": "admin@example.com", + "acls": [], + "balance": -100, + "pending_charges": 60, + "last_payment_date": "2020-10-10T01:56:20+00:00", + "last_payment_amount": -1 + } +} diff --git a/test/fixtures/users/create.json b/test/fixtures/users/create.json new file mode 100644 index 0000000..5c4b201 --- /dev/null +++ b/test/fixtures/users/create.json @@ -0,0 +1,9 @@ +{ + "user": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "name": "Example User", + "email": "user@example.com", + "api_enabled": true, + "acls": [] + } +} diff --git a/test/fixtures/users/delete.json b/test/fixtures/users/delete.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/fixtures/users/delete.json @@ -0,0 +1 @@ +{} diff --git a/test/fixtures/users/list.json b/test/fixtures/users/list.json new file mode 100644 index 0000000..3eaca8a --- /dev/null +++ b/test/fixtures/users/list.json @@ -0,0 +1,29 @@ +{ + "users": [ + { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "email": "user@example.com", + "api_enabled": true, + "acls": [ + "manage_users", + "subscriptions_view", + "subscriptions", + "provisioning", + "billing", + "support", + "abuse", + "dns", + "upgrade", + "objstore", + "loadbalancer" + ] + } + ], + "meta": { + "total": 1, + "links": { + "next": "nextid", + "prev": "previd" + } + } +} diff --git a/test/fixtures/users/retrieve.json b/test/fixtures/users/retrieve.json new file mode 100644 index 0000000..7c50b55 --- /dev/null +++ b/test/fixtures/users/retrieve.json @@ -0,0 +1,20 @@ +{ + "user": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "email": "user@example.com", + "api_enabled": true, + "acls": [ + "manage_users", + "subscriptions_view", + "subscriptions", + "provisioning", + "billing", + "support", + "abuse", + "dns", + "upgrade", + "objstore", + "loadbalancer" + ] + } +} diff --git a/test/fixtures/users/update.json b/test/fixtures/users/update.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/fixtures/users/update.json @@ -0,0 +1 @@ +{} diff --git a/test/test_helper.rb b/test/test_helper.rb index c656465..694026e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,3 +1,19 @@ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__) require "vultr" require "minitest/autorun" +require "faraday" +require "json" + +class Minitest::Test + def stub_response(fixture:, status: 200, headers: {"Content-Type" => "application/json"}) + [status, headers, File.read("test/fixtures/#{fixture}.json")] + end + + def stub_request(path, response:, method: :get, body: {}) + Faraday::Adapter::Test::Stubs.new do |stub| + arguments = [method, "/v2/#{path}"] + arguments << body.to_json if [:post, :put, :patch].include?(method) + stub.send(*arguments) { |env| response } + end + end +end diff --git a/test/vultr/resources/account_test.rb b/test/vultr/resources/account_test.rb index afabc32..179cd71 100644 --- a/test/vultr/resources/account_test.rb +++ b/test/vultr/resources/account_test.rb @@ -2,8 +2,11 @@ class AccountResourceTest < Minitest::Test def test_info - end + stub = stub_request("account", response: stub_response(fixture: "account/info")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + account = client.account.info - def test_retrieve + assert_equal Vultr::Account, account.class + assert_equal "admin@example.com", account.email end end diff --git a/test/vultr/resources/users_test.rb b/test/vultr/resources/users_test.rb new file mode 100644 index 0000000..2d1ecab --- /dev/null +++ b/test/vultr/resources/users_test.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +require "test_helper" + +class UsersResourceTest < Minitest::Test + def test_list + stub = stub_request("users", response: stub_response(fixture: "users/list")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + users = client.users.list + + assert_equal Vultr::Collection, users.class + assert_equal Vultr::User, users.data.first.class + assert_equal 1, users.total + end + + def test_create + body = {name: "Test user", email: "user@example.com", password: "password"} + stub = stub_request("users", method: :post, body: body, response: stub_response(fixture: "users/create", status: 201)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + user = client.users.create(**body) + + assert_equal Vultr::User, user.class + assert_equal "user@example.com", user.email + end + + def test_retrieve + user_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("users/#{user_id}", response: stub_response(fixture: "users/retrieve")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + user = client.users.retrieve(user_id: user_id) + + assert_equal Vultr::User, user.class + assert_equal "user@example.com", user.email + end + + def test_update + user_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + body = {name: "Changed"} + stub = stub_request("users/#{user_id}", method: :patch, body: body, response: stub_response(fixture: "users/update")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.users.update(user_id: user_id, **body) + end + + def test_delete + user_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("users/#{user_id}", method: :delete, response: stub_response(fixture: "users/delete")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.users.delete(user_id: user_id) + end +end From 804b68a1ef80242eb8661ac661fb18f50181a5cc Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Thu, 19 Aug 2021 18:02:56 -0500 Subject: [PATCH 15/50] Update readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9b6abdb..99faa22 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ -# VULTR Rubygem +# Vultr API Rubygem [![Build Status](https://img.shields.io/travis/tolbkni/vultr.rb.svg)](https://travis-ci.org/tolbkni/vultr.rb) [![Gem Version](https://badge.fury.io/rb/vultr.svg)](https://badge.fury.io/rb/vultr) -The easiest and most complete rubygem for [VULTR](https://www.vultr.com) [API v2](https://www.vultr.com/api/v2). +The easiest and most complete rubygem for [Vultr](https://www.vultr.com). Currently supports [API v2](https://www.vultr.com/api/v2). ## Installation From 6a1f3d4beee5fd521d10913beecf3eae18e31fa6 Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Thu, 19 Aug 2021 18:03:12 -0500 Subject: [PATCH 16/50] Update badge and install instructions to point to fork --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 99faa22..abfd67b 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ # Vultr API Rubygem -[![Build Status](https://img.shields.io/travis/tolbkni/vultr.rb.svg)](https://travis-ci.org/tolbkni/vultr.rb) -[![Gem Version](https://badge.fury.io/rb/vultr.svg)](https://badge.fury.io/rb/vultr) +[![Build Status](https://github.com/excid3/vultr.rb/workflows/Tests/badge.svg)](https://github.com/excid3/vultr.rb/actions) The easiest and most complete rubygem for [Vultr](https://www.vultr.com). Currently supports [API v2](https://www.vultr.com/api/v2). @@ -9,7 +8,7 @@ The easiest and most complete rubygem for [Vultr](https://www.vultr.com). Curren Add this line to your application's Gemfile: - gem 'vultr' + gem 'vultr', github: "excid3/vultr.rb" And then execute: From 44dc6cb95dc362fd83feca53d56b5874249ef1b2 Mon Sep 17 00:00:00 2001 From: Leo Catane Date: Fri, 20 Aug 2021 08:02:37 +0800 Subject: [PATCH 17/50] Add tests for startup_scripts resource --- lib/vultr/resources/startup_scripts.rb | 2 +- test/fixtures/startup_scripts/create.json | 10 ++++ test/fixtures/startup_scripts/delete.json | 1 + test/fixtures/startup_scripts/list.json | 18 +++++++ test/fixtures/startup_scripts/retrieve.json | 10 ++++ test/fixtures/startup_scripts/update.json | 1 + test/vultr/resources/startup_scripts_test.rb | 50 ++++++++++++++++++++ 7 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/startup_scripts/create.json create mode 100644 test/fixtures/startup_scripts/delete.json create mode 100644 test/fixtures/startup_scripts/list.json create mode 100644 test/fixtures/startup_scripts/retrieve.json create mode 100644 test/fixtures/startup_scripts/update.json create mode 100644 test/vultr/resources/startup_scripts_test.rb diff --git a/lib/vultr/resources/startup_scripts.rb b/lib/vultr/resources/startup_scripts.rb index 36ef19e..4207f59 100644 --- a/lib/vultr/resources/startup_scripts.rb +++ b/lib/vultr/resources/startup_scripts.rb @@ -2,7 +2,7 @@ module Vultr class StartupScriptsResource < Resource def list(**params) response = get_request("startup-scripts", params: params) - Collection.from_response(response: response, key: "startup_scripts", type: StartupScript) + Collection.from_response(response, key: "startup_scripts", type: StartupScript) end def create(**attributes) diff --git a/test/fixtures/startup_scripts/create.json b/test/fixtures/startup_scripts/create.json new file mode 100644 index 0000000..e773fb0 --- /dev/null +++ b/test/fixtures/startup_scripts/create.json @@ -0,0 +1,10 @@ +{ + "startup_script": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "date_created": "2020-10-10T01:56:20+00:00", + "date_modified": "2020-10-10T01:56:20+00:00", + "name": "Example Startup Script", + "type": "pxe", + "script": "QmFzZTY0IEV4YW1wbGUgRGF0YQ==" + } +} diff --git a/test/fixtures/startup_scripts/delete.json b/test/fixtures/startup_scripts/delete.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/fixtures/startup_scripts/delete.json @@ -0,0 +1 @@ +{} diff --git a/test/fixtures/startup_scripts/list.json b/test/fixtures/startup_scripts/list.json new file mode 100644 index 0000000..4a73918 --- /dev/null +++ b/test/fixtures/startup_scripts/list.json @@ -0,0 +1,18 @@ +{ + "startup_scripts": [ + { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "date_created": "2020-10-10T01:56:20+00:00", + "date_modified": "2020-10-10T01:59:20+00:00", + "name": "Example Startup Script", + "type": "pxe" + } + ], + "meta": { + "total": 1, + "links": { + "next": "", + "prev": "" + } + } +} diff --git a/test/fixtures/startup_scripts/retrieve.json b/test/fixtures/startup_scripts/retrieve.json new file mode 100644 index 0000000..4760cc9 --- /dev/null +++ b/test/fixtures/startup_scripts/retrieve.json @@ -0,0 +1,10 @@ +{ + "startup_script": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "date_created": "2020-10-10T01:56:20+00:00", + "date_modified": "2020-10-10T01:59:20+00:00", + "name": "Example Startup Script", + "type": "pxe", + "script": "QmFzZTY0IEV4YW1wbGUgRGF0YQ==" + } +} diff --git a/test/fixtures/startup_scripts/update.json b/test/fixtures/startup_scripts/update.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/fixtures/startup_scripts/update.json @@ -0,0 +1 @@ +{} diff --git a/test/vultr/resources/startup_scripts_test.rb b/test/vultr/resources/startup_scripts_test.rb new file mode 100644 index 0000000..88044d3 --- /dev/null +++ b/test/vultr/resources/startup_scripts_test.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +require "test_helper" + +class StartupScriptsResourceTest < Minitest::Test + def test_list + stub = stub_request("startup-scripts", response: stub_response(fixture: "startup_scripts/list")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + startup_scripts = client.startup_scripts.list + + assert_equal Vultr::Collection, startup_scripts.class + assert_equal Vultr::StartupScript, startup_scripts.data.first.class + assert_equal 1, startup_scripts.total + end + + def test_create + body = {name: "Example Startup Script", type: "pxe", script: "QmFzZTY0IEV4YW1wbGUgRGF0YQ=="} + stub = stub_request("startup-scripts", method: :post, body: body, response: stub_response(fixture: "startup_scripts/create", status: 201)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + startup_script = client.startup_scripts.create(**body) + + assert_equal Vultr::StartupScript, startup_script.class + assert_equal "Example Startup Script", startup_script.name + end + + def test_restrieve + startup_script_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("startup-scripts/#{startup_script_id}", response: stub_response(fixture: "startup_scripts/retrieve")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + startup_script = client.startup_scripts.retrieve(startup_script_id: startup_script_id) + + assert_equal Vultr::StartupScript, startup_script.class + assert_equal "Example Startup Script", startup_script.name + end + + def test_update + startup_script_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + body = {name: "Changed"} + stub = stub_request("startup-scripts/#{startup_script_id}", method: :patch, body: body, response: stub_response(fixture: "startup_scripts/update")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.startup_scripts.update(startup_script_id: startup_script_id, **body) + end + + def test_delete + startup_script_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("startup-scripts/#{startup_script_id}", method: :delete, response: stub_response(fixture: "startup_scripts/delete")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.startup_scripts.delete(startup_script_id: startup_script_id) + end +end From 3b34dc02e450b63a171d9f41b25c15acdc937398 Mon Sep 17 00:00:00 2001 From: Steve Polito Date: Fri, 20 Aug 2021 08:21:50 -0400 Subject: [PATCH 18/50] Add fixture. https://www.vultr.com/api/#operation/list-applications --- test/fixtures/applications/list.json | 29 ++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 test/fixtures/applications/list.json diff --git a/test/fixtures/applications/list.json b/test/fixtures/applications/list.json new file mode 100644 index 0000000..9e19563 --- /dev/null +++ b/test/fixtures/applications/list.json @@ -0,0 +1,29 @@ +{ + "applications": [ + { + "id": 1, + "name": "LEMP", + "short_name": "lemp", + "deploy_name": "LEMP on CentOS 6 x64", + "type": "one-click", + "vendor": "vultr", + "image_id": "" + }, + { + "id": 1028, + "name": "OpenLiteSpeed WordPress", + "short_name": "openlitespeedwordpress", + "deploy_name": "OpenLiteSpeed WordPress on Ubuntu 20.04 x64", + "type": "marketplace", + "vendor": "LiteSpeed_Technologies", + "image_id": "openlitespeed-wordpress" + } + ], + "meta": { + "total": 2, + "links": { + "next": "", + "prev": "" + } + } +} \ No newline at end of file From 1e36589de2ecca66e782fc582eae7cf9910126ef Mon Sep 17 00:00:00 2001 From: Steve Polito Date: Fri, 20 Aug 2021 08:28:39 -0400 Subject: [PATCH 19/50] Write tests for Applications Resource. Simple test for the list endpoint inspired by the existing test created for the Users Resources list endpoint. --- test/vultr/resources/applications_test.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 test/vultr/resources/applications_test.rb diff --git a/test/vultr/resources/applications_test.rb b/test/vultr/resources/applications_test.rb new file mode 100644 index 0000000..cb971b4 --- /dev/null +++ b/test/vultr/resources/applications_test.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require "test_helper" + +class ApplicationsResourceTest < Minitest::Test + def test_list + stub = stub_request("applications", response: stub_response(fixture: "applications/list")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + applications = client.applications.list + + assert_equal Vultr::Collection, applications.class + assert_equal Vultr::Application, applications.data.first.class + assert_equal 2, applications.total + end +end From 29b7685412138041679133e9762f6b94371a81c2 Mon Sep 17 00:00:00 2001 From: Steve Polito Date: Fri, 20 Aug 2021 09:17:08 -0400 Subject: [PATCH 20/50] Add fixture data. https://www.vultr.com/api/#operation/list-backups https://www.vultr.com/api/#operation/get-backup --- test/fixtures/backups/list.json | 18 ++++++++++++++++++ test/fixtures/backups/retrieve.json | 9 +++++++++ 2 files changed, 27 insertions(+) create mode 100644 test/fixtures/backups/list.json create mode 100644 test/fixtures/backups/retrieve.json diff --git a/test/fixtures/backups/list.json b/test/fixtures/backups/list.json new file mode 100644 index 0000000..901ccf5 --- /dev/null +++ b/test/fixtures/backups/list.json @@ -0,0 +1,18 @@ +{ + "backups": [ + { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "date_created": "2020-10-10T01:56:20+00:00", + "description": "Example Automatic Backup", + "size": 10000000, + "status": "complete" + } + ], + "meta": { + "total": 1, + "links": { + "next": "", + "prev": "" + } + } +} \ No newline at end of file diff --git a/test/fixtures/backups/retrieve.json b/test/fixtures/backups/retrieve.json new file mode 100644 index 0000000..52eb20b --- /dev/null +++ b/test/fixtures/backups/retrieve.json @@ -0,0 +1,9 @@ +{ + "backup": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "date_created": "2020-10-10T01:56:20+00:00", + "description": "Example Description", + "size": 10000000, + "status": "complete" + } +} \ No newline at end of file From 23acb4d402e9cfc65adbc9c3d128d80573640f53 Mon Sep 17 00:00:00 2001 From: Steve Polito Date: Fri, 20 Aug 2021 09:21:40 -0400 Subject: [PATCH 21/50] Write tests for Backups Resource Simple tests for the list and retrieve endpoints inspired by the existing test created for the Users Resources list and retrieve endpoints. --- test/vultr/resources/backups_test.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 test/vultr/resources/backups_test.rb diff --git a/test/vultr/resources/backups_test.rb b/test/vultr/resources/backups_test.rb new file mode 100644 index 0000000..34dd416 --- /dev/null +++ b/test/vultr/resources/backups_test.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require "test_helper" + +class BackupsResourceTest < Minitest::Test + def test_list + stub = stub_request("backups", response: stub_response(fixture: "backups/list")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + backups = client.backups.list + assert_equal Vultr::Collection, backups.class + assert_equal Vultr::Backup, backups.data.first.class + assert_equal 1, backups.total + end + + def test_retrieve + backup_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("backups/#{backup_id}", response: stub_response(fixture: "backups/retrieve")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + backup = client.backups.retrieve(backup_id: backup_id) + + assert_equal Vultr::Backup, backup.class + assert_equal "complete", backup.status + end +end From ebc15fb98cbdf2700aa0cf0115d2e99bc423bcd5 Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Fri, 20 Aug 2021 18:41:18 -0500 Subject: [PATCH 22/50] Fix: remove string variable --- lib/vultr/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vultr/version.rb b/lib/vultr/version.rb index 070c215..41578e1 100644 --- a/lib/vultr/version.rb +++ b/lib/vultr/version.rb @@ -5,6 +5,6 @@ module VERSION TINY = 0 PRE = nil - STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".") + [MAJOR, MINOR, TINY, PRE].compact.join(".") end end From 50cb091ebf96c8d0194489005dc49726b80f40c2 Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Fri, 20 Aug 2021 19:48:53 -0500 Subject: [PATCH 23/50] add plans tests - [x] list - [x] list_metal --- test/fixtures/plans/list.json | 22 ++++++++++++++++++++++ test/fixtures/plans/list_metal.json | 24 ++++++++++++++++++++++++ test/vultr/resources/plans_test.rb | 25 +++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 test/fixtures/plans/list.json create mode 100644 test/fixtures/plans/list_metal.json create mode 100644 test/vultr/resources/plans_test.rb diff --git a/test/fixtures/plans/list.json b/test/fixtures/plans/list.json new file mode 100644 index 0000000..c817f34 --- /dev/null +++ b/test/fixtures/plans/list.json @@ -0,0 +1,22 @@ +{ + "plans": [ + { + "id": "vhf-8c-32gb", + "vcpu_count": 8, + "ram": 32768, + "disk": 512, + "disk_count": 1, + "bandwidth": 6144, + "monthly_cost": 192, + "type": "vhf", + "locations": ["sea"] + } + ], + "meta": { + "total": 1, + "links": { + "next": "next_id", + "prev": "prev_id" + } + } +} diff --git a/test/fixtures/plans/list_metal.json b/test/fixtures/plans/list_metal.json new file mode 100644 index 0000000..e1f2cf8 --- /dev/null +++ b/test/fixtures/plans/list_metal.json @@ -0,0 +1,24 @@ +{ + "plans_metal": [ + { + "id": "vbm-4c-32gb", + "cpu_count": 4, + "cpu_threads": 8, + "cpu_model": "E3-1270v6", + "ram": 32768, + "disk": 240, + "disk_count": 2, + "bandwidth": 5120, + "monthly_cost": 300, + "type": "SSD", + "locations": ["ewr"] + } + ], + "meta": { + "total": 1, + "links": { + "next": "next_id", + "prev": "prev_id" + } + } +} diff --git a/test/vultr/resources/plans_test.rb b/test/vultr/resources/plans_test.rb new file mode 100644 index 0000000..7321c1d --- /dev/null +++ b/test/vultr/resources/plans_test.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require "test_helper" + +class PlansResourceTest < Minitest::Test + def test_list + stub = stub_request("plans", response: stub_response(fixture: "plans/list")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + plans = client.plans.list + + assert_equal Vultr::Collection, plans.class + assert_equal Vultr::Plan, plans.data.first.class + assert_equal 1, plans.total + end + + def test_list_metal + stub = stub_request("plans-metal", response: stub_response(fixture: "plans/list_metal")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + plans = client.plans.list_metal + + assert_equal Vultr::Collection, plans.class + assert_equal Vultr::Plan, plans.data.first.class + assert_equal 1, plans.total + end +end From a4e4b1f93e4d4d5b2daea6d97f71c2b829441c1e Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Sat, 21 Aug 2021 09:27:16 -0500 Subject: [PATCH 24/50] Update version.rb --- lib/vultr/version.rb | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/lib/vultr/version.rb b/lib/vultr/version.rb index 41578e1..1e56dd2 100644 --- a/lib/vultr/version.rb +++ b/lib/vultr/version.rb @@ -1,10 +1,3 @@ module Vultr - module VERSION - MAJOR = 2 - MINOR = 0 - TINY = 0 - PRE = nil - - [MAJOR, MINOR, TINY, PRE].compact.join(".") - end + VERSION = "2.0.0" end From f897b805cd45b59ce9c1b9b7807c442ebc2abbeb Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Sat, 21 Aug 2021 09:31:01 -0500 Subject: [PATCH 25/50] add DNS tests (#16) * add dns test references #3 * Update dns_test.rb Co-authored-by: Chris Oliver --- lib/vultr/resources/dns.rb | 4 +- test/fixtures/domains/create.json | 6 ++ test/fixtures/domains/dnssec.json | 7 ++ test/fixtures/domains/list.json | 15 ++++ test/fixtures/domains/record.json | 10 +++ test/fixtures/domains/records.json | 19 ++++ test/fixtures/domains/retrieve.json | 6 ++ test/fixtures/domains/soa.json | 6 ++ test/vultr/resources/dns_test.rb | 134 ++++++++++++++++++++++++++++ 9 files changed, 205 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/domains/create.json create mode 100644 test/fixtures/domains/dnssec.json create mode 100644 test/fixtures/domains/list.json create mode 100644 test/fixtures/domains/record.json create mode 100644 test/fixtures/domains/records.json create mode 100644 test/fixtures/domains/retrieve.json create mode 100644 test/fixtures/domains/soa.json create mode 100644 test/vultr/resources/dns_test.rb diff --git a/lib/vultr/resources/dns.rb b/lib/vultr/resources/dns.rb index a45581f..c2de413 100644 --- a/lib/vultr/resources/dns.rb +++ b/lib/vultr/resources/dns.rb @@ -14,7 +14,7 @@ def retrieve(dns_domain:) end def update(dns_domain:, **attributes) - patch_request("domains/#{dns_domain}", body: attributes) + put_request("domains/#{dns_domain}", body: attributes) end def delete(dns_domain:) @@ -26,7 +26,7 @@ def soa(dns_domain:) end def update_soa(dns_domain:, **attributes) - post_request("domains/#{dns_domain}/soa", body: attributes) + patch_request("domains/#{dns_domain}/soa", body: attributes) end def dnssec(dns_domain:) diff --git a/test/fixtures/domains/create.json b/test/fixtures/domains/create.json new file mode 100644 index 0000000..ef288cd --- /dev/null +++ b/test/fixtures/domains/create.json @@ -0,0 +1,6 @@ +{ + "domain": { + "domain": "example.com", + "date_created": "2020-10-10T01:56:20+00:00" + } +} diff --git a/test/fixtures/domains/dnssec.json b/test/fixtures/domains/dnssec.json new file mode 100644 index 0000000..8203c01 --- /dev/null +++ b/test/fixtures/domains/dnssec.json @@ -0,0 +1,7 @@ +{ + "dns_sec": [ + "example.com IN DNSKEY 257 3 13 kRrxANp7YTGqVbaWtMy8hhsK0jcG4ajjICZKMb4fKv79Vx/RSn76vNjzIT7/Uo0BXil01Fk8RRQc4nWZctGJBA==", + "example.com IN DS 27933 13 1 2d9ac457e5c11a104e25d971d0a6254562bddde7", + "example.com IN DS 27933 13 2 8858e7b0dfb881280ce2ca1e0eafcd93d5b53687c21da284d4f8799ba82208a9" + ] +} diff --git a/test/fixtures/domains/list.json b/test/fixtures/domains/list.json new file mode 100644 index 0000000..ae8fad9 --- /dev/null +++ b/test/fixtures/domains/list.json @@ -0,0 +1,15 @@ +{ + "domains": [ + { + "domain": "example.com", + "date_created": "2020-10-10T01:56:20+00:00" + } + ], + "meta": { + "total": 1, + "links": { + "next": "next", + "prev": "prev" + } + } +} diff --git a/test/fixtures/domains/record.json b/test/fixtures/domains/record.json new file mode 100644 index 0000000..684d94e --- /dev/null +++ b/test/fixtures/domains/record.json @@ -0,0 +1,10 @@ +{ + "record": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "type": "A", + "name": "www", + "data": "192.0.2.123", + "priority": 0, + "ttl": 300 + } +} diff --git a/test/fixtures/domains/records.json b/test/fixtures/domains/records.json new file mode 100644 index 0000000..ebf75f7 --- /dev/null +++ b/test/fixtures/domains/records.json @@ -0,0 +1,19 @@ +{ + "records": [ + { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "type": "A", + "name": "foo.example.com", + "data": "192.0.2.123", + "priority": 0, + "ttl": 300 + } + ], + "meta": { + "total": 1, + "links": { + "next": "next", + "prev": "prev" + } + } +} diff --git a/test/fixtures/domains/retrieve.json b/test/fixtures/domains/retrieve.json new file mode 100644 index 0000000..ef288cd --- /dev/null +++ b/test/fixtures/domains/retrieve.json @@ -0,0 +1,6 @@ +{ + "domain": { + "domain": "example.com", + "date_created": "2020-10-10T01:56:20+00:00" + } +} diff --git a/test/fixtures/domains/soa.json b/test/fixtures/domains/soa.json new file mode 100644 index 0000000..3fd5ae9 --- /dev/null +++ b/test/fixtures/domains/soa.json @@ -0,0 +1,6 @@ +{ + "dns_soa": { + "nsprimary": "ns1.vultr.com", + "email": "admin@example.com" + } +} diff --git a/test/vultr/resources/dns_test.rb b/test/vultr/resources/dns_test.rb new file mode 100644 index 0000000..749d21e --- /dev/null +++ b/test/vultr/resources/dns_test.rb @@ -0,0 +1,134 @@ +# frozen_string_literal: true + +require "test_helper" + +class DnsResourceTest < Minitest::Test + def test_list + stub = stub_request("domains", response: stub_response(fixture: "domains/list")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + dns = client.dns.list + + assert_equal Vultr::Collection, dns.class + assert_equal Vultr::Domain, dns.data.first.class + assert_equal 1, dns.total + assert_equal "next", dns.next_cursor + assert_equal "prev", dns.prev_cursor + end + + def test_create + body = {domain: "example.com", ip: "192.0.2.123", dns_sec: :enabled} + stub = stub_request("domains", method: :post, body: body, response: stub_response(fixture: "domains/create", status: 201)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + dns = client.dns.create(**body) + + assert_equal "example.com", dns.domain + assert_equal "2020-10-10T01:56:20+00:00", dns.date_created + end + + def test_retrieve + dns_domain = "example.com" + stub = stub_request("domains/#{dns_domain}", response: stub_response(fixture: "domains/retrieve")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + dns = client.dns.retrieve(dns_domain: dns_domain) + + assert_equal "example.com", dns.domain + assert_equal "2020-10-10T01:56:20+00:00", dns.date_created + end + + def test_update + dns_domain = "example.com" + body = {dns_sec: "enabled"} + stub = stub_request("domains/#{dns_domain}", method: :put, body: body, response: {}) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + assert client.dns.update(dns_domain: dns_domain, **body) + end + + def test_delete + dns_domain = "example.com" + stub = stub_request("domains/#{dns_domain}", method: :delete, response: {}) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + assert client.dns.delete(dns_domain: dns_domain) + end + + def test_soa + dns_domain = "example.com" + stub = stub_request("domains/#{dns_domain}/soa", response: stub_response(fixture: "domains/soa")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + soa = client.dns.soa(dns_domain: dns_domain) + + assert_equal "ns1.vultr.com", soa.nsprimary + assert_equal "admin@example.com", soa.email + end + + def test_update_soa + dns_domain = "example.com" + body = {nsprimary: "ns1.vultr.com", email: "admin@example.com"} + stub = stub_request("domains/#{dns_domain}/soa", method: :patch, body: body, response: {}) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + assert client.dns.update_soa(dns_domain: dns_domain, **body) + end + + def test_dnssec + dns_domain = "example.com" + stub = stub_request("domains/#{dns_domain}/dnssec", response: stub_response(fixture: "domains/dnssec")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.dns.dnssec(dns_domain: dns_domain) + end + + def test_list_records + dns_domain = "example.com" + stub = stub_request("domains/#{dns_domain}/records", response: stub_response(fixture: "domains/records")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + dns = client.dns.list_records(dns_domain: dns_domain) + + assert_equal Vultr::Collection, dns.class + assert_equal Vultr::Object, dns.data.first.class + end + + def test_create_record + dns_domain = "example.com" + body = {name: "www", type: "A", data: "192.0.2.123", ttl: 300, priority: 0} + stub = stub_request("domains/#{dns_domain}/records", method: :post, body: body, response: stub_response(fixture: "domains/record", status: 201)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + dns = client.dns.create_record(dns_domain: dns_domain, **body) + + assert dns.id + assert "www", dns.name + assert "A", dns.type + assert "192.0.2.123", dns.data + assert 300, dns.ttl + assert 0, dns.priority + end + + def test_retrieve_record + dns_domain = "example.com" + record_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("domains/#{dns_domain}/records/#{record_id}", response: stub_response(fixture: "domains/record")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + dns = client.dns.retrieve_record(dns_domain: dns_domain, record_id: record_id) + + assert_equal record_id, dns.id + end + + def test_update_record + dns_domain = "example.com" + record_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + body = {name: "CNAME", data: "foo.example.com", ttl: 300, priority: 0} + stub = stub_request("domains/#{dns_domain}/records/#{record_id}", method: :patch, body: body, response: {}) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + assert client.dns.update_record(dns_domain: dns_domain, record_id: record_id, **body) + end + + def test_delete_record + dns_domain = "example.com" + record_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("domains/#{dns_domain}/records/#{record_id}", method: :delete, response: {}) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + assert client.dns.delete_record(dns_domain: dns_domain, record_id: record_id) + end +end From 13b6351c0bde0585cc7649f64cb357fc95ec7b45 Mon Sep 17 00:00:00 2001 From: Leo Anthony Catane Date: Sat, 21 Aug 2021 22:32:35 +0800 Subject: [PATCH 26/50] Add tests for ssh_keys resource (#11) --- lib/vultr/resources/ssh_keys.rb | 2 +- test/fixtures/ssh_keys/create.json | 8 +++++ test/fixtures/ssh_keys/delete.json | 1 + test/fixtures/ssh_keys/list.json | 17 +++++++++ test/fixtures/ssh_keys/retrieve.json | 8 +++++ test/fixtures/ssh_keys/update.json | 1 + test/vultr/resources/ssh_keys_test.rb | 50 +++++++++++++++++++++++++++ 7 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/ssh_keys/create.json create mode 100644 test/fixtures/ssh_keys/delete.json create mode 100644 test/fixtures/ssh_keys/list.json create mode 100644 test/fixtures/ssh_keys/retrieve.json create mode 100644 test/fixtures/ssh_keys/update.json create mode 100644 test/vultr/resources/ssh_keys_test.rb diff --git a/lib/vultr/resources/ssh_keys.rb b/lib/vultr/resources/ssh_keys.rb index c3e23fb..8df4f00 100644 --- a/lib/vultr/resources/ssh_keys.rb +++ b/lib/vultr/resources/ssh_keys.rb @@ -2,7 +2,7 @@ module Vultr class SshKeysResource < Resource def list(**params) response = get_request("ssh-keys", params: params) - Collection.from_response(response: response, key: "ssh_keys", type: SshKey) + Collection.from_response(response, key: "ssh_keys", type: SshKey) end def create(**attributes) diff --git a/test/fixtures/ssh_keys/create.json b/test/fixtures/ssh_keys/create.json new file mode 100644 index 0000000..5b02677 --- /dev/null +++ b/test/fixtures/ssh_keys/create.json @@ -0,0 +1,8 @@ +{ + "ssh_key": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "date_created": "2020-10-10T01:56:20+00:00", + "name": "Example SSH Key", + "ssh_key": "ssh-rsa AA... user@example.com" + } +} diff --git a/test/fixtures/ssh_keys/delete.json b/test/fixtures/ssh_keys/delete.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/fixtures/ssh_keys/delete.json @@ -0,0 +1 @@ +{} diff --git a/test/fixtures/ssh_keys/list.json b/test/fixtures/ssh_keys/list.json new file mode 100644 index 0000000..7204f8f --- /dev/null +++ b/test/fixtures/ssh_keys/list.json @@ -0,0 +1,17 @@ +{ + "ssh_keys": [ + { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "date_created": "2020-10-10T01:56:20+00:00", + "name": "Example SSH Key", + "ssh_key": "ssh-rsa AA... user@example.com" + } + ], + "meta": { + "total": 1, + "links": { + "next": "", + "prev": "" + } + } +} diff --git a/test/fixtures/ssh_keys/retrieve.json b/test/fixtures/ssh_keys/retrieve.json new file mode 100644 index 0000000..dfa777d --- /dev/null +++ b/test/fixtures/ssh_keys/retrieve.json @@ -0,0 +1,8 @@ +{ + "ssh_key": { + "id": "3b8066a7-b438-455a-9688-44afc9a3597f", + "date_created": "2020-10-10T01:56:20+00:00", + "name": "Example SSH Key", + "ssh_key": "ssh-rsa AA... user@example.com" + } +} diff --git a/test/fixtures/ssh_keys/update.json b/test/fixtures/ssh_keys/update.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/fixtures/ssh_keys/update.json @@ -0,0 +1 @@ +{} diff --git a/test/vultr/resources/ssh_keys_test.rb b/test/vultr/resources/ssh_keys_test.rb new file mode 100644 index 0000000..7a4296b --- /dev/null +++ b/test/vultr/resources/ssh_keys_test.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +require "test_helper" + +class SshKeysResourceTest < Minitest::Test + def test_list + stub = stub_request("ssh-keys", response: stub_response(fixture: "ssh_keys/list")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + ssh_keys = client.ssh_keys.list + + assert_equal Vultr::Collection, ssh_keys.class + assert_equal Vultr::SshKey, ssh_keys.data.first.class + assert_equal 1, ssh_keys.total + end + + def test_create + body = {name: "Example SSH Key", ssh_key: "ssh-rsa AA... user@example.com"} + stub = stub_request("ssh-keys", method: :post, body: body, response: stub_response(fixture: "ssh_keys/create", status: 201)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + ssh_key = client.ssh_keys.create(**body) + + assert_equal Vultr::SshKey, ssh_key.class + assert_equal "Example SSH Key", ssh_key.name + end + + def test_restrieve + ssh_key_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("ssh-keys/#{ssh_key_id}", response: stub_response(fixture: "ssh_keys/retrieve")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + ssh_key = client.ssh_keys.retrieve(ssh_key_id: ssh_key_id) + + assert_equal Vultr::SshKey, ssh_key.class + assert_equal "Example SSH Key", ssh_key.name + end + + def test_update + ssh_key_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + body = {name: "Changed"} + stub = stub_request("ssh-keys/#{ssh_key_id}", method: :patch, body: body, response: stub_response(fixture: "ssh_keys/update")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.ssh_keys.update(ssh_key_id: ssh_key_id, **body) + end + + def test_delete + ssh_key_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("ssh-keys/#{ssh_key_id}", method: :delete, response: stub_response(fixture: "ssh_keys/delete")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.ssh_keys.delete(ssh_key_id: ssh_key_id) + end +end From dfcc58079f85d54931b6f0867874389117390b21 Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Sat, 21 Aug 2021 09:34:39 -0500 Subject: [PATCH 27/50] Update gemspec to match --- vultr.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vultr.gemspec b/vultr.gemspec index 6e475f8..ae43f6d 100644 --- a/vultr.gemspec +++ b/vultr.gemspec @@ -6,7 +6,7 @@ require "vultr/version" Gem::Specification.new do |s| s.name = "vultr" - s.version = Vultr::VERSION::STRING + s.version = Vultr::VERSION s.authors = ["Gao Jiangmiao"] s.email = ["tolbkni@gmail.com"] s.summary = "Ruby bindings for VULTR API." From d193372469be5e8e799bfbb718d5a0dbb486bb46 Mon Sep 17 00:00:00 2001 From: Alejandro Ventura Date: Sat, 21 Aug 2021 16:24:03 -0500 Subject: [PATCH 28/50] Add tests for operating_systems section. (#17) --- test/fixtures/operating_systems/list.json | 17 +++++++++++++++++ test/vultr/resources/operating_systems_test.rb | 15 +++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 test/fixtures/operating_systems/list.json create mode 100644 test/vultr/resources/operating_systems_test.rb diff --git a/test/fixtures/operating_systems/list.json b/test/fixtures/operating_systems/list.json new file mode 100644 index 0000000..101f53c --- /dev/null +++ b/test/fixtures/operating_systems/list.json @@ -0,0 +1,17 @@ +{ + "os": [ + { + "id": 127, + "name": "CentOS 6 x64", + "arch": "x64", + "family": "centos" + } + ], + "meta": { + "total": 1, + "links": { + "next": "", + "prev": "" + } + } +} diff --git a/test/vultr/resources/operating_systems_test.rb b/test/vultr/resources/operating_systems_test.rb new file mode 100644 index 0000000..12b4811 --- /dev/null +++ b/test/vultr/resources/operating_systems_test.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require "test_helper" + +class OperatingSystemsResourceTest < Minitest::Test + def test_list + stub = stub_request("os", response: stub_response(fixture: "operating_systems/list")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + operating_systems = client.operating_systems.list + + assert_equal Vultr::Collection, operating_systems.class + assert_equal Vultr::OperatingSystem, operating_systems.data.first.class + assert_equal 1, operating_systems.total + end +end From abba4ae8f16e4757d8adb890ece1c5cb727bbd18 Mon Sep 17 00:00:00 2001 From: Alejandro Ventura Date: Sat, 21 Aug 2021 16:24:37 -0500 Subject: [PATCH 29/50] Add tests for Regions resource. (#18) --- test/fixtures/regions/list.json | 20 +++++++++++++++ test/fixtures/regions/list_availability.json | 24 ++++++++++++++++++ test/vultr/resources/regions_test.rb | 26 ++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 test/fixtures/regions/list.json create mode 100644 test/fixtures/regions/list_availability.json create mode 100644 test/vultr/resources/regions_test.rb diff --git a/test/fixtures/regions/list.json b/test/fixtures/regions/list.json new file mode 100644 index 0000000..8f86452 --- /dev/null +++ b/test/fixtures/regions/list.json @@ -0,0 +1,20 @@ +{ + "regions": [ + { + "id": "ams", + "city": "Amsterdam", + "country": "NL", + "continent": "Europe", + "options": [ + "ddos_protection" + ] + } + ], + "meta": { + "total": 1, + "links": { + "next": "", + "prev": "" + } + } +} diff --git a/test/fixtures/regions/list_availability.json b/test/fixtures/regions/list_availability.json new file mode 100644 index 0000000..aa229c4 --- /dev/null +++ b/test/fixtures/regions/list_availability.json @@ -0,0 +1,24 @@ +{ + "available_plans": [ + "vc2-1c-1gb", + "vc2-1c-2gb", + "vc2-2c-4gb", + "vc2-4c-8gb", + "vc2-6c-16gb", + "vc2-8c-32gb", + "vc2-16c-64gb", + "vc2-24c-96gb", + "vdc-4vcpu-8gb", + "vdc-4vcpu-16gb", + "vdc-6vcpu-24gb", + "vdc-8vcpu-32gb", + "vhf-1c-1gb", + "vhf-1c-2gb", + "vhf-2c-4gb", + "vhf-3c-8gb", + "vhf-4c-16gb", + "vhf-6c-24gb", + "vhf-8c-32gb", + "vhf-12c-48gb" + ] +} diff --git a/test/vultr/resources/regions_test.rb b/test/vultr/resources/regions_test.rb new file mode 100644 index 0000000..e7538aa --- /dev/null +++ b/test/vultr/resources/regions_test.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require "test_helper" + +class RegionsResourceTest < Minitest::Test + def test_list + stub = stub_request("regions", response: stub_response(fixture: "regions/list")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + regions = client.regions.list + + assert_equal Vultr::Collection, regions.class + assert_equal Vultr::Region, regions.data.first.class + assert_equal 1, regions.total + end + + def test_list_availability + region_id = "all" + stub = stub_request("regions/#{region_id}/availability", response: stub_response(fixture: "regions/list_availability")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + availabilities = client.regions.list_availability(region_id: region_id) + + assert_equal Vultr::Object, availabilities.class + assert_equal availabilities.available_plans.first, "vc2-1c-1gb" + assert_equal availabilities.available_plans.size, 20 + end +end From a227af1c6213823276fd51046628222c54b36102 Mon Sep 17 00:00:00 2001 From: Leo Anthony Catane Date: Mon, 23 Aug 2021 21:24:39 +0800 Subject: [PATCH 30/50] Add tests for snapshots resource (#19) --- test/fixtures/snapshots/create.json | 11 ++++ test/fixtures/snapshots/create_from_url.json | 11 ++++ test/fixtures/snapshots/delete.json | 1 + test/fixtures/snapshots/list.json | 20 +++++++ test/fixtures/snapshots/retrieve.json | 11 ++++ test/fixtures/snapshots/update.json | 1 + test/vultr/resources/snapshots_test.rb | 60 ++++++++++++++++++++ 7 files changed, 115 insertions(+) create mode 100644 test/fixtures/snapshots/create.json create mode 100644 test/fixtures/snapshots/create_from_url.json create mode 100644 test/fixtures/snapshots/delete.json create mode 100644 test/fixtures/snapshots/list.json create mode 100644 test/fixtures/snapshots/retrieve.json create mode 100644 test/fixtures/snapshots/update.json create mode 100644 test/vultr/resources/snapshots_test.rb diff --git a/test/fixtures/snapshots/create.json b/test/fixtures/snapshots/create.json new file mode 100644 index 0000000..87a2850 --- /dev/null +++ b/test/fixtures/snapshots/create.json @@ -0,0 +1,11 @@ +{ + "snapshot": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "date_created": "2020-10-10T01:56:20+00:00", + "description": "Example Snapshot", + "size": 42949672960, + "status": "complete", + "os_id": 215, + "app_id": 0 + } +} diff --git a/test/fixtures/snapshots/create_from_url.json b/test/fixtures/snapshots/create_from_url.json new file mode 100644 index 0000000..590610a --- /dev/null +++ b/test/fixtures/snapshots/create_from_url.json @@ -0,0 +1,11 @@ +{ + "snapshot": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "date_created": "2020-10-10T01:56:20+00:00", + "description": "Example Snapshot", + "size": 0, + "status": "pending", + "os_id": 215, + "app_id": 0 + } +} diff --git a/test/fixtures/snapshots/delete.json b/test/fixtures/snapshots/delete.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/fixtures/snapshots/delete.json @@ -0,0 +1 @@ +{} diff --git a/test/fixtures/snapshots/list.json b/test/fixtures/snapshots/list.json new file mode 100644 index 0000000..d6980e7 --- /dev/null +++ b/test/fixtures/snapshots/list.json @@ -0,0 +1,20 @@ +{ + "snapshots": [ + { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "date_created": "2020-10-10T01:56:20+00:00", + "description": "Example Snapshot", + "size": 42949672960, + "status": "complete", + "os_id": 215, + "app_id": 0 + } + ], + "meta": { + "total": 1, + "links": { + "next": "", + "prev": "" + } + } +} diff --git a/test/fixtures/snapshots/retrieve.json b/test/fixtures/snapshots/retrieve.json new file mode 100644 index 0000000..87a2850 --- /dev/null +++ b/test/fixtures/snapshots/retrieve.json @@ -0,0 +1,11 @@ +{ + "snapshot": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "date_created": "2020-10-10T01:56:20+00:00", + "description": "Example Snapshot", + "size": 42949672960, + "status": "complete", + "os_id": 215, + "app_id": 0 + } +} diff --git a/test/fixtures/snapshots/update.json b/test/fixtures/snapshots/update.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/fixtures/snapshots/update.json @@ -0,0 +1 @@ +{} diff --git a/test/vultr/resources/snapshots_test.rb b/test/vultr/resources/snapshots_test.rb new file mode 100644 index 0000000..6dd1894 --- /dev/null +++ b/test/vultr/resources/snapshots_test.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +require "test_helper" + +class SnapshotsResourceTest < Minitest::Test + def test_list + stub = stub_request("snapshots", response: stub_response(fixture: "snapshots/list")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + snapshots = client.snapshots.list + + assert_equal Vultr::Collection, snapshots.class + assert_equal Vultr::Snapshot, snapshots.data.first.class + assert_equal 1, snapshots.total + end + + def test_create + body = {instance_id: "cb676a46-66fd-4dfb-b839-443f2e6c0b60", description: "Example Snapshot"} + stub = stub_request("snapshots", method: :post, body: body, response: stub_response(fixture: "snapshots/create", status: 201)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + snapshot = client.snapshots.create(**body) + + assert_equal Vultr::Snapshot, snapshot.class + assert_equal "Example Snapshot", snapshot.description + end + + def test_retrieve + snapshot_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("snapshots/#{snapshot_id}", response: stub_response(fixture: "snapshots/retrieve")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + snapshot = client.snapshots.retrieve(snapshot_id: snapshot_id) + + assert_equal Vultr::Snapshot, snapshot.class + assert_equal "Example Snapshot", snapshot.description + end + + def test_create_from_url + body = {url: "http://example.com/disk_image.raw"} + stub = stub_request("snapshots/create-from-url", method: :post, body: body, response: stub_response(fixture: "snapshots/create_from_url")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + snapshot = client.snapshots.create_from_url(body[:url]) + + assert_equal Vultr::Snapshot, snapshot.class + assert_equal "Example Snapshot", snapshot.description + end + + def test_update + snapshot_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + body = {description: "Changed"} + stub = stub_request("snapshots/#{snapshot_id}", method: :put, body: body, response: stub_response(fixture: "snapshots/update")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.snapshots.update(snapshot_id: snapshot_id, **body) + end + + def test_delete + snapshot_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("snapshots/#{snapshot_id}", method: :delete, response: stub_response(fixture: "snapshots/delete")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.snapshots.delete(snapshot_id: snapshot_id) + end +end From 11cfe00036b0f93f1b03dffd2cecf06248455b48 Mon Sep 17 00:00:00 2001 From: Leo Anthony Catane Date: Tue, 24 Aug 2021 22:43:59 +0800 Subject: [PATCH 31/50] Add tests for reserved_ips resource (#20) --- lib/vultr/resources/reserved_ips.rb | 2 +- test/fixtures/reserved_ips/attach.json | 1 + test/fixtures/reserved_ips/convert.json | 11 ++++ test/fixtures/reserved_ips/create.json | 11 ++++ test/fixtures/reserved_ips/delete.json | 1 + test/fixtures/reserved_ips/detach.json | 1 + test/fixtures/reserved_ips/list.json | 29 ++++++++++ test/fixtures/reserved_ips/retrieve.json | 11 ++++ test/vultr/resources/reserved_ips_test.rb | 67 +++++++++++++++++++++++ 9 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/reserved_ips/attach.json create mode 100644 test/fixtures/reserved_ips/convert.json create mode 100644 test/fixtures/reserved_ips/create.json create mode 100644 test/fixtures/reserved_ips/delete.json create mode 100644 test/fixtures/reserved_ips/detach.json create mode 100644 test/fixtures/reserved_ips/list.json create mode 100644 test/fixtures/reserved_ips/retrieve.json create mode 100644 test/vultr/resources/reserved_ips_test.rb diff --git a/lib/vultr/resources/reserved_ips.rb b/lib/vultr/resources/reserved_ips.rb index 6f365bb..4f2454a 100644 --- a/lib/vultr/resources/reserved_ips.rb +++ b/lib/vultr/resources/reserved_ips.rb @@ -22,7 +22,7 @@ def attach(reserved_ip:, instance_id:) end def detach(reserved_ip:) - post_request("reserved-ips/#{reserved_ip}/detach") + post_request("reserved-ips/#{reserved_ip}/detach", body: {}) end def convert(**attributes) diff --git a/test/fixtures/reserved_ips/attach.json b/test/fixtures/reserved_ips/attach.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/fixtures/reserved_ips/attach.json @@ -0,0 +1 @@ +{} diff --git a/test/fixtures/reserved_ips/convert.json b/test/fixtures/reserved_ips/convert.json new file mode 100644 index 0000000..b6d69c6 --- /dev/null +++ b/test/fixtures/reserved_ips/convert.json @@ -0,0 +1,11 @@ +{ + "reserved_ip": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "region": "ewr", + "ip_type": "v4", + "subnet": "192.0.2.123", + "subnet_size": 64, + "label": "Example Reserved IPv4", + "instance_id": "3f26dfe9-6a18-4f3d-a543-0cbca7a3e496" + } +} diff --git a/test/fixtures/reserved_ips/create.json b/test/fixtures/reserved_ips/create.json new file mode 100644 index 0000000..cf5cf81 --- /dev/null +++ b/test/fixtures/reserved_ips/create.json @@ -0,0 +1,11 @@ +{ + "reserved_ip": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "region": "ewr", + "ip_type": "v4", + "subnet": "192.0.2.123", + "subnet_size": 32, + "label": "Example Reserved IPv4", + "instance_id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + } +} diff --git a/test/fixtures/reserved_ips/delete.json b/test/fixtures/reserved_ips/delete.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/fixtures/reserved_ips/delete.json @@ -0,0 +1 @@ +{} diff --git a/test/fixtures/reserved_ips/detach.json b/test/fixtures/reserved_ips/detach.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/fixtures/reserved_ips/detach.json @@ -0,0 +1 @@ +{} diff --git a/test/fixtures/reserved_ips/list.json b/test/fixtures/reserved_ips/list.json new file mode 100644 index 0000000..a22c358 --- /dev/null +++ b/test/fixtures/reserved_ips/list.json @@ -0,0 +1,29 @@ +{ + "reserved_ips": [ + { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "region": "ewr", + "ip_type": "v4", + "subnet": "192.0.2.123", + "subnet_size": 32, + "label": "Example Reserved IPv4", + "instance_id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + }, + { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "region": "ewr", + "ip_type": "v6", + "subnet": "2001:0db8:5:5157::", + "subnet_size": 64, + "label": "Example Reserved IPv6", + "instance_id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + } + ], + "meta": { + "total": 2, + "links": { + "next": "", + "prev": "" + } + } +} diff --git a/test/fixtures/reserved_ips/retrieve.json b/test/fixtures/reserved_ips/retrieve.json new file mode 100644 index 0000000..cf5cf81 --- /dev/null +++ b/test/fixtures/reserved_ips/retrieve.json @@ -0,0 +1,11 @@ +{ + "reserved_ip": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "region": "ewr", + "ip_type": "v4", + "subnet": "192.0.2.123", + "subnet_size": 32, + "label": "Example Reserved IPv4", + "instance_id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + } +} diff --git a/test/vultr/resources/reserved_ips_test.rb b/test/vultr/resources/reserved_ips_test.rb new file mode 100644 index 0000000..c3af385 --- /dev/null +++ b/test/vultr/resources/reserved_ips_test.rb @@ -0,0 +1,67 @@ +# frozen_string_literal: true + +require "test_helper" + +class ReservedIpsResourceTest < Minitest::Test + def test_list + stub = stub_request("reserved-ips", response: stub_response(fixture: "reserved_ips/list")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + reserved_ips = client.reserved_ips.list + + assert_equal Vultr::Collection, reserved_ips.class + assert_equal Vultr::ReservedIp, reserved_ips.data.first.class + assert_equal 2, reserved_ips.total + end + + def test_create + body = {region: "ewr", ip_type: "v4", label: "Example Reserved IPv4"} + stub = stub_request("reserved-ips", method: :post, body: body, response: stub_response(fixture: "reserved_ips/create", status: 201)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + reserved_ip = client.reserved_ips.create(**body) + + assert_equal Vultr::ReservedIp, reserved_ip.class + assert_equal "Example Reserved IPv4", reserved_ip.label + end + + def test_retrieve + reserved_ip_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("reserved-ips/#{reserved_ip_id}", response: stub_response(fixture: "reserved_ips/retrieve")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + reserved_ip = client.reserved_ips.retrieve(reserved_ip: reserved_ip_id) + + assert_equal Vultr::ReservedIp, reserved_ip.class + assert_equal "Example Reserved IPv4", reserved_ip.label + end + + def test_delete + reserved_ip_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("reserved-ips/#{reserved_ip_id}", method: :delete, response: stub_response(fixture: "reserved_ips/retrieve")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.reserved_ips.delete(reserved_ip: reserved_ip_id) + end + + def test_attach + reserved_ip_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + body = {instance_id: "3f26dfe9-6a18-4f3d-a543-0cbca7a3e496"} + stub = stub_request("reserved-ips/#{reserved_ip_id}/attach", method: :post, body: body, response: stub_response(fixture: "reserved_ips/attach")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.reserved_ips.attach(reserved_ip: reserved_ip_id, instance_id: body[:instance_id]) + end + + def test_detach + reserved_ip_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("reserved-ips/#{reserved_ip_id}/detach", method: :post, response: stub_response(fixture: "reserved_ips/attach")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.reserved_ips.detach(reserved_ip: reserved_ip_id) + end + + def test_convert + body = {ip_address: "192.0.2.123", label: "Example Reserved IPv4"} + stub = stub_request("reserved-ips/convert", method: :post, body: body, response: stub_response(fixture: "reserved_ips/convert", status: 201)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + reserved_ip = client.reserved_ips.convert(**body) + + assert Vultr::ReservedIp, reserved_ip.class + assert "Example Resreved IPv4", reserved_ip.label + end +end From c177c08a7c9e1de7164b164630ae64568220b93b Mon Sep 17 00:00:00 2001 From: Leo Anthony Catane Date: Wed, 25 Aug 2021 20:51:34 +0800 Subject: [PATCH 32/50] Add tests for object storage and private networks (#21) * Add tests for private_networks resource * Add tests for object_storage resource --- lib/vultr/resources/object_storage.rb | 6 +- test/fixtures/object_storage/create.json | 14 ++++ test/fixtures/object_storage/delete.json | 1 + test/fixtures/object_storage/list.json | 22 ++++++ .../object_storage/list_clusters.json | 17 +++++ .../object_storage/regenerate_keys.json | 7 ++ test/fixtures/object_storage/retrieve.json | 13 ++++ test/fixtures/object_storage/update.json | 1 + test/fixtures/private_networks/create.json | 10 +++ test/fixtures/private_networks/delete.json | 1 + test/fixtures/private_networks/list.json | 19 +++++ test/fixtures/private_networks/retrieve.json | 10 +++ test/fixtures/private_networks/update.json | 1 + test/vultr/resources/object_storage_test.rb | 71 +++++++++++++++++++ test/vultr/resources/private_networks_test.rb | 50 +++++++++++++ 15 files changed, 240 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/object_storage/create.json create mode 100644 test/fixtures/object_storage/delete.json create mode 100644 test/fixtures/object_storage/list.json create mode 100644 test/fixtures/object_storage/list_clusters.json create mode 100644 test/fixtures/object_storage/regenerate_keys.json create mode 100644 test/fixtures/object_storage/retrieve.json create mode 100644 test/fixtures/object_storage/update.json create mode 100644 test/fixtures/private_networks/create.json create mode 100644 test/fixtures/private_networks/delete.json create mode 100644 test/fixtures/private_networks/list.json create mode 100644 test/fixtures/private_networks/retrieve.json create mode 100644 test/fixtures/private_networks/update.json create mode 100644 test/vultr/resources/object_storage_test.rb create mode 100644 test/vultr/resources/private_networks_test.rb diff --git a/lib/vultr/resources/object_storage.rb b/lib/vultr/resources/object_storage.rb index 7620a77..9c5a700 100644 --- a/lib/vultr/resources/object_storage.rb +++ b/lib/vultr/resources/object_storage.rb @@ -2,7 +2,7 @@ module Vultr class ObjectStorageResource < Resource def list(**params) response = get_request("object-storage", params: params) - Collection.from_response(response: response, key: "object_storages", type: ObjectStorage) + Collection.from_response(response, key: "object_storages", type: ObjectStorage) end def create(**attributes) @@ -22,12 +22,12 @@ def delete(object_storage_id:) end def regenerate_keys(object_storage_id:) - Object.new post_request("object-storage/#{object_storage_id}/regenerate-keys").body + Object.new post_request("object-storage/#{object_storage_id}/regenerate-keys", body: {}).body end def list_clusters(**params) response = get_request("object-storage/clusters", params: params) - Collection.from_response(response: response, key: "clusters", type: Object) + Collection.from_response(response, key: "clusters", type: Object) end end end diff --git a/test/fixtures/object_storage/create.json b/test/fixtures/object_storage/create.json new file mode 100644 index 0000000..2bce3d2 --- /dev/null +++ b/test/fixtures/object_storage/create.json @@ -0,0 +1,14 @@ +{ + "object_storage": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "date_created": "2020-10-10T01:56:20+00:00", + "cluster_id": 2, + "region": "ewr", + "location": "New Jersey", + "label": "Example Object Storage", + "status": "pending", + "s3_hostname": "ewr1.vultrobjects.com", + "s3_access_key": "00example11223344", + "s3_secret_key": "00example1122334455667788990011" + } +} diff --git a/test/fixtures/object_storage/delete.json b/test/fixtures/object_storage/delete.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/fixtures/object_storage/delete.json @@ -0,0 +1 @@ +{} diff --git a/test/fixtures/object_storage/list.json b/test/fixtures/object_storage/list.json new file mode 100644 index 0000000..b1990c9 --- /dev/null +++ b/test/fixtures/object_storage/list.json @@ -0,0 +1,22 @@ +{ + "object_storages": [ + { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "date_created": "2020-10-10T01:56:20+00:00", + "cluster_id": 2, + "region": "ewr", + "label": "Example Object Storage", + "status": "active", + "s3_hostname": "ewr1.vultrobjects.com", + "s3_access_key": "00example11223344", + "s3_secret_key": "00example1122334455667788990011" + } + ], + "meta": { + "total": 1, + "links": { + "next": "", + "prev": "" + } + } +} diff --git a/test/fixtures/object_storage/list_clusters.json b/test/fixtures/object_storage/list_clusters.json new file mode 100644 index 0000000..62ddb68 --- /dev/null +++ b/test/fixtures/object_storage/list_clusters.json @@ -0,0 +1,17 @@ +{ + "clusters": [ + { + "id": 2, + "region": "ewr", + "hostname": "ewr1.vultrobjects.com", + "deploy": "yes" + } + ], + "meta": { + "total": 1, + "links": { + "next": "", + "prev": "" + } + } +} diff --git a/test/fixtures/object_storage/regenerate_keys.json b/test/fixtures/object_storage/regenerate_keys.json new file mode 100644 index 0000000..e162c2b --- /dev/null +++ b/test/fixtures/object_storage/regenerate_keys.json @@ -0,0 +1,7 @@ +{ + "s3_credentials": { + "s3_hostname": "ewr1.vultrobjects.com", + "s3_access_key": "00example11223344", + "s3_secret_key": "00example1122334455667788990011" + } +} diff --git a/test/fixtures/object_storage/retrieve.json b/test/fixtures/object_storage/retrieve.json new file mode 100644 index 0000000..3f4cd81 --- /dev/null +++ b/test/fixtures/object_storage/retrieve.json @@ -0,0 +1,13 @@ +{ + "object_storage": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "date_created": "2020-10-10T01:56:20+00:00", + "cluster_id": 2, + "region": "ewr", + "label": "Example Object Storage", + "status": "active", + "s3_hostname": "ewr1.vultrobjects.com", + "s3_access_key": "00example11223344", + "s3_secret_key": "00example1122334455667788990011" + } +} diff --git a/test/fixtures/object_storage/update.json b/test/fixtures/object_storage/update.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/fixtures/object_storage/update.json @@ -0,0 +1 @@ +{} diff --git a/test/fixtures/private_networks/create.json b/test/fixtures/private_networks/create.json new file mode 100644 index 0000000..9615321 --- /dev/null +++ b/test/fixtures/private_networks/create.json @@ -0,0 +1,10 @@ +{ + "network": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "date_created": "2020-10-10T01:56:20+00:00", + "region": "ewr", + "description": "Example Private Network", + "v4_subnet": "10.99.0.0", + "v4_subnet_mask": 24 + } +} diff --git a/test/fixtures/private_networks/delete.json b/test/fixtures/private_networks/delete.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/fixtures/private_networks/delete.json @@ -0,0 +1 @@ +{} diff --git a/test/fixtures/private_networks/list.json b/test/fixtures/private_networks/list.json new file mode 100644 index 0000000..f905ea2 --- /dev/null +++ b/test/fixtures/private_networks/list.json @@ -0,0 +1,19 @@ +{ + "networks": [ + { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "region": "ewr", + "date_created": "2020-10-10T01:56:20+00:00", + "description": "Example Network Description", + "v4_subnet": "10.99.0.0", + "v4_subnet_mask": 24 + } + ], + "meta": { + "total": 1, + "links": { + "next": "", + "prev": "" + } + } +} diff --git a/test/fixtures/private_networks/retrieve.json b/test/fixtures/private_networks/retrieve.json new file mode 100644 index 0000000..f33b4f4 --- /dev/null +++ b/test/fixtures/private_networks/retrieve.json @@ -0,0 +1,10 @@ +{ + "network": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "date_created": "2020-10-10T01:56:20+00:00", + "region": "ewr", + "description": "Example Network Description", + "v4_subnet": "10.99.0.0", + "v4_subnet_mask": 24 + } +} diff --git a/test/fixtures/private_networks/update.json b/test/fixtures/private_networks/update.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/fixtures/private_networks/update.json @@ -0,0 +1 @@ +{} diff --git a/test/vultr/resources/object_storage_test.rb b/test/vultr/resources/object_storage_test.rb new file mode 100644 index 0000000..4c6daf0 --- /dev/null +++ b/test/vultr/resources/object_storage_test.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require "test_helper" + +class ObjectStorageResourceTest < Minitest::Test + def test_list + stub = stub_request("object-storage", response: stub_response(fixture: "object_storage/list")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + object_storage = client.object_storage.list + + assert_equal Vultr::Collection, object_storage.class + assert_equal Vultr::ObjectStorage, object_storage.data.first.class + assert_equal 1, object_storage.total + end + + def test_create + body = {label: "Example Object Storage", cluster_id: 2} + stub = stub_request("object-storage", method: :post, body: body, response: stub_response(fixture: "object_storage/create")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + object_storage = client.object_storage.create(**body) + + assert_equal Vultr::ObjectStorage, object_storage.class + assert_equal "Example Object Storage", object_storage.label + end + + def test_retrieve + object_storage_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("object-storage/#{object_storage_id}", response: stub_response(fixture: "object_storage/retrieve")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + object_storage = client.object_storage.retrieve(object_storage_id: object_storage_id) + + assert_equal Vultr::ObjectStorage, object_storage.class + assert_equal "Example Object Storage", object_storage.label + end + + def test_update + object_storage_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + body = {label: "Updated Object Storage Label"} + stub = stub_request("object-storage/#{object_storage_id}", method: :put, body: body, response: stub_response(fixture: "object_storage/create")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.object_storage.update(object_storage_id: object_storage_id, **body) + end + + def test_delete + object_storage_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("object-storage/#{object_storage_id}", method: :delete, response: stub_response(fixture: "object_storage/delete")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.object_storage.delete(object_storage_id: object_storage_id) + end + + def test_regenerate_keys + object_storage_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("object-storage/#{object_storage_id}/regenerate-keys", method: :post, response: stub_response(fixture: "object_storage/regenerate_keys", status: 201)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + object_storage_keys = client.object_storage.regenerate_keys(object_storage_id: object_storage_id) + + assert_equal "ewr1.vultrobjects.com", object_storage_keys.dig("s3_credentials", "s3_hostname") + assert_equal "00example11223344", object_storage_keys.dig("s3_credentials", "s3_access_key") + assert_equal "00example1122334455667788990011", object_storage_keys.dig("s3_credentials", "s3_secret_key") + end + + def test_list_clusters + stub = stub_request("object-storage/clusters", response: stub_response(fixture: "object_storage/list_clusters")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + clusters = client.object_storage.list_clusters + + assert_equal Vultr::Collection, clusters.class + assert_equal Vultr::Object, clusters.data.first.class + assert_equal 1, clusters.total + end +end diff --git a/test/vultr/resources/private_networks_test.rb b/test/vultr/resources/private_networks_test.rb new file mode 100644 index 0000000..cdcc35a --- /dev/null +++ b/test/vultr/resources/private_networks_test.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +require "test_helper" + +class PrivateNetworksResourceTest < Minitest::Test + def test_list + stub = stub_request("private-networks", response: stub_response(fixture: "private_networks/list")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + private_networks = client.private_networks.list + + assert_equal Vultr::Collection, private_networks.class + assert_equal Vultr::PrivateNetwork, private_networks.data.first.class + assert_equal 1, private_networks.total + end + + def test_create + body = {region: "ewr", description: "Example Private Network", v4_subnet: "10.99.0.0", v4_subnet_mask: 24} + stub = stub_request("private-networks", method: :post, body: body, response: stub_response(fixture: "private_networks/create", status: 201)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + private_network = client.private_networks.create(**body) + + assert_equal Vultr::PrivateNetwork, private_network.class + assert_equal "Example Private Network", private_network.description + end + + def test_retrieve + private_network_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("private-networks/#{private_network_id}", response: stub_response(fixture: "private_networks/retrieve")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + private_network = client.private_networks.retrieve(network_id: private_network_id) + + assert_equal Vultr::PrivateNetwork, private_network.class + assert_equal "Example Network Description", private_network.description + end + + def test_update + private_network_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + body = {description: "Example Private Network"} + stub = stub_request("private-networks/#{private_network_id}", method: :put, body: body, response: stub_response(fixture: "private_networks/update")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.private_networks.update(network_id: private_network_id, **body) + end + + def test_delete + private_network_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("private-networks/#{private_network_id}", method: :delete, response: stub_response(fixture: "private_networks/delete")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.private_networks.delete(network_id: private_network_id) + end +end From 03d65836153ae6aebe60375baa446111011ff4ec Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Wed, 25 Aug 2021 08:36:33 -0500 Subject: [PATCH 33/50] Recursively convert to OpenStruct instead of converting to and from JSON redundantly --- lib/vultr/object.rb | 9 ++++++++- test/vultr/object_test.rb | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/vultr/object.rb b/lib/vultr/object.rb index 33b7036..acb915b 100644 --- a/lib/vultr/object.rb +++ b/lib/vultr/object.rb @@ -5,7 +5,14 @@ module Vultr class Object < SimpleDelegator def initialize(attributes) - super(JSON.parse(attributes.to_json, object_class: OpenStruct)) + super to_recursive_ostruct(attributes) + end + + def to_recursive_ostruct(hash) + result = hash.each_with_object({}) do |(key, val), memo| + memo[key] = val.is_a?(Hash) ? to_recursive_ostruct(val) : val + end + OpenStruct.new(result) end end end diff --git a/test/vultr/object_test.rb b/test/vultr/object_test.rb index caff32f..ee5b641 100644 --- a/test/vultr/object_test.rb +++ b/test/vultr/object_test.rb @@ -2,11 +2,11 @@ class ObjectTest < Minitest::Test def test_creating_object_from_hash - assert_equal "bar", Vultr::Object.new(foo: :bar).foo + assert_equal "bar", Vultr::Object.new(foo: "bar").foo end def test_nested_hash - assert_equal "foobar", Vultr::Object.new(foo: {bar: {baz: :foobar}}).foo.bar.baz + assert_equal "foobar", Vultr::Object.new(foo: {bar: {baz: "foobar"}}).foo.bar.baz end def test_nested_number From 4ff0f95d80295ab151c3debc0a5453a117884ded Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Wed, 25 Aug 2021 11:15:08 -0500 Subject: [PATCH 34/50] Try testing old rubies --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e87a409..6cf6cc9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,7 +12,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - ruby: ["2.7", "3.0"] + ruby: ["1.9", "2.0", "2.7", "3.0"] steps: - uses: actions/checkout@master From b067dc311bd201257f6f7ffd7c0e59a620656547 Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Wed, 25 Aug 2021 11:15:50 -0500 Subject: [PATCH 35/50] add ISO test (#22) reference #3 --- lib/vultr/resources/iso.rb | 6 +-- test/fixtures/isos/create.json | 8 ++++ test/fixtures/isos/list.json | 20 ++++++++++ test/fixtures/isos/list_public.json | 17 +++++++++ test/fixtures/isos/retrieve.json | 11 ++++++ test/vultr/resources/iso_test.rb | 58 +++++++++++++++++++++++++++++ 6 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/isos/create.json create mode 100644 test/fixtures/isos/list.json create mode 100644 test/fixtures/isos/list_public.json create mode 100644 test/fixtures/isos/retrieve.json create mode 100644 test/vultr/resources/iso_test.rb diff --git a/lib/vultr/resources/iso.rb b/lib/vultr/resources/iso.rb index 0ca19b3..3e3da46 100644 --- a/lib/vultr/resources/iso.rb +++ b/lib/vultr/resources/iso.rb @@ -6,15 +6,15 @@ def list(**params) end def create(**attributes) - Iso.new post_request("isos", body: attributes).body.dig("iso") + Iso.new post_request("iso", body: attributes).body.dig("iso") end def retrieve(iso_id:) - Iso.new get_request("isos/#{iso_id}").body.dig("iso") + Iso.new get_request("iso/#{iso_id}").body.dig("iso") end def delete(iso_id:) - delete_request("isos/#{iso_id}") + delete_request("iso/#{iso_id}") end def list_public(**params) diff --git a/test/fixtures/isos/create.json b/test/fixtures/isos/create.json new file mode 100644 index 0000000..5b85d94 --- /dev/null +++ b/test/fixtures/isos/create.json @@ -0,0 +1,8 @@ +{ + "iso": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "date_created": "2020-10-10T01:56:20+00:00", + "filename": "my-iso.iso", + "status": "pending" + } +} diff --git a/test/fixtures/isos/list.json b/test/fixtures/isos/list.json new file mode 100644 index 0000000..6f4690b --- /dev/null +++ b/test/fixtures/isos/list.json @@ -0,0 +1,20 @@ +{ + "isos": [ + { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "date_created": "2020-10-10T01:56:20+00:00", + "filename": "my-iso.iso", + "size": 120586240, + "md5sum": "77ba289bdc966ec996278a5a740d96d8", + "sha512sum": "2b31b6fcab34d6ea9a6b293601c39b90cb044e5679fcc5", + "status": "complete" + } + ], + "meta": { + "total": 1, + "links": { + "next": "", + "prev": "" + } + } +} diff --git a/test/fixtures/isos/list_public.json b/test/fixtures/isos/list_public.json new file mode 100644 index 0000000..ceb6880 --- /dev/null +++ b/test/fixtures/isos/list_public.json @@ -0,0 +1,17 @@ +{ + "public_isos": [ + { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b604", + "name": "CentOS 7", + "description": "7 x86_64 Minimal", + "md5sum": "7f4df50f42ee1b52b193e79855a3aa19" + } + ], + "meta": { + "total": 1, + "links": { + "next": "", + "prev": "" + } + } +} diff --git a/test/fixtures/isos/retrieve.json b/test/fixtures/isos/retrieve.json new file mode 100644 index 0000000..efd9b93 --- /dev/null +++ b/test/fixtures/isos/retrieve.json @@ -0,0 +1,11 @@ +{ + "iso": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "date_created": "2020-10-10T01:56:20+00:00", + "filename": "my-iso.iso", + "size": 120586240, + "md5sum": "77ba289bdc966ec996278a5a740d96d8", + "sha512sum": "2b31b6fcab34d6ea9a6b293601c39b90cb044e5679fcc5", + "status": "complete" + } +} diff --git a/test/vultr/resources/iso_test.rb b/test/vultr/resources/iso_test.rb new file mode 100644 index 0000000..3fd52e4 --- /dev/null +++ b/test/vultr/resources/iso_test.rb @@ -0,0 +1,58 @@ +require "test_helper" + +class IsoResourceTest < Minitest::Test + def test_list + stub = stub_request("iso", response: stub_response(fixture: "isos/list")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + iso = client.iso.list + + keys = [:id, :date_created, :filename, :size, :md5sum, :sha512sum, :status] + assert_equal Vultr::Collection, iso.class + assert_equal Vultr::Iso, iso.data.first.class + assert_equal keys, iso.data.first.to_h.keys + assert_equal 1, iso.total + end + + def test_create + body = {url: "http://example.com/my-iso.iso"} + stub = stub_request("iso", method: :post, body: body, response: stub_response(fixture: "isos/create", status: 201)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + iso = client.iso.create(**body) + + assert_equal "cb676a46-66fd-4dfb-b839-443f2e6c0b60", iso.id + assert_equal "2020-10-10T01:56:20+00:00", iso.date_created + assert_equal "my-iso.iso", iso.filename + assert_equal "pending", iso.status + end + + def test_retrieve + iso_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b604" + stub = stub_request("iso/#{iso_id}", response: stub_response(fixture: "isos/retrieve")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + iso = client.iso.retrieve(iso_id: iso_id) + + keys = [:id, :date_created, :filename, :size, :md5sum, :sha512sum, :status] + assert_equal Vultr::Iso, iso.class + assert_equal keys, iso.to_h.keys + end + + def test_delete + iso_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b604" + stub = stub_request("iso/#{iso_id}", method: :delete, response: {}) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + assert client.iso.delete(iso_id: iso_id) + end + + def test_list_public + stub = stub_request("iso-public", response: stub_response(fixture: "isos/list_public")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + iso = client.iso.list_public + + keys = [:id, :name, :description, :md5sum] + assert_equal Vultr::Collection, iso.class + assert_equal Vultr::Iso, iso.data.first.class + assert_equal keys, iso.data.first.to_h.keys + assert_equal 1, iso.total + end +end From f4bcfc55ba27c201bd73265bae4724ab8e4ac22a Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Wed, 25 Aug 2021 11:18:33 -0500 Subject: [PATCH 36/50] Update ruby versions for CI --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6cf6cc9..e16a1e1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,7 +12,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - ruby: ["1.9", "2.0", "2.7", "3.0"] + ruby: ["2.2", "2.3", "2.4", "2.5", "2.6", "2.7", "3.0"] steps: - uses: actions/checkout@master From 12628f156ad34d4f536488b7ed8435ee5491dd55 Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Wed, 25 Aug 2021 11:21:42 -0500 Subject: [PATCH 37/50] Update minimum Ruby dependency to match Faraday --- .github/workflows/ci.yml | 2 +- vultr.gemspec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e16a1e1..62b49d8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,7 +12,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - ruby: ["2.2", "2.3", "2.4", "2.5", "2.6", "2.7", "3.0"] + ruby: ["2.4", "2.5", "2.6", "2.7", "3.0"] steps: - uses: actions/checkout@master diff --git a/vultr.gemspec b/vultr.gemspec index ae43f6d..297b4c3 100644 --- a/vultr.gemspec +++ b/vultr.gemspec @@ -19,7 +19,7 @@ Gem::Specification.new do |s| s.test_files = s.files.grep(%r{^(test|spec|features)/}) s.require_paths = ["lib"] - s.required_ruby_version = ">= 1.9" + s.required_ruby_version = ">= 2.4" s.add_dependency "faraday", "~> 1.7" s.add_dependency "faraday_middleware", "~> 1.1" From 102fdc354a057d0a97b2e3662278422a0e460a69 Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Wed, 25 Aug 2021 11:25:00 -0500 Subject: [PATCH 38/50] Change to version 1.0.0 --- lib/vultr/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vultr/version.rb b/lib/vultr/version.rb index 1e56dd2..efeca7e 100644 --- a/lib/vultr/version.rb +++ b/lib/vultr/version.rb @@ -1,3 +1,3 @@ module Vultr - VERSION = "2.0.0" + VERSION = "1.0.0" end From 72410b25975056dd0ddd83a3654c7fe3a58accb9 Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Wed, 25 Aug 2021 15:25:52 -0500 Subject: [PATCH 39/50] Add rate limit status handling --- lib/vultr/resource.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/vultr/resource.rb b/lib/vultr/resource.rb index f709af2..33f6cc2 100644 --- a/lib/vultr/resource.rb +++ b/lib/vultr/resource.rb @@ -46,6 +46,8 @@ def handle_response(response) raise Error, "Your request exceeded the API rate limit. #{response.body["error"]}" when 500 raise Error, "We were unable to perform the request due to server-side problems. #{response.body["error"]}" + when 503 + raise Error, "You have been rate limited for sending more than 20 requests per second. #{response.body["error"]}" end response From 064dd19e507a3c682f420a04faac28ab6827f488 Mon Sep 17 00:00:00 2001 From: Andrew Porter Date: Wed, 25 Aug 2021 14:53:09 -0600 Subject: [PATCH 40/50] Use Faraday middleware to include Authorization header (#23) * Use OAuth2 middleware to include Authorization header * Add Authorization header to client configuration Adding directly instead of using the OAuth2 middleware since this gem technically doesn't use OAuth. * Remove default_headers method It's no longer needed now that we set the Authorization header in Faraday. Co-authored-by: Chris Oliver --- lib/vultr/client.rb | 4 ++-- lib/vultr/resource.rb | 14 +++++--------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/lib/vultr/client.rb b/lib/vultr/client.rb index 0f2fca9..8ec7513 100644 --- a/lib/vultr/client.rb +++ b/lib/vultr/client.rb @@ -97,8 +97,8 @@ def users end def connection - @connection ||= Faraday.new do |conn| - conn.url_prefix = BASE_URL + @connection ||= Faraday.new(BASE_URL) do |conn| + conn.request :authorization, :Bearer, api_key conn.request :json conn.response :json, content_type: "application/json" conn.adapter adapter, @stubs diff --git a/lib/vultr/resource.rb b/lib/vultr/resource.rb index f709af2..e872d6e 100644 --- a/lib/vultr/resource.rb +++ b/lib/vultr/resource.rb @@ -9,27 +9,23 @@ def initialize(client) private def get_request(url, params: {}, headers: {}) - handle_response client.connection.get(url, params, default_headers.merge(headers)) + handle_response client.connection.get(url, params, headers) end def post_request(url, body:, headers: {}) - handle_response client.connection.post(url, body, default_headers.merge(headers)) + handle_response client.connection.post(url, body, headers) end def patch_request(url, body:, headers: {}) - handle_response client.connection.patch(url, body, default_headers.merge(headers)) + handle_response client.connection.patch(url, body, headers) end def put_request(url, body:, headers: {}) - handle_response client.connection.put(url, body, default_headers.merge(headers)) + handle_response client.connection.put(url, body, headers) end def delete_request(url, params: {}, headers: {}) - handle_response client.connection.delete(url, params, default_headers.merge(headers)) - end - - def default_headers - {Authorization: "Bearer #{client.api_key}"} + handle_response client.connection.delete(url, params, headers) end def handle_response(response) From 783612ab97b56d0d00a17d062e0c65faf3860595 Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Wed, 25 Aug 2021 21:36:14 -0500 Subject: [PATCH 41/50] Use recursive open struct for Objects --- lib/vultr/object.rb | 17 +++++++++-------- test/vultr/object_test.rb | 6 ++++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/vultr/object.rb b/lib/vultr/object.rb index acb915b..a87be83 100644 --- a/lib/vultr/object.rb +++ b/lib/vultr/object.rb @@ -1,18 +1,19 @@ require "ostruct" -require "delegate" -require "json" module Vultr - class Object < SimpleDelegator + class Object < OpenStruct def initialize(attributes) - super to_recursive_ostruct(attributes) + super to_ostruct(attributes) end - def to_recursive_ostruct(hash) - result = hash.each_with_object({}) do |(key, val), memo| - memo[key] = val.is_a?(Hash) ? to_recursive_ostruct(val) : val + def to_ostruct(obj) + if obj.is_a?(Hash) + OpenStruct.new(obj.map{ |key, val| [ key, to_ostruct(val) ] }.to_h) + elsif obj.is_a?(Array) + obj.map{ |o| to_ostruct(o) } + else # Assumed to be a primitive value + obj end - OpenStruct.new(result) end end end diff --git a/test/vultr/object_test.rb b/test/vultr/object_test.rb index ee5b641..5dc488f 100644 --- a/test/vultr/object_test.rb +++ b/test/vultr/object_test.rb @@ -12,4 +12,10 @@ def test_nested_hash def test_nested_number assert_equal 1, Vultr::Object.new(foo: {bar: 1}).foo.bar end + + def test_array + object = Vultr::Object.new(foo: [{bar: :baz}]) + assert_equal OpenStruct, object.foo.first.class + assert_equal :baz, object.foo.first.bar + end end From 36dba1edf65fe704fa7dc30d248e0cbc21547352 Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Wed, 25 Aug 2021 21:36:30 -0500 Subject: [PATCH 42/50] Dnssec should return array of strings, not an object --- lib/vultr/resources/dns.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/vultr/resources/dns.rb b/lib/vultr/resources/dns.rb index c2de413..e176cdc 100644 --- a/lib/vultr/resources/dns.rb +++ b/lib/vultr/resources/dns.rb @@ -29,8 +29,9 @@ def update_soa(dns_domain:, **attributes) patch_request("domains/#{dns_domain}/soa", body: attributes) end + # Returns an Array of strings def dnssec(dns_domain:) - Object.new get_request("domains/#{dns_domain}/dnssec").body.dig("dns_sec") + get_request("domains/#{dns_domain}/dnssec").body.dig("dns_sec") end def list_records(dns_domain:, **params) From 1303aaa7a55f8d6926fee87317396db132de6a60 Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Wed, 25 Aug 2021 21:43:27 -0500 Subject: [PATCH 43/50] standardize --- lib/vultr/object.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/vultr/object.rb b/lib/vultr/object.rb index a87be83..4ec9d36 100644 --- a/lib/vultr/object.rb +++ b/lib/vultr/object.rb @@ -8,9 +8,9 @@ def initialize(attributes) def to_ostruct(obj) if obj.is_a?(Hash) - OpenStruct.new(obj.map{ |key, val| [ key, to_ostruct(val) ] }.to_h) + OpenStruct.new(obj.map { |key, val| [key, to_ostruct(val)] }.to_h) elsif obj.is_a?(Array) - obj.map{ |o| to_ostruct(o) } + obj.map { |o| to_ostruct(o) } else # Assumed to be a primitive value obj end From 22ea63ea3be9b45a48d4e5189b63c92bf7131d30 Mon Sep 17 00:00:00 2001 From: Andrew Porter Date: Wed, 25 Aug 2021 22:16:42 -0600 Subject: [PATCH 44/50] Parse ISO8601 date strings into `Time` objects --- lib/vultr/client.rb | 3 +++ test/vultr/resources/dns_test.rb | 4 ++-- test/vultr/resources/iso_test.rb | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/vultr/client.rb b/lib/vultr/client.rb index 8ec7513..e494f7b 100644 --- a/lib/vultr/client.rb +++ b/lib/vultr/client.rb @@ -100,7 +100,10 @@ def connection @connection ||= Faraday.new(BASE_URL) do |conn| conn.request :authorization, :Bearer, api_key conn.request :json + + conn.response :dates conn.response :json, content_type: "application/json" + conn.adapter adapter, @stubs end end diff --git a/test/vultr/resources/dns_test.rb b/test/vultr/resources/dns_test.rb index 749d21e..6e0c9bf 100644 --- a/test/vultr/resources/dns_test.rb +++ b/test/vultr/resources/dns_test.rb @@ -22,7 +22,7 @@ def test_create dns = client.dns.create(**body) assert_equal "example.com", dns.domain - assert_equal "2020-10-10T01:56:20+00:00", dns.date_created + assert_equal Time.parse("2020-10-10T01:56:20+00:00"), dns.date_created end def test_retrieve @@ -32,7 +32,7 @@ def test_retrieve dns = client.dns.retrieve(dns_domain: dns_domain) assert_equal "example.com", dns.domain - assert_equal "2020-10-10T01:56:20+00:00", dns.date_created + assert_equal Time.parse("2020-10-10T01:56:20+00:00"), dns.date_created end def test_update diff --git a/test/vultr/resources/iso_test.rb b/test/vultr/resources/iso_test.rb index 3fd52e4..cfa0c2e 100644 --- a/test/vultr/resources/iso_test.rb +++ b/test/vultr/resources/iso_test.rb @@ -20,7 +20,7 @@ def test_create iso = client.iso.create(**body) assert_equal "cb676a46-66fd-4dfb-b839-443f2e6c0b60", iso.id - assert_equal "2020-10-10T01:56:20+00:00", iso.date_created + assert_equal Time.parse("2020-10-10T01:56:20+00:00"), iso.date_created assert_equal "my-iso.iso", iso.filename assert_equal "pending", iso.status end From 086a36c82a56e1e53252a89e94eef37bec1e4821 Mon Sep 17 00:00:00 2001 From: feipinghuang Date: Thu, 26 Aug 2021 15:31:56 +0800 Subject: [PATCH 45/50] Update block_storage.rb --- lib/vultr/resources/block_storage.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vultr/resources/block_storage.rb b/lib/vultr/resources/block_storage.rb index 020a6a6..2c7b02f 100644 --- a/lib/vultr/resources/block_storage.rb +++ b/lib/vultr/resources/block_storage.rb @@ -25,7 +25,7 @@ def attach(block_id:, **params) post_request("blocks/#{block_id}/attach", body: params) end - def detach(block_id:) + def detach(block_id:, **params) post_request("blocks/#{block_id}/detach", body: params) end end From 6072868d2f649e9f1df94e0c82328a273b584771 Mon Sep 17 00:00:00 2001 From: Ashwin M Date: Fri, 27 Aug 2021 01:55:18 +0530 Subject: [PATCH 46/50] Tests for Instances Resource (#27) * added tests for instances resourse * standardb * Fix instance neighbors * Update instances_test.rb * standardb fixes Co-authored-by: Chris Oliver --- lib/vultr/resources/instances.rb | 24 +- test/fixtures/instances/attach_iso.json | 6 + .../instances/attach_private_network.json | 1 + test/fixtures/instances/backup_schedule.json | 10 + test/fixtures/instances/bandwidth.json | 12 + test/fixtures/instances/create.json | 36 ++ .../instances/create_ipv4_reverse.json | 1 + .../instances/create_ipv6_reverse.json | 1 + test/fixtures/instances/delete.json | 1 + test/fixtures/instances/delete_ipv4.json | 1 + test/fixtures/instances/delete_ipv6.json | 1 + test/fixtures/instances/detach_iso.json | 5 + .../instances/detach_private_network.json | 1 + test/fixtures/instances/halt.json | 1 + test/fixtures/instances/halt_instances.json | 1 + test/fixtures/instances/ipv4.json | 9 + test/fixtures/instances/ipv6_reverse.json | 15 + test/fixtures/instances/iso.json | 6 + test/fixtures/instances/list.json | 44 +++ test/fixtures/instances/list_ipv4.json | 32 ++ test/fixtures/instances/list_ipv6.json | 17 + .../instances/list_private_networks.json | 16 + test/fixtures/instances/neighbors.json | 6 + test/fixtures/instances/reboot.json | 1 + test/fixtures/instances/reboot_instances.json | 1 + test/fixtures/instances/reinstall.json | 35 ++ test/fixtures/instances/restore.json | 7 + test/fixtures/instances/retrieve.json | 35 ++ .../instances/set_backup_schedule.json | 1 + .../set_default_reverse_dns_entry.json | 1 + test/fixtures/instances/start.json | 1 + test/fixtures/instances/start_instances.json | 1 + test/fixtures/instances/update.json | 1 + test/fixtures/instances/upgrades.json | 55 +++ test/fixtures/instances/user_data.json | 5 + test/vultr/resources/instances_test.rb | 325 ++++++++++++++++++ 36 files changed, 704 insertions(+), 12 deletions(-) create mode 100644 test/fixtures/instances/attach_iso.json create mode 100644 test/fixtures/instances/attach_private_network.json create mode 100644 test/fixtures/instances/backup_schedule.json create mode 100644 test/fixtures/instances/bandwidth.json create mode 100644 test/fixtures/instances/create.json create mode 100644 test/fixtures/instances/create_ipv4_reverse.json create mode 100644 test/fixtures/instances/create_ipv6_reverse.json create mode 100644 test/fixtures/instances/delete.json create mode 100644 test/fixtures/instances/delete_ipv4.json create mode 100644 test/fixtures/instances/delete_ipv6.json create mode 100644 test/fixtures/instances/detach_iso.json create mode 100644 test/fixtures/instances/detach_private_network.json create mode 100644 test/fixtures/instances/halt.json create mode 100644 test/fixtures/instances/halt_instances.json create mode 100644 test/fixtures/instances/ipv4.json create mode 100644 test/fixtures/instances/ipv6_reverse.json create mode 100644 test/fixtures/instances/iso.json create mode 100644 test/fixtures/instances/list.json create mode 100644 test/fixtures/instances/list_ipv4.json create mode 100644 test/fixtures/instances/list_ipv6.json create mode 100644 test/fixtures/instances/list_private_networks.json create mode 100644 test/fixtures/instances/neighbors.json create mode 100644 test/fixtures/instances/reboot.json create mode 100644 test/fixtures/instances/reboot_instances.json create mode 100644 test/fixtures/instances/reinstall.json create mode 100644 test/fixtures/instances/restore.json create mode 100644 test/fixtures/instances/retrieve.json create mode 100644 test/fixtures/instances/set_backup_schedule.json create mode 100644 test/fixtures/instances/set_default_reverse_dns_entry.json create mode 100644 test/fixtures/instances/start.json create mode 100644 test/fixtures/instances/start_instances.json create mode 100644 test/fixtures/instances/update.json create mode 100644 test/fixtures/instances/upgrades.json create mode 100644 test/fixtures/instances/user_data.json create mode 100644 test/vultr/resources/instances_test.rb diff --git a/lib/vultr/resources/instances.rb b/lib/vultr/resources/instances.rb index b6f7ee8..db47d76 100644 --- a/lib/vultr/resources/instances.rb +++ b/lib/vultr/resources/instances.rb @@ -10,7 +10,7 @@ def create(**attributes) end def retrieve(instance_id:) - Instance.new get_request("instances/#{instance_id}").body + Instance.new get_request("instances/#{instance_id}").body.dig("instance") end def update(instance_id:, **attributes) @@ -21,24 +21,24 @@ def delete(instance_id:) delete_request("instances/#{instance_id}") end - def start(instance_id:) - post_request("instances/#{instance_id}/start") + def start(instance_id:, **attributes) + post_request("instances/#{instance_id}/start", body: attributes) end - def reboot(instance_id:) - post_request("instances/#{instance_id}/reboot") + def reboot(instance_id:, **attributes) + post_request("instances/#{instance_id}/reboot", body: attributes) end - def reinstall(instance_id:) - Instance.new post_request("instances/#{instance_id}/reinstall").body.dig("instance") + def reinstall(instance_id:, **attributes) + Instance.new post_request("instances/#{instance_id}/reinstall", body: attributes).body.dig("instance") end def restore(instance_id:, **attributes) - Object.new post_request("instances/#{instance_id}/restore", attributes).body.dig("status") + Object.new post_request("instances/#{instance_id}/restore", body: attributes).body.dig("status") end - def halt(instance_id:) - post_request("instances/#{instance_id}/halt") + def halt(instance_id:, **attributes) + post_request("instances/#{instance_id}/halt", body: attributes) end def bandwidth(instance_id:) @@ -46,7 +46,7 @@ def bandwidth(instance_id:) end def neighbors(instance_id:) - Object.new get_request("instances/#{instance_id}/neighbors").body.dig("neighbors") + get_request("instances/#{instance_id}/neighbors").body.dig("neighbors") end def user_data(instance_id:) @@ -84,7 +84,7 @@ def list_ipv6(instance_id:, **params) end def ipv6_reverse(instance_id:) - response = get_request("instances/#{instance_id}/ipv6") + response = get_request("instances/#{instance_id}/ipv6/reverse") Collection.from_response(response, key: "reverse_ipv6s", type: Object) end diff --git a/test/fixtures/instances/attach_iso.json b/test/fixtures/instances/attach_iso.json new file mode 100644 index 0000000..c8b8bf6 --- /dev/null +++ b/test/fixtures/instances/attach_iso.json @@ -0,0 +1,6 @@ +{ + "iso_status": { + "status": "ismounting", + "iso_id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + } +} \ No newline at end of file diff --git a/test/fixtures/instances/attach_private_network.json b/test/fixtures/instances/attach_private_network.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/test/fixtures/instances/attach_private_network.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test/fixtures/instances/backup_schedule.json b/test/fixtures/instances/backup_schedule.json new file mode 100644 index 0000000..c78fdf1 --- /dev/null +++ b/test/fixtures/instances/backup_schedule.json @@ -0,0 +1,10 @@ +{ + "backup_schedule": { + "enabled": true, + "type": "daily", + "next_scheduled_time_utc": "2020-07-28 00:00:00", + "hour": 10, + "dow": 1, + "dom": 1 + } +} \ No newline at end of file diff --git a/test/fixtures/instances/bandwidth.json b/test/fixtures/instances/bandwidth.json new file mode 100644 index 0000000..5e9d02a --- /dev/null +++ b/test/fixtures/instances/bandwidth.json @@ -0,0 +1,12 @@ +{ + "bandwidth": { + "2020-07-25": { + "incoming_bytes": 15989787, + "outgoing_bytes": 25327729 + }, + "2020-07-26": { + "incoming_bytes": 13964112, + "outgoing_bytes": 22257069 + } + } +} \ No newline at end of file diff --git a/test/fixtures/instances/create.json b/test/fixtures/instances/create.json new file mode 100644 index 0000000..7b9773b --- /dev/null +++ b/test/fixtures/instances/create.json @@ -0,0 +1,36 @@ +{ + "instance": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "os": "CentOS 8 x64", + "ram": 2048, + "disk": 55, + "main_ip": "192.0.2.123", + "vcpu_count": 1, + "region": "ewr", + "plan": "vc2-6c-16gb", + "default_password": "example-password", + "date_created": "2020-10-10T01:56:20+00:00", + "status": "active", + "allowed_bandwidth": 2000, + "netmask_v4": "255.255.254.0", + "gateway_v4": "192.0.2.1", + "power_status": "running", + "server_status": "ok", + "v6_network": "2001:0db8:1112:18fb::", + "v6_main_ip": "2001:0db8:1112:18fb:0200:00ff:fe00:0000", + "v6_network_size": 64, + "label": "Example Instance", + "internal_ip": "", + "kvm": "https://my.vultr.com/subs/vps/novnc/api.php?data=00example11223344", + "tag": "Example Tag", + "os_id": 215, + "app_id": 0, + "image_id": "", + "firewall_group_id": "", + "features": [ + "ddos_protection", + "ipv6", + "auto_backups" + ] + } +} \ No newline at end of file diff --git a/test/fixtures/instances/create_ipv4_reverse.json b/test/fixtures/instances/create_ipv4_reverse.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/test/fixtures/instances/create_ipv4_reverse.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test/fixtures/instances/create_ipv6_reverse.json b/test/fixtures/instances/create_ipv6_reverse.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/test/fixtures/instances/create_ipv6_reverse.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test/fixtures/instances/delete.json b/test/fixtures/instances/delete.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/test/fixtures/instances/delete.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test/fixtures/instances/delete_ipv4.json b/test/fixtures/instances/delete_ipv4.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/test/fixtures/instances/delete_ipv4.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test/fixtures/instances/delete_ipv6.json b/test/fixtures/instances/delete_ipv6.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/test/fixtures/instances/delete_ipv6.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test/fixtures/instances/detach_iso.json b/test/fixtures/instances/detach_iso.json new file mode 100644 index 0000000..d38927e --- /dev/null +++ b/test/fixtures/instances/detach_iso.json @@ -0,0 +1,5 @@ +{ + "iso_status": { + "status": "isunmounting" + } +} \ No newline at end of file diff --git a/test/fixtures/instances/detach_private_network.json b/test/fixtures/instances/detach_private_network.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/test/fixtures/instances/detach_private_network.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test/fixtures/instances/halt.json b/test/fixtures/instances/halt.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/test/fixtures/instances/halt.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test/fixtures/instances/halt_instances.json b/test/fixtures/instances/halt_instances.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/test/fixtures/instances/halt_instances.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test/fixtures/instances/ipv4.json b/test/fixtures/instances/ipv4.json new file mode 100644 index 0000000..7e57a6e --- /dev/null +++ b/test/fixtures/instances/ipv4.json @@ -0,0 +1,9 @@ +{ + "ipv4": { + "ip": "192.0.2.123", + "netmask": "255.255.254.0", + "gateway": "192.0.2.1", + "type": "secondary_ip", + "reverse": "192.0.2.123.vultr.com" + } +} \ No newline at end of file diff --git a/test/fixtures/instances/ipv6_reverse.json b/test/fixtures/instances/ipv6_reverse.json new file mode 100644 index 0000000..8aaea98 --- /dev/null +++ b/test/fixtures/instances/ipv6_reverse.json @@ -0,0 +1,15 @@ +{ + "reverse_ipv6s": [ + { + "ip": "2001:0db8:5:6bb:5400:2ff:fee5:1", + "reverse": "foo.example.com" + } + ], + "meta": { + "total": 1, + "links": { + "next": "", + "prev": "" + } + } +} \ No newline at end of file diff --git a/test/fixtures/instances/iso.json b/test/fixtures/instances/iso.json new file mode 100644 index 0000000..6f919af --- /dev/null +++ b/test/fixtures/instances/iso.json @@ -0,0 +1,6 @@ +{ + "iso_status": { + "state": "ready", + "iso_id": "0532a75b-14e8-48b8-b27e-1ebcf382a804" + } +} \ No newline at end of file diff --git a/test/fixtures/instances/list.json b/test/fixtures/instances/list.json new file mode 100644 index 0000000..5ae7b71 --- /dev/null +++ b/test/fixtures/instances/list.json @@ -0,0 +1,44 @@ +{ + "instances": [ + { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "os": "CentOS SELinux 8 x64", + "ram": 2048, + "disk": 55, + "main_ip": "192.0.2.123", + "vcpu_count": 1, + "region": "atl", + "plan": "vc2-6c-16gb", + "date_created": "2020-10-10T01:56:20+00:00", + "status": "active", + "allowed_bandwidth": 2000, + "netmask_v4": "255.255.252.0", + "gateway_v4": "192.0.2.1", + "power_status": "running", + "server_status": "ok", + "v6_network": "2001:0db8:1112:18fb::", + "v6_main_ip": "2001:0db8:1112:18fb:0200:00ff:fe00:0000", + "v6_network_size": 64, + "label": "Example Instance", + "internal_ip": "", + "kvm": "https://my.vultr.com/subs/vps/novnc/api.php?data=00example11223344", + "tag": "Example Tag", + "os_id": 215, + "app_id": 0, + "image_id": "", + "firewall_group_id": "", + "features": [ + "ddos_protection", + "ipv6", + "auto_backups" + ] + } + ], + "meta": { + "total": 3, + "links": { + "next": "WxYzExampleNext", + "prev": "" + } + } +} \ No newline at end of file diff --git a/test/fixtures/instances/list_ipv4.json b/test/fixtures/instances/list_ipv4.json new file mode 100644 index 0000000..b71637a --- /dev/null +++ b/test/fixtures/instances/list_ipv4.json @@ -0,0 +1,32 @@ +{ + "ipv4s": [ + { + "ip": "192.0.2.123", + "netmask": "255.255.254.0", + "gateway": "192.0.2.1", + "type": "main_ip", + "reverse": "host1.example.com" + }, + { + "ip": "203.0.113.123", + "netmask": "255.255.255.255", + "gateway": "", + "type": "secondary_ip", + "reverse": "host2.example.com" + }, + { + "ip": "10.99.0.123", + "netmask": "255.255.0.0", + "gateway": "", + "type": "private", + "reverse": "" + } + ], + "meta": { + "total": 3, + "links": { + "next": "", + "prev": "" + } + } +} \ No newline at end of file diff --git a/test/fixtures/instances/list_ipv6.json b/test/fixtures/instances/list_ipv6.json new file mode 100644 index 0000000..41f265d --- /dev/null +++ b/test/fixtures/instances/list_ipv6.json @@ -0,0 +1,17 @@ +{ + "ipv6s": [ + { + "ip": "2001:0db8:5:6bb:5400:02ff:fee5:3fe3", + "network": "2001:0db8:5:6bb::", + "network_size": 64, + "type": "main_ip" + } + ], + "meta": { + "total": 1, + "links": { + "next": "", + "prev": "" + } + } +} \ No newline at end of file diff --git a/test/fixtures/instances/list_private_networks.json b/test/fixtures/instances/list_private_networks.json new file mode 100644 index 0000000..fcc6daa --- /dev/null +++ b/test/fixtures/instances/list_private_networks.json @@ -0,0 +1,16 @@ +{ + "private_networks": [ + { + "network_id": "d325e78b-bcec-4d4e-b9b8-9c294f37b04d", + "mac_address": "00:00:5e:00:53:5e", + "ip_address": "10.99.0.123" + } + ], + "meta": { + "total": 1, + "links": { + "next": "", + "prev": "" + } + } +} \ No newline at end of file diff --git a/test/fixtures/instances/neighbors.json b/test/fixtures/instances/neighbors.json new file mode 100644 index 0000000..53e2da1 --- /dev/null +++ b/test/fixtures/instances/neighbors.json @@ -0,0 +1,6 @@ +{ + "neighbors": [ + "478dbfe7-43f8-4dc8-940c-8bb61f547400", + "a8047e6b-16bd-42be-8351-58df7e5ab89c" + ] +} \ No newline at end of file diff --git a/test/fixtures/instances/reboot.json b/test/fixtures/instances/reboot.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/test/fixtures/instances/reboot.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test/fixtures/instances/reboot_instances.json b/test/fixtures/instances/reboot_instances.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/test/fixtures/instances/reboot_instances.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test/fixtures/instances/reinstall.json b/test/fixtures/instances/reinstall.json new file mode 100644 index 0000000..bfab9f8 --- /dev/null +++ b/test/fixtures/instances/reinstall.json @@ -0,0 +1,35 @@ +{ + "instance": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "os": "Ubuntu 20.04 x64", + "ram": 16384, + "disk": 384, + "main_ip": "192.0.2.123", + "vcpu_count": 4, + "region": "ewr", + "plan": "vc2-6c-16gb", + "date_created": "2020-10-10T01:56:20+00:00", + "status": "active", + "allowed_bandwidth": 5000, + "netmask_v4": "255.255.254.0", + "gateway_v4": "192.0.2.1", + "power_status": "running", + "server_status": "ok", + "v6_network": "", + "v6_main_ip": "", + "v6_network_size": 0, + "label": "Example Instance", + "internal_ip": "", + "kvm": "https://my.vultr.com/subs/vps/novnc/api.php?data=00example11223344", + "tag": "Example Tag", + "os_id": 215, + "app_id": 0, + "image_id": "", + "firewall_group_id": "", + "features": [ + "ddos_protection", + "ipv6", + "auto_backups" + ] + } +} \ No newline at end of file diff --git a/test/fixtures/instances/restore.json b/test/fixtures/instances/restore.json new file mode 100644 index 0000000..b98a3dc --- /dev/null +++ b/test/fixtures/instances/restore.json @@ -0,0 +1,7 @@ +{ + "status": { + "restore_type": "backup_id", + "restore_id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "status": "inprogress" + } +} \ No newline at end of file diff --git a/test/fixtures/instances/retrieve.json b/test/fixtures/instances/retrieve.json new file mode 100644 index 0000000..bfab9f8 --- /dev/null +++ b/test/fixtures/instances/retrieve.json @@ -0,0 +1,35 @@ +{ + "instance": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "os": "Ubuntu 20.04 x64", + "ram": 16384, + "disk": 384, + "main_ip": "192.0.2.123", + "vcpu_count": 4, + "region": "ewr", + "plan": "vc2-6c-16gb", + "date_created": "2020-10-10T01:56:20+00:00", + "status": "active", + "allowed_bandwidth": 5000, + "netmask_v4": "255.255.254.0", + "gateway_v4": "192.0.2.1", + "power_status": "running", + "server_status": "ok", + "v6_network": "", + "v6_main_ip": "", + "v6_network_size": 0, + "label": "Example Instance", + "internal_ip": "", + "kvm": "https://my.vultr.com/subs/vps/novnc/api.php?data=00example11223344", + "tag": "Example Tag", + "os_id": 215, + "app_id": 0, + "image_id": "", + "firewall_group_id": "", + "features": [ + "ddos_protection", + "ipv6", + "auto_backups" + ] + } +} \ No newline at end of file diff --git a/test/fixtures/instances/set_backup_schedule.json b/test/fixtures/instances/set_backup_schedule.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/test/fixtures/instances/set_backup_schedule.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test/fixtures/instances/set_default_reverse_dns_entry.json b/test/fixtures/instances/set_default_reverse_dns_entry.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/test/fixtures/instances/set_default_reverse_dns_entry.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test/fixtures/instances/start.json b/test/fixtures/instances/start.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/test/fixtures/instances/start.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test/fixtures/instances/start_instances.json b/test/fixtures/instances/start_instances.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/test/fixtures/instances/start_instances.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test/fixtures/instances/update.json b/test/fixtures/instances/update.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/test/fixtures/instances/update.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test/fixtures/instances/upgrades.json b/test/fixtures/instances/upgrades.json new file mode 100644 index 0000000..f448d4e --- /dev/null +++ b/test/fixtures/instances/upgrades.json @@ -0,0 +1,55 @@ +{ + "upgrades": { + "os": [ + { + "id": 900, + "name": "Example CentOS 6 x64", + "arch": "x64", + "family": "centos" + }, + { + "id": 901, + "name": "Example CentOS 6 i386", + "arch": "i386", + "family": "centos" + } + ], + "applications": [ + { + "id": 1, + "name": "LEMP", + "short_name": "lemp", + "deploy_name": "LEMP on CentOS 6 x64", + "type": "one-click", + "vendor": "vultr", + "image_id": "" + }, + { + "id": 39, + "name": "LEMP", + "short_name": "lemp", + "deploy_name": "LEMP on CentOS 7 x64", + "type": "one-click", + "vendor": "vultr", + "image_id": "" + }, + { + "id": 1028, + "name": "OpenLiteSpeed WordPress", + "short_name": "openlitespeedwordpress", + "deploy_name": "OpenLiteSpeed WordPress on Ubuntu 20.04 x64", + "type": "marketplace", + "vendor": "LiteSpeed_Technologies", + "image_id": "openlitespeed-wordpress" + } + ], + "plans": [ + "vc2-2c-4gb", + "vc2-4c-8gb", + "vc2-6c-16gb", + "vc2-8c-64gb", + "vc2-16c-64gb", + "vc2-24c-97gb" + ] + } +} \ No newline at end of file diff --git a/test/fixtures/instances/user_data.json b/test/fixtures/instances/user_data.json new file mode 100644 index 0000000..c89a5cc --- /dev/null +++ b/test/fixtures/instances/user_data.json @@ -0,0 +1,5 @@ +{ + "user_data": { + "data": "QmFzZTY0IEV4YW1wbGUgRGF0YQ==" + } +} \ No newline at end of file diff --git a/test/vultr/resources/instances_test.rb b/test/vultr/resources/instances_test.rb new file mode 100644 index 0000000..d505c57 --- /dev/null +++ b/test/vultr/resources/instances_test.rb @@ -0,0 +1,325 @@ +# frozen_string_literal: true + +require "test_helper" + +class InstancesResourceTest < Minitest::Test + def test_list + stub = stub_request("instances", response: stub_response(fixture: "instances/list", status: 200)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + instances = client.instances.list + + assert_equal Vultr::Collection, instances.class + assert_equal Vultr::Instance, instances.data.first.class + assert_equal 3, instances.total + end + + def test_create + body = {region: "ewr", plan: "vc2-6c-16gb", label: "Example Instance", os_id: 215, user_data: "QmFzZTY0IEV4YW1wbGUgRGF0YQ==", backups: "enabled"} + stub = stub_request("instances", method: :post, body: body, response: stub_response(fixture: "instances/create", status: 202)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + instance = client.instances.create(**body) + + assert_equal Vultr::Instance, instance.class + assert_equal "Example Instance", instance.label + end + + def test_retrieve + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("instances/#{instance_id}", response: stub_response(fixture: "instances/retrieve")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + instance = client.instances.retrieve(instance_id: instance_id) + + assert_equal Vultr::Instance, instance.class + assert_equal "Example Instance", instance.label + end + + def test_update + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + body = {label: "Example Instance", tag: "Example Tag", plan: "vc2-24c-97gb"} + stub = stub_request("instances/#{instance_id}", method: :patch, body: body, response: stub_response(fixture: "instances/update", status: 202)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.instances.update(instance_id: instance_id, **body) + end + + def test_delete + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("instances/#{instance_id}", method: :delete, response: stub_response(fixture: "instances/delete", status: 204)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.instances.delete(instance_id: instance_id) + end + + def test_start + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("instances/#{instance_id}/start", method: :post, response: stub_response(fixture: "instances/start", status: 204)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.instances.start(instance_id: instance_id) + end + + def test_reboot + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("instances/#{instance_id}/reboot", method: :post, response: stub_response(fixture: "instances/reboot", status: 204)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.instances.reboot(instance_id: instance_id) + end + + def test_reinstall + body = {hostname: "Example Instance"} + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("instances/#{instance_id}/reinstall", method: :post, body: body, response: stub_response(fixture: "instances/reinstall", status: 204)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + instance = client.instances.reinstall(instance_id: instance_id, **body) + + assert_equal Vultr::Instance, instance.class + assert_equal "Example Instance", instance.label + end + + def test_restore + body = {backup_id: "cb676a46-66fd-4dfb-b839-443f2e6c0b60"} + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("instances/#{instance_id}/restore", method: :post, body: body, response: stub_response(fixture: "instances/restore", status: 202)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + instance = client.instances.restore(instance_id: instance_id, **body) + + assert_equal Vultr::Object, instance.class + assert_equal "backup_id", instance.restore_type + end + + def test_halt + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("instances/#{instance_id}/halt", method: :post, response: stub_response(fixture: "instances/halt", status: 204)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.instances.halt(instance_id: instance_id) + end + + def test_bandwidth + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("instances/#{instance_id}/bandwidth", response: stub_response(fixture: "instances/bandwidth")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + instance = client.instances.bandwidth(instance_id: instance_id) + + assert_equal Vultr::Object, instance.class + end + + def test_neighbors + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("instances/#{instance_id}/neighbors", response: stub_response(fixture: "instances/neighbors")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + instance = client.instances.neighbors(instance_id: instance_id) + + assert_equal Array, instance.class + end + + def test_user_data + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("instances/#{instance_id}/user-data", response: stub_response(fixture: "instances/user_data")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + instance = client.instances.user_data(instance_id: instance_id) + + assert_equal Vultr::Object, instance.class + assert_equal "QmFzZTY0IEV4YW1wbGUgRGF0YQ==", instance.data + end + + def test_upgrades + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("instances/#{instance_id}/upgrades", response: stub_response(fixture: "instances/upgrades")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + instance = client.instances.upgrades(instance_id: instance_id) + + assert_equal Vultr::Object, instance.class + assert_equal "Example CentOS 6 x64", instance.os.first["name"] + end + + def test_list_ipv4 + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("instances/#{instance_id}/ipv4", response: stub_response(fixture: "instances/list_ipv4")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + instance = client.instances.list_ipv4(instance_id: instance_id) + + assert_equal Vultr::Collection, instance.class + assert_equal 3, instance.total + end + + def test_create_ipv4 + body = {reboot: true} + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("instances/#{instance_id}/ipv4", method: :post, body: body, response: stub_response(fixture: "instances/ipv4", status: 202)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + instance = client.instances.create_ipv4(instance_id: instance_id, **body) + + assert_equal Vultr::Object, instance.class + end + + def test_delete_ipv4 + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + ipv4_id = "192.0.2.123" + stub = stub_request("instances/#{instance_id}/ipv4/#{ipv4_id}", method: :delete, response: stub_response(fixture: "instances/delete_ipv4", status: 204)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.instances.delete_ipv4(instance_id: instance_id, ipv4: ipv4_id) + end + + def test_create_ipv4_reverse + body = {ip: "192.0.2.123", reverse: "foo.example.com"} + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("instances/#{instance_id}/ipv4/reverse", method: :post, body: body, response: stub_response(fixture: "instances/create_ipv4_reverse", status: 202)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.instances.create_ipv4_reverse(instance_id: instance_id, **body) + end + + def test_set_default_reverse_dns_entry + body = {ip: "192.0.2.123"} + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("instances/#{instance_id}/ipv4/reverse/default", method: :post, body: body, response: stub_response(fixture: "instances/set_default_reverse_dns_entry")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.instances.set_default_reverse_dns_entry(instance_id: instance_id, **body) + end + + def test_list_ipv6 + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("instances/#{instance_id}/ipv6", response: stub_response(fixture: "instances/list_ipv6")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + instance = client.instances.list_ipv6(instance_id: instance_id) + + assert_equal Vultr::Collection, instance.class + assert_equal 1, instance.total + end + + def test_ipv6_reverse + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("instances/#{instance_id}/ipv6/reverse", response: stub_response(fixture: "instances/ipv6_reverse")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + instance = client.instances.ipv6_reverse(instance_id: instance_id) + + assert_equal Vultr::Collection, instance.class + assert_equal 1, instance.total + end + + def test_create_ipv6_reverse + body = {ip: "2001:0db8:0005:6bb0:5400:2ff0:fee5:0002", reverse: "foo.example.com"} + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("instances/#{instance_id}/ipv6/reverse", method: :post, body: body, response: stub_response(fixture: "instances/create_ipv6_reverse", status: 202)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.instances.create_ipv6_reverse(instance_id: instance_id, **body) + end + + def test_delete_ipv6_reverse + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + ipv6_id = "2001:0db8:0005:6bb0:5400:2ff0:fee5:0002" + stub = stub_request("instances/#{instance_id}/ipv6/reverse/#{ipv6_id}", method: :delete, response: stub_response(fixture: "instances/delete_ipv6", status: 204)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.instances.delete_ipv6_reverse(instance_id: instance_id, ipv6: ipv6_id) + end + + def test_list_private_networks + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("instances/#{instance_id}/private-networks", response: stub_response(fixture: "instances/list_private_networks")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + instance = client.instances.list_private_networks(instance_id: instance_id) + + assert_equal Vultr::Collection, instance.class + assert_equal 1, instance.total + end + + def test_attach_private_network + body = {network_id: "cb676a46-66fd-4dfb-b839-443f2e6c0b60"} + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("instances/#{instance_id}/private-networks/attach", method: :post, body: body, response: stub_response(fixture: "instances/attach_private_network")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.instances.attach_private_network(instance_id: instance_id, **body) + end + + def test_detach_private_network + body = {network_id: "cb676a46-66fd-4dfb-b839-443f2e6c0b60"} + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("instances/#{instance_id}/private-networks/detach", method: :post, body: body, response: stub_response(fixture: "instances/detach_private_network")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.instances.detach_private_network(instance_id: instance_id, **body) + end + + def test_iso + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("instances/#{instance_id}/iso", response: stub_response(fixture: "instances/iso")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + instance = client.instances.iso(instance_id: instance_id) + + assert_equal Vultr::Object, instance.class + end + + def test_attach_iso + body = {iso_id: "cb676a46-66fd-4dfb-b839-443f2e6c0b60"} + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("instances/#{instance_id}/iso/attach", method: :post, body: body, response: stub_response(fixture: "instances/attach_iso", status: 204)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + instance = client.instances.attach_iso(instance_id: instance_id, iso_id: body[:iso_id]) + + assert_equal Vultr::Object, instance.class + end + + def test_detach_iso + body = {iso_id: "cb676a46-66fd-4dfb-b839-443f2e6c0b60"} + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("instances/#{instance_id}/iso/detach", method: :post, body: body, response: stub_response(fixture: "instances/detach_iso")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + instance = client.instances.detach_iso(instance_id: instance_id, iso_id: body[:iso_id]) + + assert_equal Vultr::Object, instance.class + end + + def test_backup_schedule + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("instances/#{instance_id}/backup-schedule", response: stub_response(fixture: "instances/backup_schedule")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + instance = client.instances.backup_schedule(instance_id: instance_id) + + assert_equal Vultr::Object, instance.class + end + + def test_set_backup_schedule + body = {type: "daily", hour: 10, dow: 1, dom: 1} + instance_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("instances/#{instance_id}/backup-schedule", method: :post, body: body, response: stub_response(fixture: "instances/set_backup_schedule")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.instances.set_backup_schedule(instance_id: instance_id, **body) + end + + def test_halt_instances + body = { + instance_ids: [ + "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "1d651bd2-b93c-4bb6-8b91-0546fd765f15", + "c2790719-278d-474c-8dff-cb35d6e5503f" + ] + } + + stub = stub_request("instances/halt", method: :post, body: body, response: stub_response(fixture: "instances/halt_instances")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.instances.halt_instances(instance_ids: body[:instance_ids], **body) + end + + def test_reboot_instances + body = { + instance_ids: [ + "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "1d651bd2-b93c-4bb6-8b91-0546fd765f15", + "c2790719-278d-474c-8dff-cb35d6e5503f" + ] + } + + stub = stub_request("instances/reboot", method: :post, body: body, response: stub_response(fixture: "instances/reboot_instances")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.instances.reboot_instances(instance_ids: body[:instance_ids], **body) + end + + def test_start_instances + body = { + instance_ids: [ + "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "1d651bd2-b93c-4bb6-8b91-0546fd765f15", + "c2790719-278d-474c-8dff-cb35d6e5503f" + ] + } + + stub = stub_request("instances/start", method: :post, body: body, response: stub_response(fixture: "instances/start_instances")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.instances.start_instances(instance_ids: body[:instance_ids], **body) + end +end From c92a19faaa2cf8497c2f6ff448f5789c9094b796 Mon Sep 17 00:00:00 2001 From: Leo Anthony Catane Date: Tue, 31 Aug 2021 23:50:57 +0800 Subject: [PATCH 47/50] Add tests for load_balancers resource (#28) --- lib/vultr/resources/load_balancers.rb | 6 +- test/fixtures/load_balancers/create.json | 48 ++++++++ .../create_forwarding_rule.json | 1 + test/fixtures/load_balancers/delete.json | 1 + .../delete_forwarding_rule.json | 1 + test/fixtures/load_balancers/list.json | 57 +++++++++ .../load_balancers/list_firewall_rules.json | 17 +++ .../load_balancers/list_forwarding_rules.json | 18 +++ test/fixtures/load_balancers/retrieve.json | 48 ++++++++ .../retrieve_firewall_rule.json | 8 ++ .../retrieve_forwarding_rule.json | 9 ++ test/fixtures/load_balancers/update.json | 1 + test/vultr/resources/load_balancers_test.rb | 110 ++++++++++++++++++ 13 files changed, 322 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/load_balancers/create.json create mode 100644 test/fixtures/load_balancers/create_forwarding_rule.json create mode 100644 test/fixtures/load_balancers/delete.json create mode 100644 test/fixtures/load_balancers/delete_forwarding_rule.json create mode 100644 test/fixtures/load_balancers/list.json create mode 100644 test/fixtures/load_balancers/list_firewall_rules.json create mode 100644 test/fixtures/load_balancers/list_forwarding_rules.json create mode 100644 test/fixtures/load_balancers/retrieve.json create mode 100644 test/fixtures/load_balancers/retrieve_firewall_rule.json create mode 100644 test/fixtures/load_balancers/retrieve_forwarding_rule.json create mode 100644 test/fixtures/load_balancers/update.json create mode 100644 test/vultr/resources/load_balancers_test.rb diff --git a/lib/vultr/resources/load_balancers.rb b/lib/vultr/resources/load_balancers.rb index 7312978..39dc049 100644 --- a/lib/vultr/resources/load_balancers.rb +++ b/lib/vultr/resources/load_balancers.rb @@ -2,7 +2,7 @@ module Vultr class LoadBalancersResource < Resource def list(**params) response = get_request("load-balancers", params: params) - Collection.from_response(response: response, key: "load_balancers", type: LoadBalancer) + Collection.from_response(response, key: "load_balancers", type: LoadBalancer) end def create(**attributes) @@ -23,7 +23,7 @@ def delete(load_balancer_id:) def list_forwarding_rules(load_balancer_id:, **params) response = get_request("load-balancers/#{load_balancer_id}/forwarding-rules", params: params) - Collection.from_response(response: response, key: "forwarding_rules", type: Object) + Collection.from_response(response, key: "forwarding_rules", type: Object) end def create_forwarding_rule(load_balancer_id:, **attributes) @@ -40,7 +40,7 @@ def delete_forwarding_rule(load_balancer_id:, forwarding_rule_id:) def list_firewall_rules(load_balancer_id:, **params) response = get_request("load-balancers/#{load_balancer_id}/firewall-rules", params: params) - Collection.from_response(response: response, key: "firewall_rules", type: Object) + Collection.from_response(response, key: "firewall_rules", type: Object) end def retrieve_firewall_rule(load_balancer_id:, firewall_rule_id:) diff --git a/test/fixtures/load_balancers/create.json b/test/fixtures/load_balancers/create.json new file mode 100644 index 0000000..f0de8c9 --- /dev/null +++ b/test/fixtures/load_balancers/create.json @@ -0,0 +1,48 @@ +{ + "load_balancer": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "date_created": "2020-10-10T01:56:20+00:00", + "region": "ewr", + "label": "Example Load Balancer", + "status": "pending", + "ipv4": "", + "ipv6": "", + "generic_info": { + "balancing_algorithm": "roundrobin", + "ssl_redirect": false, + "sticky_sessions": { + "cookie_name": "example-cookie" + }, + "proxy_protocol": false, + "private_network": "9fed374c-0afc-42bf-926c-abcf840b5406" + }, + "health_check": { + "protocol": "http", + "port": 80, + "path": "/health", + "check_interval": 10, + "response_timeout": 5, + "unhealthy_threshold": 3, + "healthy_threshold": 3 + }, + "has_ssl": false, + "forwarding_rules": [ + { + "id": "73d85156c2c3129d", + "frontend_protocol": "http", + "frontend_port": 80, + "backend_protocol": "http", + "backend_port": 80 + } + ], + "firewall_rules": [ + { + "id": "abcd123b93016eafb", + "port": 80, + "source": "24.187.16.196/16", + "ip_type": "v4" + } + ], + "instances": [] + } +} diff --git a/test/fixtures/load_balancers/create_forwarding_rule.json b/test/fixtures/load_balancers/create_forwarding_rule.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/fixtures/load_balancers/create_forwarding_rule.json @@ -0,0 +1 @@ +{} diff --git a/test/fixtures/load_balancers/delete.json b/test/fixtures/load_balancers/delete.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/fixtures/load_balancers/delete.json @@ -0,0 +1 @@ +{} diff --git a/test/fixtures/load_balancers/delete_forwarding_rule.json b/test/fixtures/load_balancers/delete_forwarding_rule.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/fixtures/load_balancers/delete_forwarding_rule.json @@ -0,0 +1 @@ +{} diff --git a/test/fixtures/load_balancers/list.json b/test/fixtures/load_balancers/list.json new file mode 100644 index 0000000..1012cb2 --- /dev/null +++ b/test/fixtures/load_balancers/list.json @@ -0,0 +1,57 @@ +{ + "load_balancers": [ + { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "date_created": "2020-10-10T01:56:20+00:00", + "region": "ewr", + "label": "Example Load Balancer", + "status": "active", + "ipv4": "192.0.2.123", + "ipv6": "2001:0db8:5:4973:ffff:ffff:ffff:ffff", + "generic_info": { + "balancing_algorithm": "roundrobin", + "ssl_redirect": false, + "sticky_sessions": { + "cookie_name": "example-cookie" + }, + "proxy_protocol": false, + "private_network": "9fed374c-0afc-42bf-926c-abcf840b5406" + }, + "health_check": { + "protocol": "http", + "port": 80, + "path": "/health", + "check_interval": 10, + "response_timeout": 5, + "unhealthy_threshold": 3, + "healthy_threshold": 3 + }, + "has_ssl": false, + "forwarding_rules": [ + { + "id": "73d85156c2c3129d", + "frontend_protocol": "http", + "frontend_port": 80, + "backend_protocol": "http", + "backend_port": 80 + } + ], + "firewall_rules": [ + { + "id": "abcd123b93016eafb", + "port": 80, + "source": "198.51.100.0/24", + "ip_type": "v4" + } + ], + "instances": [] + } + ], + "meta": { + "total": 1, + "links": { + "next": "", + "prev": "" + } + } +} diff --git a/test/fixtures/load_balancers/list_firewall_rules.json b/test/fixtures/load_balancers/list_firewall_rules.json new file mode 100644 index 0000000..9eae61a --- /dev/null +++ b/test/fixtures/load_balancers/list_firewall_rules.json @@ -0,0 +1,17 @@ +{ + "firewall_rules": [ + { + "id": "asb123f2e6c0b60", + "port": 80, + "source": "24.187.16.196/16", + "ip_type": "v4" + } + ], + "meta": { + "total": 1, + "links": { + "next": "", + "prev": "" + } + } +} diff --git a/test/fixtures/load_balancers/list_forwarding_rules.json b/test/fixtures/load_balancers/list_forwarding_rules.json new file mode 100644 index 0000000..5ce4d64 --- /dev/null +++ b/test/fixtures/load_balancers/list_forwarding_rules.json @@ -0,0 +1,18 @@ +{ + "forwarding_rules": [ + { + "id": "443f2e6c0b60", + "frontend_protocol": "http", + "frontend_port": 80, + "backend_protocol": "http", + "backend_port": 80 + } + ], + "meta": { + "total": 1, + "links": { + "next": "", + "prev": "" + } + } +} diff --git a/test/fixtures/load_balancers/retrieve.json b/test/fixtures/load_balancers/retrieve.json new file mode 100644 index 0000000..9981fd2 --- /dev/null +++ b/test/fixtures/load_balancers/retrieve.json @@ -0,0 +1,48 @@ +{ + "load_balancer": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "date_created": "2020-10-10T01:56:20+00:00", + "region": "ewr", + "label": "Example Load Balancer", + "status": "active", + "ipv4": "192.0.2.123", + "ipv6": "2001:0db8:5:4973:ffff:ffff:ffff:ffff", + "generic_info": { + "balancing_algorithm": "roundrobin", + "ssl_redirect": false, + "sticky_sessions": { + "cookie_name": "example-cookie" + }, + "proxy_protocol": false, + "private_network": "9fed374c-0afc-42bf-926c-abcf840b5406" + }, + "health_check": { + "protocol": "http", + "port": 80, + "path": "/health", + "check_interval": 10, + "response_timeout": 5, + "unhealthy_threshold": 3, + "healthy_threshold": 3 + }, + "has_ssl": false, + "forwarding_rules": [ + { + "id": "73d85156c2c3129d", + "frontend_protocol": "http", + "frontend_port": 80, + "backend_protocol": "http", + "backend_port": 80 + } + ], + "firewall_rules": [ + { + "id": "abcd123b93016eafb", + "port": 80, + "source": "198.51.100.0/24", + "ip_type": "v4" + } + ], + "instances": [] + } +} diff --git a/test/fixtures/load_balancers/retrieve_firewall_rule.json b/test/fixtures/load_balancers/retrieve_firewall_rule.json new file mode 100644 index 0000000..09f4f70 --- /dev/null +++ b/test/fixtures/load_balancers/retrieve_firewall_rule.json @@ -0,0 +1,8 @@ +{ + "firewall_rule": { + "id": "asb123f2e6c0b60", + "port": 80, + "source": "24.187.16.196/16", + "ip_type": "v4" + } +} diff --git a/test/fixtures/load_balancers/retrieve_forwarding_rule.json b/test/fixtures/load_balancers/retrieve_forwarding_rule.json new file mode 100644 index 0000000..2d6c801 --- /dev/null +++ b/test/fixtures/load_balancers/retrieve_forwarding_rule.json @@ -0,0 +1,9 @@ +{ + "forwarding_rule": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "frontend_protocol": "http", + "frontend_port": 8080, + "backend_protocol": "http", + "backend_port": 80 + } +} diff --git a/test/fixtures/load_balancers/update.json b/test/fixtures/load_balancers/update.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/fixtures/load_balancers/update.json @@ -0,0 +1 @@ +{} diff --git a/test/vultr/resources/load_balancers_test.rb b/test/vultr/resources/load_balancers_test.rb new file mode 100644 index 0000000..12f6eac --- /dev/null +++ b/test/vultr/resources/load_balancers_test.rb @@ -0,0 +1,110 @@ +# frozen_string_literal: true + +require "test_helper" + +class LoadBalancersResourceTest < Minitest::Test + def test_list + stub = stub_request("load-balancers", response: stub_response(fixture: "load_balancers/list")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + load_balancers = client.load_balancers.list + + assert_equal Vultr::Collection, load_balancers.class + assert_equal Vultr::LoadBalancer, load_balancers.data.first.class + assert_equal 1, load_balancers.total + end + + def test_create + body = {region: "ewr", balancing_algorithm: "roundrobin", label: "Example Load Balancer"} + stub = stub_request("load-balancers", method: :post, body: body, response: stub_response(fixture: "load_balancers/create")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + load_balancer = client.load_balancers.create(**body) + + assert_equal Vultr::LoadBalancer, load_balancer.class + assert_equal "Example Load Balancer", load_balancer.label + end + + def test_retrieve + load_balancer_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("load-balancers/#{load_balancer_id}", response: stub_response(fixture: "load_balancers/retrieve")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + load_balancer = client.load_balancers.retrieve(load_balancer_id: load_balancer_id) + + assert_equal Vultr::LoadBalancer, load_balancer.class + assert_equal "Example Load Balancer", load_balancer.label + end + + def test_update + load_balancer_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + body = {balancing_algorithm: "Changed"} + stub = stub_request("load-balancers/#{load_balancer_id}", method: :patch, body: body, response: stub_response(fixture: "load_balancers/update")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.load_balancers.update(load_balancer_id: load_balancer_id, **body) + end + + def test_delete + load_balancer_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("load-balancers/#{load_balancer_id}", method: :delete, response: stub_response(fixture: "load_balancers/delete")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.load_balancers.delete(load_balancer_id: load_balancer_id) + end + + def test_list_forwarding_rules + load_balancer_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("load-balancers/#{load_balancer_id}/forwarding-rules", response: stub_response(fixture: "load_balancers/list_forwarding_rules")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + forwarding_rules = client.load_balancers.list_forwarding_rules(load_balancer_id: load_balancer_id) + + assert Vultr::Collection, forwarding_rules.class + assert Vultr::Object, forwarding_rules.data.first.class + assert 1, forwarding_rules.total + end + + def test_create_forwarding_rule + load_balancer_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + body = {frontend_protocol: "http", frontend_port: 8080, backend_protocol: "http", backend_port: 80} + stub = stub_request("load-balancers/#{load_balancer_id}/forwarding-rules", method: :post, body: body, response: stub_response(fixture: "load_balancers/create_forwarding_rule")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.load_balancers.create_forwarding_rule(load_balancer_id: load_balancer_id, **body) + end + + def test_retrieve_forwarding_rule + load_balancer_id = "load-balancer-id" + forwarding_rule_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("load-balancers/#{load_balancer_id}/forwarding-rules/#{forwarding_rule_id}", response: stub_response(fixture: "load_balancers/retrieve_forwarding_rule")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + forwarding_rule = client.load_balancers.retrieve_forwarding_rule(load_balancer_id: load_balancer_id, forwarding_rule_id: forwarding_rule_id) + + assert Vultr::Object, forwarding_rule.class + assert "http", forwarding_rule.frontend_protocol + end + + def test_delete_forwarding_rule + load_balancer_id = "load-balancer-id" + forwarding_rule_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("load-balancers/#{load_balancer_id}/forwarding-rules/#{forwarding_rule_id}", method: :delete, response: stub_response(fixture: "load_balancers/delete_forwarding_rule")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.load_balancers.delete_forwarding_rule(load_balancer_id: load_balancer_id, forwarding_rule_id: forwarding_rule_id) + end + + def test_list_firewall_rules + load_balancer_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("load-balancers/#{load_balancer_id}/firewall-rules", response: stub_response(fixture: "load_balancers/list_firewall_rules")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + firewall_rules = client.load_balancers.list_firewall_rules(load_balancer_id: load_balancer_id) + + assert Vultr::Collection, firewall_rules.class + assert Vultr::Object, firewall_rules.data.first.class + assert 1, firewall_rules.total + end + + def test_retrieve_firewall_rule + load_balancer_id = "load-balancer-id" + firewall_rule_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("load-balancers/#{load_balancer_id}/firewall-rules/#{firewall_rule_id}", response: stub_response(fixture: "load_balancers/retrieve_firewall_rule")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + firewall_rule = client.load_balancers.retrieve_firewall_rule(load_balancer_id: load_balancer_id, firewall_rule_id: firewall_rule_id) + + assert Vultr::Object, firewall_rule + assert 80, firewall_rule.port + end +end From ae25cf7d79051d4de7e2b089eb324e3eaddda435 Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Sun, 5 Sep 2021 00:30:24 -0500 Subject: [PATCH 48/50] Add firewall tests --- lib/vultr/resources/firewall.rb | 4 +- test/fixtures/firewall/create.json | 11 +++ test/fixtures/firewall/create_rule.json | 9 +++ test/fixtures/firewall/delete.json | 1 + test/fixtures/firewall/delete_rule.json | 1 + test/fixtures/firewall/list.json | 20 ++++++ test/fixtures/firewall/list_rules.json | 22 ++++++ test/fixtures/firewall/retrieve.json | 11 +++ test/fixtures/firewall/retrieve_rule.json | 13 ++++ test/fixtures/firewall/update.json | 1 + test/vultr/resources/firewall_test.rb | 88 +++++++++++++++++++++++ 11 files changed, 179 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/firewall/create.json create mode 100644 test/fixtures/firewall/create_rule.json create mode 100644 test/fixtures/firewall/delete.json create mode 100644 test/fixtures/firewall/delete_rule.json create mode 100644 test/fixtures/firewall/list.json create mode 100644 test/fixtures/firewall/list_rules.json create mode 100644 test/fixtures/firewall/retrieve.json create mode 100644 test/fixtures/firewall/retrieve_rule.json create mode 100644 test/fixtures/firewall/update.json create mode 100644 test/vultr/resources/firewall_test.rb diff --git a/lib/vultr/resources/firewall.rb b/lib/vultr/resources/firewall.rb index a89b3e8..00b016f 100644 --- a/lib/vultr/resources/firewall.rb +++ b/lib/vultr/resources/firewall.rb @@ -2,7 +2,7 @@ module Vultr class FirewallResource < Resource def list(**params) response = get_request("firewalls", params: params) - Collection.from_response(response: response, key: "firewall_groups", type: FirewallGroup) + Collection.from_response(response, key: "firewall_groups", type: FirewallGroup) end def create(**attributes) @@ -24,7 +24,7 @@ def delete(firewall_group_id:) # Firewall Rules def list_rules(firewall_group_id:, **params) response = get_request("firewalls/#{firewall_group_id}/rules", params: params) - Collection.from_response(response: response, key: "firewall_rules", type: Object) + Collection.from_response(response, key: "firewall_rules", type: Object) end def create_rule(firewall_group_id:, **attributes) diff --git a/test/fixtures/firewall/create.json b/test/fixtures/firewall/create.json new file mode 100644 index 0000000..8b23e0d --- /dev/null +++ b/test/fixtures/firewall/create.json @@ -0,0 +1,11 @@ +{ + "firewall_group": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "description": "Example Firewall Group", + "date_created": "2020-10-10T01:56:20+00:00", + "date_modified": "2020-10-10T01:56:20+00:00", + "instance_count": 0, + "rule_count": 0, + "max_rule_count": 50 + } +} diff --git a/test/fixtures/firewall/create_rule.json b/test/fixtures/firewall/create_rule.json new file mode 100644 index 0000000..6819f7b --- /dev/null +++ b/test/fixtures/firewall/create_rule.json @@ -0,0 +1,9 @@ +{ + "ip_type": "v4", + "protocol": "tcp", + "port": "80", + "subnet": "192.0.2.0", + "subnet_size": 24, + "source": "", + "notes": "Example Firewall Rule" +} diff --git a/test/fixtures/firewall/delete.json b/test/fixtures/firewall/delete.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/fixtures/firewall/delete.json @@ -0,0 +1 @@ +{} diff --git a/test/fixtures/firewall/delete_rule.json b/test/fixtures/firewall/delete_rule.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/fixtures/firewall/delete_rule.json @@ -0,0 +1 @@ +{} diff --git a/test/fixtures/firewall/list.json b/test/fixtures/firewall/list.json new file mode 100644 index 0000000..699aace --- /dev/null +++ b/test/fixtures/firewall/list.json @@ -0,0 +1,20 @@ +{ + "firewall_groups": [ + { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "description": "Example Firewall Group", + "date_created": "2020-10-10T01:56:20+00:00", + "date_modified": "2020-10-10T01:59:20+00:00", + "instance_count": 0, + "rule_count": 0, + "max_rule_count": 50 + } + ], + "meta": { + "total": 1, + "links": { + "next": "", + "prev": "" + } + } +} diff --git a/test/fixtures/firewall/list_rules.json b/test/fixtures/firewall/list_rules.json new file mode 100644 index 0000000..c96a8ac --- /dev/null +++ b/test/fixtures/firewall/list_rules.json @@ -0,0 +1,22 @@ +{ + "firewall_rules": [ + { + "id": 1, + "ip_type": "v4", + "action": "accept", + "protocol": "tcp", + "port": "80", + "subnet": "192.0.2.0", + "subnet_size": 24, + "source": "", + "notes": "Example Firewall Rule" + } + ], + "meta": { + "total": 1, + "links": { + "next": "", + "prev": "" + } + } +} diff --git a/test/fixtures/firewall/retrieve.json b/test/fixtures/firewall/retrieve.json new file mode 100644 index 0000000..8b23e0d --- /dev/null +++ b/test/fixtures/firewall/retrieve.json @@ -0,0 +1,11 @@ +{ + "firewall_group": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "description": "Example Firewall Group", + "date_created": "2020-10-10T01:56:20+00:00", + "date_modified": "2020-10-10T01:56:20+00:00", + "instance_count": 0, + "rule_count": 0, + "max_rule_count": 50 + } +} diff --git a/test/fixtures/firewall/retrieve_rule.json b/test/fixtures/firewall/retrieve_rule.json new file mode 100644 index 0000000..3926d4f --- /dev/null +++ b/test/fixtures/firewall/retrieve_rule.json @@ -0,0 +1,13 @@ +{ + "firewall_rule": { + "id": 1, + "ip_type": "v4", + "action": "accept", + "protocol": "tcp", + "port": "80", + "subnet": "192.0.2.0", + "subnet_size": 24, + "source": "", + "notes": "Example Firewall Rule" + } +} diff --git a/test/fixtures/firewall/update.json b/test/fixtures/firewall/update.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/fixtures/firewall/update.json @@ -0,0 +1 @@ +{} diff --git a/test/vultr/resources/firewall_test.rb b/test/vultr/resources/firewall_test.rb new file mode 100644 index 0000000..af6ce9b --- /dev/null +++ b/test/vultr/resources/firewall_test.rb @@ -0,0 +1,88 @@ +# frozen_string_literal: true + +require "test_helper" + +class FirewallResourceTest < Minitest::Test + def test_list + stub = stub_request("firewalls", response: stub_response(fixture: "firewall/list")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + firewall_group = client.firewall.list + + assert_equal Vultr::Collection, firewall_group.class + assert_equal Vultr::FirewallGroup, firewall_group.data.first.class + assert_equal 1, firewall_group.total + end + + def test_create + body = {description: "Example Firewall Group"} + stub = stub_request("firewalls", method: :post, body: body, response: stub_response(fixture: "firewall/create")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + firewall_group = client.firewall.create(**body) + + assert_equal Vultr::FirewallGroup, firewall_group.class + assert_equal "Example Firewall Group", firewall_group.description + end + + def test_retrieve + id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("firewalls/#{id}", response: stub_response(fixture: "firewall/retrieve")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + firewall_group = client.firewall.retrieve(firewall_group_id: id) + + assert_equal Vultr::FirewallGroup, firewall_group.class + assert_equal "Example Firewall Group", firewall_group.description + end + + def test_update + id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + body = {description: "Example Firewall Group"} + stub = stub_request("firewalls/#{id}", method: :put, body: body, response: stub_response(fixture: "firewall/update")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.firewall.update(firewall_group_id: id, **body) + end + + def test_delete + id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("firewalls/#{id}", method: :delete, response: stub_response(fixture: "firewall/delete")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.firewall.delete(firewall_group_id: id) + end + + def test_list_rules + id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("firewalls/#{id}/rules", response: stub_response(fixture: "firewall/list_rules")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + rules = client.firewall.list_rules(firewall_group_id: id) + + assert Vultr::Collection, rules.class + assert Vultr::Object, rules.data.first.class + assert 1, rules.total + end + + def test_create_rule + id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + body = {ip_type: "v4", protocol: "tcp", subnet: "192.0.2.0", subnet_size: 24} + stub = stub_request("firewalls/#{id}/rules", method: :post, body: body, response: stub_response(fixture: "firewall/create_rule")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.firewall.create_rule(firewall_group_id: id, **body) + end + + def test_retrieve_rule + firewall_id = "id" + rule_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("firewalls/#{firewall_id}/rules/#{rule_id}", response: stub_response(fixture: "firewall/retrieve_rule")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + rule = client.firewall.retrieve_rule(firewall_group_id: firewall_id, firewall_rule_id: rule_id) + + assert Vultr::Object, rule.class + assert "tcp", rule.protocol + end + + def test_delete_forwarding_rule + firewall_id = "id" + rule_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("firewalls/#{firewall_id}/rules/#{rule_id}", method: :delete, response: stub_response(fixture: "firewall/delete_rule")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.firewall.delete_rule(firewall_group_id: firewall_id, firewall_rule_id: rule_id) + end +end From 2ce3cd453edfd0511e35cca320c67a1f90fbc398 Mon Sep 17 00:00:00 2001 From: Luke Gordon Date: Sun, 5 Sep 2021 04:24:39 -1000 Subject: [PATCH 49/50] Add tests for bare_metals resource (#30) * Add bare metal tests (#1) * test(bare_metal): test list * test(bare_metal): test create * fix(bare_metal): correcting typo from doc * test(bare_metal): test #retrieve * test(bare_metals): #update * test(bare_metals): delete * test(bare_metals): start * test(bare_metals): reboot * test(bare_metals): reinstall * test(bare_metals): halt * test(bare_metals): bandwidth * test(bare_metal): cleaning up assertions * test(bare_metals): user_data * test(bare_metals): upgrades * test(bare_metals): vnc * test(bare_metals): list_ipv4 * test(bare_metals): list_ipv6 * test(bare_metals): halt_instances * test(bare_metals): reboot_instances * test(bare_metals): start_instances * fix(bare_metal): addressing styling errors --- lib/vultr/resources/bare_metal.rb | 18 +- test/fixtures/bare_metals/bandwidth.json | 12 + test/fixtures/bare_metals/create.json | 26 +++ test/fixtures/bare_metals/list.json | 36 +++ test/fixtures/bare_metals/list_ipv4.json | 18 ++ test/fixtures/bare_metals/list_ipv6.json | 17 ++ test/fixtures/bare_metals/reinstall.json | 25 +++ test/fixtures/bare_metals/retrieve.json | 26 +++ test/fixtures/bare_metals/update.json | 26 +++ test/fixtures/bare_metals/upgrades.json | 47 ++++ test/fixtures/bare_metals/user_data.json | 5 + test/fixtures/bare_metals/vnc.json | 5 + test/vultr/resources/bare_metal_test.rb | 267 +++++++++++++++++++++++ 13 files changed, 519 insertions(+), 9 deletions(-) create mode 100644 test/fixtures/bare_metals/bandwidth.json create mode 100644 test/fixtures/bare_metals/create.json create mode 100644 test/fixtures/bare_metals/list.json create mode 100644 test/fixtures/bare_metals/list_ipv4.json create mode 100644 test/fixtures/bare_metals/list_ipv6.json create mode 100644 test/fixtures/bare_metals/reinstall.json create mode 100644 test/fixtures/bare_metals/retrieve.json create mode 100644 test/fixtures/bare_metals/update.json create mode 100644 test/fixtures/bare_metals/upgrades.json create mode 100644 test/fixtures/bare_metals/user_data.json create mode 100644 test/fixtures/bare_metals/vnc.json create mode 100644 test/vultr/resources/bare_metal_test.rb diff --git a/lib/vultr/resources/bare_metal.rb b/lib/vultr/resources/bare_metal.rb index b794fc2..f6547bc 100644 --- a/lib/vultr/resources/bare_metal.rb +++ b/lib/vultr/resources/bare_metal.rb @@ -22,19 +22,19 @@ def delete(baremetal_id:) end def start(baremetal_id:) - post_request("bare-metals/#{baremetal_id}/start") + post_request("bare-metals/#{baremetal_id}/start", body: {}) end def reboot(baremetal_id:) - post_request("bare-metals/#{baremetal_id}/reboot") + post_request("bare-metals/#{baremetal_id}/reboot", body: {}) end def reinstall(baremetal_id:) - BareMetal.new post_request("bare-metals/#{baremetal_id}/reinstall").body.dig("bare_metal") + BareMetal.new post_request("bare-metals/#{baremetal_id}/reinstall", body: {}).body.dig("bare_metal") end def halt(baremetal_id:) - post_request("bare-metals/#{baremetal_id}/halt") + post_request("bare-metals/#{baremetal_id}/halt", body: {}) end def bandwidth(baremetal_id:) @@ -50,16 +50,16 @@ def upgrades(baremetal_id:, **params) end def vnc(baremetal_id:) - Object.new get_request("bare-metals/#{baremetal_id}/vnc", params: params).body.dig("vnc") + Object.new get_request("bare-metals/#{baremetal_id}/vnc").body.dig("vnc") end - def list_ipv4(baremetal_id:, **params) - response = get_request("bare-metals/#{baremetal_id}/ipv4", params: params) + def list_ipv4(baremetal_id:) + response = get_request("bare-metals/#{baremetal_id}/ipv4") Collection.from_response(response, key: "ipv4s", type: Object) end - def list_ipv6(baremetal_id:, **params) - response = get_request("bare-metals/#{baremetal_id}/ipv6", params: params) + def list_ipv6(baremetal_id:) + response = get_request("bare-metals/#{baremetal_id}/ipv6") Collection.from_response(response, key: "ipv6s", type: Object) end diff --git a/test/fixtures/bare_metals/bandwidth.json b/test/fixtures/bare_metals/bandwidth.json new file mode 100644 index 0000000..077c613 --- /dev/null +++ b/test/fixtures/bare_metals/bandwidth.json @@ -0,0 +1,12 @@ +{ + "bandwidth": { + "2020-07-25": { + "incoming_bytes": 15989787, + "outgoing_bytes": 25327729 + }, + "2020-07-26": { + "incoming_bytes": 13964112, + "outgoing_bytes": 22257069 + } + } +} diff --git a/test/fixtures/bare_metals/create.json b/test/fixtures/bare_metals/create.json new file mode 100644 index 0000000..7ed9b77 --- /dev/null +++ b/test/fixtures/bare_metals/create.json @@ -0,0 +1,26 @@ +{ + "bare_metal": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "os": "Application", + "ram": "32768 MB", + "disk": "2x 240GB SSD", + "main_ip": "", + "cpu_count": 4, + "region": "ams", + "date_created": "2020-10-10T01:56:20+00:00", + "status": "pending", + "netmask_v4": "", + "gateway_v4": "", + "plan": "vbm-4c-32gb", + "v6_network": "", + "v6_main_ip": "", + "v6_network_size": 0, + "label": "Example Bare Metal", + "mac_address": 2199756823533, + "tag": "Example Tag", + "os_id": 186, + "app_id": 3, + "image_id": "", + "features": ["ipv6"] + } +} diff --git a/test/fixtures/bare_metals/list.json b/test/fixtures/bare_metals/list.json new file mode 100644 index 0000000..b2b99c5 --- /dev/null +++ b/test/fixtures/bare_metals/list.json @@ -0,0 +1,36 @@ +{ + "bare_metals": [ + { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "os": "Application", + "ram": "32768 MB", + "disk": "2x 240GB SSD", + "main_ip": "192.0.2.123", + "cpu_count": 4, + "region": "ams", + "default_password": "example-password", + "date_created": "2020-10-10T01:56:20+00:00", + "status": "active", + "netmask_v4": "255.255.254.0", + "gateway_v4": "192.0.2.1", + "plan": "vbm-4c-32gb", + "v6_network": "2001:0db8:5001:3990::", + "v6_main_ip": "2001:0db8:5001:3990:0ec4:7aff:fe8e:f97a", + "v6_network_size": 64, + "label": "Example Bare Metal", + "tag": "Example Tag", + "mac_address": 2199756823533, + "os_id": 186, + "app_id": 3, + "image_id": "", + "features": ["ipv6"] + } + ], + "meta": { + "total": 1, + "links": { + "next": "next", + "prev": "prev" + } + } +} diff --git a/test/fixtures/bare_metals/list_ipv4.json b/test/fixtures/bare_metals/list_ipv4.json new file mode 100644 index 0000000..7dd15dd --- /dev/null +++ b/test/fixtures/bare_metals/list_ipv4.json @@ -0,0 +1,18 @@ +{ + "ipv4s": [ + { + "ip": "192.0.2.123", + "netmask": "255.255.254.0", + "gateway": "192.0.2.1", + "type": "main_ip", + "reverse": "192.0.2.123.vultr.com" + } + ], + "meta": { + "total": 1, + "links": { + "next": "next", + "prev": "prev" + } + } +} diff --git a/test/fixtures/bare_metals/list_ipv6.json b/test/fixtures/bare_metals/list_ipv6.json new file mode 100644 index 0000000..75d88c1 --- /dev/null +++ b/test/fixtures/bare_metals/list_ipv6.json @@ -0,0 +1,17 @@ +{ + "ipv6s": [ + { + "ip": "2001:0db8:5001:3990:0ec4:7aff:fe8e:f97a", + "network": "2001:0db8:5001:3990::", + "network_size": 64, + "type": "main_ip" + } + ], + "meta": { + "total": 1, + "links": { + "next": "next", + "prev": "prev" + } + } +} diff --git a/test/fixtures/bare_metals/reinstall.json b/test/fixtures/bare_metals/reinstall.json new file mode 100644 index 0000000..9344253 --- /dev/null +++ b/test/fixtures/bare_metals/reinstall.json @@ -0,0 +1,25 @@ +{ + "bare_metal": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "os": "Application", + "ram": "32768 MB", + "disk": "2x 240GB SSD", + "main_ip": "192.0.2.123", + "cpu_count": 4, + "region": "ams", + "date_created": "2020-10-10T01:56:20+00:00", + "status": "pending", + "netmask_v4": "255.255.254.0", + "gateway_v4": "192.0.2.1", + "plan": "vbm-4c-32gb", + "v6_network": "2001:0db8:5001:3990::", + "v6_main_ip": "2001:0db8:5001:3990:0ec4:7aff:fe8e:f97a", + "v6_network_size": 64, + "label": "Example Bare Metal", + "mac_address": 2199756823533, + "tag": "Example Tag", + "os_id": 183, + "app_id": 3, + "image_id": "" + } +} diff --git a/test/fixtures/bare_metals/retrieve.json b/test/fixtures/bare_metals/retrieve.json new file mode 100644 index 0000000..5642db8 --- /dev/null +++ b/test/fixtures/bare_metals/retrieve.json @@ -0,0 +1,26 @@ +{ + "bare_metal": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "os": "Application", + "ram": "32768 MB", + "disk": "2x 240GB SSD", + "main_ip": "192.0.2.123", + "cpu_count": 4, + "region": "ams", + "date_created": "2020-10-10T01:56:20+00:00", + "status": "pending", + "netmask_v4": "255.255.254.0", + "gateway_v4": "192.0.2.1", + "plan": "vbm-4c-32gb", + "v6_network": "2001:0db8:5001:3990::", + "v6_main_ip": "2001:0db8:5001:3990:0ec4:7aff:fe8e:f97a", + "v6_network_size": 64, + "mac_address": 2199756823533, + "label": "Example Bare Metal", + "tag": "Example Tag", + "os_id": 183, + "app_id": 3, + "image_id": "", + "features": ["ipv6"] + } +} diff --git a/test/fixtures/bare_metals/update.json b/test/fixtures/bare_metals/update.json new file mode 100644 index 0000000..0dd80de --- /dev/null +++ b/test/fixtures/bare_metals/update.json @@ -0,0 +1,26 @@ +{ + "bare_metal": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "os": "Application", + "ram": "32768 MB", + "disk": "2x 240GB SSD", + "main_ip": "192.0.2.123", + "cpu_count": 4, + "region": "ams", + "date_created": "2020-10-10T01:56:20+00:00", + "status": "pending", + "netmask_v4": "255.255.254.0", + "gateway_v4": "192.0.2.1", + "plan": "vbm-4c-32gb", + "v6_network": "2001:0db8:5001:3990::", + "v6_main_ip": "2001:0db8:5001:3990:0ec4:7aff:fe8e:f97a", + "v6_network_size": 64, + "mac_address": 2199756823533, + "label": "Updated Bare Metal Label", + "tag": "Updated Tag", + "os_id": 183, + "app_id": 3, + "image_id": "", + "features": ["ipv6"] + } +} diff --git a/test/fixtures/bare_metals/upgrades.json b/test/fixtures/bare_metals/upgrades.json new file mode 100644 index 0000000..a4551f2 --- /dev/null +++ b/test/fixtures/bare_metals/upgrades.json @@ -0,0 +1,47 @@ +{ + "upgrades": { + "os": [ + { + "id": 127, + "name": "CentOS 6 x64", + "arch": "x64", + "family": "centos" + }, + { + "id": 147, + "name": "CentOS 6 i386", + "arch": "i386", + "family": "centos" + } + ], + "applications": [ + { + "id": 1, + "name": "LEMP", + "short_name": "lemp", + "deploy_name": "LEMP on CentOS 6 x64", + "type": "one-click", + "vendor": "vultr", + "image_id": "" + }, + { + "id": 39, + "name": "LEMP", + "short_name": "lemp", + "deploy_name": "LEMP on CentOS 7 x64", + "type": "one-click", + "vendor": "vultr", + "image_id": "" + }, + { + "id": 1028, + "name": "OpenLiteSpeed WordPress", + "short_name": "openlitespeedwordpress", + "deploy_name": "OpenLiteSpeed WordPress on Ubuntu 20.04 x64", + "type": "marketplace", + "vendor": "LiteSpeed_Technologies", + "image_id": "openlitespeed-wordpress" + } + ] + } +} diff --git a/test/fixtures/bare_metals/user_data.json b/test/fixtures/bare_metals/user_data.json new file mode 100644 index 0000000..990908a --- /dev/null +++ b/test/fixtures/bare_metals/user_data.json @@ -0,0 +1,5 @@ +{ + "user_data": { + "data": "QmFzZTY0IEV4YW1wbGUgRGF0YQ==" + } +} diff --git a/test/fixtures/bare_metals/vnc.json b/test/fixtures/bare_metals/vnc.json new file mode 100644 index 0000000..31d0ff6 --- /dev/null +++ b/test/fixtures/bare_metals/vnc.json @@ -0,0 +1,5 @@ +{ + "vnc": { + "url": "https://my.vultr.com/subs/baremetal/novnc/api.php?data=00example11223344" + } +} diff --git a/test/vultr/resources/bare_metal_test.rb b/test/vultr/resources/bare_metal_test.rb new file mode 100644 index 0000000..75e3547 --- /dev/null +++ b/test/vultr/resources/bare_metal_test.rb @@ -0,0 +1,267 @@ +# frozen_string_literal: true + +require "test_helper" + +class BareMetalResourceTest < Minitest::Test + def test_list + stub = stub_request("bare-metals", response: stub_response(fixture: "bare_metals/list")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + bare_metal = client.bare_metal.list + + assert_equal Vultr::Collection, bare_metal.class + assert_equal Vultr::BareMetal, bare_metal.data.first.class + assert_equal 1, bare_metal.total + assert_equal "next", bare_metal.next_cursor + assert_equal "prev", bare_metal.prev_cursor + end + + def test_create + body = {region: "ams", plan: "vbm-4c-32gb", label: "Example Bare Metal", app_id: 3, enable_ipv6: true} + stub = stub_request("bare-metals", method: :post, body: body, response: stub_response(fixture: "bare_metals/create", status: 201)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + bare_metal = client.bare_metal.create(**body) + + assert_equal bare_metal.region, "ams" + assert_equal bare_metal.date_created, Time.parse("2020-10-10T01:56:20+00:00") + assert_equal bare_metal.plan, "vbm-4c-32gb" + assert_equal bare_metal.label, "Example Bare Metal" + assert_equal bare_metal.features, ["ipv6"] + end + + def test_retrieve + baremetal_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("bare-metals/#{baremetal_id}", response: stub_response(fixture: "bare_metals/retrieve")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + bare_metal = client.bare_metal.retrieve(baremetal_id: baremetal_id) + + assert_equal bare_metal.id, baremetal_id + assert_equal bare_metal.os, "Application" + assert_equal bare_metal.ram, "32768 MB" + assert_equal bare_metal.disk, "2x 240GB SSD" + assert_equal bare_metal.main_ip, "192.0.2.123" + assert_equal bare_metal.cpu_count, 4 + assert_equal bare_metal.region, "ams" + assert_equal bare_metal.date_created, Time.parse("2020-10-10T01:56:20+00:00") + assert_equal bare_metal.status, "pending" + assert_equal bare_metal.netmask_v4, "255.255.254.0" + assert_equal bare_metal.gateway_v4, "192.0.2.1" + assert_equal bare_metal.plan, "vbm-4c-32gb" + assert_equal bare_metal.v6_network, "2001:0db8:5001:3990::" + assert_equal bare_metal.v6_main_ip, "2001:0db8:5001:3990:0ec4:7aff:fe8e:f97a" + assert_equal bare_metal.v6_network_size, 64 + assert_equal bare_metal.mac_address, 2199756823533 + assert_equal bare_metal.label, "Example Bare Metal" + assert_equal bare_metal.tag, "Example Tag" + assert_equal bare_metal.os_id, 183 + assert_equal bare_metal.app_id, 3 + assert_equal bare_metal.image_id, "" + assert_equal bare_metal.features, ["ipv6"] + end + + def test_update + baremetal_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + body = {label: "Updated Bare Metal Label", tag: "Updated Tag", user_data: "QmFzZTY0IEV4YW1wbGUgRGF0YQ=="} + stub = stub_request("bare-metals/#{baremetal_id}", method: :patch, body: body, response: stub_response(fixture: "bare_metals/update", status: 202)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + bare_metal = client.bare_metal.update(baremetal_id: baremetal_id, **body) + + assert_equal bare_metal.id, baremetal_id + assert_equal bare_metal.os, "Application" + assert_equal bare_metal.ram, "32768 MB" + assert_equal bare_metal.disk, "2x 240GB SSD" + assert_equal bare_metal.main_ip, "192.0.2.123" + assert_equal bare_metal.cpu_count, 4 + assert_equal bare_metal.region, "ams" + assert_equal bare_metal.date_created, Time.parse("2020-10-10T01:56:20+00:00") + assert_equal bare_metal.status, "pending" + assert_equal bare_metal.netmask_v4, "255.255.254.0" + assert_equal bare_metal.gateway_v4, "192.0.2.1" + assert_equal bare_metal.plan, "vbm-4c-32gb" + assert_equal bare_metal.v6_network, "2001:0db8:5001:3990::" + assert_equal bare_metal.v6_main_ip, "2001:0db8:5001:3990:0ec4:7aff:fe8e:f97a" + assert_equal bare_metal.v6_network_size, 64 + assert_equal bare_metal.mac_address, 2199756823533 + assert_equal bare_metal.label, "Updated Bare Metal Label" + assert_equal bare_metal.tag, "Updated Tag" + assert_equal bare_metal.os_id, 183 + assert_equal bare_metal.app_id, 3 + assert_equal bare_metal.image_id, "" + assert_equal bare_metal.features, ["ipv6"] + end + + def test_delete + baremetal_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("bare-metals/#{baremetal_id}", method: :delete, response: {}) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + assert client.bare_metal.delete(baremetal_id: baremetal_id) + end + + def test_start + baremetal_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("bare-metals/#{baremetal_id}/start", method: :post, body: {}, response: {}) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + assert client.bare_metal.start(baremetal_id: baremetal_id) + end + + def test_reboot + baremetal_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("bare-metals/#{baremetal_id}/reboot", method: :post, body: {}, response: {}) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + assert client.bare_metal.reboot(baremetal_id: baremetal_id) + end + + def test_reinstall + baremetal_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("bare-metals/#{baremetal_id}/reinstall", method: :post, body: {}, response: stub_response(fixture: "bare_metals/reinstall", status: 202)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + bare_metal = client.bare_metal.reinstall(baremetal_id: baremetal_id) + + assert_equal bare_metal.id, baremetal_id + assert_equal bare_metal.os, "Application" + assert_equal bare_metal.ram, "32768 MB" + assert_equal bare_metal.disk, "2x 240GB SSD" + assert_equal bare_metal.main_ip, "192.0.2.123" + assert_equal bare_metal.cpu_count, 4 + assert_equal bare_metal.region, "ams" + assert_equal bare_metal.date_created, Time.parse("2020-10-10T01:56:20+00:00") + assert_equal bare_metal.status, "pending" + assert_equal bare_metal.netmask_v4, "255.255.254.0" + assert_equal bare_metal.gateway_v4, "192.0.2.1" + assert_equal bare_metal.plan, "vbm-4c-32gb" + assert_equal bare_metal.v6_network, "2001:0db8:5001:3990::" + assert_equal bare_metal.v6_main_ip, "2001:0db8:5001:3990:0ec4:7aff:fe8e:f97a" + assert_equal bare_metal.v6_network_size, 64 + assert_equal bare_metal.label, "Example Bare Metal" + assert_equal bare_metal.mac_address, 2199756823533 + assert_equal bare_metal.tag, "Example Tag" + assert_equal bare_metal.os_id, 183 + assert_equal bare_metal.app_id, 3 + assert_equal bare_metal.image_id, "" + end + + def test_halt + baremetal_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("bare-metals/#{baremetal_id}/halt", method: :post, body: {}, response: {}) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + assert client.bare_metal.halt(baremetal_id: baremetal_id) + end + + def test_bandwidth + baremetal_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("bare-metals/#{baremetal_id}/bandwidth", method: :get, response: stub_response(fixture: "bare_metals/bandwidth")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + bandwidth = client.bare_metal.bandwidth(baremetal_id: baremetal_id) + + assert_equal bandwidth["2020-07-25"].class, OpenStruct + assert_equal bandwidth["2020-07-25"].incoming_bytes, 15989787 + assert_equal bandwidth["2020-07-25"].outgoing_bytes, 25327729 + + assert_equal bandwidth["2020-07-26"].class, OpenStruct + assert_equal bandwidth["2020-07-26"].incoming_bytes, 13964112 + assert_equal bandwidth["2020-07-26"].outgoing_bytes, 22257069 + end + + def test_user_data + baremetal_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("bare-metals/#{baremetal_id}/user-data", method: :get, response: stub_response(fixture: "bare_metals/user_data")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + user_data = client.bare_metal.user_data(baremetal_id: baremetal_id) + + assert_equal user_data.data, "QmFzZTY0IEV4YW1wbGUgRGF0YQ==" + end + + def test_upgrades + baremetal_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("bare-metals/#{baremetal_id}/upgrades", method: :get, response: stub_response(fixture: "bare_metals/upgrades")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + upgrades = client.bare_metal.upgrades(baremetal_id: baremetal_id) + + assert_equal upgrades.os.class, Array + assert_equal upgrades.applications.class, Array + end + + def test_vnc + baremetal_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("bare-metals/#{baremetal_id}/vnc", method: :get, response: stub_response(fixture: "bare_metals/vnc")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + vnc = client.bare_metal.vnc(baremetal_id: baremetal_id) + + assert_equal vnc.url, "https://my.vultr.com/subs/baremetal/novnc/api.php?data=00example11223344" + end + + def test_list_ipv4 + baremetal_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("bare-metals/#{baremetal_id}/ipv4", method: :get, response: stub_response(fixture: "bare_metals/list_ipv4")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + ipv4 = client.bare_metal.list_ipv4(baremetal_id: baremetal_id) + + assert_equal Vultr::Collection, ipv4.class + assert_equal Vultr::Object, ipv4.data.first.class + assert_equal 1, ipv4.total + assert_equal "next", ipv4.next_cursor + assert_equal "prev", ipv4.prev_cursor + end + + def test_list_ipv6 + baremetal_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("bare-metals/#{baremetal_id}/ipv6", method: :get, response: stub_response(fixture: "bare_metals/list_ipv6")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + ipv6 = client.bare_metal.list_ipv6(baremetal_id: baremetal_id) + + assert_equal Vultr::Collection, ipv6.class + assert_equal Vultr::Object, ipv6.data.first.class + assert_equal 1, ipv6.total + assert_equal "next", ipv6.next_cursor + assert_equal "prev", ipv6.prev_cursor + end + + def test_halt_instances + baremetal_ids = [ + "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "7f6f84ea-8f87-4d9e-af01-ac44db05911c", + "54a83807-64ce-42e8-a0da-4d6c31c5b93b" + ] + + stub = stub_request("bare-metals/halt", method: :post, body: {baremetal_ids: baremetal_ids}, response: {}) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + assert client.bare_metal.halt_instances(baremetal_ids: baremetal_ids) + end + + def test_reboot_instances + baremetal_ids = [ + "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "7f6f84ea-8f87-4d9e-af01-ac44db05911c", + "54a83807-64ce-42e8-a0da-4d6c31c5b93b" + ] + + stub = stub_request("bare-metals/reboot", method: :post, body: {baremetal_ids: baremetal_ids}, response: {}) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + assert client.bare_metal.reboot_instances(baremetal_ids: baremetal_ids) + end + + def test_start_instances + baremetal_ids = [ + "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "7f6f84ea-8f87-4d9e-af01-ac44db05911c", + "54a83807-64ce-42e8-a0da-4d6c31c5b93b" + ] + + stub = stub_request("bare-metals/start", method: :post, body: {baremetal_ids: baremetal_ids}, response: {}) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + assert client.bare_metal.start_instances(baremetal_ids: baremetal_ids) + end +end From 9e92cce720ff2223bcc3d529e508bfe61ae3256f Mon Sep 17 00:00:00 2001 From: Luke Gordon Date: Sun, 5 Sep 2021 15:26:54 -1000 Subject: [PATCH 50/50] Resource tests for Kubernetes & Block Storage (#32) * Add bare metal tests (#1) * test(bare_metal): test list * test(bare_metal): test create * fix(bare_metal): correcting typo from doc * test(bare_metal): test #retrieve * test(bare_metals): #update * test(bare_metals): delete * test(bare_metals): start * test(bare_metals): reboot * test(bare_metals): reinstall * test(bare_metals): halt * test(bare_metals): bandwidth * test(bare_metal): cleaning up assertions * test(bare_metals): user_data * test(bare_metals): upgrades * test(bare_metals): vnc * test(bare_metals): list_ipv4 * test(bare_metals): list_ipv6 * test(bare_metals): halt_instances * test(bare_metals): reboot_instances * test(bare_metals): start_instances * fix(bare_metal): addressing styling errors * Kubernetes resource tests (#2) * test(kubernetes): assertions for #list * test(kubernetes): assertions for #create * test(kubernetes): assertions for #retrieve * test(kubernetes): assertions for #update * test(kubernetes): assertions for #delete * test(kubernetes): assertions for #config * test(kubernetes): assertions for #list_resources * test(kubernetes): assertions for #list_node_pools * test(kubernetes): assertions for #retrieve_node_pool * test(kubernetes): assertions for #create_node_pool * test(kubernetes): assertions for #update_node_pool * test(kubernetes): assertions for #delete_node_pool * test(kubernetes): assertions for #delete_node_pool_instance * test(kubernetes): assertions for #recycle_node_pool_instance * fix(kubernetes): address style errors * fix(kubernetes): adding pagination to #list_node_pools * test(block_storage): covers all actions for resource * fix(block_storage): correcting style errors * fix(blocks): remove patch file --- lib/vultr/resources/block_storage.rb | 8 +- lib/vultr/resources/kubernetes.rb | 7 +- test/fixtures/block_storage/attach.json | 4 + test/fixtures/block_storage/create.json | 13 ++ test/fixtures/block_storage/detach.json | 4 + test/fixtures/block_storage/list.json | 22 +++ test/fixtures/block_storage/retrieve.json | 13 ++ test/fixtures/kubernetes/config.json | 3 + test/fixtures/kubernetes/create.json | 39 ++++ .../fixtures/kubernetes/create_node_pool.json | 25 +++ test/fixtures/kubernetes/list.json | 85 ++++++++ test/fixtures/kubernetes/list_node_pools.json | 56 ++++++ test/fixtures/kubernetes/list_resources.json | 26 +++ test/fixtures/kubernetes/retrieve.json | 39 ++++ .../kubernetes/retrieve_node_pool.json | 25 +++ .../fixtures/kubernetes/update_node_pool.json | 19 ++ test/vultr/resources/block_storage_test.rb | 85 ++++++++ test/vultr/resources/kubernetes_test.rb | 185 ++++++++++++++++++ 18 files changed, 650 insertions(+), 8 deletions(-) create mode 100644 test/fixtures/block_storage/attach.json create mode 100644 test/fixtures/block_storage/create.json create mode 100644 test/fixtures/block_storage/detach.json create mode 100644 test/fixtures/block_storage/list.json create mode 100644 test/fixtures/block_storage/retrieve.json create mode 100644 test/fixtures/kubernetes/config.json create mode 100644 test/fixtures/kubernetes/create.json create mode 100644 test/fixtures/kubernetes/create_node_pool.json create mode 100644 test/fixtures/kubernetes/list.json create mode 100644 test/fixtures/kubernetes/list_node_pools.json create mode 100644 test/fixtures/kubernetes/list_resources.json create mode 100644 test/fixtures/kubernetes/retrieve.json create mode 100644 test/fixtures/kubernetes/retrieve_node_pool.json create mode 100644 test/fixtures/kubernetes/update_node_pool.json create mode 100644 test/vultr/resources/block_storage_test.rb create mode 100644 test/vultr/resources/kubernetes_test.rb diff --git a/lib/vultr/resources/block_storage.rb b/lib/vultr/resources/block_storage.rb index 2c7b02f..14b89c9 100644 --- a/lib/vultr/resources/block_storage.rb +++ b/lib/vultr/resources/block_storage.rb @@ -21,12 +21,12 @@ def delete(block_id:) delete_request("blocks/#{block_id}") end - def attach(block_id:, **params) - post_request("blocks/#{block_id}/attach", body: params) + def attach(block_id:) + post_request("blocks/#{block_id}/attach", body: {}) end - def detach(block_id:, **params) - post_request("blocks/#{block_id}/detach", body: params) + def detach(block_id:) + post_request("blocks/#{block_id}/detach", body: {}) end end end diff --git a/lib/vultr/resources/kubernetes.rb b/lib/vultr/resources/kubernetes.rb index 779a80c..8e5ea39 100644 --- a/lib/vultr/resources/kubernetes.rb +++ b/lib/vultr/resources/kubernetes.rb @@ -25,9 +25,8 @@ def config(vke_id:) Object.new get_request("kubernetes/clusters/#{vke_id}/config").body end - def list_resources(vke_id:, **params) - response = get_request("kubernetes/clusters/#{vke_id}/resources", params: params) - Collection.from_response(response, key: "resources", type: Object) + def list_resources(vke_id:) + Object.new get_request("kubernetes/clusters/#{vke_id}/resources").body.dig("resources") end def list_node_pools(vke_id:, **params) @@ -56,7 +55,7 @@ def delete_node_pool_instance(vke_id:, nodepool_id:, node_id:) end def recycle_node_pool_instance(vke_id:, nodepool_id:, node_id:) - post_request("kubernetes/clusters/#{vke_id}/node-pools/#{nodepool_id}/nodes/#{node_id}/recycle") + post_request("kubernetes/clusters/#{vke_id}/node-pools/#{nodepool_id}/nodes/#{node_id}/recycle", body: {}) end end end diff --git a/test/fixtures/block_storage/attach.json b/test/fixtures/block_storage/attach.json new file mode 100644 index 0000000..8fe1c06 --- /dev/null +++ b/test/fixtures/block_storage/attach.json @@ -0,0 +1,4 @@ +{ + "instance_id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "live": true +} diff --git a/test/fixtures/block_storage/create.json b/test/fixtures/block_storage/create.json new file mode 100644 index 0000000..6dad0e1 --- /dev/null +++ b/test/fixtures/block_storage/create.json @@ -0,0 +1,13 @@ +{ + "block": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "date_created": "2020-10-10T01:56:20+00:00", + "cost": 5, + "status": "active", + "size_gb": 50, + "region": "ewr", + "attached_to_instance": "742c9913-d088-4d67-bc61-5a10e922fbd1", + "label": "Example Block Storage", + "mount_id": "ewr-example112233" + } +} diff --git a/test/fixtures/block_storage/detach.json b/test/fixtures/block_storage/detach.json new file mode 100644 index 0000000..8fe1c06 --- /dev/null +++ b/test/fixtures/block_storage/detach.json @@ -0,0 +1,4 @@ +{ + "instance_id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "live": true +} diff --git a/test/fixtures/block_storage/list.json b/test/fixtures/block_storage/list.json new file mode 100644 index 0000000..f18dcd5 --- /dev/null +++ b/test/fixtures/block_storage/list.json @@ -0,0 +1,22 @@ +{ + "blocks": [ + { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "date_created": "2020-10-10T01:56:20+00:00", + "cost": 5, + "status": "pending", + "size_gb": 50, + "region": "ewr", + "attached_to_instance": "742c9913-d088-4d67-bc61-5a10e922fbd1", + "label": "Example Block Storage", + "mount_id": "ewr-example112233" + } + ], + "meta": { + "total": 1, + "links": { + "next": "next", + "prev": "prev" + } + } +} diff --git a/test/fixtures/block_storage/retrieve.json b/test/fixtures/block_storage/retrieve.json new file mode 100644 index 0000000..6dad0e1 --- /dev/null +++ b/test/fixtures/block_storage/retrieve.json @@ -0,0 +1,13 @@ +{ + "block": { + "id": "cb676a46-66fd-4dfb-b839-443f2e6c0b60", + "date_created": "2020-10-10T01:56:20+00:00", + "cost": 5, + "status": "active", + "size_gb": 50, + "region": "ewr", + "attached_to_instance": "742c9913-d088-4d67-bc61-5a10e922fbd1", + "label": "Example Block Storage", + "mount_id": "ewr-example112233" + } +} diff --git a/test/fixtures/kubernetes/config.json b/test/fixtures/kubernetes/config.json new file mode 100644 index 0000000..c06108d --- /dev/null +++ b/test/fixtures/kubernetes/config.json @@ -0,0 +1,3 @@ +{ + "kube_config": "kube_config" +} diff --git a/test/fixtures/kubernetes/create.json b/test/fixtures/kubernetes/create.json new file mode 100644 index 0000000..e75cfcc --- /dev/null +++ b/test/fixtures/kubernetes/create.json @@ -0,0 +1,39 @@ +{ + "vke_cluster": { + "id": "455dcd32-e621-48ee-a10e-0cb5f754e13e", + "label": "vke", + "date_created": "2021-07-07T22:57:01+00:00", + "cluster_subnet": "10.244.0.0/16", + "service_subnet": "10.96.0.0/12", + "ip": "0.0.0.0", + "endpoint": "455dcd32-e621-48ee-a10e-0cb5f754e13e.vultr-k8s.com", + "version": "v1.20.0+1", + "region": "lax", + "status": "pending", + "node_pools": [ + { + "id": "11e4443a-f92a-46d6-94c8-61c1a1a7514e", + "date_created": "2021-07-07T22:57:01+00:00", + "date_updated": "2021-07-07T22:58:44+00:00", + "label": "my-label-48770259", + "plan_id": "vc2-1c-2gb", + "status": "pending", + "count": 2, + "nodes": [ + { + "id": "43eda5c8-67f7-4c63-88bc-2f568b48b2b0", + "label": "my-label-48770259-6ac60e6313dd1", + "date_created": "2021-07-07T22:57:01+00:00", + "status": "pending" + }, + { + "id": "15a7893d-d584-45d5-a74c-d9f46866aa3c", + "label": "my-label-48770259-6ac60e6313ddc", + "date_created": "2021-07-07T22:57:01+00:00", + "status": "pending" + } + ] + } + ] + } +} diff --git a/test/fixtures/kubernetes/create_node_pool.json b/test/fixtures/kubernetes/create_node_pool.json new file mode 100644 index 0000000..5067c76 --- /dev/null +++ b/test/fixtures/kubernetes/create_node_pool.json @@ -0,0 +1,25 @@ +{ + "node_pool": { + "id": "4130764b-5276-4552-546f-32513239732b", + "date_created": "2021-07-07T23:29:18+00:00", + "date_updated": "2021-07-08T23:29:18+00:00", + "label": "nodepool-48770716", + "plan_id": "vc2-1c-2gb", + "status": "pending", + "count": 2, + "nodes": [ + { + "id": "2f863151-d784-4184-804e-31e4e60945bd", + "label": "nodepool-48770716-6c360e638ce61", + "date_created": "2021-07-07T23:29:18+00:00", + "status": "pending" + }, + { + "id": "73a459dc-293f-4c2b-92f7-61be459a033b", + "label": "nodepool-48770716-6c360e638ce6c", + "date_created": "2021-07-07T23:29:18+00:00", + "status": "pending" + } + ] + } +} diff --git a/test/fixtures/kubernetes/list.json b/test/fixtures/kubernetes/list.json new file mode 100644 index 0000000..1928cb1 --- /dev/null +++ b/test/fixtures/kubernetes/list.json @@ -0,0 +1,85 @@ +{ + "vke_clusters": [ + { + "id": "c907e832-3080-48a6-a54d-7379e645c0b7", + "label": "my-vke", + "date_created": "2021-07-02T12:12:43+00:00", + "cluster_subnet": "10.244.0.0/16", + "service_subnet": "10.96.0.0/12", + "ip": "8.9.30.155", + "endpoint": "c907e832-3080-48a6-a54d-7379e645c0b7.vultr-k8s.com", + "version": "v1.20.0+1", + "region": "ewr", + "status": "active", + "node_pools": [ + { + "id": "74de1914-63ea-4a78-9da5-b7220063c701", + "date_created": "2021-07-02T12:12:44+00:00", + "date_updated": "2021-07-03T12:12:44+00:00", + "label": "nodepool-48597451", + "plan_id": "vc2-1c-2gb", + "status": "active", + "count": 2, + "nodes": [ + { + "id": "cafd4673-2a62-49c4-a045-44d05ecc0a7b", + "label": "nodepool-48597451-6a960df02bc1b", + "date_created": "2021-07-02T12:12:44+00:00", + "status": "active" + }, + { + "id": "5fc5ae88-f73e-46b5-9fa1-ac5ed8dcd33c", + "label": "nodepool-48597451-6a960df02bc25", + "date_created": "2021-07-02T12:12:44+00:00", + "status": "active" + } + ] + } + ] + }, + { + "id": "455dcd32-e621-48ee-a10e-0cb5f754e13e", + "label": "vke", + "date_created": "2021-07-07T22:57:01+00:00", + "cluster_subnet": "10.244.0.0/16", + "service_subnet": "10.96.0.0/12", + "ip": "207.246.109.187", + "endpoint": "455dcd32-e621-48ee-a10e-0cb5f754e13e.vultr-k8s.com", + "version": "v1.20.0+1", + "region": "lax", + "status": "active", + "node_pools": [ + { + "id": "11e4443a-f92a-46d6-94c8-61c1a1a7514e", + "date_created": "2021-07-07T22:57:01+00:00", + "date_updated": "2021-07-08T12:12:44+00:00", + "label": "my-label-48770259", + "plan_id": "vc2-1c-2gb", + "status": "active", + "count": 2, + "nodes": [ + { + "id": "43eda5c8-67f7-4c63-88bc-2f568b48b2b0", + "label": "my-label-48770259-6ac60e6313dd1", + "date_created": "2021-07-07T22:57:01+00:00", + "status": "active" + }, + { + "id": "15a7893d-d584-45d5-a74c-d9f46866aa3c", + "label": "my-label-48770259-6ac60e6313ddc", + "date_created": "2021-07-07T22:57:01+00:00", + "status": "active" + } + ] + } + ] + } + ], + "meta": { + "total": 2, + "links": { + "next": "next", + "prev": "prev" + } + } +} diff --git a/test/fixtures/kubernetes/list_node_pools.json b/test/fixtures/kubernetes/list_node_pools.json new file mode 100644 index 0000000..96d4511 --- /dev/null +++ b/test/fixtures/kubernetes/list_node_pools.json @@ -0,0 +1,56 @@ +{ + "node_pools": [ + { + "id": "e97bdee9-2781-4f31-be03-60fc75f399ae", + "date_created": "2021-07-07T23:27:08+00:00", + "date_updated": "2021-07-08T12:12:44+00:00", + "label": "my-label-48770703", + "plan_id": "vc2-1c-2gb", + "status": "active", + "count": 2, + "nodes": [ + { + "id": "f2e11430-76e5-4dc6-a1c9-ef5682c21ddf", + "label": "my-label-48770703-44060e6384c45", + "date_created": "2021-07-07T23:27:08+00:00", + "status": "active" + }, + { + "id": "c0a160eb-a7bc-4377-a6fb-52a9531167ca", + "label": "my-label-48770703-44060e6384c50", + "date_created": "2021-07-07T23:27:08+00:00", + "status": "active" + } + ] + }, + { + "id": "4130764b-5276-4552-546f-32513239732b", + "date_created": "2021-07-07T23:29:18+00:00", + "label": "nodepool-48770716", + "plan_id": "vc2-1c-2gb", + "status": "active", + "count": 2, + "nodes": [ + { + "id": "2f863151-d784-4184-804e-31e4e60945bd", + "label": "nodepool-48770716-6c360e638ce61", + "date_created": "2021-07-07T23:29:18+00:00", + "status": "active" + }, + { + "id": "73a459dc-293f-4c2b-92f7-61be459a033b", + "label": "nodepool-48770716-6c360e638ce6c", + "date_created": "2021-07-07T23:29:18+00:00", + "status": "active" + } + ] + } + ], + "meta": { + "total": 2, + "links": { + "next": "next", + "prev": "prev" + } + } +} diff --git a/test/fixtures/kubernetes/list_resources.json b/test/fixtures/kubernetes/list_resources.json new file mode 100644 index 0000000..1b7c008 --- /dev/null +++ b/test/fixtures/kubernetes/list_resources.json @@ -0,0 +1,26 @@ +{ + "resources": { + "block_storage": [ + { + "id": "29479a12-6edd-48cf-a883-24eccafab094", + "label": "29479a12-6edd-48cf-a883-24eccafab094", + "date_created": "2021-07-29T16:41:07+00:00", + "status": "active" + }, + { + "id": "0fa3097e-aef9-475e-958a-56f697ed3998", + "label": "0fa3097e-aef9-475e-958a-56f697ed3998", + "date_created": "2021-08-04T15:34:50+00:00", + "status": "pending" + } + ], + "load_balancer": [ + { + "id": "369ed902-2ec4-4a22-b959-cb1709394c3a", + "label": "369ed902-2ec4-4a22-b959-cb1709394c3a", + "date_created": "2021-07-29T16:46:12+00:00", + "status": "active" + } + ] + } +} diff --git a/test/fixtures/kubernetes/retrieve.json b/test/fixtures/kubernetes/retrieve.json new file mode 100644 index 0000000..1b80bbb --- /dev/null +++ b/test/fixtures/kubernetes/retrieve.json @@ -0,0 +1,39 @@ +{ + "vke_cluster": { + "id": "455dcd32-e621-48ee-a10e-0cb5f754e13e", + "label": "vke", + "date_created": "2021-07-07T22:57:01+00:00", + "cluster_subnet": "10.244.0.0/16", + "service_subnet": "10.96.0.0/12", + "ip": "207.246.109.187", + "endpoint": "455dcd32-e621-48ee-a10e-0cb5f754e13e.vultr-k8s.com", + "version": "v1.20.0+1", + "region": "lax", + "status": "active", + "node_pools": [ + { + "id": "11e4443a-f92a-46d6-94c8-61c1a1a7514e", + "date_created": "2021-07-07T22:57:01+00:00", + "date_updated": "2021-07-08T12:12:44+00:00", + "label": "my-label-48770259", + "plan_id": "vc2-1c-2gb", + "status": "active", + "count": 2, + "nodes": [ + { + "id": "43eda5c8-67f7-4c63-88bc-2f568b48b2b0", + "label": "my-label-48770259-6ac60e6313dd1", + "date_created": "2021-07-07T22:57:01+00:00", + "status": "active" + }, + { + "id": "15a7893d-d584-45d5-a74c-d9f46866aa3c", + "label": "my-label-48770259-6ac60e6313ddc", + "date_created": "2021-07-07T22:57:01+00:00", + "status": "active" + } + ] + } + ] + } +} diff --git a/test/fixtures/kubernetes/retrieve_node_pool.json b/test/fixtures/kubernetes/retrieve_node_pool.json new file mode 100644 index 0000000..3627de8 --- /dev/null +++ b/test/fixtures/kubernetes/retrieve_node_pool.json @@ -0,0 +1,25 @@ +{ + "node_pool": { + "id": "e97bdee9-2781-4f31-be03-60fc75f399ae", + "date_created": "2021-07-07T23:27:08+00:00", + "date_updated": "2021-07-08T12:12:44+00:00", + "label": "my-label-48770703", + "plan_id": "vc2-1c-2gb", + "status": "active", + "count": 2, + "nodes": [ + { + "id": "f2e11430-76e5-4dc6-a1c9-ef5682c21ddf", + "label": "my-label-48770703-44060e6384c45", + "date_created": "2021-07-07T23:27:08+00:00", + "status": "active" + }, + { + "id": "c0a160eb-a7bc-4377-a6fb-52a9531167ca", + "label": "my-label-48770703-44060e6384c50", + "date_created": "2021-07-07T23:27:08+00:00", + "status": "active" + } + ] + } +} diff --git a/test/fixtures/kubernetes/update_node_pool.json b/test/fixtures/kubernetes/update_node_pool.json new file mode 100644 index 0000000..af3a644 --- /dev/null +++ b/test/fixtures/kubernetes/update_node_pool.json @@ -0,0 +1,19 @@ +{ + "node_pool": { + "id": "e97bdee9-2781-4f31-be03-60fc75f399ae", + "date_created": "2021-07-07T23:27:08+00:00", + "date_updated": "2021-07-08T12:12:44+00:00", + "label": "my-label-48770703", + "plan_id": "vc2-1c-2gb", + "status": "active", + "count": 1, + "nodes": [ + { + "id": "f2e11430-76e5-4dc6-a1c9-ef5682c21ddf", + "label": "my-label-48770703-44060e6384c45", + "date_created": "2021-07-07T23:27:08+00:00", + "status": "active" + } + ] + } +} diff --git a/test/vultr/resources/block_storage_test.rb b/test/vultr/resources/block_storage_test.rb new file mode 100644 index 0000000..75ec5eb --- /dev/null +++ b/test/vultr/resources/block_storage_test.rb @@ -0,0 +1,85 @@ +# frozen_string_literal: true + +require "test_helper" + +class BlockStorageResourceTest < Minitest::Test + def test_list + stub = stub_request("blocks", response: stub_response(fixture: "block_storage/list")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + blocks = client.block_storage.list + + assert_equal Vultr::Collection, blocks.class + assert_equal Vultr::BlockStorage, blocks.data.first.class + assert_equal 1, blocks.total + assert_equal "next", blocks.next_cursor + assert_equal "prev", blocks.prev_cursor + end + + def test_create + body = {region: "ewr", size_gb: 50, label: "Example Block Storage"} + stub = stub_request("blocks", method: :post, body: body, response: stub_response(fixture: "block_storage/create", status: 202)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + blocks = client.block_storage.create(**body) + + assert_equal blocks.id, "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + assert_equal blocks.date_created, Time.parse("2020-10-10T01:56:20+00:00") + assert_equal blocks.cost, 5 + assert_equal blocks.status, "active" + assert_equal blocks.size_gb, 50 + assert_equal blocks.region, "ewr" + assert_equal blocks.attached_to_instance, "742c9913-d088-4d67-bc61-5a10e922fbd1" + assert_equal blocks.label, "Example Block Storage" + assert_equal blocks.mount_id, "ewr-example112233" + end + + def test_retrieve + block_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + body = {region: "ewr", size_gb: 50, label: "Example Block Storage"} + stub = stub_request("blocks/#{block_id}", body: body, response: stub_response(fixture: "block_storage/retrieve")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + blocks = client.block_storage.retrieve(block_id: block_id) + + assert_equal blocks.id, "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + assert_equal blocks.date_created, Time.parse("2020-10-10T01:56:20+00:00") + assert_equal blocks.cost, 5 + assert_equal blocks.status, "active" + assert_equal blocks.size_gb, 50 + assert_equal blocks.region, "ewr" + assert_equal blocks.attached_to_instance, "742c9913-d088-4d67-bc61-5a10e922fbd1" + assert_equal blocks.label, "Example Block Storage" + assert_equal blocks.mount_id, "ewr-example112233" + end + + def test_update + block_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + body = {size_gb: 50, label: "Example Block Storage"} + stub = stub_request("blocks/#{block_id}", method: :patch, body: body, response: {}) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + assert client.block_storage.update(block_id: block_id, **body) + end + + def test_delete + block_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("blocks/#{block_id}", method: :delete, response: {}) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + assert client.block_storage.delete(block_id: block_id) + end + + def test_attach + block_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("blocks/#{block_id}/attach", method: :post, response: stub_response(fixture: "block_storage/attach")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + assert client.block_storage.attach(block_id: block_id) + end + + def test_detach + block_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("blocks/#{block_id}/detach", method: :post, response: stub_response(fixture: "block_storage/detach")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + assert client.block_storage.detach(block_id: block_id) + end +end diff --git a/test/vultr/resources/kubernetes_test.rb b/test/vultr/resources/kubernetes_test.rb new file mode 100644 index 0000000..dbe65e8 --- /dev/null +++ b/test/vultr/resources/kubernetes_test.rb @@ -0,0 +1,185 @@ +# frozen_string_literal: true + +require "test_helper" + +class KubernetesResourceTest < Minitest::Test + def test_list + stub = stub_request("kubernetes/clusters", response: stub_response(fixture: "kubernetes/list")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + kubernetes = client.kubernetes.list + + assert_equal Vultr::Collection, kubernetes.class + assert_equal Vultr::KubernetesCluster, kubernetes.data.first.class + assert_equal 2, kubernetes.total + assert_equal "next", kubernetes.next_cursor + assert_equal "prev", kubernetes.prev_cursor + end + + def test_create + body = {label: "vke", region: "lax", version: "v1.20.0+1", node_pools: [{node_quantity: 2, label: "my-label", plan: "vc2-1c-2gb"}]} + stub = stub_request("kubernetes/clusters", method: :post, body: body, response: stub_response(fixture: "kubernetes/create", status: 201)) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + kubernetes = client.kubernetes.create(**body) + + assert_equal kubernetes.id, "455dcd32-e621-48ee-a10e-0cb5f754e13e" + assert_equal kubernetes.label, "vke" + assert_equal kubernetes.date_created, Time.parse("2021-07-07T22:57:01+00:00") + assert_equal kubernetes.cluster_subnet, "10.244.0.0/16" + assert_equal kubernetes.service_subnet, "10.96.0.0/12" + assert_equal kubernetes.ip, "0.0.0.0" + assert_equal kubernetes.endpoint, "455dcd32-e621-48ee-a10e-0cb5f754e13e.vultr-k8s.com" + assert_equal kubernetes.version, "v1.20.0+1" + assert_equal kubernetes.region, "lax" + assert_equal kubernetes.status, "pending" + assert_equal kubernetes.node_pools.class, Array + end + + def test_retrieve + vke_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("kubernetes/clusters/#{vke_id}", response: stub_response(fixture: "kubernetes/retrieve")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + vke_cluster = client.kubernetes.retrieve(vke_id: vke_id) + + assert_equal vke_cluster.id, "455dcd32-e621-48ee-a10e-0cb5f754e13e" + assert_equal vke_cluster.label, "vke" + assert_equal vke_cluster.date_created, Time.parse("2021-07-07T22:57:01+00:00") + assert_equal vke_cluster.cluster_subnet, "10.244.0.0/16" + assert_equal vke_cluster.service_subnet, "10.96.0.0/12" + assert_equal vke_cluster.ip, "207.246.109.187" + assert_equal vke_cluster.endpoint, "455dcd32-e621-48ee-a10e-0cb5f754e13e.vultr-k8s.com" + assert_equal vke_cluster.version, "v1.20.0+1" + assert_equal vke_cluster.region, "lax" + assert_equal vke_cluster.status, "active" + assert_equal vke_cluster.node_pools.class, Array + end + + def test_update + vke_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + body = {label: "My New Label"} + stub = stub_request("kubernetes/clusters/#{vke_id}", method: :put, body: body, response: {}) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + assert client.kubernetes.update(vke_id: vke_id, **body) + end + + def test_delete + vke_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("kubernetes/clusters/#{vke_id}", method: :delete, response: {}) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + assert client.kubernetes.delete(vke_id: vke_id) + end + + def test_config + vke_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("kubernetes/clusters/#{vke_id}/config", response: stub_response(fixture: "kubernetes/config")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + vke_cluster = client.kubernetes.config(vke_id: vke_id) + + assert_equal vke_cluster.kube_config, "kube_config" + end + + def test_list_resources + vke_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("kubernetes/clusters/#{vke_id}/resources", response: stub_response(fixture: "kubernetes/list_resources")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + vke_cluster = client.kubernetes.list_resources(vke_id: vke_id) + + assert_equal vke_cluster.block_storage.class, Array + assert_equal vke_cluster.load_balancer.class, Array + end + + def test_list_node_pools + vke_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + stub = stub_request("kubernetes/clusters/#{vke_id}/node-pools", response: stub_response(fixture: "kubernetes/list_node_pools")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + node_pools = client.kubernetes.list_node_pools(vke_id: vke_id) + + assert_equal Vultr::Collection, node_pools.class + assert_equal Vultr::Object, node_pools.data.first.class + assert_equal 2, node_pools.total + assert_equal "next", node_pools.next_cursor + assert_equal "prev", node_pools.prev_cursor + end + + def test_retrieve_node_pool + vke_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + nodepool_id = "e97bdee9-2781-4f31-be03-60fc75f399ae" + + stub = stub_request("kubernetes/clusters/#{vke_id}/node-pools/#{nodepool_id}", response: stub_response(fixture: "kubernetes/retrieve_node_pool")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + node_pool = client.kubernetes.retrieve_node_pool(vke_id: vke_id, nodepool_id: nodepool_id) + + assert_equal node_pool.id, "e97bdee9-2781-4f31-be03-60fc75f399ae" + assert_equal node_pool.date_created, Time.parse("2021-07-07T23:27:08+00:00") + assert_equal node_pool.date_updated, Time.parse("2021-07-08T12:12:44+00:00") + assert_equal node_pool.label, "my-label-48770703" + assert_equal node_pool.plan_id, "vc2-1c-2gb" + assert_equal node_pool.status, "active" + assert_equal node_pool.count, 2 + assert_equal node_pool.nodes.class, Array + end + + def test_create_node_pool + vke_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + body = {node_quantity: 2, label: "nodepool", paln: "vc2-lc-2gb"} + stub = stub_request("kubernetes/clusters/#{vke_id}/node-pools", method: :post, body: body, response: stub_response(fixture: "kubernetes/create_node_pool")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + node_pool = client.kubernetes.create_node_pool(vke_id: vke_id, **body) + + assert_equal node_pool.id, "4130764b-5276-4552-546f-32513239732b" + assert_equal node_pool.date_created, Time.parse("2021-07-07T23:29:18+00:00") + assert_equal node_pool.date_updated, Time.parse("2021-07-08T23:29:18+00:00") + assert_equal node_pool.label, "nodepool-48770716" + assert_equal node_pool.plan_id, "vc2-1c-2gb" + assert_equal node_pool.status, "pending" + assert_equal node_pool.count, 2 + assert_equal node_pool.nodes.class, Array + end + + def test_update_node_pool + vke_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + nodepool_id = "e97bdee9-2781-4f31-be03-60fc75f399ae" + body = {node_quantity: 1} + stub = stub_request("kubernetes/clusters/#{vke_id}/node-pools/#{nodepool_id}", method: :patch, body: body, response: stub_response(fixture: "kubernetes/update_node_pool")) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + node_pool = client.kubernetes.update_node_pool(vke_id: vke_id, nodepool_id: nodepool_id, **body) + + assert_equal node_pool.id, "e97bdee9-2781-4f31-be03-60fc75f399ae" + assert_equal node_pool.date_created, Time.parse("2021-07-07T23:27:08+00:00") + assert_equal node_pool.date_updated, Time.parse("2021-07-08T12:12:44+00:00") + assert_equal node_pool.label, "my-label-48770703" + assert_equal node_pool.plan_id, "vc2-1c-2gb" + assert_equal node_pool.status, "active" + assert_equal node_pool.count, 1 + assert_equal node_pool.nodes.class, Array + end + + def test_delete_node_pool + vke_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + nodepool_id = "e97bdee9-2781-4f31-be03-60fc75f399ae" + stub = stub_request("kubernetes/clusters/#{vke_id}/node-pools/#{nodepool_id}", method: :delete, response: {}) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + assert client.kubernetes.delete_node_pool(vke_id: vke_id, nodepool_id: nodepool_id) + end + + def test_delete_node_pool_instance + vke_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + nodepool_id = "e97bdee9-2781-4f31-be03-60fc75f399ae" + node_id = "ab809b79-1358-4c59-8c8e-b73add699bf9" + stub = stub_request("kubernetes/clusters/#{vke_id}/node-pools/#{nodepool_id}/nodes/#{node_id}", method: :delete, response: {}) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + assert client.kubernetes.delete_node_pool_instance(vke_id: vke_id, nodepool_id: nodepool_id, node_id: node_id) + end + + def test_recycle_node_pool_instance + vke_id = "cb676a46-66fd-4dfb-b839-443f2e6c0b60" + nodepool_id = "e97bdee9-2781-4f31-be03-60fc75f399ae" + node_id = "ab809b79-1358-4c59-8c8e-b73add699bf9" + stub = stub_request("kubernetes/clusters/#{vke_id}/node-pools/#{nodepool_id}/nodes/#{node_id}/recycle", method: :post, response: {}) + client = Vultr::Client.new(api_key: "fake", adapter: :test, stubs: stub) + + assert client.kubernetes.recycle_node_pool_instance(vke_id: vke_id, nodepool_id: nodepool_id, node_id: node_id) + end +end