occi-api 4.2.0.beta.4 → 4.2.0.beta.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,70 @@
1
+ module Occi::Api::Client
2
+ module Base
3
+
4
+ module KindMethods
5
+
6
+ # Retrieves all kind type identifiers related to a given type identifier
7
+ #
8
+ # @example
9
+ # client.get_kind_type_identifiers_related_to 'http://schemas.ogf.org/occi/infrastructure#network'
10
+ # # => [ "http://schemas.ogf.org/occi/infrastructure#network",
11
+ # # "http://schemas.ogf.org/occi/infrastructure#ipnetwork" ]
12
+ #
13
+ # @param [String] type identifier
14
+ # @return [Array<String>] list of available kind type identifiers related to
15
+ # the given type identifier
16
+ def get_kind_type_identifiers_related_to(type_identifier)
17
+ Occi::Log.debug("Getting kind type identifiers related to #{type_identifier.inspect}")
18
+ collection = @model.get(type_identifier)
19
+ collection.kinds.to_a.collect { |kind| kind.type_identifier }
20
+ end
21
+
22
+ # Retrieves all available kind types.
23
+ #
24
+ # @example
25
+ # client.get_kind_types # => [ "entity", "resource", "link" ]
26
+ #
27
+ # @return [Array<String>] list of available kind types in a human-readable format
28
+ def get_kind_types
29
+ @model.kinds.to_a.collect { |kind| kind.term }
30
+ end
31
+
32
+ # Retrieves all available kind type identifiers.
33
+ #
34
+ # @example
35
+ # client.get_kind_type_identifiers
36
+ # # => [ "http://schemas.ogf.org/occi/core#entity",
37
+ # # "http://schemas.ogf.org/occi/core#resource",
38
+ # # "http://schemas.ogf.org/occi/core#link" ]
39
+ #
40
+ # @return [Array<String>] list of available kind type identifiers
41
+ def get_kind_type_identifiers
42
+ @model.kinds.to_a.collect { |kind| kind.type_identifier }
43
+ end
44
+
45
+ # Retrieves available kind type identifier for the given kind type.
46
+ #
47
+ # @example
48
+ # client.get_kind_type_identifier("compute")
49
+ # # => 'http://schemas.ogf.org/occi/infrastructure#compute'
50
+ #
51
+ # @return [String, nil] kind type identifier for the given kind type
52
+ def get_kind_type_identifier(type)
53
+ return type if (type =~ URI::ABS_URI) || (type && type.start_with?('/'))
54
+
55
+ kinds = @model.kinds.to_a.select { |k| k.term == type }
56
+ tis = kinds.collect { |k| k.type_identifier }
57
+ tis.uniq!
58
+
59
+ if tis.length > 1
60
+ raise Occi::Api::Client::Errors::AmbiguousNameError,
61
+ "Kind type #{type.inspect} is ambiguous, use a type identifier!"
62
+ end
63
+
64
+ tis.first
65
+ end
66
+
67
+ end
68
+
69
+ end
70
+ end
@@ -0,0 +1,223 @@
1
+ module Occi::Api::Client
2
+ module Base
3
+
4
+ module MixinMethods
5
+
6
+ # Looks up a mixin using its name and, optionally, a type as well.
7
+ # Will return mixin's full location (a link) or a description.
8
+ #
9
+ # @example
10
+ # client.get_mixin "debian6"
11
+ # # => "http://my.occi.service/occi/infrastructure/os_tpl#debian6"
12
+ # client.get_mixin "debian6", "os_tpl", true
13
+ # # => #<Occi::Core::Mixin>
14
+ # client.get_mixin "large", "resource_tpl"
15
+ # # => "http://my.occi.service/occi/infrastructure/resource_tpl#large"
16
+ # client.get_mixin "debian6", "resource_tpl" # => nil
17
+ #
18
+ # @param [String] name of the mixin
19
+ # @param [String] type of the mixin
20
+ # @param [Boolean] should we describe the mixin or return its link?
21
+ # @return [String, Occi::Core::Mixin, nil] link, mixin description or nothing found
22
+ def get_mixin(name, type = nil, describe = false)
23
+ # TODO: mixin fix
24
+ Occi::Log.debug("Looking for mixin #{name} + #{type} + #{describe}")
25
+
26
+ # TODO: extend this code to support multiple matches and regex filters
27
+ # should we look for links or descriptions?
28
+ describe ? describe_mixin(name, type) : list_mixin(name, type)
29
+ end
30
+
31
+ # Looks up a mixin using its name and, optionally, a type as well.
32
+ # Will return mixin's full description.
33
+ #
34
+ # @example
35
+ # client.describe_mixin "debian6"
36
+ # # => #<Occi::Core::Mixin>
37
+ # client.describe_mixin "debian6", "os_tpl"
38
+ # # => #<Occi::Core::Mixin>
39
+ # client.describe_mixin "large", "resource_tpl"
40
+ # # => #<Occi::Core::Mixin>
41
+ # client.describe_mixin "debian6", "resource_tpl" # => nil
42
+ #
43
+ # @param [String] name of the mixin
44
+ # @param [String] type of the mixin
45
+ # @return [Occi::Core::Mixin, nil] mixin description or nothing found
46
+ def describe_mixin(name, type = nil)
47
+ mixins = get_mixins(type)
48
+
49
+ mixins = mixins.to_a.select { |m| m.term == name }
50
+ mixins.any? ? mixins.first : nil
51
+ end
52
+
53
+ # Looks up a mixin with a specific type, will return
54
+ # mixin's full description.
55
+ #
56
+ # @param [String] name of the mixin
57
+ # @param [String] type of the mixin
58
+ # @return [Occi::Core::Mixin] mixin description
59
+ def describe_mixin_w_type(name, type)
60
+ describe_mixin(name, type)
61
+ end
62
+
63
+ # Looks up a mixin in all available mixin types, will
64
+ # return mixin's full description. Returns always the
65
+ # first match found, search will start in os_tpl.
66
+ #
67
+ # @param [String] name of the mixin
68
+ # @return [Occi::Core::Mixin] mixin description
69
+ def describe_mixin_wo_type(name)
70
+ describe_mixin(name, nil)
71
+ end
72
+
73
+ # Looks up a mixin using its name and, optionally, a type as well.
74
+ # Will return mixin's full location.
75
+ #
76
+ # @example
77
+ # client.list_mixin "debian6"
78
+ # # => "http://my.occi.service/occi/infrastructure/os_tpl#debian6"
79
+ # client.list_mixin "debian6", "os_tpl"
80
+ # # => "http://my.occi.service/occi/infrastructure/os_tpl#debian6"
81
+ # client.list_mixin "large", "resource_tpl"
82
+ # # => "http://my.occi.service/occi/infrastructure/resource_tpl#large"
83
+ # client.list_mixin "debian6", "resource_tpl" # => nil
84
+ #
85
+ # @param [String] name of the mixin
86
+ # @param [String] type of the mixin
87
+ # @return [String, nil] link or nothing found
88
+ def list_mixin(name, type = nil)
89
+ mixin = describe_mixin(name, type)
90
+ mixin ? mixin.type_identifier : nil
91
+ end
92
+
93
+ # Retrieves available mixins of a specified type or all available
94
+ # mixins if the type wasn't specified. Mixins are returned in the
95
+ # form of mixin instances.
96
+ #
97
+ # @example
98
+ # client.get_mixins
99
+ # # => #<Occi::Core::Mixins>
100
+ # client.get_mixins "os_tpl"
101
+ # # => #<Occi::Core::Mixins>
102
+ # client.get_mixins "resource_tpl"
103
+ # # => #<Occi::Core::Mixins>
104
+ #
105
+ # @param [String] type of mixins
106
+ # @return [Occi::Core::Mixins] collection of available mixins
107
+ def get_mixins(type = nil)
108
+ unless type.blank?
109
+ type_id = get_mixin_type_identifier(type)
110
+ unless type_id
111
+ raise ArgumentError,
112
+ "There is no such mixin type registered in the model! #{type.inspect}"
113
+ end
114
+
115
+ mixins = @model.mixins.to_a.select { |m| m.related_to?(type_id) }
116
+
117
+ # drop the type mixin itself
118
+ mixins.delete_if { |m| m.type_identifier == type_id }
119
+ else
120
+ # we did not get a type, return all mixins
121
+ mixins = Occi::Core::Mixins.new(@model.mixins)
122
+ end
123
+
124
+ unless mixins.kind_of? Occi::Core::Mixins
125
+ col = Occi::Core::Mixins.new
126
+ mixins.each { |m| col << m }
127
+ else
128
+ col = mixins
129
+ end
130
+
131
+ col
132
+ end
133
+
134
+ # Retrieves available mixins of a specified type or all available
135
+ # mixins if the type wasn't specified. Mixins are returned in the
136
+ # form of mixin identifiers.
137
+ #
138
+ # @example
139
+ # client.list_mixins
140
+ # # => #<Array<String>>
141
+ # client.list_mixins "os_tpl"
142
+ # # => #<Array<String>>
143
+ # client.list_mixins "resource_tpl"
144
+ # # => #<Array<String>>
145
+ #
146
+ # @param [String] type of mixins
147
+ # @return [Array<String>] collection of available mixin identifiers
148
+ def list_mixins(type = nil)
149
+ mixins = get_mixins(type)
150
+ mixins.to_a.collect { |m| m.type_identifier }
151
+ end
152
+
153
+ # Retrieves available mixin types. Mixin types are presented
154
+ # in a shortened format (i.e. not as type identifiers).
155
+ #
156
+ # @example
157
+ # client.get_mixin_types # => [ "os_tpl", "resource_tpl" ]
158
+ #
159
+ # @return [Array<String>] list of available mixin types
160
+ def get_mixin_types
161
+ get_mixins.to_a.collect { |m| m.term }
162
+ end
163
+
164
+ # Retrieves available mixin type identifiers.
165
+ #
166
+ # @example
167
+ # client.get_mixin_type_identifiers
168
+ # # => ['http://schemas.ogf.org/occi/infrastructure#os_tpl',
169
+ # # 'http://schemas.ogf.org/occi/infrastructure#resource_tpl']
170
+ #
171
+ # @return [Array<String>] list of available mixin type identifiers
172
+ def get_mixin_type_identifiers
173
+ list_mixins(nil)
174
+ end
175
+
176
+ # Retrieves available mixin type identifier for the given mixin type.
177
+ #
178
+ # @example
179
+ # client.get_mixin_type_identifier("os_tpl")
180
+ # # => 'http://schemas.ogf.org/occi/infrastructure#os_tpl'
181
+ #
182
+ # @return [String, nil] mixin type identifier for the given mixin type
183
+ def get_mixin_type_identifier(type)
184
+ return type if (type =~ URI::ABS_URI) || (type && type.start_with?('/'))
185
+
186
+ mixins = @model.mixins.to_a.select { |m| m.term == type }
187
+ tis = mixins.collect { |m| m.type_identifier }
188
+ tis.uniq!
189
+
190
+ if tis.length > 1
191
+ raise Occi::Api::Client::Errors::AmbiguousNameError,
192
+ "Mixin type #{type.inspect} is ambiguous, use a type identifier!"
193
+ end
194
+
195
+ tis.first
196
+ end
197
+
198
+ # Retrieves available os_tpls from the model.
199
+ #
200
+ # @example
201
+ # get_os_templates # => #<Occi::Core::Mixins>
202
+ #
203
+ # @return [Occi::Core::Mixins] collection containing all registered OS templates
204
+ def get_os_templates
205
+ get_mixins Occi::Infrastructure::OsTpl.mixin.type_identifier
206
+ end
207
+ alias_method :get_os_tpls, :get_os_templates
208
+
209
+ # Retrieves available resource_tpls from the model.
210
+ #
211
+ # @example
212
+ # get_resource_templates # => #<Occi::Core::Mixins>
213
+ #
214
+ # @return [Occi::Core::Mixins] collection containing all registered resource templates
215
+ def get_resource_templates
216
+ get_mixins Occi::Infrastructure::ResourceTpl.mixin.type_identifier
217
+ end
218
+ alias_method :get_resource_tpls, :get_resource_templates
219
+
220
+ end
221
+
222
+ end
223
+ end
@@ -0,0 +1,79 @@
1
+ module Occi::Api::Client
2
+ module Base
3
+
4
+ module ProtectedHelpers
5
+
6
+ # Sets the logger and log levels. This allows users to pass existing logger
7
+ # instances to the rOCCI client.
8
+ #
9
+ # @example
10
+ # get_logger { :out => STDERR, :level => Occi::Log::WARN, :logger => nil }
11
+ #
12
+ # @param [Hash] logger options
13
+ # @return [Occi::Log] instance of the logger
14
+ def get_logger(log_options)
15
+ unless log_options[:logger].kind_of?(Occi::Log)
16
+ logger = Occi::Log.new(log_options[:out])
17
+ logger.level = log_options[:level]
18
+ else
19
+ logger = log_options[:logger]
20
+ end
21
+
22
+ logger
23
+ end
24
+
25
+ # Checks whether the given endpoint URI is valid and converts it
26
+ # to a URI instance.
27
+ #
28
+ # @example
29
+ # get_endpoint_uri "http://localhost:3300" # => #<URI::*>
30
+ #
31
+ # @param [String] endpoint URI in a non-canonical string
32
+ # @return [URI] canonical endpoint URI
33
+ def get_endpoint_uri(endpoint)
34
+ unless endpoint =~ URI::ABS_URI
35
+ raise "Endpoint not a valid absolute URI! #{endpoint.inspect}"
36
+ end
37
+
38
+ # normalize URIs, remove trailing slashes
39
+ endpoint = URI(endpoint)
40
+ endpoint.path = endpoint.path.gsub(/\/+/, '/').chomp('/')
41
+
42
+ endpoint
43
+ end
44
+
45
+ # Creates an Occi::Model from data retrieved from the server.
46
+ #
47
+ # @example
48
+ # model_collection = get('/-/')
49
+ # get_model model_collection # => #<Occi::Model>
50
+ #
51
+ # @param [Occi::Collection] parsed representation of server's model
52
+ # @return [Occi::Model] Model instance
53
+ def get_model(model_collection)
54
+ # build model
55
+ Occi::Model.new(model_collection)
56
+ end
57
+
58
+ # Returns mixin type identifiers for os_tpl mixins
59
+ # in an array.
60
+ #
61
+ # @return [Array] array of os_tpl mixin identifiers
62
+ def get_os_tpl_mixins_ary
63
+ mixins = get_os_tpls
64
+ mixins.to_a.collect { |m| m.type_identifier }
65
+ end
66
+
67
+ # Returns mixin type identifiers for resource_tpl mixins
68
+ # in an array.
69
+ #
70
+ # @return [Array] array of resource_tpl mixin identifiers
71
+ def get_resource_tpl_mixins_ary
72
+ mixins = get_resource_tpls
73
+ mixins.to_a.collect { |m| m.type_identifier }
74
+ end
75
+
76
+ end
77
+
78
+ end
79
+ end
@@ -0,0 +1,44 @@
1
+ module Occi::Api::Client
2
+ module Base
3
+
4
+ module ProtectedStubs
5
+
6
+ # Sets auth method and appropriate httparty attributes. Supported auth methods
7
+ # are: ["basic", "digest", "x509", "none"]
8
+ #
9
+ # @example
10
+ # get_auth { :type => "none" }
11
+ # get_auth { :type => "basic", :username => "123", :password => "321" }
12
+ # get_auth { :type => "digest", :username => "123", :password => "321" }
13
+ # get_auth { :type => "x509", :user_cert => "~/cert.pem",
14
+ # :user_cert_password => "321", :ca_path => nil }
15
+ #
16
+ # @param [Hash] authentication options
17
+ # @param [Boolean] allow fallback-only options
18
+ # @return [Hash] transformed hash with authN information
19
+ def get_auth(auth_options, fallback = false)
20
+ raise Occi::Api::Client::Errors::NotImplementedError, "#{__method__} is just a stub!"
21
+ end
22
+
23
+ # Attempts to establish a preliminary connection with the server
24
+ # to verify provided credentials and perform fallback authN
25
+ # if necessary. Has to be invoked after @auth_options have been set.
26
+ def preauthenticate
27
+ raise Occi::Api::Client::Errors::NotImplementedError, "#{__method__} is just a stub!"
28
+ end
29
+
30
+ # Sets media type. Will choose either application/occi+json or text/plain
31
+ # based on the formats supported by the server.
32
+ #
33
+ # @example
34
+ # get_media_type # => 'application/occi+json'
35
+ #
36
+ # @return [String] chosen media type
37
+ def get_media_type(force_type = nil)
38
+ raise Occi::Api::Client::Errors::NotImplementedError, "#{__method__} is just a stub!"
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,142 @@
1
+ module Occi::Api::Client
2
+ module Base
3
+
4
+ module Stubs
5
+
6
+ # Retrieves available resources represented by resource locations (URIs).
7
+ # If no type identifier is specified, all available resource are listed.
8
+ # Type identifier can be specified in its shortened format (e.g. "compute",
9
+ # "storage", "network").
10
+ #
11
+ # @example
12
+ # client.list
13
+ # # => [ "http://localhost:3300/compute/jh425jhj3h413-7dj29d7djd9e3-djh2jh4j4j",
14
+ # # "http://localhost:3300/network/kh425jhj3h413-7dj29d7djd9e3-djh2jh4j4j",
15
+ # # "http://localhost:3300/storage/lh425jhj3h413-7dj29d7djd9e3-djh2jh4j4j" ]
16
+ # client.list "compute"
17
+ # # => [ "http://localhost:3300/compute/jh425jhj3h413-7dj29d7djd9e3-djh2jh4j4j" ]
18
+ # client.list "http://schemas.ogf.org/occi/infrastructure#compute"
19
+ # # => [ "http://localhost:3300/compute/jh425jhj3h413-7dj29d7djd9e3-djh2jh4j4j" ]
20
+ #
21
+ # @param [String] resource type identifier or just type name
22
+ # @return [Array<String>] list of links
23
+ def list(resource_type_identifier=nil)
24
+ raise Occi::Api::Client::Errors::NotImplementedError, "#{__method__} is just a stub!"
25
+ end
26
+
27
+ # Retrieves descriptions for available resources specified by a type
28
+ # identifier or resource location. If no type identifier or location
29
+ # is specified, all available resources in all available resource types
30
+ # will be described.
31
+ #
32
+ # @example
33
+ # client.describe
34
+ # # => #<Occi::Core::Resources>
35
+ # client.describe "compute"
36
+ # # => #<Occi::Core::Resources>
37
+ # client.describe "http://schemas.ogf.org/occi/infrastructure#compute"
38
+ # # => #<Occi::Core::Resources>
39
+ # client.describe "http://localhost:3300/compute/j5hk1234jk2524-2j3j2k34jjh234-adfaf1234"
40
+ # # => #<Occi::Core::Resources>
41
+ #
42
+ # @param [String] resource type identifier, type name or resource location
43
+ # @return [Occi::Core::Resources] list of resource descriptions
44
+ def describe(resource_type_identifier=nil)
45
+ raise Occi::Api::Client::Errors::NotImplementedError, "#{__method__} is just a stub!"
46
+ end
47
+
48
+ # Creates a new resource on the server. Resource must be provided
49
+ # as an instance of Occi::Core::Entity, e.g. instantiated using
50
+ # the get_resource method.
51
+ #
52
+ # @example
53
+ # res = client.get_resource "compute"
54
+ #
55
+ # res.title = "MyComputeResource1"
56
+ # res.mixins << client.get_mixin('small', "resource_tpl")
57
+ # res.mixins << client.get_mixin('debian6', "os_tpl")
58
+ #
59
+ # client.create res # => "http://localhost:3300/compute/df7698...f987fa"
60
+ #
61
+ # @param [Occi::Core::Entity] resource to be created on the server
62
+ # @return [String] URI of the new resource
63
+ def create(entity)
64
+ raise Occi::Api::Client::Errors::NotImplementedError, "#{__method__} is just a stub!"
65
+ end
66
+
67
+ # Deploys a compute resource based on an OVF/OVA descriptor available
68
+ # on a local file system.
69
+ #
70
+ # @example
71
+ # client.deploy "~/MyVMs/rOcciVM.ovf" # => "http://localhost:3300/compute/343423...42njhdafa"
72
+ #
73
+ # @param [String] location of an OVF/OVA file
74
+ # @return [String] URI of the new resource
75
+ def deploy(location)
76
+ raise Occi::Api::Client::Errors::NotImplementedError, "#{__method__} is just a stub!"
77
+ end
78
+
79
+ # Deploys a compute resource based on an OVF descriptor available
80
+ # directly as a String.
81
+ #
82
+ # @example
83
+ # client.deploy_ovf "OVF DESCRIPTOR HERE" # => "http://localhost:3300/compute/343423...42njhdafa"
84
+ #
85
+ # @param [String] OVF descriptor (e.g., already read from a file or generated)
86
+ # @return [String] URI of the new resource
87
+ def deploy_ovf(descriptor)
88
+ raise Occi::Api::Client::Errors::NotImplementedError, "#{__method__} is just a stub!"
89
+ end
90
+
91
+ # Deploys a compute resource based on an OVA descriptor available
92
+ # directly as a String.
93
+ #
94
+ # @example
95
+ # client.deploy_ova "OVA DESCRIPTOR HERE" # => "http://localhost:3300/compute/343423...42njhdafa"
96
+ #
97
+ # @param [String] OVA descriptor (e.g., already read from a file or generated)
98
+ # @return [String] URI of the new resource
99
+ def deploy_ova(descriptor)
100
+ raise Occi::Api::Client::Errors::NotImplementedError, "#{__method__} is just a stub!"
101
+ end
102
+
103
+ # Deletes a resource or all resource of a certain resource type
104
+ # from the server.
105
+ #
106
+ # @example
107
+ # client.delete "compute" # => true
108
+ # client.delete "http://schemas.ogf.org/occi/infrastructure#compute" # => true
109
+ # client.delete "http://localhost:3300/compute/245j42594...98s9df8s9f" # => true
110
+ #
111
+ # @param [String] resource type identifier, type name or location
112
+ # @return [Boolean] status
113
+ def delete(resource_type_identifier)
114
+ raise Occi::Api::Client::Errors::NotImplementedError, "#{__method__} is just a stub!"
115
+ end
116
+
117
+ # Triggers given action on a specific resource.
118
+ #
119
+ # @example
120
+ # TODO: add examples
121
+ #
122
+ # @param [String] resource type or type identifier
123
+ # @param [Occi::Core::ActionInstance] type of action
124
+ # @return [String] resource location
125
+ def trigger(resource_type_identifier, action_instance)
126
+ raise Occi::Api::Client::Errors::NotImplementedError, "#{__method__} is just a stub!"
127
+ end
128
+
129
+ # Refreshes the Occi::Model used inside the client. Useful for
130
+ # updating the model without creating a new instance or
131
+ # reconnecting. Saves a lot of time in an interactive mode.
132
+ #
133
+ # @example
134
+ # client.refresh
135
+ def refresh
136
+ raise Occi::Api::Client::Errors::NotImplementedError, "#{__method__} is just a stub!"
137
+ end
138
+
139
+ end
140
+
141
+ end
142
+ end