Class: Occi::Api::Client::ClientHttp

Inherits:
ClientBase show all
Includes:
HTTParty, Http::CodeHelpers, Http::Helpers, Http::PartyWrappers
Defined in:
lib/occi/api/client/client_http.rb

Constant Summary collapse

DEFAULT_HEADERS =

TODO: change default Accept to JSON as soon as it is properly implemented in OpenStack's OCCI-OS 'Accept' => 'application/occi+json,text/plain;q=0.8,text/occi;q=0.2'

{
  'Accept'     => 'text/plain,text/occi;q=0.2',
  'User-Agent' => "rOCCI-core/#{Occi::VERSION} rOCCI-api/#{Occi::Api::VERSION} OCCI/1.1 " \
                  "#{RUBY_ENGINE}-#{RUBY_PLATFORM}/#{RUBY_VERSION}p#{RUBY_PATCHLEVEL}"
}

Constants included from Http::CodeHelpers

Http::CodeHelpers::HTTP_CODES

Instance Attribute Summary

Attributes inherited from ClientBase

#auth_options, #connected, #endpoint, #last_response, #logger, #media_type, #model, #options

Instance Method Summary collapse

Methods included from Http::CodeHelpers

#reason_phrase

Methods included from Http::Helpers

#configure_connection, #get_auth, #get_logger, #get_media_type, #preauthenticate, #response_message

Methods included from Http::PartyWrappers

#del, #get, #get_process_response, #post, #post_action, #post_create, #put, #report_failure, #send_coll_request

Methods inherited from ClientBase

#connect

Methods included from Base::ProtectedHelpers

#get_endpoint_uri, #get_logger, #get_model, #get_os_tpl_mixins_ary, #get_resource_tpl_mixins_ary

Methods included from Base::ProtectedStubs

#configure_connection, #get_auth, #get_media_type, #preauthenticate

Methods included from Base::Helpers

#path_for_instance, #path_for_kind_type_identifier, #sanitize_instance_link

Methods included from Base::MixinMethods

#describe_mixin, #describe_mixin_w_type, #describe_mixin_wo_type, #get_mixin, #get_mixin_type_identifier, #get_mixin_type_identifiers, #get_mixin_types, #get_mixins, #get_os_templates, #get_resource_templates, #list_mixin, #list_mixins

Methods included from Base::EntityMethods

#get_entity_type_identifier, #get_entity_type_identifiers, #get_entity_types, #get_link_type_identifier, #get_link_type_identifiers, #get_link_types, #get_resource, #get_resource_type_identifier, #get_resource_type_identifiers, #get_resource_types, #get_type_identifier, #get_types

Methods included from Base::KindMethods

#get_kind_type_identifier, #get_kind_type_identifiers, #get_kind_type_identifiers_related_to, #get_kind_types

Methods included from Base::CategoryMethods

#get_category_type_identifier, #get_category_type_identifiers, #get_category_types

Methods included from Base::ActionMethods

#get_action_type_identifier, #get_action_type_identifiers, #get_action_types

Constructor Details

#initialize(options = {}) ⇒ Occi::Api::Client::ClientHttp

Initializes client data structures and retrieves OCCI model from the server.

Examples:

options = {
  :endpoint => "http://localhost:3000/",
  :timeout => 15
  :auth => {:type => "none"},
  :log => {:out => STDERR, :level => Occi::Api::Log::WARN, :logger => nil},
  :auto_connect => true,
  :media_type => nil
}

Occi::Api::Client::ClientHttp.new options # => #<Occi::Api::Client::ClientHttp>

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/occi/api/client/client_http.rb', line 45

def initialize(options = {})
  super options

  # set a global base URI for all subsequent requests
  # must be done after authN calls
  self.class.base_uri @endpoint.to_s

  # get model information from the endpoint
  # and create Occi::Model instance
  model_collection = get('/-/')
  @model = get_model(model_collection)

  # auto-connect?
  @connected = @options[:auto_connect]
end

Instance Method Details

#create(entity) ⇒ Object


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/occi/api/client/client_http.rb', line 128

def create(entity)
  raise "#{entity.class.name.inspect} not an entity!" unless entity.kind_of? Occi::Core::Entity

  Occi::Api::Log.debug "Entity kind: #{entity.kind.type_identifier.inspect}"
  raise "No kind found for #{entity.inspect}" unless entity.kind

  # get location for this kind of entity
  path = path_for_kind_type_identifier(entity.kind.type_identifier)
  collection = Occi::Collection.new

  # is this entity a Resource or a Link?
  Occi::Api::Log.debug "Entity class: #{entity.class.name.inspect}"
  collection.resources << entity if entity.kind_of? Occi::Core::Resource
  collection.links << entity if entity.kind_of? Occi::Core::Link

  # make the request
  post path, collection
end

#delete(resource_type_identifier) ⇒ Object


148
149
150
151
152
153
154
# File 'lib/occi/api/client/client_http.rb', line 148

def delete(resource_type_identifier)
  raise 'Resource not provided!' if resource_type_identifier.blank?
  path = path_for_kind_type_identifier(resource_type_identifier)

  Occi::Api::Log.debug("Deleting #{path.inspect} for #{resource_type_identifier.inspect}")
  del path
end

#describe(resource_type_identifier = nil) ⇒ Object


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
124
125
# File 'lib/occi/api/client/client_http.rb', line 86

def describe(resource_type_identifier=nil)
  if resource_type_identifier
    resource_type_identifier = get_resource_type_identifier(resource_type_identifier)
  end

  descriptions = Occi::Collection.new

  if resource_type_identifier.blank?
    # no filters, describe all available resources
    descriptions.merge! get('/')
  elsif @model.get_by_id(resource_type_identifier)
    # we got type identifier
    # get all available resources of this type

    if @media_type == 'application/occi+json'
      # all at once
      descriptions = get(path_for_kind_type_identifier(resource_type_identifier))
    else
      # one resource at a time
      locations = list(resource_type_identifier)

      # make the requests
      locations.each do |location|
        path = sanitize_instance_link(location)
        descriptions.merge! get(path)
      end
    end
  else
    # this is a link of a specific resource (absolute or relative)
    path = sanitize_instance_link(resource_type_identifier)
    descriptions.merge! get(path)
  end

  # decide what to return, in case of mixed collections prefer resources
  if descriptions.resources.empty?
    descriptions.links
  else
    descriptions.resources
  end
end

#list(resource_type_identifier = nil) ⇒ Object


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/occi/api/client/client_http.rb', line 62

def list(resource_type_identifier=nil)
  if resource_type_identifier
    resource_type_identifier = get_resource_type_identifier(resource_type_identifier)
    path = path_for_kind_type_identifier(resource_type_identifier)
  end

  path = '/' unless path

  headers = self.class.headers.clone
  headers['Accept'] = 'text/uri-list'

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

  response_msg = response_message(response)
  raise "HTTP GET failed! #{response_msg}" unless response.code == 200

  # TODO: remove the gsub OCCI-OS hack as soon as they stop using 'uri:'
  response.body.gsub(/\# uri:\/(compute|storage|network)\/[\n]?/, '').split("\n").compact
end

#refreshObject


175
176
177
178
179
# File 'lib/occi/api/client/client_http.rb', line 175

def refresh
  # re-download the model from the server
  model_collection = get('/-/')
  @model = get_model(model_collection)
end

#trigger(resource_type_identifier, action_instance) ⇒ Object


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/occi/api/client/client_http.rb', line 157

def trigger(resource_type_identifier, action_instance)
  raise 'Resource not provided!' if resource_type_identifier.blank?
  raise 'ActionInstance not provided!' if action_instance.blank?

  # attempt to resolve shortened identifiers
  resource_type_identifier = get_resource_type_identifier(resource_type_identifier)
  path = path_for_kind_type_identifier(resource_type_identifier)

  # prepare data
  path = "#{path}?action=#{action_instance.action.term}"
  collection = Occi::Collection.new
  collection << action_instance

  # make the request
  post path, collection
end