Class: CFoundry::V2::Base

Inherits:
BaseClient show all
Includes:
BaseClientMethods
Defined in:
lib/cfoundry/v2/base.rb

Instance Attribute Summary

Attributes inherited from BaseClient

#rest_client

Instance Method Summary collapse

Methods inherited from BaseClient

#delete, #get, #info, #initialize, #password_score, #post, #put, #refresh_token!, #request, #request_raw, #stream_url, #token=, #trace=, #uaa

Methods included from ProxyOptions

#proxy_options_for

Constructor Details

This class inherits a constructor from CFoundry::BaseClient

Instance Method Details

#all_pages(paginated) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/cfoundry/v2/base.rb', line 98

def all_pages(paginated)
  payload = []
  for_each(paginated) do |resource|
    payload << resource
  end
  payload
end

#crashes(guid) ⇒ Object



74
75
76
# File 'lib/cfoundry/v2/base.rb', line 74

def crashes(guid)
  get("v2", "apps", guid, "crashes", :accept => :json)
end

#files(guid, instance, *path) ⇒ Object Also known as: file



53
54
55
# File 'lib/cfoundry/v2/base.rb', line 53

def files(guid, instance, *path)
  get("v2", "apps", guid, "instances", instance, "files", *path)
end

#for_each(paginated, &block) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/cfoundry/v2/base.rb', line 89

def for_each(paginated, &block)
  paginated[:resources].each &block

  while next_page = paginated[:next_url]
    paginated = get(next_page, :accept => :json)
    paginated[:resources].each &block
  end
end

#instances(guid) ⇒ Object



70
71
72
# File 'lib/cfoundry/v2/base.rb', line 70

def instances(guid)
  get("v2", "apps", guid, "instances", :accept => :json)
end

#poll_upload_until_finished(guid) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cfoundry/v2/base.rb', line 41

def poll_upload_until_finished(guid)
  while true
    response = get("v2", "jobs", guid, :accept => :json)
    break if response[:entity][:status] == "finished"

    if response[:entity][:status] == "failed"
      raise CFoundry::BadResponse
    end
    sleep 0.2
  end
end

#resource_match(fingerprints) ⇒ Object



12
13
14
# File 'lib/cfoundry/v2/base.rb', line 12

def resource_match(fingerprints)
  put("v2", "resource_match", :content => :json, :accept => :json, :payload => fingerprints)
end

#stats(guid) ⇒ Object



78
79
80
# File 'lib/cfoundry/v2/base.rb', line 78

def stats(guid)
  get("v2", "apps", guid, "stats", :accept => :json)
end

#stream_file(guid, instance, *path, &blk) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/cfoundry/v2/base.rb', line 59

def stream_file(guid, instance, *path, &blk)
  path_and_options = path + [{:return_response => true, :follow_redirects => false}]
  redirect = get("v2", "apps", guid, "instances", instance, "files", *path_and_options)

  if location = redirect[:headers]["location"]
    stream_url(location + "&tail", &blk)
  else
    yield redirect[:body]
  end
end

#update_app(guid, diff) ⇒ Object



82
83
84
85
86
87
# File 'lib/cfoundry/v2/base.rb', line 82

def update_app(guid, diff)
  put("v2", "apps", guid,
      :content => :json,
      :payload => diff,
      :return_response => true)
end

#upload_app(guid, zipfile = nil, resources = []) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cfoundry/v2/base.rb', line 16

def upload_app(guid, zipfile = nil, resources = [])
  payload = {}
  payload[:resources] = MultiJson.dump(resources)

  if zipfile
    payload[:application] =
      UploadIO.new(
        if zipfile.is_a? File
          zipfile
        elsif zipfile.is_a? String
          File.new(zipfile, "rb")
        end,
        "application/zip")
  end

  response = put("v2", "apps", guid, "bits",
    :payload => payload,
    :params => {"async" => "true"},
    :accept => :json)

  poll_upload_until_finished(response[:metadata][:guid])
rescue EOFError
  retry
end