megam_api 0.73 → 0.75

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,231 @@
1
+ ##
2
+ ## Copyright [2013-2015] [Megam Systems]
3
+ ##
4
+ ## Licensed under the Apache License, Version 2.0 (the "License");
5
+ ## you may not use this file except in compliance with the License.
6
+ ## You may obtain a copy of the License at
7
+ ##
8
+ ## http://www.apache.org/licenses/LICENSE-2.0
9
+ ##
10
+ ## Unless required by applicable law or agreed to in writing, software
11
+ ## distributed under the License is distributed on an "AS IS" BASIS,
12
+ ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ ## See the License for the specific language governing permissions and
14
+ ## limitations under the License.
15
+ ##
16
+ module Megam
17
+ class Invoices < Megam::ServerAPI
18
+ def initialize(email=nil, api_key=nil, host=nil)
19
+ @id = nil
20
+ @accounts_id = nil
21
+ @from_date = nil
22
+ @to_date = nil
23
+ @month = nil
24
+ @bill_type = nil
25
+ @billing_amount = nil
26
+ @currency_type = nil
27
+ @created_at = nil
28
+ @some_msg = {}
29
+ super(email, api_key, host)
30
+ end
31
+
32
+ def invoices
33
+ self
34
+ end
35
+
36
+ def id(arg=nil)
37
+ if arg != nil
38
+ @id = arg
39
+ else
40
+ @id
41
+ end
42
+ end
43
+
44
+ def accounts_id(arg=nil)
45
+ if arg != nil
46
+ @accounts_id= arg
47
+ else
48
+ @accounts_id
49
+ end
50
+ end
51
+
52
+ def from_date(arg=nil)
53
+ if arg != nil
54
+ @from_date = arg
55
+ else
56
+ @from_date
57
+ end
58
+ end
59
+
60
+ def to_date(arg=nil)
61
+ if arg != nil
62
+ @to_date = arg
63
+ else
64
+ @to_date
65
+ end
66
+ end
67
+
68
+ def month(arg=nil)
69
+ if arg != nil
70
+ @month =arg
71
+ else
72
+ @month
73
+ end
74
+ end
75
+
76
+ def bill_type(arg=nil)
77
+ if arg != nil
78
+ @bill_type = arg
79
+ else
80
+ @bill_type
81
+ end
82
+ end
83
+
84
+ def billing_amount(arg=nil)
85
+ if arg != nil
86
+ @billing_amount= arg
87
+ else
88
+ @billing_amount
89
+ end
90
+ end
91
+
92
+ def currency_type(arg=nil)
93
+ if arg != nil
94
+ @currency_type = arg
95
+ else
96
+ @currency_type
97
+ end
98
+ end
99
+
100
+ def created_at(arg=nil)
101
+ if arg != nil
102
+ @created_at = arg
103
+ else
104
+ @created_at
105
+ end
106
+ end
107
+
108
+ def some_msg(arg=nil)
109
+ if arg != nil
110
+ @some_msg = arg
111
+ else
112
+ @some_msg
113
+ end
114
+ end
115
+
116
+ def error?
117
+ crocked = true if (some_msg.has_key?(:msg_type) && some_msg[:msg_type] == "error")
118
+ end
119
+
120
+ # Transform the ruby obj -> to a Hash
121
+ def to_hash
122
+ index_hash = Hash.new
123
+ index_hash["json_claz"] = self.class.name
124
+ index_hash["id"] = id
125
+ index_hash["accounts_id"] = accounts_id
126
+ index_hash["from_date"] = from_date
127
+ index_hash["to_date"] = to_date
128
+ index_hash["month"] = month
129
+ index_hash["bill_type"] = bill_type
130
+ index_hash["billing_amount"] = billing_amount
131
+ index_hash["currency_type"] = currency_type
132
+ index_hash["created_at"] = created_at
133
+ index_hash
134
+ end
135
+
136
+ # Serialize this object as a hash: called from JsonCompat.
137
+ # Verify if this called from JsonCompat during testing.
138
+ def to_json(*a)
139
+ for_json.to_json(*a)
140
+ end
141
+
142
+ def for_json
143
+ result = {
144
+ "id" => id,
145
+ "accounts_id" => accounts_id,
146
+ "from_date" => from_date,
147
+ "to_date" => to_date,
148
+ "month" => month,
149
+ "bill_type" => bill_type,
150
+ "billing_amount" => billing_amount,
151
+ "currency_type" => currency_type,
152
+ "created_at" => created_at
153
+ }
154
+ result
155
+ end
156
+
157
+ #
158
+ def self.json_create(o)
159
+ bal = new
160
+ bal.id(o["id"]) if o.has_key?("id")
161
+ bal.accounts_id(o["accounts_id"]) if o.has_key?("accounts_id")
162
+ bal.from_date(o["from_date"]) if o.has_key?("from_date")
163
+ bal.to_date(o["to_date"]) if o.has_key?("to_date")
164
+ bal.month(o["month"]) if o.has_key?("month")
165
+ bal.bill_type(o["bill_type"]) if o.has_key?("bill_type")
166
+ bal.billing_amount(o["billing_amount"]) if o.has_key?("billing_amount")
167
+ bal.currency_type(o["currency_type"]) if o.has_key?("currency_type")
168
+ bal.created_at(o["created_at"]) if o.has_key?("created_at")
169
+ #success or error
170
+ bal.some_msg[:code] = o["code"] if o.has_key?("code")
171
+ bal.some_msg[:msg_type] = o["msg_type"] if o.has_key?("msg_type")
172
+ bal.some_msg[:msg]= o["msg"] if o.has_key?("msg")
173
+ bal.some_msg[:links] = o["links"] if o.has_key?("links")
174
+ bal
175
+ end
176
+
177
+ def self.from_hash(o,tmp_email=nil, tmp_api_key=nil, tmp_host=nil)
178
+ bal = self.new(tmp_email, tmp_api_key, tmp_host)
179
+ bal.from_hash(o)
180
+ bal
181
+ end
182
+
183
+ def from_hash(o)
184
+ @id = o[:id] if o.has_key?(:id)
185
+ @accounts_id = o[:accounts_id] if o.has_key?(:accounts_id)
186
+ @from_date = o[:from_date] if o.has_key?(:from_date)
187
+ @to_date = o[:to_date] if o.has_key?(:to_date)
188
+ @month = o[:month] if o.has_key?(:month)
189
+ @bill_type = o[:bill_type] if o.has_key?(:bill_type)
190
+ @billing_amount = o[:billing_amount] if o.has_key?(:billing_amount)
191
+ @currency_type = o[:currency_type] if o.has_key?(:currency_type)
192
+ @created_at = o[:created_at] if o.has_key?(:created_at)
193
+ self
194
+ end
195
+
196
+ def self.create(o,tmp_email=nil, tmp_api_key=nil, tmp_host=nil)
197
+ acct = from_hash(o,tmp_email, tmp_api_key, tmp_host)
198
+ acct.create
199
+ end
200
+
201
+ # Create the invoices via the REST API
202
+ def create
203
+ megam_rest.post_invoices(to_hash)
204
+ end
205
+
206
+ # Load all billing histories -
207
+ # returns a BillingHistoriesCollection
208
+ # don't return self. check if the Megam::BillingHistoriesCollection is returned.
209
+ def self.list(params)
210
+ invoices = self.new(params["email"], params["api_key"], params["host"])
211
+ invoices.megam_rest.get_invoices
212
+ end
213
+
214
+ # Show a particular billing history by name,
215
+ # Megam::BillingHistory
216
+ def self.show(p_name,tmp_email=nil, tmp_api_key=nil, tmp_host=nil)
217
+ pre = self.new(tmp_email,tmp_api_key, tmp_host)
218
+ pre.megam_rest.get_invoice(p_name)
219
+ end
220
+
221
+ def self.delete(p_name,tmp_email=nil, tmp_api_key=nil, tmp_host=nil)
222
+ pre = self.new(tmp_email,tmp_api_key, tmp_host)
223
+ pre.megam_rest.delete_invoice(p_name)
224
+ end
225
+
226
+ def to_s
227
+ Megam::Stuff.styled_hash(to_hash)
228
+ end
229
+
230
+ end
231
+ end
@@ -0,0 +1,144 @@
1
+ ##
2
+ ## Copyright [2013-2015] [Megam Systems]
3
+ ##
4
+ ## Licensed under the Apache License, Version 2.0 (the "License");
5
+ ## you may not use this file except in compliance with the License.
6
+ ## You may obtain a copy of the License at
7
+ ##
8
+ ## http://www.apache.org/licenses/LICENSE-2.0
9
+ ##
10
+ ## Unless required by applicable law or agreed to in writing, software
11
+ ## distributed under the License is distributed on an "AS IS" BASIS,
12
+ ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ ## See the License for the specific language governing permissions and
14
+ ## limitations under the License.
15
+ ##
16
+ module Megam
17
+ class InvoicesCollection
18
+ include Enumerable
19
+
20
+ attr_reader :iterator
21
+ def initialize
22
+ @invoices = Array.new
23
+ @invoices_by_name = Hash.new
24
+ @insert_after_idx = nil
25
+ end
26
+
27
+ def all_invoices
28
+ @invoices
29
+ end
30
+
31
+ def [](index)
32
+ @invoices[index]
33
+ end
34
+
35
+ def []=(index, arg)
36
+ is_megam_invoices(arg)
37
+ @invoices[index] = arg
38
+ @invoices_by_name[arg.accounts_id] = index
39
+ end
40
+
41
+ def <<(*args)
42
+ args.flatten.each do |a|
43
+ is_megam_invoices(a)
44
+ @invoices << a
45
+ @invoices_by_name[a.accounts_id] =@invoices.length - 1
46
+ end
47
+ self
48
+ end
49
+
50
+ # 'push' is an alias method to <<
51
+ alias_method :push, :<<
52
+
53
+ def insert(invoices)
54
+ is_megam_invoices(invoices)
55
+ if @insert_after_idx
56
+ # in the middle of executing a run, so any predefs inserted now should
57
+ # be placed after the most recent addition done by the currently executing
58
+ # invoices
59
+ @invoices.insert(@insert_after_idx + 1, invoices)
60
+ # update name -> location mappings and register new invoices
61
+ @invoices_by_name.each_key do |key|
62
+ @invoices_by_name[key] += 1 if@invoices_by_name[key] > @insert_after_idx
63
+ end
64
+ @invoices_by_name[invoices.accounts_id] = @insert_after_idx + 1
65
+ @insert_after_idx += 1
66
+ else
67
+ @invoices << invoices
68
+ @invoices_by_name[invoices.accounts_id] =@invoices.length - 1
69
+ end
70
+ end
71
+
72
+ def each
73
+ @invoices.each do |invoices|
74
+ yield invoices
75
+ end
76
+ end
77
+
78
+ def each_index
79
+ @invoices.each_index do |i|
80
+ yield i
81
+ end
82
+ end
83
+
84
+ def empty?
85
+ @invoices.empty?
86
+ end
87
+
88
+ def lookup(invoices)
89
+ lookup_by = nil
90
+ if invoices.kind_of?(Megam::invoices)
91
+ lookup_by = invoices.accounts_id
92
+ elsif invoices.kind_of?(String)
93
+ lookup_by = invoices
94
+ else
95
+ raise ArgumentError, "Must pass a Megam::invoices or String to lookup"
96
+ end
97
+ res =@invoices_by_name[lookup_by]
98
+ unless res
99
+ raise ArgumentError, "Cannot find a invoices matching #{lookup_by} (did you define it first?)"
100
+ end
101
+ @invoices[res]
102
+ end
103
+
104
+ # Transform the ruby obj -> to a Hash
105
+ def to_hash
106
+ index_hash = Hash.new
107
+ self.each do |invoices|
108
+ index_hash[invoices.accounts_id] = invoices.to_s
109
+ end
110
+ index_hash
111
+ end
112
+
113
+ # Serialize this object as a hash: called from JsonCompat.
114
+ # Verify if this called from JsonCompat during testing.
115
+ def to_json(*a)
116
+ for_json.to_json(*a)
117
+ end
118
+
119
+ def self.json_create(o)
120
+ collection = self.new()
121
+ o["results"].each do |invoices_list|
122
+ invoices_array = invoices_list.kind_of?(Array) ? invoices_list : [ invoices_list ]
123
+ invoices_array.each do |invoices|
124
+ collection.insert(invoices)
125
+ end
126
+ end
127
+ collection
128
+ end
129
+
130
+ private
131
+
132
+ def is_megam_invoices(arg)
133
+ unless arg.kind_of?(Megam::Invoices)
134
+ raise ArgumentError, "Members must be Megam::invoices's"
135
+ end
136
+ true
137
+ end
138
+
139
+ def to_s
140
+ Megam::Stuff.styled_hash(to_hash)
141
+ end
142
+
143
+ end
144
+ end
@@ -32,9 +32,10 @@ module Megam
32
32
  MEGAM_AVAILABLEUNITSCOLLECTION = "Megam::AvailableunitsCollection".freeze
33
33
  MEGAM_BALANCES = "Megam::Balances".freeze
34
34
  MEGAM_BALANCESCOLLECTION = "Megam::BalancesCollection".freeze
35
- MEGAM_BILLEDHISTORIES = "Megam::Billedhistories".freeze
36
- MEGAM_BILLEDHISTORIESCOLLECTION = "Megam::BilledhistoriesCollection".freeze
37
- MEGAM_BILLINGS = "Megam::Billings".freeze
35
+ MEGAM_BILLEDHISTORIES = "Megam::Billedhistories".freeze
36
+ MEGAM_BILLEDHISTORIESCOLLECTION = "Megam::BilledhistoriesCollection".freeze
37
+ MEGAM_INVOICESCOLLECTION = "Megam::InvoicesCollection".freeze
38
+ MEGAM_INVOICES = "Megam::Invoices".freeze
38
39
  MEGAM_BILLINGSCOLLECTION = "Megam::BillingsCollection".freeze
39
40
  MEGAM_CLOUDTOOLSETTING = "Megam::CloudToolSetting".freeze
40
41
  MEGAM_CLOUDTOOLSETTINGCOLLECTION = "Megam::CloudToolSettingCollection".freeze
@@ -198,6 +199,10 @@ module Megam
198
199
  Megam::Billedhistories
199
200
  when MEGAM_BILLEDHISTORIESCOLLECTION
200
201
  Megam::BilledhistoriesCollection
202
+ when MEGAM_INVOICES
203
+ Megam::Invoices
204
+ when MEGAM_INVOICESCOLLECTION
205
+ Megam::InvoicesCollection
201
206
  when MEGAM_BILLINGS
202
207
  Megam::Billings
203
208
  when MEGAM_BILLINGSCOLLECTION
data/megam_api.gemspec CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
11
11
  s.license = "Apache V2"
12
12
  s.extra_rdoc_files = ["README.md", "LICENSE" ]
13
13
  s.summary = %q{Ruby Client for the Megam}
14
- s.description = %q{Ruby Client for the Megam CMP. Performs REST calls to Megam Gateway - http://github.com/megamsys/megam_gateway.git}
14
+ s.description = %q{Ruby Client for the Megam cloud platform. Performs REST calls to Megam Gateway - http://github.com/megamsys/megam_gateway.git}
15
15
  s.files = `git ls-files`.split("\n")
16
16
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
17
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
@@ -8,20 +8,84 @@ class TestApps < MiniTest::Unit::TestCase
8
8
  assert_equal(200, response.status)
9
9
  end
10
10
  =end
11
+ =begin
11
12
  def test_get_assembly
12
13
  response = megams.get_one_assemblies("AMS1257720849197301760")
13
14
  assert_equal(200, response.status)
14
15
  end
15
- =begin
16
+ =end
17
+
18
+ #=begin
16
19
  def test_post_assembly
17
20
  tmp_hash = {
18
21
  "name" => "testAsm",
19
22
  "org_id" => "ORG123",
20
- "assemblies" => [],
23
+ "assemblies" => [{
24
+ "name" => "PaulineHarper",
25
+ "components" => [{
26
+ "name" => "GussieMathis",
27
+ "tosca_type" => "tosca.web.riak",
28
+ "inputs" => [],
29
+ "status" => "",
30
+ "related_components" => ["VernonDennis.megam.co"],
31
+ "outputs" => [],
32
+ "artifacts" => [{
33
+ "artifact_type" => "tosca_type",
34
+ "content" => "",
35
+ "artifact_requirements" => []
36
+ }],
37
+ "operations" => [{
38
+ "operation_type" => "CI",
39
+ "description" => "continuous Integration",
40
+ "properties" => []
41
+ }],
42
+ "repo" => {
43
+ "rtype" => "image",
44
+ "source" => "github",
45
+ "oneclick" => "yes",
46
+ "url" => "imagename"
47
+ }
48
+ }],
49
+ "tosca_type" => "tosca.torpedo.coreos",
50
+ "inputs" => [{
51
+ "key" => "Domain",
52
+ "value" => "megmbox.com"
53
+ },
54
+ {
55
+ "key" => "sshKey",
56
+ "value" => "rsa"
57
+ }
58
+
59
+ ],
60
+ "outputs" => [],
61
+ "status" => "",
62
+ "policies" => [
63
+ {
64
+ "name" => "placement policy",
65
+ "ptype" => "colocated",
66
+ "members" => ["apache2-megam"]
67
+ },
68
+ {
69
+ "name" => "HA policy",
70
+ "ptype" => "colocated",
71
+ "members" => ["apache2-megam"]
72
+ },
73
+ {
74
+ "name" => "CPU scaling for apache2 server",
75
+ "ptype" => "scaling",
76
+ "members" => ["apache2-megam"],
77
+ "rules" => {
78
+ "name" => "cpu load",
79
+ "type" => "cpu",
80
+ "cpu_threshhold" => "80"
81
+ }
82
+ }
83
+ ]
84
+ } ],
21
85
  "inputs" => []
22
86
  }
23
87
  response = megams.post_assemblies(tmp_hash)
24
88
  assert_equal(200, response.status)
25
89
  end
26
- =end
90
+ #=end
27
91
  end