Class: VagrantPlugins::OneCloud::Commands::CreateNetwork
- Inherits:
-
Object
- Object
- VagrantPlugins::OneCloud::Commands::CreateNetwork
- Includes:
- Vagrant::Util::Retryable, Helpers
- Defined in:
- lib/vagrant-1cloud/commands/create_network.rb
Class Method Summary collapse
-
.synopsis ⇒ Object
Show description when ‘vagrant list-commands` is triggered.
Instance Method Summary collapse
- #execute ⇒ Object
- #request(token, path, params = {}, method = :get) ⇒ Object
- #wait_for_network(token, net_id) ⇒ Object
Class Method Details
.synopsis ⇒ Object
Show description when ‘vagrant list-commands` is triggered
14 15 16 |
# File 'lib/vagrant-1cloud/commands/create_network.rb', line 14 def self.synopsis "plugin: vagrant-1cloud: creates new private network" end |
Instance Method Details
#execute ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/vagrant-1cloud/commands/create_network.rb', line 18 def execute = {} optparse = OptionParser.new do |opts| opts. = 'Usage: vagrant create-network [options]' opts.on('-n', '--name NAME', 'Network name') do |name| [:Name] = name end [:IsDHCP] = false opts.on('-d', '--[no-]dhcp', "Use dhcp or not (default #{[:IsDHCP]})") do |dhcp| [:IsDHCP] = dhcp end opts.on('-l', '--location LOCATION', 'Network location') do |location| [:DCLocation] = location end opts.on('-t', '--token TOKEN', '1cloud type token') do |token| [:token] = token end opts.on('-h', '--help', 'Display this screen') do puts opts exit end end begin optparse.parse! mandatory = [:Name, :DCLocation, :token] missing = mandatory.select{ |param| [param].nil? } unless missing.empty? raise OptionParser::MissingArgument.new(missing.join(', ')) end rescue OptionParser::InvalidOption, OptionParser::MissingArgument puts $!.to_s puts optparse exit end result = request([:token], '/network') private_network = result['body'].find { |network| network['Name'] == [:Name] } if private_network @env.ui.info I18n.t('vagrant_1cloud.info.network_exists', network: [:Name]) else @env.ui.info I18n.t('vagrant_1cloud.info.network_missing', network: [:Name]) result = request([:token], '/network', .except(:token), :post) # Waiting for private network to create @env.ui.info I18n.t('vagrant_1cloud.info.creating_private_network') wait_for_network([:token], result['body']['ID']) end 0 end |
#request(token, path, params = {}, method = :get) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/vagrant-1cloud/commands/create_network.rb', line 78 def request(token, path, params = {}, method = :get) connection = Faraday.new({ :url => 'https://api.1cloud.ru/' }) begin @env.ui.info I18n.t('vagrant_1cloud.info.request', path: path) @env.ui.info I18n.t('vagrant_1cloud.info.params', params: params) result = connection.send(method) do |req| req.url path req.headers['Authorization'] = "Bearer #{token}" req.body = params end rescue Faraday::Error::ConnectionFailed => e # TODO this is suspect but because faraday wraps the exception # in something generic there doesn't appear to be another # way to distinguish different connection errors :( if e. =~ /certificate verify failed/ raise Errors::CertificateError end raise e end begin body = JSON.parse(%Q[{"body":#{result.body}}]) @env.ui.info I18n.t('vagrant_1cloud.info.response', body: body) rescue JSON::ParserError => e raise(Errors::JSONError, { :message => e., :path => path, :params => params, :response => result.body }) end unless /^2\d\d$/ =~ result.status.to_s raise(Errors::APIStatusError, { :path => path, :params => params, :status => result.status, :response => body.inspect }) end Result.new(body) end |
#wait_for_network(token, net_id) ⇒ Object
125 126 127 128 129 130 131 |
# File 'lib/vagrant-1cloud/commands/create_network.rb', line 125 def wait_for_network(token, net_id) retryable(:tries => 400, :sleep => 10) do # check network status result = request(token, "/network/#{net_id}") raise 'Network is not active' if result['body']['State'] != 'Active' end end |