Class: VagrantPlugins::SimpleCloud::Helpers::SimpleClient

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Util::Retryable
Defined in:
lib/vagrant-simplecloud/helpers/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(machine) ⇒ SimpleClient

Returns a new instance of SimpleClient.



19
20
21
22
23
# File 'lib/vagrant-simplecloud/helpers/client.rb', line 19

def initialize(machine)
  @logger = Log4r::Logger.new('vagrant::simplecloud::apiclient')
  @config = machine.provider_config
  @url = "https://api.simplecloud.ru"
end

Instance Method Details

#delete(path, params = {}) ⇒ Object



25
26
27
# File 'lib/vagrant-simplecloud/helpers/client.rb', line 25

def delete(path, params = {})
    self.request(path, params, :delete)
end

#post(path, params = {}) ⇒ Object



29
30
31
# File 'lib/vagrant-simplecloud/helpers/client.rb', line 29

def post(path, params = {})
    self.request(path, params, :post)
end

#request(path, params = {}, method = :get) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/vagrant-simplecloud/helpers/client.rb', line 33

def request(path, params = {}, method = :get)
      @logger.info "Request: #{@url}#{path} #{params}"
      uri = URI.parse("#{@url}#{path}")
      https = Net::HTTP.new(uri.host,uri.port)
      https.use_ssl = true
      req = case method
          when :get
              Net::HTTP::Get.new(uri.path)
          when :post
              Net::HTTP::Post.new(uri.path)
          when :delete
              Net::HTTP::Delete.new(uri.path)
      end
      req['Content-Type'] = "application/json"
      req['Authorization'] = "Bearer #{@config.token}"
      req.set_form_data(params)
      res = https.request(req)
      unless /^2\d\d$/ =~ res.code.to_s
        raise "Server response error #{res.code} #{path} #{params} #{res.message} #{res.body}"
      end
      JSON.parse(res.body) unless res.body.nil?
end

#wait_for_event(env, id) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/vagrant-simplecloud/helpers/client.rb', line 56

def wait_for_event(env, id)
  retryable(:tries => 120, :sleep => 10) do
    # stop waiting if interrupted
    next if env[:interrupted]

    # check action status
    result = self.request("/v2/actions/#{id}")

    yield result if block_given?
    raise 'not ready' if result['action']['status'] != 'completed'
  end
end