Module: Occi::Api::Client::Http::PartyWrappers

Included in:
ClientHttp
Defined in:
lib/occi/api/client/http/party_wrappers.rb

Instance Method Summary collapse

Instance Method Details

#del(path, filter = nil) ⇒ Boolean

Performs DELETE requests and returns True on success.

Examples:

del "/compute/65sf4g65sf4g-sf6g54sf5g-sfgsf32g3" # => true

Raises:

  • (ArgumentError)

87
88
89
90
91
92
# File 'lib/occi/api/client/http/party_wrappers.rb', line 87

def del(path, filter=nil)
  raise ArgumentError, "Path is a required argument!" if path.blank?
  report_failure(self.class.delete(path))

  true
end

#get(path = '/', filter = nil) ⇒ Occi::Collection

Performs GET request and parses the responses to collections.

Examples:

get "/-/" # => #<Occi::Collection>
get "/compute/" # => #<Occi::Collection>
get "/compute/fs65g4fs6g-sf54g54gsf-aa12faddf52" # => #<Occi::Collection>

Raises:

  • (ArgumentError)

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/occi/api/client/http/party_wrappers.rb', line 16

def get(path='/', filter=nil)
  raise ArgumentError, "Path is a required argument!" if path.blank?

  # apply filters if present
  headers = self.class.headers.clone
  unless filter.blank?
    categories = filter.categories.to_a.collect { |category| category.to_string_short }.join(',')
    attributes = filter.entities.to_a.collect { |entity| entity.attributes.to_header }.join(',')

    headers['Content-Type'] = 'text/occi'
    headers['Category'] = categories unless categories.empty?
    headers['X-OCCI-Attribute'] = attributes unless attributes.empty?
  end

  response = self.class.get(path, :headers => headers)
  report_failure(response)

  get_process_response(path, response)
end

#get_process_response(path, response) ⇒ Object (private)


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/occi/api/client/http/party_wrappers.rb', line 96

def get_process_response(path, response)
  Occi::Api::Log.debug "Response from location: #{path.inspect}"
  kind = @model.get_by_location(path) if @model

  Occi::Api::Log.debug "Response should contain kind: #{kind ? kind.type_identifier.inspect : 'none'}"
  entity_type = if kind && kind.related_to?(Occi::Core::Link.kind)
    Occi::Core::Link
  else
    Occi::Core::Resource
  end

  Occi::Api::Log.debug "Parser call: #{response.content_type.inspect} #{path.include?('/-/')} " \
                  "#{entity_type} #{response.headers.inspect}"
  collection = Occi::Parser.parse(
    response.content_type, response.body,
    path.include?('/-/'), entity_type, response.headers
  )

  Occi::Api::Log.debug "Parsed collection: empty? #{collection.empty?}"
  collection
end

#post(path, collection) ⇒ Occi::Collection, ...

Performs POST requests and returns URI locations. Resource data must be provided in an Occi::Collection instance.

Examples:

collection = Occi::Collection.new
collection.resources << entity if entity.kind_of? Occi::Core::Resource
collection.links << entity if entity.kind_of? Occi::Core::Link

post "/compute/", collection # => "http://localhost:3300/compute/23sf4g65as-asdgsg2-sdfgsf2g"
post "/network/", collection # => "http://localhost:3300/network/23sf4g65as-asdgsg2-sdfgsf2g"
post "/storage/", collection # => "http://localhost:3300/storage/23sf4g65as-asdgsg2-sdfgsf2g"

Raises:

  • (ArgumentError)

51
52
53
54
55
56
57
58
59
# File 'lib/occi/api/client/http/party_wrappers.rb', line 51

def post(path, collection)
  raise ArgumentError, "Path is a required argument!" if path.blank?
  raise ArgumentError, "Collection is a required argument!" if collection.blank?

  response = send_coll_request(path, collection)
  report_failure(response)

  collection.send(:standalone_action_instance?) ? post_action(response) : post_create(response)
end

#post_action(response) ⇒ Object (private)


144
145
146
# File 'lib/occi/api/client/http/party_wrappers.rb', line 144

def post_action(response)
  true
end

#post_create(response) ⇒ Object (private)


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/occi/api/client/http/party_wrappers.rb', line 148

def post_create(response)
  if response.code == 200
    collection = Occi::Parser.parse(
      response.content_type,
      response.body
    )

    if collection.empty?
      Occi::Parser.locations(
        response.content_type,
        response.body,
        response.headers
      ).first
    else
      raise "HTTP POST response does not " \
            "contain required resource rendering!" unless collection.resources.first
      collection.resources.first.location
    end
  else
    Occi::Parser.locations(
      response.content_type,
      response.body,
      response.headers
    ).first
  end
end

#put(path, collection) ⇒ Occi::Collection

Performs PUT requests and parses responses to collections.

Examples:

TODO: add examples

Raises:

  • (ArgumentError)

69
70
71
72
73
74
75
76
77
# File 'lib/occi/api/client/http/party_wrappers.rb', line 69

def put(path, collection)
  raise ArgumentError, "Path is a required argument!" if path.blank?
  raise ArgumentError, "Collection is a required argument!" if collection.blank?

  response = send_coll_request(path, collection, :put)
  report_failure(response)

  Occi::Parser.parse(response.content_type, response.body)
end

#report_failure(response) ⇒ Object (private)


175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/occi/api/client/http/party_wrappers.rb', line 175

def report_failure(response)
  # Is there something to report?
  return if response.code.between? 200, 202

  # get a human-readable response message
  response_msg = response_message(response)

  # include a Request ID if it is available
  if response.headers["x-request-id"]
    message = "#{response.request.http_method} with " \
              "ID[#{response.headers["x-request-id"].inspect}] failed! " \
              "#{response_msg} : #{response.body.inspect}"
  else
    message = "#{response.request.http_method} failed! " \
              "#{response_msg} : #{response.body.inspect}"
  end

  raise message
end

#send_coll_request(path, collection, type = :post) ⇒ Object (private)

Raises:

  • (ArgumentError)

118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/occi/api/client/http/party_wrappers.rb', line 118

def send_coll_request(path, collection, type = :post)
  type ||= :post
  raise ArgumentError, "Unsupported send " \
                       "type #{type.to_s.inspect}!" unless [:post, :put].include?(type)

  headers = self.class.headers.clone
  headers['Content-Type'] = @media_type

  case @media_type
  when 'application/occi+json'
    self.class.send type,
                    path,
                    :body => collection.to_json,
                    :headers => headers
  when 'text/occi'
    self.class.send type,
                    path,
                    :headers => collection.to_header.merge(headers)
  else
    self.class.send type,
                    path,
                    :body => collection.to_text,
                    :headers => headers
  end
end