megam_api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.project +17 -0
  4. data/.travis.yml +11 -0
  5. data/Gemfile +5 -0
  6. data/README.md +83 -0
  7. data/Rakefile +10 -0
  8. data/lib/certs/cacert.pem +3554 -0
  9. data/lib/megam/api.rb +244 -0
  10. data/lib/megam/api/accounts.rb +29 -0
  11. data/lib/megam/api/appdefns.rb +26 -0
  12. data/lib/megam/api/appreqs.rb +27 -0
  13. data/lib/megam/api/boltdefns.rb +27 -0
  14. data/lib/megam/api/boltreqs.rb +27 -0
  15. data/lib/megam/api/cloud_tools.rb +35 -0
  16. data/lib/megam/api/errors.rb +27 -0
  17. data/lib/megam/api/login.rb +14 -0
  18. data/lib/megam/api/logs.rb +18 -0
  19. data/lib/megam/api/nodes.rb +50 -0
  20. data/lib/megam/api/predef_clouds.rb +35 -0
  21. data/lib/megam/api/predefs.rb +35 -0
  22. data/lib/megam/api/requests.rb +37 -0
  23. data/lib/megam/api/version.rb +5 -0
  24. data/lib/megam/core/account.rb +170 -0
  25. data/lib/megam/core/appdefns.rb +192 -0
  26. data/lib/megam/core/appdefns_collection.rb +148 -0
  27. data/lib/megam/core/appreqs.rb +224 -0
  28. data/lib/megam/core/appreqs_collection.rb +148 -0
  29. data/lib/megam/core/auth.rb +91 -0
  30. data/lib/megam/core/boltdefns.rb +198 -0
  31. data/lib/megam/core/boltdefns_collection.rb +148 -0
  32. data/lib/megam/core/boltreqs.rb +224 -0
  33. data/lib/megam/core/boltreqs_collection.rb +148 -0
  34. data/lib/megam/core/cloudinstruction.rb +110 -0
  35. data/lib/megam/core/cloudinstruction_collection.rb +145 -0
  36. data/lib/megam/core/cloudinstruction_group.rb +99 -0
  37. data/lib/megam/core/cloudtemplate.rb +127 -0
  38. data/lib/megam/core/cloudtemplate_collection.rb +145 -0
  39. data/lib/megam/core/cloudtool.rb +153 -0
  40. data/lib/megam/core/cloudtool_collection.rb +145 -0
  41. data/lib/megam/core/config.rb +44 -0
  42. data/lib/megam/core/error.rb +99 -0
  43. data/lib/megam/core/json_compat.rb +183 -0
  44. data/lib/megam/core/log.rb +33 -0
  45. data/lib/megam/core/node.rb +347 -0
  46. data/lib/megam/core/node_collection.rb +166 -0
  47. data/lib/megam/core/predef.rb +208 -0
  48. data/lib/megam/core/predef_collection.rb +164 -0
  49. data/lib/megam/core/predefcloud.rb +229 -0
  50. data/lib/megam/core/predefcloud_collection.rb +168 -0
  51. data/lib/megam/core/request.rb +187 -0
  52. data/lib/megam/core/request_collection.rb +145 -0
  53. data/lib/megam/core/stuff.rb +69 -0
  54. data/lib/megam/core/text.rb +88 -0
  55. data/lib/megam_api.rb +1 -0
  56. data/megam_api.gemspec +26 -0
  57. data/test/test_accounts.rb +46 -0
  58. data/test/test_appdefns.rb +23 -0
  59. data/test/test_appreqs.rb +28 -0
  60. data/test/test_boltdefns.rb +23 -0
  61. data/test/test_boltreqs.rb +28 -0
  62. data/test/test_cloudtools.rb +22 -0
  63. data/test/test_helper.rb +67 -0
  64. data/test/test_login.rb +12 -0
  65. data/test/test_logs.rb +15 -0
  66. data/test/test_nodes.rb +141 -0
  67. data/test/test_predefclouds.rb +67 -0
  68. data/test/test_predefs.rb +72 -0
  69. data/test/test_requests.rb +85 -0
  70. metadata +213 -0
@@ -0,0 +1,14 @@
1
+ module Megam
2
+ class API
3
+ def post_auth
4
+ @options = {:path => '/auth', :body => ""}.merge(@options)
5
+
6
+ request(
7
+ :expects => 200,
8
+ :method => :post,
9
+ :body => @options[:body]
10
+
11
+ )
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ module Megam
2
+ class API
3
+ # PUT /logs/:node_id
4
+ #empty body posted to get the logs
5
+ #path having the node_id which is the key_name to fetch that particular log
6
+ def get_logs(node_id)
7
+ @options = {:path => "/logs/#{node_id}",
8
+ :body => OkJson.encode({:nothing =>"nothing in body"})}.merge(@options)
9
+
10
+ request(
11
+ :expects => 200,
12
+ :method => :post,
13
+ :body => @options[:body]
14
+ )
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,50 @@
1
+ module Megam
2
+ class API
3
+
4
+ # GET /nodes
5
+ def get_nodes
6
+ @options = {:path => '/nodes',:body => ""}.merge(@options)
7
+
8
+ request(
9
+ :expects => 200,
10
+ :method => :get,
11
+ :body => @options[:body]
12
+ )
13
+ end
14
+
15
+ def get_node(node_id)
16
+ @options = {:path => "/nodes/#{node_id}",:body => ""}.merge(@options)
17
+
18
+ request(
19
+ :expects => 200,
20
+ :method => :get,
21
+ :body => @options[:body]
22
+ )
23
+ end
24
+
25
+ def post_node(new_node)
26
+ @options = {:path => '/nodes/content',
27
+ :body => Megam::JSONCompat.to_json(new_node)}.merge(@options)
28
+
29
+ request(
30
+ :expects => 201,
31
+ :method => :post,
32
+ :body => @options[:body]
33
+ )
34
+ end
35
+
36
+ #Yet to be tested
37
+ # DELETE /nodes/:node_id
38
+ def delete_node(node_id)
39
+ @options = {:path => '/nodes/#{node_id}',
40
+ :body => ""}.merge(@options)
41
+
42
+ request(
43
+ :expects => 200,
44
+ :method => :delete,
45
+ :body => @options[:body]
46
+ )
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,35 @@
1
+ module Megam
2
+ class API
3
+ def get_predefclouds
4
+ @options = {:path => '/predefclouds',:body => ""}.merge(@options)
5
+
6
+ request(
7
+ :expects => 200,
8
+ :method => :get,
9
+ :body => @options[:body]
10
+ )
11
+ end
12
+
13
+ def get_predefcloud(predefcloud_name)
14
+ @options = {:path => "/predefclouds/#{predefcloud_name}",:body => ""}.merge(@options)
15
+
16
+ request(
17
+ :expects => 200,
18
+ :method => :get,
19
+ :body => @options[:body]
20
+ )
21
+ end
22
+
23
+ def post_predefcloud(new_predefcloud)
24
+ @options = {:path => '/predefclouds/content',
25
+ :body => Megam::JSONCompat.to_json(new_predefcloud)}.merge(@options)
26
+
27
+ request(
28
+ :expects => 201,
29
+ :method => :post,
30
+ :body => @options[:body]
31
+ )
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,35 @@
1
+ module Megam
2
+ class API
3
+ def get_predefs
4
+ @options = {:path => '/predefs',:body => ""}.merge(@options)
5
+
6
+ request(
7
+ :expects => 200,
8
+ :method => :get,
9
+ :body => @options[:body]
10
+ )
11
+ end
12
+
13
+ def get_predef(predef_name)
14
+ @options = {:path => "/predefs/#{predef_name}",:body => ""}.merge(@options)
15
+
16
+ request(
17
+ :expects => 200,
18
+ :method => :get,
19
+ :body => @options[:body]
20
+ )
21
+ end
22
+
23
+ def post_predef(new_predef)
24
+ @options = {:path => '/predefs/content',
25
+ :body => Megam::JSONCompat.to_json(new_predef)}.merge(@options)
26
+
27
+ request(
28
+ :expects => 201,
29
+ :method => :post,
30
+ :body => @options[:body]
31
+ )
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,37 @@
1
+ module Megam
2
+ class API
3
+
4
+ # GET /requests
5
+ def get_requests
6
+ @options = {:path => '/requests',:body => ""}.merge(@options)
7
+
8
+ request(
9
+ :expects => 200,
10
+ :method => :get,
11
+ :body => @options[:body]
12
+ )
13
+ end
14
+
15
+ def get_request(node_name)
16
+ @options = {:path => "/requests/#{node_name}",:body => ""}.merge(@options)
17
+
18
+ request(
19
+ :expects => 200,
20
+ :method => :get,
21
+ :body => @options[:body]
22
+ )
23
+ end
24
+
25
+ def post_request(new_req)
26
+ @options = {:path => '/requests/content',
27
+ :body => Megam::JSONCompat.to_json(new_req)}.merge(@options)
28
+
29
+ request(
30
+ :expects => 201,
31
+ :method => :post,
32
+ :body => @options[:body]
33
+ )
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ module Megam
2
+ class API
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,170 @@
1
+ # Copyright:: Copyright (c) 2012-2013 Megam Systems, Inc.
2
+ # License:: Apache License, Version 2.0
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 Account
18
+ def initialize
19
+ @id = nil
20
+ @email = nil
21
+ @api_key = nil
22
+ @authority = nil
23
+ @created_at = nil
24
+ @some_msg = {}
25
+ end
26
+
27
+ #used by resque workers and any other background job
28
+ def account
29
+ self
30
+ end
31
+
32
+ def megam_rest
33
+ options = { :email => Megam::Config[:email], :api_key => Megam::Config[:api_key]}
34
+ Megam::API.new(options)
35
+ end
36
+
37
+ def id(arg=nil)
38
+ if arg != nil
39
+ @id = arg
40
+ else
41
+ @id
42
+ end
43
+ end
44
+
45
+ def email(arg=nil)
46
+ if arg != nil
47
+ @email = arg
48
+ else
49
+ @email
50
+ end
51
+ end
52
+
53
+ def api_key(arg=nil)
54
+ if arg != nil
55
+ @api_key = arg
56
+ else
57
+ @api_key
58
+ end
59
+ end
60
+
61
+ def authority(arg=nil)
62
+ if arg != nil
63
+ @authority = arg
64
+ else
65
+ @authority
66
+ end
67
+ end
68
+
69
+ def created_at(arg=nil)
70
+ if arg != nil
71
+ @created_at = arg
72
+ else
73
+ @created_at
74
+ end
75
+ end
76
+
77
+ def some_msg(arg=nil)
78
+ if arg != nil
79
+ @some_msg = arg
80
+ else
81
+ @some_msg
82
+ end
83
+ end
84
+
85
+ def error?
86
+ crocked = true if (some_msg.has_key?(:msg_type) && some_msg[:msg_type] == "error")
87
+ end
88
+
89
+ # Transform the ruby obj -> to a Hash
90
+ def to_hash
91
+ index_hash = Hash.new
92
+ index_hash["json_claz"] = self.class.name
93
+ index_hash["id"] = id
94
+ index_hash["email"] = email
95
+ index_hash["api_key"] = api_key
96
+ index_hash["authority"] = authority
97
+ index_hash["created_at"] = created_at
98
+ index_hash["some_msg"] = some_msg
99
+ index_hash
100
+ end
101
+
102
+ # Serialize this object as a hash: called from JsonCompat.
103
+ # Verify if this called from JsonCompat during testing.
104
+ def to_json(*a)
105
+ for_json.to_json(*a)
106
+ end
107
+
108
+ def for_json
109
+ result = {
110
+ "id" => id,
111
+ "email" => email,
112
+ "api_key" => api_key,
113
+ "authority" => authority,
114
+ "created_at" => created_at
115
+ }
116
+ result
117
+ end
118
+
119
+ # Create a Megam::Account from JSON (used by the backgroud job workers)
120
+ def self.json_create(o)
121
+ acct = new
122
+ acct.id(o["id"]) if o.has_key?("id")
123
+ acct.email(o["email"]) if o.has_key?("email")
124
+ acct.api_key(o["api_key"]) if o.has_key?("api_key")
125
+ acct.authority(o["authority"]) if o.has_key?("authority")
126
+ acct.created_at(o["created_at"]) if o.has_key?("created_at")
127
+ acct.some_msg[:code] = o["code"] if o.has_key?("code")
128
+ acct.some_msg[:msg_type] = o["msg_type"] if o.has_key?("msg_type")
129
+ acct.some_msg[:msg]= o["msg"] if o.has_key?("msg")
130
+ acct.some_msg[:links] = o["links"] if o.has_key?("links")
131
+ acct
132
+ end
133
+
134
+ def self.from_hash(o)
135
+ acct = self.new()
136
+ acct.from_hash(o)
137
+ acct
138
+ end
139
+
140
+ def from_hash(o)
141
+ @id = o[:id] if o.has_key?(:id)
142
+ @email = o[:email] if o.has_key?(:email)
143
+ @api_key = o[:api_key] if o.has_key?(:api_key)
144
+ @authority = o[:authority] if o.has_key?(:authority)
145
+ @created_at = o[:created_at] if o.has_key?(:created_at)
146
+ self
147
+ end
148
+
149
+ def self.create(o)
150
+ acct = from_hash(o)
151
+ acct.create
152
+ end
153
+
154
+ # Load a account by email_p
155
+ def self.show(email_p)
156
+ megam_rest.get_accounts(email)
157
+ self
158
+ end
159
+
160
+ # Create the node via the REST API
161
+ def create
162
+ megam_rest.post_accounts(to_hash)
163
+ end
164
+
165
+ def to_s
166
+ Megam::Stuff.styled_hash(to_hash)
167
+ #"---> Megam::Account:[error=#{error?}]\n"+
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,192 @@
1
+ # Copyright:: Copyright (c) 2012, 2013 Megam Systems
2
+ # License:: Apache License, Version 2.0
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 Appdefns
18
+ # Each notify entry is a resource/action pair, modeled as an
19
+ # Struct with a #resource and #action member
20
+ =begin
21
+ def self.hash_tree
22
+ Hash.new do |hash, key|
23
+ hash[key] = hash_tree
24
+ end
25
+ end
26
+ =end
27
+
28
+ def initialize
29
+ @id = nil
30
+ @node_id = nil
31
+ @node_name = nil
32
+ @appdefns ={}
33
+ @created_at = nil
34
+ end
35
+ def node
36
+ self
37
+ end
38
+
39
+ def megam_rest
40
+ options = { :email => Megam::Config[:email], :api_key => Megam::Config[:api_key]}
41
+ Megam::API.new(options)
42
+ end
43
+
44
+ def id(arg=nil)
45
+ if arg != nil
46
+ @id = arg
47
+ else
48
+ @id
49
+ end
50
+ end
51
+
52
+ def node_id(arg=nil)
53
+ if arg != nil
54
+ @node_id = arg
55
+ else
56
+ @node_id
57
+ end
58
+ end
59
+
60
+ def node_name(arg=nil)
61
+ if arg != nil
62
+ @node_name = arg
63
+ else
64
+ @node_name
65
+ end
66
+ end
67
+
68
+ def appdefns(arg=nil)
69
+ if arg != nil
70
+ @appdefns = arg
71
+ else
72
+ @appdefns
73
+ end
74
+ end
75
+
76
+
77
+ def created_at(arg=nil)
78
+ if arg != nil
79
+ @created_at = arg
80
+ else
81
+ @created_at
82
+ end
83
+ end
84
+
85
+ def error?
86
+ crocked = true if (some_msg.has_key?(:msg_type) && some_msg[:msg_type] == "error")
87
+ end
88
+
89
+ # Transform the ruby obj -> to a Hash
90
+ def to_hash
91
+ puts "===================> CLASS NAME ====================================="
92
+ puts self.class.name
93
+ index_hash = Hash.new
94
+ index_hash["json_claz"] = self.class.name
95
+ index_hash["id"] = id
96
+ index_hash["node_id"] = node_id
97
+ index_hash["node_name"] = node_name
98
+ index_hash["appdefns"] = appdefns
99
+ index_hash["created_at"] = created_at
100
+ index_hash
101
+ end
102
+
103
+ # Serialize this object as a hash: called from JsonCompat.
104
+ # Verify if this called from JsonCompat during testing.
105
+ def to_json(*a)
106
+ for_json.to_json(*a)
107
+ end
108
+
109
+ def for_json
110
+ result = {
111
+ "id" => id,
112
+ "node_id" => node_id,
113
+ "node_name" => node_name,
114
+ "appdefns" => appdefns,
115
+ "created_at" => created_at
116
+ }
117
+ result
118
+ end
119
+
120
+ # Create a Megam::Node from NodeResult-JSON
121
+ #
122
+ #[{
123
+ #"id":"NOD362212018897289216",
124
+ #"accounts_id":"ACT362211962353876992",
125
+ #"json_claz":"Megam::Node",
126
+ #"request":{
127
+ #"req_id":"NOD362212018897289216",
128
+ #"command":"commands"
129
+ #},
130
+ #"predefs":{
131
+ #"name":"",
132
+ #"scm":"",
133
+ #"war":"",
134
+ #"db":"",
135
+ #"queue":""
136
+ #}
137
+ #}]
138
+ #
139
+ def self.json_create(o)
140
+ node = new
141
+ node.id(o["id"]) if o.has_key?("id")
142
+ node.node_id(o["node_id"]) if o.has_key?("node_id")
143
+ node.node_name(o["node_name"]) if o.has_key?("node_name")
144
+ node.created_at(o["created_at"]) if o.has_key?("created_at")
145
+
146
+ #APP DEFINITIONS
147
+ op = o["appdefns"]
148
+ node.appdefns[:timetokill] = op["timetokill"] if op && op.has_key?("timetokill")
149
+ node.appdefns[:metered] = op["metered"] if op && op.has_key?("metered")
150
+ node.appdefns[:logging]= op["logging"] if op && op.has_key?("logging")
151
+ node.appdefns[:runtime_exec] = op["runtime_exec"] if op && op.has_key?("runtime_exec")
152
+ node
153
+ end
154
+
155
+ def self.from_hash(o)
156
+ node = self.new()
157
+ node.from_hash(o)
158
+ node
159
+ end
160
+
161
+ def from_hash(o)
162
+ @id = o["id"] if o.has_key?("id")
163
+ @node_id = o["node_id"] if o.has_key?("node_id")
164
+ @node_name = o["node_name"] if o.has_key?("node_name")
165
+ @appdefns = o["appdefns"] if o.has_key?("appdefns")
166
+ @created_at = o["created_at"] if o.has_key?("created_at")
167
+ self
168
+ end
169
+
170
+ def self.create(o)
171
+ acct = from_hash(o)
172
+ acct.create
173
+ end
174
+
175
+ # Create the node via the REST API
176
+ def create
177
+ megam_rest.post_appdefn(to_hash)
178
+ end
179
+
180
+ # Load a account by email_p
181
+ def self.show(node_name)
182
+ megam_rest.get_appdefn(node_name)
183
+ self
184
+ end
185
+
186
+ def to_s
187
+ Megam::Stuff.styled_hash(to_hash)
188
+ #"---> Megam::Account:[error=#{error?}]\n"+
189
+ end
190
+
191
+ end
192
+ end