lib/vagrant-vultr/helpers/client.rb in vagrant-vultr-0.1.2 vs lib/vagrant-vultr/helpers/client.rb in vagrant-vultr-0.1.3

- old
+ new

@@ -32,14 +32,20 @@ def server(sub_id) servers.find { |server| server['SUBID'] == sub_id } end def create_server(attributes) + location_id = region_id(attributes[:region]) params = { - DCID: region_id(attributes[:region]), - VPSPLANID: vps_plan_id(attributes[:plan]), - SSHKEYID: ssh_key_id(attributes[:ssh_key_name]) + DCID: location_id, + VPSPLANID: vps_plan_id(attributes[:plan], location_id.class.name == "String" ? location_id.to_i : location_id), + SSHKEYID: ssh_key_id(attributes[:ssh_key_name]), + enable_ipv6: attributes[:enable_ipv6], + enable_private_network: attributes[:enable_private_network], + label: attributes[:label], + tag: attributes[:tag], + hostname: attributes[:hostname] } if attributes[:snapshot] params.merge!(OSID: os_id('Snapshot'), SNAPSHOTID: attributes[:snapshot]) else @@ -79,13 +85,13 @@ def region_id(region) regions.find { |r| r['name'] == region }['DCID'] end - def vps_plan_id(plan) + def vps_plan_id(plan, location_id) plans = request { ::Vultr::Plans.list } - plans.values.find { |p| p['name'] == plan }['VPSPLANID'] + plans.values.find { |p| p['name'] == plan && p['available_locations']&.include?(location_id) }['VPSPLANID'] end def ssh_keys ssh_keys = request { ::Vultr::SSHKey.list } ssh_keys = ssh_keys.values if ssh_keys.any? @@ -132,9 +138,13 @@ end private def request + if interval = ENV['VULTR_RATE_LIMIT_INTERVAL_MS'] + sleep interval.to_f / 1000 + end + response = yield if response[:status] != 200 raise "API request failed: #{response[:result]}." else response[:result]