ocs 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 577b3c6848ab14e6b0955816682d593cac6dd8ec
4
- data.tar.gz: 95055bd21dac7c1cfbfb993685322cf12c17866d
3
+ metadata.gz: 930d79abb1e657b74654c2b85f3a0d4189e2b3d5
4
+ data.tar.gz: 2a407305940592e09f836cff4958dc9ef1ad6c82
5
5
  SHA512:
6
- metadata.gz: 0783b506af579edc04433a27020c802e0d8b57fa15aab815e658d4dfe0416af632963f72fc075bcac3f008c5d2e31724afb713875e11fbde38344ead96ddf526
7
- data.tar.gz: 967915519022b16e200e4a7497956ed21233e30f7c111a12151900edf7c556ce8a02076e54624dd5d2af0913aa8fa41cf35481658c9f85f81ef6bf5b56e8f086
6
+ metadata.gz: f7d19e2f35dbc4c173414c33d1a33c773dc58760a42a5247a6d0ea33ded8e23738f3b430483c57f0db1979ce8a2d1346ee0985ea6a2d218fa801bd2089a752ef
7
+ data.tar.gz: d9403441d146cd9ab773ee9e0c647a3b7540643d9f0319bcc93a4a082e33ffcf5f1a80447201b73062682784bb9b15f2a8988404adab19fa0df865dfb4f2928c
data/lib/ocs.rb CHANGED
@@ -13,6 +13,7 @@ module Ocs
13
13
  class OcsError < StandardError; end
14
14
  end
15
15
 
16
+ require "ocs/api_error"
16
17
  require "ocs/client"
17
18
  require "ocs/request"
18
19
  require "ocs/resources"
@@ -0,0 +1,35 @@
1
+ module Ocs
2
+ class ApiError < OcsError
3
+ attr_reader :api, :parameters, :error_response
4
+
5
+ def initialize(api, parameters, error_response)
6
+ @api = api
7
+ @parameters = parameters
8
+ @error_response = error_response
9
+ end
10
+
11
+ def error_code
12
+ error_response.content[:cserrorcode]
13
+ end
14
+
15
+ def error_text
16
+ error_response.content[:errortext]
17
+ end
18
+
19
+ def message
20
+ {
21
+ api: api,
22
+ parameters: parameters,
23
+ error_response: error_response.raw_body
24
+ }
25
+ end
26
+
27
+ def status_code
28
+ error_response.content[:errorcode]
29
+ end
30
+
31
+ def to_s
32
+ "[#{error_code}] #{error_text}"
33
+ end
34
+ end
35
+ end
data/lib/ocs/client.rb CHANGED
@@ -1,18 +1,18 @@
1
1
  module Ocs
2
2
  class Client
3
- attr_reader :api_key, :host, :path, :secret_key, :logger
3
+ attr_reader :api_key, :host, :path, :secret_key, :logger, :ssl
4
4
 
5
- def initialize(host:, api_key:, secret_key:, path: "/client/api", logger: nil)
5
+ def initialize(host:, api_key:, secret_key:, path: "/client/api", logger: nil, ssl: true)
6
6
  @host = host
7
7
  @api_key = api_key
8
8
  @secret_key = secret_key
9
9
  @path = path
10
10
  @logger = logger
11
+ @ssl = ssl
11
12
  end
12
13
 
13
14
  def call(name, options = {})
14
- send(name, options)
15
- .body["#{name.downcase.pluralize}response"]
15
+ send(name, options).content
16
16
  end
17
17
 
18
18
  def connection
@@ -42,7 +42,11 @@ module Ocs
42
42
  end
43
43
 
44
44
  def url_prefix
45
- "https://#{host}"
45
+ "#{url_protocol}://#{host}"
46
+ end
47
+
48
+ def url_protocol
49
+ ssl ? "https" : "http"
46
50
  end
47
51
 
48
52
  def method_missing(method, *args)
data/lib/ocs/resources.rb CHANGED
@@ -10,6 +10,7 @@ module Ocs
10
10
  autoload :Group, "ocs/resources/group"
11
11
  autoload :Host, "ocs/resources/host"
12
12
  autoload :Iso, "ocs/resources/iso"
13
+ autoload :Network, "ocs/resources/network"
13
14
  autoload :Nic, "ocs/resources/nic"
14
15
  autoload :OsType, "ocs/resources/os_type"
15
16
  autoload :ResourceDetail, "ocs/resources/resource_detail"
@@ -1,8 +1,28 @@
1
1
  module Ocs
2
- module Resource
2
+ module Resources
3
3
  class AsyncJob < Base
4
4
  define_attribute :jobid, type: String
5
5
  define_attribute :jobstatus, type: Integer
6
+
7
+ def done?
8
+ reload!
9
+ jobstatus != 0
10
+ end
11
+
12
+ def failed?
13
+ reload!
14
+ jobstatus == 2
15
+ end
16
+
17
+ def success?
18
+ reload!
19
+ jobstatus == 1
20
+ end
21
+
22
+ def reload!
23
+ res = client.call("queryAsyncJobResult", jobid: jobid)
24
+ self.jobstatus = res[:jobstatus]
25
+ end
6
26
  end
7
27
  end
8
28
  end
@@ -19,6 +19,10 @@ module Ocs
19
19
  name.downcase
20
20
  end
21
21
 
22
+ def inherited(sub_class)
23
+ sub_class.delegations = {}
24
+ end
25
+
22
26
  def name
23
27
  to_s.split(/::/).last
24
28
  end
@@ -32,8 +36,13 @@ module Ocs
32
36
  end
33
37
 
34
38
  def list(client, parameters = {})
35
- client.call("list#{pluralized_name}", parameters)[downcased_name].map do |attributes|
36
- new(client, attributes)
39
+ response = client.call("list#{pluralized_name}", parameters)
40
+ if response.empty?
41
+ []
42
+ else
43
+ response[downcased_name].map do |attributes|
44
+ new(client, attributes)
45
+ end
37
46
  end
38
47
  end
39
48
 
@@ -61,10 +70,10 @@ module Ocs
61
70
  end
62
71
 
63
72
  def define_action(action_name, required: [], optional: [], api_name: nil)
64
- define_method(action_name) do
73
+ define_method(action_name) do |special_parameters = {}|
65
74
  api = api_name || "#{action_name}#{self.class.name}"
66
- parameters = action_parameters(required, optional)
67
- client.call(api, parameters)
75
+ parameters = action_parameters(required, optional).merge(special_parameters)
76
+ send_and_update(api, parameters)
68
77
  end
69
78
  end
70
79
 
@@ -143,12 +152,20 @@ module Ocs
143
152
 
144
153
  define_attribute :id, type: String
145
154
 
146
- attr_reader :client
155
+ attr_reader :client, :error
147
156
 
148
157
  def initialize(client, raw_hash = {})
149
158
  @client = client
150
159
  @raw_hash = raw_hash
151
- update_attributes(raw_hash)
160
+ update_attributes!(raw_hash)
161
+ end
162
+
163
+ def reload!
164
+ response = client.call("list#{self.class.pluralized_name}", id: id)[self.class.downcased_name]
165
+ if response && !response.empty?
166
+ update_attributes!(response.first)
167
+ end
168
+ self
152
169
  end
153
170
 
154
171
  private
@@ -160,14 +177,17 @@ module Ocs
160
177
 
161
178
  def check_required_keys(required_keys)
162
179
  required_keys.each do |key|
180
+ key = key[:attribute] if key.is_a?(Hash)
163
181
  raise MissingKeyError.new("#{key} key is required") if public_send(key).nil?
164
182
  end
165
183
  end
166
184
 
167
185
  def parameters(keys)
168
186
  keys.inject({}) do |params, key|
169
- value = public_send(key)
170
- params[key.to_s.delete("_")] = value if value
187
+ attribute_name = key.is_a?(Hash) ? key[:attribute] : key
188
+ request_key = key.is_a?(Hash) ? key[:as].to_s : key.to_s.delete("_")
189
+ value = public_send(attribute_name)
190
+ params[request_key] = value if value
171
191
  params
172
192
  end
173
193
  end
@@ -176,7 +196,19 @@ module Ocs
176
196
  "ocs/resources/#{class_name}".camelize.constantize
177
197
  end
178
198
 
179
- def update_attributes(hash)
199
+ def send_and_update(api, parameters)
200
+ response = client.send(api, parameters)
201
+ if response.success?
202
+ @error = nil
203
+ update_attributes!(response.content)
204
+ true
205
+ else
206
+ @error = ApiError.new(api, parameters, response)
207
+ false
208
+ end
209
+ end
210
+
211
+ def update_attributes!(hash)
180
212
  hash.each do |key, value|
181
213
  if delegations.has_key?(key.to_sym)
182
214
  public_send(delegations[key.to_sym], value)
@@ -1,7 +1,15 @@
1
1
  module Ocs
2
2
  module Resources
3
3
  class Domain < Base
4
+ has_one :domain
5
+
6
+ define_attribute :haschild, type: BOOLEAN
7
+ define_attribute :level, type: Integer
4
8
  define_attribute :name, type: String
9
+ define_attribute :path, type: String
10
+
11
+ delegate_attribute :parentdomainid, to: :domain, as: :id
12
+ delegate_attribute :parentdomainname, to: :domain, as: :name
5
13
  end
6
14
  end
7
15
  end
@@ -0,0 +1,59 @@
1
+ module Ocs
2
+ module Resources
3
+ class Network < Base
4
+ has_one :domain
5
+ has_one :project
6
+ has_one :zone
7
+
8
+ has_many :tags
9
+
10
+ define_attribute :account, type: String
11
+ define_attribute :aclid, type: String
12
+ define_attribute :acltype, type: String
13
+ define_attribute :broadcastdomaintype, type: String
14
+ define_attribute :broadcasturi, type: String
15
+ define_attribute :canusefordeploy, type: BOOLEAN
16
+ define_attribute :cidr, type: String
17
+ define_attribute :displaynetwork, type: BOOLEAN
18
+ define_attribute :displaytext, type: String
19
+ define_attribute :dns1, type: String
20
+ define_attribute :dns2, type: String
21
+ define_attribute :gateway, type: String
22
+ define_attribute :ip6cidr, type: String
23
+ define_attribute :ip6gateway, type: String
24
+ define_attribute :isdefault, type: BOOLEAN
25
+ define_attribute :ispersistent, type: BOOLEAN
26
+ define_attribute :issystem, type: BOOLEAN
27
+ define_attribute :name, type: String
28
+ define_attribute :netmask, type: String
29
+ define_attribute :networkcidr, type: String
30
+ define_attribute :networkdomain, type: String
31
+ define_attribute :networkofferingavailability, type: String
32
+ define_attribute :networkofferingconservemode, type: BOOLEAN
33
+ define_attribute :networkofferingdisplaytext, type: String
34
+ define_attribute :networkofferingid, type: String
35
+ define_attribute :networkofferingname, type: String
36
+ define_attribute :physicalnetworkid, type: String
37
+ define_attribute :related, type: String
38
+ define_attribute :reservediprange, type: String
39
+ define_attribute :restartrequired, type: BOOLEAN
40
+ define_attribute :service, type: Array
41
+ define_attribute :specifyipranges, type: BOOLEAN
42
+ define_attribute :state, type: String
43
+ define_attribute :subdomainaccess, type: BOOLEAN
44
+ define_attribute :traffictype, type: String
45
+ define_attribute :type, type: String
46
+ define_attribute :vlan, type: String
47
+ define_attribute :vpcid, type: String
48
+
49
+ delegate_attribute :domain, to: :domain, as: :name
50
+ delegate_attribute :domainid, to: :domain, as: :id
51
+ delegate_attribute :project, to: :project, as: :name
52
+ delegate_attribute :projectid, to: :project, as: :id
53
+ delegate_attribute :zoneid, to: :zone, as: :id
54
+ delegate_attribute :zonename, to: :zone, as: :name
55
+
56
+ delegate_attributes :tags, to: :tags
57
+ end
58
+ end
59
+ end
@@ -1,6 +1,8 @@
1
1
  module Ocs
2
2
  module Resources
3
3
  class Nic < Base
4
+ has_one :network
5
+
4
6
  define_attribute :broadcasturi, type: String
5
7
  define_attribute :gateway, type: String
6
8
  define_attribute :ipaddress, type: String
@@ -12,6 +14,9 @@ module Ocs
12
14
  define_attribute :networkname, type: String
13
15
  define_attribute :traffictype, type: String
14
16
  define_attribute :type, type: String
17
+
18
+ delegate_attribute :networkid, to: :network, as: :id
19
+ delegate_attribute :networkname, to: :network, as: :name
15
20
  end
16
21
  end
17
22
  end
@@ -0,0 +1,10 @@
1
+ module Ocs
2
+ module Resources
3
+ class ResourceDetail < Base
4
+ define_attribute :type, type: String
5
+
6
+ alias_attribute :resourceid, :id
7
+ alias_attribute :resourcetype, :type
8
+ end
9
+ end
10
+ end
@@ -1,6 +1,13 @@
1
1
  module Ocs
2
2
  module Resources
3
3
  class SshKeyPair < Base
4
+ class << self
5
+ def name
6
+ "SSHKeyPair"
7
+ end
8
+ end
9
+
10
+ define_attribute :fingerprint, type: String
4
11
  define_attribute :name, type: String
5
12
  end
6
13
  end
@@ -10,7 +10,7 @@ module Ocs
10
10
  define_attribute :value, type: String
11
11
  define_attribute :resourcetype, type: String
12
12
 
13
- delegate_attribute :acount, to: :account, as: :name
13
+ delegate_attribute :account, to: :account, as: :name
14
14
  delegate_attribute :domain, to: :domain, as: :name
15
15
  delegate_attribute :domainid, to: :domain, as: :id
16
16
  delegate_attribute :project, to: :project, as: :id
@@ -3,6 +3,7 @@ module Ocs
3
3
  class VirtualMachine < Base
4
4
  has_one :account
5
5
  has_one :address
6
+ has_one :async_job
6
7
  has_one :disk_offering
7
8
  has_one :domain
8
9
  has_one :group
@@ -34,7 +35,7 @@ module Ocs
34
35
  #define_attribute :forvirtualnetwork
35
36
  define_attribute :haenable, type: BOOLEAN
36
37
  define_attribute :hypervisor, type: String
37
- #define_attribute :instancename
38
+ define_attribute :instancename, type: String
38
39
  define_attribute :isdynamicallyscalable, type: BOOLEAN
39
40
  define_attribute :memory, type: Integer
40
41
  define_attribute :name, type: String
@@ -65,8 +66,8 @@ module Ocs
65
66
  delegate_attribute :jobstatus, to: :async_job, as: :jobstatus
66
67
  delegate_attribute :keypair, to: :ssh_key_pair, as: :name
67
68
  delegate_attribute :ostypeid, to: :os_type, as: :id
68
- #delegate_attribute :project, to: :project, as: :name
69
- #delegate_attribute :projectid, to: :project, as: :id
69
+ delegate_attribute :project, to: :project, as: :name
70
+ delegate_attribute :projectid, to: :project, as: :id
70
71
  delegate_attribute :publicip, to: :address, as: :id
71
72
  delegate_attribute :publicipid, to: :address, as: :id
72
73
  delegate_attribute :serviceofferingid, to: :service_offering, as: :id
@@ -84,7 +85,38 @@ module Ocs
84
85
 
85
86
  define_action :deploy,
86
87
  required: %i(service_offering_id template_id zone_id),
87
- optional: %i(displayname group_name name)
88
+ optional: %i(displayname name displayvm) + [
89
+ {attribute: :group_name, as: :group},
90
+ {attribute: :ssh_key_pair_name, as: :keypair}
91
+ ]
92
+
93
+ def destroyed?
94
+ state == "Destroyed"
95
+ end
96
+
97
+ def running?
98
+ state == "Running"
99
+ end
100
+
101
+ def starting?
102
+ state == "Starting"
103
+ end
104
+
105
+ def stopped?
106
+ state == "Stopped"
107
+ end
108
+
109
+ def add_nic(network:, ipaddress: nil)
110
+ network_id = network.is_a?(Resources::Base) ? network.id : network
111
+ parameters = {networkid: network_id, virtualmachineid: id}
112
+ parameters[:ipaddress] = ipaddress if ipaddress
113
+ send_and_update("addNicToVirtualMachine", parameters)
114
+ end
115
+
116
+ def remove_nic(nic:)
117
+ parameters = {nicid: nic.id, virtualmachineid: id}
118
+ send_and_update("removeNicFromVirtualMachine", parameters)
119
+ end
88
120
  end
89
121
  end
90
122
  end
data/lib/ocs/response.rb CHANGED
@@ -1,17 +1,27 @@
1
1
  module Ocs
2
2
  class Response
3
+ attr_reader :raw_body
4
+
3
5
  def initialize(faraday_response)
4
- @raw_body = faraday_response.body
6
+ @raw_body = faraday_response.body.with_indifferent_access
5
7
  @raw_headers = faraday_response.headers
6
8
  @raw_status = faraday_response.status
7
9
  end
8
10
 
9
- def body
10
- @body ||= @raw_body.with_indifferent_access
11
+ def content
12
+ @content ||= raw_body[response_key]
13
+ end
14
+
15
+ def response_key
16
+ @response_key ||= raw_body.keys.first
11
17
  end
12
18
 
13
19
  def status
14
20
  @raw_status
15
21
  end
22
+
23
+ def success?
24
+ !content.has_key?("cserrorcode")
25
+ end
16
26
  end
17
27
  end
data/lib/ocs/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ocs
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ocs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - nownabe
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-04-23 00:00:00.000000000 Z
11
+ date: 2015-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -125,6 +125,7 @@ files:
125
125
  - bin/console
126
126
  - bin/setup
127
127
  - lib/ocs.rb
128
+ - lib/ocs/api_error.rb
128
129
  - lib/ocs/client.rb
129
130
  - lib/ocs/request.rb
130
131
  - lib/ocs/resources.rb
@@ -138,8 +139,10 @@ files:
138
139
  - lib/ocs/resources/group.rb
139
140
  - lib/ocs/resources/host.rb
140
141
  - lib/ocs/resources/iso.rb
142
+ - lib/ocs/resources/network.rb
141
143
  - lib/ocs/resources/nic.rb
142
144
  - lib/ocs/resources/os_type.rb
145
+ - lib/ocs/resources/resource_detail.rb
143
146
  - lib/ocs/resources/security_group.rb
144
147
  - lib/ocs/resources/service_offering.rb
145
148
  - lib/ocs/resources/ssh_key_pair.rb