occi 2.5.19 → 3.0.0.beta.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. data/.gitignore +4 -0
  2. data/.travis.yml +3 -1
  3. data/AUTHORS +4 -3
  4. data/Gemfile +9 -0
  5. data/Gemfile.lock +64 -21
  6. data/README.md +19 -14
  7. data/Rakefile +12 -8
  8. data/Test Results - discovery_interface.html +862 -0
  9. data/bin/occi +333 -83
  10. data/examples/dsl_example.rb +6 -6
  11. data/examples/x509auth_example.rb +6 -6
  12. data/features/common/step_definitions/common_steps.rb +32 -0
  13. data/features/occi/core/create/create.feature +17 -0
  14. data/features/occi/core/create/step_definitions/create_steps.rb +0 -0
  15. data/features/occi/core/delete/delete.feature +14 -0
  16. data/features/occi/core/delete/step_definitions/delete_steps.rb +0 -0
  17. data/features/occi/core/discovery_interface/discovery_interface.feature +35 -0
  18. data/features/occi/core/discovery_interface/step_definitions/discovery_interface_steps.rb +19 -0
  19. data/features/occi/core/miscellaneous/miscellaneous.feature +14 -0
  20. data/features/occi/core/miscellaneous/step_definitions/miscellaneous_steps.rb +0 -0
  21. data/features/occi/core/read/read.feature +14 -0
  22. data/features/occi/core/read/step_definitions/read_steps.rb +0 -0
  23. data/features/occi/core/update/step_definitions/update_steps.rb +0 -0
  24. data/features/occi/core/update/update.feature +14 -0
  25. data/features/occi/infrastructure/create/create.feature +14 -0
  26. data/features/occi/infrastructure/create/step_definitions/create_steps.rb +0 -0
  27. data/features/support/env.rb +4 -0
  28. data/lib/occi.rb +29 -3
  29. data/lib/occi/api/client/client_amqp.rb +756 -0
  30. data/lib/occi/api/client/client_http.rb +922 -0
  31. data/lib/occi/api/client/http/httparty_fix.rb +53 -0
  32. data/lib/occi/api/client/http/net_http_fix.rb +46 -0
  33. data/lib/occi/api/dsl.rb +77 -73
  34. data/lib/occi/bin/helpers.rb +91 -0
  35. data/lib/occi/bin/occi_opts.rb +251 -0
  36. data/lib/occi/bin/resource_output_factory.rb +90 -0
  37. data/lib/occi/bin/templates/compute.erb +15 -0
  38. data/lib/occi/bin/templates/network.erb +11 -0
  39. data/lib/occi/bin/templates/os_tpl.erb +9 -0
  40. data/lib/occi/bin/templates/resource_tpl.erb +9 -0
  41. data/lib/occi/bin/templates/storage.erb +10 -0
  42. data/lib/occi/collection.rb +122 -25
  43. data/lib/occi/core.rb +18 -9
  44. data/lib/occi/core/action.rb +20 -4
  45. data/lib/occi/core/action_instance.rb +24 -0
  46. data/lib/occi/core/actions.rb +22 -0
  47. data/lib/occi/core/attribute_properties.rb +33 -84
  48. data/lib/occi/core/attributes.rb +32 -14
  49. data/lib/occi/core/categories.rb +46 -0
  50. data/lib/occi/core/category.rb +94 -20
  51. data/lib/occi/core/entities.rb +50 -0
  52. data/lib/occi/core/entity.rb +130 -89
  53. data/lib/occi/core/kind.rb +28 -35
  54. data/lib/occi/core/kinds.rb +22 -0
  55. data/lib/occi/core/link.rb +43 -40
  56. data/lib/occi/core/links.rb +34 -0
  57. data/lib/occi/core/mixin.rb +28 -23
  58. data/lib/occi/core/mixins.rb +22 -0
  59. data/lib/occi/core/related.rb +20 -0
  60. data/lib/occi/core/resource.rb +40 -40
  61. data/lib/occi/core/resources.rb +14 -0
  62. data/lib/occi/infrastructure.rb +27 -0
  63. data/lib/occi/infrastructure/compute.rb +159 -0
  64. data/lib/occi/infrastructure/network.rb +131 -0
  65. data/lib/occi/infrastructure/network/ipnetwork.rb +34 -0
  66. data/lib/occi/infrastructure/networkinterface.rb +124 -0
  67. data/lib/occi/infrastructure/networkinterface/ipnetworkinterface.rb +34 -0
  68. data/lib/occi/infrastructure/os_tpl.rb +19 -0
  69. data/lib/occi/infrastructure/resource_tpl.rb +19 -0
  70. data/lib/occi/infrastructure/storage.rb +96 -0
  71. data/lib/occi/infrastructure/storagelink.rb +73 -0
  72. data/lib/occi/log.rb +6 -2
  73. data/lib/occi/model.rb +38 -70
  74. data/lib/occi/parser.rb +108 -88
  75. data/lib/occi/version.rb +2 -2
  76. data/lib/occiantlr/OCCIANTLR.g +6 -5
  77. data/lib/occiantlr/OCCIANTLRLexer.rb +52 -52
  78. data/lib/occiantlr/OCCIANTLRParser.rb +678 -569
  79. data/lib/occiantlr/README.md +1 -1
  80. data/occi.gemspec +2 -1
  81. data/spec/cassettes/client_http_text_plain.yml +1066 -0
  82. data/spec/occi/api/client/client_amqp_spec.rb +148 -0
  83. data/spec/occi/api/client/client_http_0.5_spec.rb +292 -0
  84. data/spec/occi/api/client/client_http_spec.rb +259 -0
  85. data/spec/occi/api/dsl_spec.rb +0 -0
  86. data/spec/occi/collection_spec.rb +23 -10
  87. data/spec/occi/core/categories_spec.rb +30 -0
  88. data/spec/occi/core/category_spec.rb +41 -0
  89. data/spec/occi/core/entity_spec.rb +52 -0
  90. data/spec/occi/core/resource_spec.rb +21 -0
  91. data/spec/occi/infrastructure/compute_spec.rb +32 -0
  92. data/spec/occi/log_spec.rb +10 -10
  93. data/spec/occi/model_spec.rb +24 -24
  94. data/spec/occi/parser_spec.rb +89 -39
  95. data/spec/occi/test.json +22 -58
  96. data/spec/occiantlr/parser_spec.rb +5 -7
  97. data/spec/spec_helper.rb +13 -3
  98. metadata +116 -19
  99. data/etc/model/infrastructure/compute.json +0 -108
  100. data/etc/model/infrastructure/ipnetwork.json +0 -40
  101. data/etc/model/infrastructure/ipnetworkinterface.json +0 -40
  102. data/etc/model/infrastructure/network.json +0 -55
  103. data/etc/model/infrastructure/networkinterface.json +0 -38
  104. data/etc/model/infrastructure/os_template.json +0 -9
  105. data/etc/model/infrastructure/resource_template.json +0 -9
  106. data/etc/model/infrastructure/storage.json +0 -72
  107. data/etc/model/infrastructure/storagelink.json +0 -38
  108. data/lib/occi/api/client.rb +0 -596
  109. data/lib/occi/client/occiopts.rb +0 -146
  110. data/spec/occi/client_spec.rb +0 -12
@@ -3,7 +3,7 @@ require 'pp'
3
3
 
4
4
  # load and include rOCCI client DSL
5
5
  require 'occi'
6
- extend OCCI::DSL
6
+ extend Occi::Api::Dsl
7
7
 
8
8
  ## options
9
9
  use_os_temlate = true # use OS_TEMPLATE or NETWORK + STORAGE + INSTANCE TYPE
@@ -17,13 +17,13 @@ CA_PATH = '/etc/grid-security/certificates'
17
17
  ENDPOINT = 'https://localhost:3300'
18
18
 
19
19
  ## establish a connection
20
- connect(ENDPOINT,
20
+ connect(:http, ENDPOINT,
21
21
  { :type => "x509",
22
22
  :user_cert => USER_CERT,
23
23
  :user_cert_password => USER_CERT_PASSWORD,
24
24
  :ca_path => CA_PATH },
25
25
  { :out => STDERR,
26
- :level => OCCI::Log::DEBUG })
26
+ :level => Occi::Log::DEBUG })
27
27
 
28
28
  puts "\n\nListing all available resource types:"
29
29
  resource_types.each do |type|
@@ -114,8 +114,8 @@ unless use_os_temlate
114
114
 
115
115
  ## create links and attach them to the compure resource
116
116
  puts "\n Connecting to our compute:"
117
- storagelink cmpt, storage_loc
118
- networkinterface cmpt, network_loc
117
+ cmpt.storagelink storage_loc
118
+ cmpt.networkinterface network_loc
119
119
  else
120
120
  ## with OS template, we have to find the template by name
121
121
  ## optionally we can change its "size" by choosing an instance type
@@ -126,7 +126,7 @@ else
126
126
  ## attach chosen resources to the compute resource
127
127
  cmpt.mixins << os << size
128
128
  ## we can change some of the values manually
129
- cmpt.attributes.occi!.core!.title = "My rOCCI x509 VM"
129
+ cmpt.title = "My rOCCI x509 VM"
130
130
  end
131
131
 
132
132
  ## create the compute resource and print its location
@@ -13,14 +13,14 @@ USER_CERT_PASSWORD = 'mypassphrase'
13
13
  CA_PATH = '/etc/grid-security/certificates'
14
14
  ENDPOINT = 'https://localhost:3300'
15
15
 
16
- ## get an OCCI::Client instance
17
- client = OCCI::Client.new(ENDPOINT,
16
+ ## get an OCCI::Api::Client::ClientHttp instance
17
+ client = Occi::Api::Client::ClientHttp.new(ENDPOINT,
18
18
  { :type => "x509",
19
19
  :user_cert => USER_CERT,
20
20
  :user_cert_password => USER_CERT_PASSWORD,
21
21
  :ca_path => CA_PATH },
22
22
  { :out => STDERR,
23
- :level => OCCI::Log::DEBUG })
23
+ :level => Occi::Log::DEBUG })
24
24
 
25
25
  puts "\n\nListing all available resource types:"
26
26
  client.get_resource_types.each do |type|
@@ -111,8 +111,8 @@ unless use_os_temlate
111
111
 
112
112
  ## create links and attach them to the compure resource
113
113
  puts "\n Connecting to our compute:"
114
- client.storagelink cmpt, storage_loc
115
- client.networkinterface cmpt, network_loc
114
+ cmpt.storagelink storage_loc
115
+ cmpt.networkinterface network_loc
116
116
  else
117
117
  ## with OS template, we have to find the template by name
118
118
  ## optionally we can change its "size" by choosing an instance type
@@ -123,7 +123,7 @@ else
123
123
  ## attach chosen resources to the compute resource
124
124
  cmpt.mixins << os << size
125
125
  ## we can change some of the values manually
126
- cmpt.attributes.occi!.core!.title = "My rOCCI x509 VM"
126
+ cmpt.title = "My rOCCI x509 VM"
127
127
  end
128
128
 
129
129
  ## create the compute resource and print its location
@@ -0,0 +1,32 @@
1
+ Given /^endpoint : (.*)$/ do |endpoint|
2
+ @endpoint = endpoint
3
+ @endpoint =~ URI::ABS_URI
4
+ @endpoint.should_not be_nil
5
+ end
6
+
7
+ Given /^transfer_protocol : (http|amqp)$/ do |protocol|
8
+ @transfer_protocol = protocol
9
+ end
10
+
11
+ Given /^accept type : (text\/occi|text\/plain|application\/json)$/ do |accept_type|
12
+ @accept_type = accept_type
13
+ end
14
+
15
+ Given /^category filter : (.*)$/ do |category_filter|
16
+ @category_filter = category_filter
17
+ end
18
+
19
+ Given /^have an initialize Client$/ do
20
+ @client = Occi::Api::Client::ClientHttp.new(
21
+ @endpoint, #141.5.99.69 #11.5.99.82
22
+ { :type => "none" },
23
+ { :out => "/dev/null",
24
+ :level => Occi::Log::DEBUG },
25
+ true,
26
+ @accept_type#"text/plain,text/occi"
27
+ )
28
+ end
29
+
30
+ Then /^the Client should have the response code (.*)$/ do |response_code|
31
+ @client.last_response.code.should == response_code.to_i
32
+ end
@@ -0,0 +1,17 @@
1
+
2
+ Feature:
3
+ In order to create an OCCI Resource on the OCCI Server
4
+ As an OCCI Client
5
+ I want to get an success report of the create operation and receive the URI of the new Resource by the OCCI Server
6
+
7
+ Scenario Outline: Create an OCCI Resource
8
+ Given endpoint : <endpoint>
9
+ And transfer_protocol : <protocol>
10
+ And accept type : <accept_type>
11
+ And have an initialize Client
12
+ When
13
+ Then the Client should have the response code <response_code>
14
+
15
+ Scenarios:
16
+ | protocol | endpoint | accept_type | response_code |
17
+ | http | http://141.5.99.69/ | text/plain | 201 |
@@ -0,0 +1,14 @@
1
+ Feature:
2
+ ...
3
+
4
+ Scenario Outline: ...
5
+ Given endpoint : <endpoint>
6
+ And transfer_protocol : <protocol>
7
+ And accept type : <accept_type>
8
+ And have an initialize Client
9
+ When
10
+ Then the Client should have the response code <response_code>
11
+
12
+ Scenarios:
13
+ | protocol | endpoint | accept_type | response_code |
14
+ | http | http://141.5.99.69/ | text/plain | 201 |
@@ -0,0 +1,35 @@
1
+ # language: en
2
+
3
+ Feature: Discovery Interface
4
+ In order to discover the capabilities of an OCCI Server
5
+ As an OCCI Client
6
+ I want to receive all OCCI Categories supported by the OCCI Server
7
+
8
+ Scenario Outline: Retrieving all OCCI Categories supported by the OCCI Server
9
+ Given endpoint : <endpoint>
10
+ And transfer_protocol : <protocol>
11
+ And accept type : <accept_type>
12
+ And category filter : <category_filter>
13
+ And have an initialize Client
14
+ When OCCI Client request all OCCI Categories supported by the OCCI Server
15
+ Then the Client should have the response code <response_code>
16
+ And OCCI Client should display the OCCI Categories received from the OCCI Server
17
+
18
+ Scenarios:
19
+ | protocol | endpoint | accept_type | response_code | category_filter |
20
+ | http | http://141.5.99.69/ | application/json | 200 | |
21
+ | http | http://141.5.99.69/ | text/occi | 200 | |
22
+ | http | http://141.5.99.69/ | text/plain | 200 | |
23
+ | http | http://46.231.128.85:8086/| text/occi | 200 | |
24
+ | http | http://141.5.99.69/ | text/plain | 200 | action |
25
+
26
+ #Scenario: Retrieving the OCCI Categories with an OCCI Category filter from the OCCI Server
27
+ # Given an "http" OCCI Server with endpoint "http://141.5.99.69/"
28
+ # And the client accept "text/plain"
29
+ # And OCCI Client requests all OCCI Categories supported by the OCCI Server
30
+ # And select "action" Category from the OCCI Server
31
+ # When OCCI Client requests from the OCCI Server the OCCI Categories related to the selected OCCI Category
32
+ # Then OCCI Client should display the OCCI Categories received from the OCCI Server
33
+ # And displayed Categories should related to the selected OCCI Category
34
+ # And should be some
35
+
@@ -0,0 +1,19 @@
1
+ When /^OCCI Client request all OCCI Categories supported by the OCCI Server$/ do
2
+ if @category_filter.length > 0
3
+ @category_filter = @category_filter.pluralize
4
+ @selected_category = @client.model.send @category_filter.to_sym
5
+ @selected_category.should have_at_least(1).bla
6
+ pending
7
+ else
8
+ @client.refresh
9
+ end
10
+ end
11
+
12
+ Then /^OCCI Client should display the OCCI Categories received from the OCCI Server$/ do
13
+ @client.model.should_not be_empty
14
+
15
+ # @client.model.actions .should have_at_least(1).actions
16
+ @client.model.kinds .should have_at_least(4).kinds
17
+ @client.model.links .should be_empty
18
+ @client.model.resources.should be_empty
19
+ end
@@ -0,0 +1,14 @@
1
+ Feature:
2
+ ...
3
+
4
+ Scenario Outline: ...
5
+ Given endpoint : <endpoint>
6
+ And transfer_protocol : <protocol>
7
+ And accept type : <accept_type>
8
+ And have an initialize Client
9
+ When
10
+ Then the Client should have the response code <response_code>
11
+
12
+ Scenarios:
13
+ | protocol | endpoint | accept_type | response_code |
14
+ | http | http://141.5.99.69/ | text/plain | 201 |
@@ -0,0 +1,14 @@
1
+ Feature:
2
+ ...
3
+
4
+ Scenario Outline: ...
5
+ Given endpoint : <endpoint>
6
+ And transfer_protocol : <protocol>
7
+ And accept type : <accept_type>
8
+ And have an initialize Client
9
+ When
10
+ Then the Client should have the response code <response_code>
11
+
12
+ Scenarios:
13
+ | protocol | endpoint | accept_type | response_code |
14
+ | http | http://141.5.99.69/ | text/plain | 201 |
@@ -0,0 +1,14 @@
1
+ Feature:
2
+ ...
3
+
4
+ Scenario Outline: ...
5
+ Given endpoint : <endpoint>
6
+ And transfer_protocol : <protocol>
7
+ And accept type : <accept_type>
8
+ And have an initialize Client
9
+ When
10
+ Then the Client should have the response code <response_code>
11
+
12
+ Scenarios:
13
+ | protocol | endpoint | accept_type | response_code |
14
+ | http | http://141.5.99.69/ | text/plain | 201 |
@@ -0,0 +1,14 @@
1
+ Feature:
2
+ ...
3
+
4
+ Scenario Outline: ...
5
+ Given endpoint : <endpoint>
6
+ And transfer_protocol : <protocol>
7
+ And accept type : <accept_type>
8
+ And have an initialize Client
9
+ When
10
+ Then the Client should have the response code <response_code>
11
+
12
+ Scenarios:
13
+ | protocol | endpoint | accept_type | response_code |
14
+ | http | http://141.5.99.69/ | text/plain | 201 |
@@ -0,0 +1,4 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../../lib')
2
+
3
+ require 'rubygems'
4
+ require 'occi'
data/lib/occi.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require 'rubygems'
2
+
3
+ require 'set'
1
4
  require 'hashie/mash'
2
5
 
3
6
  require 'active_support/json'
@@ -19,13 +22,36 @@ require 'zlib'
19
22
  require 'tempfile'
20
23
 
21
24
  require 'occi/version'
25
+ require 'occi/collection'
22
26
  require 'occi/parser'
23
27
  require 'occi/model'
24
28
  require 'occi/log'
25
- require 'occi/api/dsl'
26
- require 'occi/api/client'
27
- require 'occi/collection'
28
29
  require 'occi/core'
30
+ require 'occi/infrastructure'
29
31
 
30
32
  require 'occiantlr/OCCIANTLRLexer'
31
33
  require 'occiantlr/OCCIANTLRParser'
34
+
35
+ require 'occi/api/dsl'
36
+ require 'occi/api/client/client_http'
37
+ require 'occi/api/client/client_amqp'
38
+
39
+ module Occi
40
+
41
+ def kinds
42
+ Occi::Core::Kinds.new
43
+ end
44
+
45
+ def mixins
46
+ Occi::Core::Mixins.new
47
+ end
48
+
49
+ def actions
50
+ Occi::Core::Actions.new
51
+ end
52
+
53
+ # @return [Array] list of Occi::Core::Categories
54
+ def categories
55
+ self.kinds + self.mixins + self.actions
56
+ end
57
+ end
@@ -0,0 +1,756 @@
1
+ require 'rubygems'
2
+ require "amqp"
3
+ require "json"
4
+
5
+ module Occi
6
+ module Api
7
+ module Client
8
+
9
+ class ClientAmqp
10
+ attr_reader :endpoint, :auth_options, :connected, :last_response_status
11
+ attr_accessor :media_type, :model
12
+
13
+ CONNECTION_SETTING = {
14
+ :host => 'localhost', #IP of the MessageBroker (RabbitMQ)
15
+ :port => 5672,
16
+ :vhost => '/' ,
17
+ :password => 'password'
18
+ }
19
+
20
+ # hash mapping HTTP response codes to human-readable messages
21
+ HTTP_CODES = {
22
+ "100" => "Continue",
23
+ "101" => "Switching Protocols",
24
+ "200" => "OK",
25
+ "201" => "Created",
26
+ "202" => "Accepted",
27
+ "203" => "Non-Authoritative Information",
28
+ "204" => "No Content",
29
+ "205" => "Reset Content",
30
+ "206" => "Partial Content",
31
+ "300" => "Multiple Choices",
32
+ "301" => "Moved Permanently",
33
+ "302" => "Found",
34
+ "303" => "See Other",
35
+ "304" => "Not Modified",
36
+ "305" => "Use Proxy",
37
+ "307" => "Temporary Redirect",
38
+ "400" => "Bad Request",
39
+ "401" => "Unauthorized",
40
+ "402" => "Payment Required",
41
+ "403" => "Forbidden",
42
+ "404" => "Not Found",
43
+ "405" => "Method Not Allowed",
44
+ "406" => "Not Acceptable",
45
+ "407" => "Proxy Authentication Required",
46
+ "408" => "Request Time-out",
47
+ "409" => "Conflict",
48
+ "410" => "Gone",
49
+ "411" => "Length Required",
50
+ "412" => "Precondition Failed",
51
+ "413" => "Request Entity Too Large",
52
+ "414" => "Request-URI Too Large",
53
+ "415" => "Unsupported Media Type",
54
+ "416" => "Requested range not satisfiable",
55
+ "417" => "Expectation Failed",
56
+ "500" => "Internal Server Error",
57
+ "501" => "Not Implemented",
58
+ "502" => "Bad Gateway",
59
+ "503" => "Service Unavailable",
60
+ "504" => "Gateway Time-out",
61
+ "505" => "HTTP Version not supported"
62
+ }
63
+
64
+ #TODO Should a client have some kind of timeout
65
+
66
+ # Initializes client data structures and retrieves OCCI model
67
+ # from the server.
68
+ #
69
+ # @example
70
+ # Occi::Api::Client::ClientAmqp.new # => #<Occi::Api::Client::ClientAmqp>
71
+ #
72
+ # @param [String] endpoint URI
73
+ # @param [Hash] auth options in a hash
74
+ # @param [Hash] logging options in a hash
75
+ # @param [Boolean] enable autoconnect
76
+ # @param [String] media type identifier
77
+ # @return [Occi::Api::Client::ClientAmqp] client instance
78
+ def initialize(endpoint = "http://localhost:3000/", auth_options = { :type => "none" },
79
+ log_options = { :out => STDERR, :level => Occi::Log::WARN, :logger => nil },
80
+ media_type = "text/plain")
81
+
82
+ # check the validity and canonize the endpoint URI
83
+ prepare_endpoint endpoint
84
+
85
+ # set Occi::Log
86
+ set_logger log_options
87
+
88
+ # pass auth options to HTTParty
89
+ change_auth auth_options
90
+
91
+ @media_type = media_type
92
+
93
+ Occi::Log.debug("Media Type: #{@media_type}")
94
+
95
+ @connected = false
96
+
97
+ Thread.new { run }
98
+
99
+ print "Waiting for connection amqp ..."
100
+
101
+ #TODO find a better solution for the thread issue
102
+ while(!@thread_error && !@connected)
103
+ #dont use sleep - it blocks the eventmachine
104
+ end
105
+
106
+ # get model information from the endpoint
107
+ # and create Occi::Model instance
108
+ set_model unless @thread_error
109
+ end
110
+
111
+ # @describe Retrieves available resources represented by resource locations (URIs).
112
+ # If no type identifier is specified, all available resource are listed.
113
+ # Type identifier can be specified in its shortened format (e.g. "compute",
114
+ # "storage", "network").
115
+ #
116
+ # @example
117
+ # client.list
118
+ # # => [ "http://localhost:3300/compute/jh425jhj3h413-7dj29d7djd9e3-djh2jh4j4j",
119
+ # # "http://localhost:3300/network/kh425jhj3h413-7dj29d7djd9e3-djh2jh4j4j",
120
+ # # "http://localhost:3300/storage/lh425jhj3h413-7dj29d7djd9e3-djh2jh4j4j" ]
121
+ # client.list "compute"
122
+ # # => [ "http://localhost:3300/compute/jh425jhj3h413-7dj29d7djd9e3-djh2jh4j4j" ]
123
+ # client.list "http://schemas.ogf.org/occi/infrastructure#compute"
124
+ # # => [ "http://localhost:3300/compute/jh425jhj3h413-7dj29d7djd9e3-djh2jh4j4j" ]
125
+ #
126
+ # @param [String] resource type identifier or just type name
127
+ # @return [Array<String>] list of links
128
+ def list(resource_type_identifier = nil, is_wait = true, is_parse_response = true)
129
+
130
+ if resource_type_identifier
131
+ # convert type to type identifier
132
+ resource_type_identifier = @model.kinds.select {
133
+ |kind| kind.term == resource_type_identifier
134
+ }.first.type_identifier if @model.kinds.select {
135
+ |kind| kind.term == resource_type_identifier
136
+ }.any?
137
+
138
+ # check some basic pre-conditions
139
+ raise "Endpoint is not connected!" unless @connected
140
+ raise "Unkown resource type identifier! [#{resource_type_identifier}]" unless @model.get_by_id resource_type_identifier
141
+
142
+ # split the type identifier and get the most important part
143
+ uri_part = resource_type_identifier.split('#').last
144
+
145
+ # request uri-list from the server
146
+ path = uri_part + '/'
147
+ else
148
+ path = '/'
149
+ end
150
+
151
+ message_id = get(path, true)
152
+
153
+ return response_value(message_id, is_wait, is_parse_response)
154
+ end
155
+
156
+ # @describe Retrieves descriptions for available resources specified by a type
157
+ # identifier or resource location. If no type identifier or location
158
+ # is specified, all available resources in all available resource types
159
+ # will be described.
160
+ #
161
+ # @example
162
+ # client.describe
163
+ # # => [#<Occi::Collection>, #<Occi::Collection>, #<Occi::Collection>]
164
+ # client.describe "compute"
165
+ # # => [#<Occi::Collection>, #<Occi::Collection>, #<Occi::Collection>]
166
+ # client.describe "http://schemas.ogf.org/occi/infrastructure#compute"
167
+ # # => [#<Occi::Collection>, #<Occi::Collection>, #<Occi::Collection>]
168
+ # client.describe "http://localhost:3300/compute/j5hk1234jk2524-2j3j2k34jjh234-adfaf1234"
169
+ # # => [#<Occi::Collection>]
170
+ #
171
+ # @param [String] resource type identifier, type name or resource location
172
+ # @return [Array<Occi::Collection>] list of resource descriptions
173
+ def describe(resource_type_identifier=nil, is_wait = true, is_parse_response = true)
174
+
175
+ raise "Endpoint is not connected!" unless @connected
176
+
177
+ # convert type to type identifier
178
+ resource_type_identifier = @model.kinds.select {
179
+ |kind| kind.term == resource_type_identifier
180
+ }.first.type_identifier if @model.kinds.select {
181
+ |kind| kind.term == resource_type_identifier
182
+ }.any?
183
+
184
+ descriptions = []
185
+
186
+ if resource_type_identifier.nil?
187
+ descriptions << response_value(get('/'), is_wait, is_parse_response)
188
+ elsif @model.get_by_id resource_type_identifier
189
+ # we got type identifier
190
+ # get all available resources of this type
191
+ locations = list resource_type_identifier
192
+
193
+ # make the requests
194
+ locations.each do |location|
195
+ descriptions << response_value(get(sanitize_resource_link(location)), is_wait, is_parse_response)
196
+ end
197
+ elsif resource_type_identifier.start_with? @endpoint
198
+ # we got resource link
199
+ # make the request
200
+ descriptions << response_value(get(sanitize_resource_link(resource_type_identifier)), is_wait, is_parse_response)
201
+ else
202
+ raise "Unkown resource type identifier! [#{resource_type_identifier}]"
203
+ end
204
+
205
+ descriptions
206
+ end
207
+
208
+ # Creates a new resource on the server. Resource must be provided
209
+ # as an instance of Occi::Core::Entity, e.g. instantiated using
210
+ # the get_resource method.
211
+ #
212
+ # @example
213
+ # res = client.get_resource "compute"
214
+ #
215
+ # res.title = "MyComputeResource1"
216
+ # res.mixins << client.find_mixin('small', "resource_tpl")
217
+ # res.mixins << client.find_mixin('debian6', "os_tpl")
218
+ #
219
+ # client.create res # => "http://localhost:3300/compute/df7698...f987fa"
220
+ #
221
+ def create(entity, is_wait = true, is_parse_response = true)
222
+
223
+ # check some basic pre-conditions
224
+ raise "Endpoint is not connected!" unless @connected
225
+ raise "#{entity} not an entity" unless entity.kind_of? Occi::Core::Entity
226
+
227
+ # is this entity valid?
228
+ entity.model = @model
229
+ entity.check
230
+ kind = entity.kind
231
+ raise "No kind found for #{entity}" unless kind
232
+
233
+ # get location for this kind of entity
234
+ location = entity.kind.location
235
+ collection = Occi::Collection.new
236
+
237
+ # is this entity a Resource or a Link?
238
+ collection.resources << entity if entity.kind_of? Occi::Core::Resource
239
+ collection.links << entity if entity.kind_of? Occi::Core::Link
240
+
241
+ # make the request
242
+ response_value(post(location, collection), is_wait, is_parse_response)
243
+ end
244
+
245
+ # Deletes a resource or all resource of a certain resource type
246
+ # from the server.
247
+ #
248
+ # @example
249
+ # client.delete "compute" # => true
250
+ # client.delete "http://schemas.ogf.org/occi/infrastructure#compute" # => true
251
+ # client.delete "http://localhost:3300/compute/245j42594...98s9df8s9f" # => true
252
+ #
253
+ # @param [String] resource type identifier, type name or location
254
+ # @return [Boolean] status
255
+ def delete(resource_type_identifier, is_wait = true, is_parse_response = true)
256
+ # convert type to type identifier
257
+ resource_type_identifier = @model.kinds.select {
258
+ |kind| kind.term == resource_type_identifier
259
+ }.first.type_identifier if @model.kinds.select {
260
+ |kind| kind.term == resource_type_identifier
261
+ }.any?
262
+
263
+ # check some basic pre-conditions
264
+ raise "Endpoint is not connected!" unless @connected
265
+
266
+ if resource_type_identifier.nil? || resource_type_identifier == "/"
267
+ path = "/"
268
+ else
269
+ raise "Unknown resource identifier! #{resource_type_identifier}" unless resource_type_identifier.start_with? @endpoint
270
+ path = sanitize_resource_link(resource_type_identifier)
271
+ end
272
+
273
+ # make the request
274
+ response_value(del(path), is_wait, is_parse_response)
275
+ end
276
+
277
+ # Triggers given action on a specific resource.
278
+ #
279
+ # @example
280
+ # TODO: add examples
281
+ #
282
+ # @param [String] resource location
283
+ # @param [String] type of action
284
+ # @return [String] resource location
285
+ def trigger(resource_type_identifier, action, is_wait = true, is_parse_response = true)
286
+
287
+ # TODO: not tested
288
+ resource_type_identifier = @model.kinds.select {
289
+ |kind| kind.term == resource_type_identifier
290
+ }.first.type_identifier if @model.kinds.select {
291
+ |kind| kind.term == resource_type_identifier
292
+ }.any?
293
+
294
+ # check some basic pre-conditions
295
+ raise "Endpoint is not connected!" unless @connected
296
+
297
+ if resource_type_identifier.nil? || resource_type_identifier == "/"
298
+ path = "/"
299
+ else
300
+ raise "Unknown resource identifier! #{resource_type_identifier}" unless resource_type_identifier.start_with? @endpoint
301
+ path = sanitize_resource_link(resource_type_identifier)
302
+ end
303
+
304
+ # encapsulate the acion in a collection
305
+ collection = Occi::Collection.new
306
+ scheme, term = action.split('#')
307
+ collection.actions << Occi::Core::Action.new(scheme + '#', term)
308
+
309
+ #@media_type = "text/plain"
310
+
311
+ # make the request
312
+ path = path + '?action=' + term
313
+ response_value(post(path, collection))
314
+ end
315
+
316
+ def parse_message(message_id, delete_response = true)
317
+ raise "message is empty for message_id(#{ message_id })" if @response_messages.nil? || @response_messages[message_id].nil?
318
+
319
+ payload = @response_messages[message_id][:payload]
320
+ type = @response_messages[message_id][:type]
321
+ metadata = @response_messages[message_id][:metadata]
322
+
323
+ @last_response_status = metadata.headers["status_code"]
324
+
325
+ @response_messages.delete(message_id) if delete_response
326
+
327
+ raise "OCCI-Server raise error: #{ payload } server status: #{ @last_response_status }" if metadata.headers["is_error"]
328
+
329
+ begin
330
+ return method("parse_#{ type }").call(payload, metadata)
331
+ rescue Exception => e
332
+ error_msg = e.message + "\n" + e.backtrace.to_s
333
+ Occi::Log.error error_msg
334
+ raise "Can not parse #{ type } payload: #{ payload } metadata: #{ metadata.inspect }"
335
+ end
336
+ end
337
+
338
+ # Looks up a mixin using its name and, optionally, a type as well.
339
+ # Will return mixin's full location (a link) or a description.
340
+ #
341
+ # @example
342
+ # client.find_mixin "debian6"
343
+ # # => "http://my.occi.service/occi/infrastructure/os_tpl#debian6"
344
+ # client.find_mixin "debian6", "os_tpl"
345
+ # # => "http://my.occi.service/occi/infrastructure/os_tpl#debian6"
346
+ # client.find_mixin "large", "resource_tpl"
347
+ # # => "http://my.occi.service/occi/infrastructure/resource_tpl#large"
348
+ # client.find_mixin "debian6", "resource_tpl" # => nil
349
+ #
350
+ # @param [String] name of the mixin
351
+ # @param [String] type of the mixin
352
+ # @param [Boolean] should we describe the mixin or return its link?
353
+ # @return [String, Occi::Collection, nil] link, mixin description or nothing found
354
+ def find_mixin(name, type = nil, describe = false)
355
+
356
+ Occi::Log.debug("Looking for mixin #{name} + #{type} + #{describe}")
357
+
358
+ # is type valid?
359
+ if type
360
+ raise "Unknown mixin type! [#{type}]" unless @mixins.has_key? type.to_sym
361
+ end
362
+
363
+ # TODO: extend this code to support multiple matches and regex filters
364
+ # should we look for links or descriptions?
365
+ if describe
366
+ # we are looking for descriptions
367
+ if type
368
+ # get the first match from either os_tpls or resource_tpls
369
+ case
370
+ when type == "os_tpl"
371
+ get_os_templates.select { |mixin| mixin.term == name }.first
372
+ when type == "resource_tpl"
373
+ get_resource_templates.select { |template| template.term == name }.first
374
+ else
375
+ nil
376
+ end
377
+ else
378
+ # try in os_tpls first
379
+ found = get_os_templates.select { |os| os.term == name }.first
380
+
381
+ # then try in resource_tpls
382
+ found = get_resource_templates.select {
383
+ |template| template.term == name
384
+ }.first unless found
385
+
386
+ found
387
+ end
388
+ else
389
+ # we are looking for links
390
+ # prefix mixin name with '#' to simplify the search
391
+ name = "#" + name
392
+ if type
393
+ # return the first match with the selected type
394
+ @mixins[type.to_sym].select {
395
+ |mixin| mixin.to_s.reverse.start_with? name.reverse
396
+ }.first
397
+ else
398
+ # there is no type preference, return first global match
399
+ @mixins.flatten(2).select {
400
+ |mixin| mixin.to_s.reverse.start_with? name.reverse
401
+ }.first
402
+ end
403
+ end
404
+ end
405
+
406
+ # Creates a new resource instance, resource should be specified
407
+ # by its name or identifier.
408
+ #
409
+ # @example
410
+ # client.get_resource "compute" # => Occi::Core::Resource
411
+ # client.get_resource "storage" # => Occi::Core::Resource
412
+ # client.get_resource "http://schemas.ogf.org/occi/infrastructure#network"
413
+ # # => Occi::Core::Resource
414
+ #
415
+ # @param [String] resource name or resource identifier
416
+ # @return [Occi::Core::Resource] new resource instance
417
+ def get_resource(resource_type)
418
+
419
+ Occi::Log.debug("Instantiating #{resource_type} ...")
420
+
421
+ if @model.get_by_id resource_type
422
+ # we got a resource type identifier
423
+ Occi::Core::Resource.new resource_type
424
+ elsif @model.kinds.select { |kind| kind.term == resource_type }.any?
425
+ # we got a resource type name
426
+ Occi::Core::Resource.new @model.kinds.select {
427
+ |kind| kind.term == resource_type
428
+ }.first.type_identifier
429
+ else
430
+ raise "Unknown resource type! [#{resource_type}]"
431
+ end
432
+
433
+ end
434
+
435
+ # Retrieves available os_tpls from the model.
436
+ #
437
+ # @example
438
+ # get_os_templates # => #<Occi::Collection>
439
+ #
440
+ # @return [Occi::Collection] collection containing all registered OS templates
441
+ def get_os_templates
442
+ @model.get.mixins.select { |mixin| mixin.related.select { |rel| rel.end_with? 'os_tpl' }.any? }
443
+ end
444
+
445
+ # Retrieves available resource_tpls from the model.
446
+ #
447
+ # @example
448
+ # get_resource_templates # => #<Occi::Collection>
449
+ #
450
+ # @return [Occi::Collection] collection containing all registered resource templates
451
+ def get_resource_templates
452
+ @model.get.mixins.select { |mixin| mixin.related.select { |rel| rel.end_with? 'resource_tpl' }.any? }
453
+ end
454
+
455
+ # Refreshes the Occi::Model used inside the client. Useful for
456
+ # updating the model without creating a new instance or
457
+ # reconnecting. Saves a lot of time in an interactive mode.
458
+ #
459
+ # @example
460
+ # client.refresh
461
+ def refresh
462
+ # re-download the model from the server
463
+ set_model
464
+ end
465
+
466
+ # private stuff --------------------------------------------------------------------------------------------------
467
+ private
468
+
469
+ def parse_get(payload, metadata)
470
+ if metadata.content_type == "text/uri-list"
471
+ return payload.split("\n")
472
+ else
473
+ path = metadata.headers["path_info"]
474
+ kind = @model.get_by_location(('/' + path).match(/\/.*\//).to_s) if @model
475
+ kind ? entity_type = kind.entity_type : entity_type = nil
476
+ collection = Occi::Parser.parse(metadata.content_type, payload, path.include?("/-/"), entity_type)
477
+ return collection
478
+ end
479
+ end
480
+
481
+ def parse_post(payload, metadata)
482
+ return URI.parse(payload).to_s
483
+ end
484
+
485
+ def parse_delete(payload, metadata)
486
+ return true
487
+ end
488
+
489
+ def run
490
+ begin
491
+ AMQP.start(CONNECTION_SETTING) do |connection, open_ok|
492
+ @channel = AMQP::Channel.new(connection)
493
+ @exchange = @channel.default_exchange
494
+
495
+ @channel.on_error(&method(:handle_channel_exception))
496
+
497
+ @replies_queue = @channel.queue(Time.now.to_f.to_s + Kernel.rand().to_s, :exclusive => true)
498
+ @replies_queue.subscribe(&method(:handle_message))
499
+
500
+ @connected = true;
501
+ end
502
+ rescue Exception => e
503
+ @thread_error = true
504
+ Occi::Log.error "Amqp Thread get an Error: #{e.message} \n #{e.backtrace.join("\n")}"
505
+ end
506
+ end
507
+
508
+ def handle_message(metadata, payload)
509
+
510
+ correlation_id = metadata.correlation_id
511
+
512
+ unless correlation_id.size > 0
513
+ raise "Message has no correlation_id (message_id)"
514
+ end
515
+
516
+ @response_messages = Hash.new if @response_messages.nil?
517
+ raise "Double Response Message ID: (#{ correlation_id })" if @response_messages.has_key?(correlation_id)
518
+
519
+ #save responses message
520
+ @response_messages[correlation_id] = {:payload => payload, :metadata => metadata, :type => @response_waiting[correlation_id][:options][:type]}
521
+
522
+ #delete message_id from waiting stack
523
+ @response_waiting.delete(correlation_id) unless @response_waiting.nil?
524
+ end
525
+
526
+ def set_model
527
+ collection = response_value(get('/-/'))
528
+ @model = Occi::Model.new(collection)
529
+
530
+ @mixins = {
531
+ :os_tpl => [],
532
+ :resource_tpl => [],
533
+ :simulation => []
534
+ }
535
+
536
+ #
537
+ get_os_templates.each do |os_tpl|
538
+ @mixins[:os_tpl] << os_tpl.type_identifier unless os_tpl.nil? or os_tpl.type_identifier.nil?
539
+ end
540
+
541
+ #
542
+ get_resource_templates.each do |res_tpl|
543
+ @mixins[:resource_tpl] << res_tpl.type_identifier unless res_tpl.nil? or res_tpl.type_identifier.nil?
544
+ end
545
+ end
546
+
547
+ # Extracts the resource path from a resource link. It will remove the leading @endpoint
548
+ # and replace it with a slash.
549
+ #
550
+ # @example
551
+ # sanitize_resource_link "http://localhost:3300/compute/35ad4f45gsf-gsfg524s6gsfg-sfgsf4gsfg"
552
+ # # => "/compute/35ad4f45gsf-gsfg524s6gsfg-sfgsf4gsfg"
553
+ #
554
+ # @param [String] string containing the full resource link
555
+ # @return [String] extracted path, with a leading slash
556
+ def sanitize_resource_link(resource_link)
557
+ raise "Resource link #{resource_link} is not valid!" unless resource_link.start_with? @endpoint
558
+
559
+ resource_link.gsub @endpoint, '/'
560
+ end
561
+
562
+ #TODO filter
563
+ def get(path='', is_uri_list = false, filter=nil)
564
+ raise "OCCI AMQP is not connected!" if !@connected
565
+
566
+ options = {
567
+ :routing_key => @endpoint_queue,
568
+ :type => "get",
569
+ :content_type => "text/plain",
570
+ :reply_to => reply_queue_name,
571
+ :message_id => next_message_id,
572
+ :headers => {
573
+ :accept => is_uri_list ? "text/uri-list" : @media_type,
574
+ :path_info => "/" + path.gsub(/\A\//, '')
575
+ }
576
+ }
577
+
578
+ publish('', options)
579
+
580
+ return options[:message_id]
581
+ end
582
+
583
+ #@description
584
+ #@param [String] path path to the resource
585
+ #@param [OCCI::Collection] collection
586
+ def post(path, collection=nil)
587
+ path = path.reverse.chomp('/').reverse
588
+
589
+ if @media_type == 'application/occi+json'
590
+ message = collection.to_json
591
+ content_type = 'application/occi+json'
592
+ else
593
+ message = collection.to_text
594
+ content_type = 'text/plain'
595
+ end
596
+
597
+ options = {
598
+ :routing_key => @endpoint_queue,
599
+ :content_type => content_type,
600
+ :type => "post",
601
+ :reply_to => reply_queue_name, #queue for response from the rOCCI
602
+ :message_id => next_message_id, #Identifier for message so that the client can match the answer from the rOCCI
603
+ :headers => {
604
+ :accept => "text/uri-list",
605
+ :path_info => "/#{ path }",
606
+ :auth => {
607
+ :type => "basic",
608
+ :username => "user",
609
+ :password => "mypass",
610
+ },
611
+ }
612
+ }
613
+
614
+ publish(message, options)
615
+
616
+ return options[:message_id]
617
+ end
618
+
619
+ # Performs DELETE requests and returns True on success.
620
+ #
621
+ # @example
622
+ # del "/compute/65sf4g65sf4g-sf6g54sf5g-sfgsf32g3" # => true
623
+ #
624
+ # @param [String] path for the DELETE request
625
+ # @param [Occi::Collection] collection of filters (currently NOT used)
626
+ # @return [Boolean] status
627
+ def del(path, filter=nil)
628
+ # remove the leading slash
629
+ path.gsub!(/\A\//, '')
630
+
631
+ options = {
632
+ :routing_key => @endpoint_queue,
633
+ :content_type => "text/plain",
634
+ :type => "delete",
635
+ :reply_to => reply_queue_name, #queue for response from the rOCCI
636
+ :message_id => next_message_id, #Identifier for message so that the client can match the answer from the rOCCI
637
+ :headers => {
638
+ :accept => @media_type,
639
+ :path_info => "/#{ path }",
640
+ :auth => {
641
+ :type => "basic",
642
+ :username => "user",
643
+ :password => "mypass",
644
+ },
645
+ }
646
+ }
647
+
648
+ publish('', options)
649
+
650
+ return options[:message_id]
651
+ end
652
+
653
+ def response_value(message_id, is_wait = true, is_parse_response = true)
654
+ waiting_for_response(message_id) if is_wait || is_parse_response
655
+
656
+ if is_parse_response
657
+ value = parse_message(message_id)
658
+ else
659
+ value = message_id
660
+ end
661
+
662
+ return value
663
+ end
664
+
665
+ # @param [String] message_id '' == all
666
+ def waiting_for_response(message_id = '')
667
+ return if @response_waiting.nil?
668
+
669
+ if message_id.size > 0
670
+ while !@response_waiting[message_id].nil?
671
+ sleep(0.1)
672
+ end
673
+ else
674
+ while size(@response_waiting) > 0
675
+ sleep(0.1)
676
+ end
677
+ end
678
+ end
679
+
680
+ def publish(message, options = {})
681
+ raise "No Message Id found" if options[:message_id] == nil?
682
+
683
+ @exchange.publish(message, options)
684
+
685
+ @response_waiting = Hash.new if @response_waiting.nil?
686
+ @response_waiting[options[:message_id]] = {:options => options, :message => message}
687
+ end
688
+
689
+ def reply_queue_name
690
+ @replies_queue.name
691
+ end
692
+
693
+ def next_message_id
694
+ @message_id = 0 if @message_id.nil?
695
+ @message_id += 1
696
+ @message_id.to_s;
697
+ end
698
+
699
+ # Sets auth method and appropriate httparty attributes. Supported auth methods
700
+ # are: ["none"] and nil
701
+ #
702
+ # @example
703
+ # change_auth { :type => "none" }
704
+ #
705
+ # @param [Hash] authentication options
706
+ def change_auth(auth_options)
707
+ @auth_options = auth_options
708
+
709
+ case @auth_options[:type]
710
+ when "none", nil
711
+ # do nothing
712
+ else
713
+ raise ArgumentError, "Unknown AUTH method [#{@auth_options[:type]}]!"
714
+ end
715
+ end
716
+
717
+ def handle_channel_exception(channel, channel_close)
718
+ Occi::Log.error "OCCI/AMQP: Channel-level exception [ code = #{channel_close.reply_code}, message = #{channel_close.reply_text} ]"
719
+ end
720
+
721
+ # Sets the logger and log levels. This allows users to pass existing logger
722
+ # instances to the rOCCI client.
723
+ #
724
+ # @example
725
+ # set_logger { :out => STDERR, :level => Occi::Log::WARN, :logger => nil }
726
+ #
727
+ # @param [Hash] logger options
728
+ def set_logger(log_options)
729
+ if log_options[:logger].nil? or (not log_options[:logger].kind_of? Occi::Log)
730
+ logger = Occi::Log.new(log_options[:out])
731
+ logger.level = log_options[:level]
732
+ end
733
+
734
+ self.class.debug_output $stderr if log_options[:level] == Occi::Log::DEBUG
735
+ end
736
+
737
+ # Checks whether the given endpoint URI is valid and adds a trailing
738
+ # slash if necessary.
739
+ #
740
+ # @example
741
+ # prepare_endpoint "http://localhost:3300" # => "http://localhost:3300/"
742
+ #
743
+ # @param [String] endpoint URI in a non-canonical string
744
+ # @return [String] canonical endpoint URI in a string, with a trailing slash
745
+ def prepare_endpoint(endpoint)
746
+ raise 'Endpoint not a valid URI' if (endpoint =~ URI::ABS_URI).nil?
747
+
748
+ @endpoint = endpoint.chomp('/') + '/'
749
+ @endpoint_queue = "amqp.occi.#{@endpoint}"
750
+ end
751
+
752
+ end
753
+
754
+ end
755
+ end
756
+ end