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

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

Constant Summary collapse

OK_RANGE =

Acceptable responses indicating OK

[200, 201, 202, 204].freeze

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

Parameters:

  • path (String)

    path for the DELETE request

  • filter (Occi::Collection) (defaults to: nil)

    collection of filters (currently NOT used)

Returns:

  • (Boolean)

    status

Raises:

  • (ArgumentError)


90
91
92
93
94
95
# File 'lib/occi/api/client/http/party_wrappers.rb', line 90

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>

Parameters:

  • path (String) (defaults to: '/')

    path for the GET request

  • filter (Occi::Collection) (defaults to: nil)

    collection of filters

Returns:

  • (Occi::Collection)

    parsed result of the request

Raises:

  • (ArgumentError)


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

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)



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

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"

Parameters:

  • path (String)

    path for the POST request

  • collection (Occi::Collection)

    resource data to be POSTed

Returns:

  • (Occi::Collection, String, Boolean)

    Collection, URI location or action result (if ActionInstance is passed)

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
61
62
# File 'lib/occi/api/client/http/party_wrappers.rb', line 54

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)



147
148
149
150
# File 'lib/occi/api/client/http/party_wrappers.rb', line 147

def post_action(response)
  coll = Occi::Parser.parse(response.content_type, response.body, true)
  coll.mixins.any? ? coll.mixins : true
end

#post_create(response) ⇒ Object (private)



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

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

Parameters:

  • path (String)

    path for the PUT request

  • collection (Occi::Collection)

    resource data to send

Returns:

  • (Occi::Collection)

    parsed result of the request

Raises:

  • (ArgumentError)


72
73
74
75
76
77
78
79
80
# File 'lib/occi/api/client/http/party_wrappers.rb', line 72

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)



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/occi/api/client/http/party_wrappers.rb', line 179

def report_failure(response)
  # Is there something to report?
  return if OK_RANGE.include? response.code

  # 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)


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

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