megam_api 0.1.0

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.
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,145 @@
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 RequestCollection
18
+ include Enumerable
19
+
20
+ attr_reader :iterator
21
+ def initialize
22
+ @requests = Array.new
23
+ @requests_by_name = Hash.new
24
+ @insert_after_idx = nil
25
+ end
26
+
27
+ def all_requests
28
+ @requests
29
+ end
30
+
31
+ def [](index)
32
+ @requests[index]
33
+ end
34
+
35
+ def []=(index, arg)
36
+ is_megam_request(arg)
37
+ @requests[index] = arg
38
+ @requests_by_name[arg.node_name] = index
39
+ end
40
+
41
+ def <<(*args)
42
+ args.flatten.each do |a|
43
+ is_megam_request(a)
44
+ @requests << a
45
+ @requests_by_name[a.node_name] = @requests.length - 1
46
+ end
47
+ self
48
+ end
49
+
50
+ # 'push' is an alias method to <<
51
+ alias_method :push, :<<
52
+
53
+ def insert(request)
54
+ is_megam_request(request)
55
+ if @insert_after_idx
56
+ # in the middle of executing a run, so any requests inserted now should
57
+ # be placed after the most recent addition done by the currently executing
58
+ # request
59
+ @requests.insert(@insert_after_idx + 1, request)
60
+ # update name -> location mappings and register new request
61
+ @requests_by_name.each_key do |key|
62
+ @requests_by_name[key] += 1 if @requests_by_name[key] > @insert_after_idx
63
+ end
64
+ @requests_by_name[request.node_name] = @insert_after_idx + 1
65
+ @insert_after_idx += 1
66
+ else
67
+ @requests << request
68
+ @requests_by_name[request.node_name] = @requests.length - 1
69
+ end
70
+ end
71
+
72
+ def each
73
+ @requests.each do |request|
74
+ yield request
75
+ end
76
+ end
77
+
78
+ def each_index
79
+ @requests.each_index do |i|
80
+ yield i
81
+ end
82
+ end
83
+
84
+ def empty?
85
+ @requests.empty?
86
+ end
87
+
88
+ def lookup(request)
89
+ lookup_by = nil
90
+ if request.kind_of?(Megam::Request)
91
+ lookup_by = request.node_name
92
+ elsif request.kind_of?(String)
93
+ lookup_by = request
94
+ else
95
+ raise ArgumentError, "Must pass a Megam::Request or String to lookup"
96
+ end
97
+ res = @requests_by_name[lookup_by]
98
+ unless res
99
+ raise ArgumentError, "Cannot find a request matching #{lookup_by} (did you define it first?)"
100
+ end
101
+ @requests[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 |request|
108
+ index_hash[request.node_name] = request.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
+
120
+ def self.json_create(o)
121
+ collection = self.new()
122
+ o["results"].each do |requests_list|
123
+ requests_array = requests_list.kind_of?(Array) ? requests_list : [ requests_list ]
124
+ requests_array.each do |request|
125
+ collection.insert(request)
126
+ end
127
+ end
128
+ collection
129
+ end
130
+
131
+ private
132
+
133
+ def is_megam_request(arg)
134
+ unless arg.kind_of?(Megam::Request)
135
+ raise ArgumentError, "Members must be Megam::Request's"
136
+ end
137
+ true
138
+ end
139
+
140
+ def to_s
141
+ Megam::Stuff.styled_hash(to_hash)
142
+ end
143
+
144
+ end
145
+ end
@@ -0,0 +1,69 @@
1
+ #
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
+ module Stuff
18
+ extend self
19
+ def has_git?
20
+ %x{ git --version }
21
+ $?.success?
22
+ end
23
+
24
+ def git(args)
25
+ return "" unless has_git?
26
+ flattened_args = [args].flatten.compact.join(" ")
27
+ %x{ git #{flattened_args} 2>&1 }.strip
28
+ end
29
+
30
+ def time_ago(since)
31
+ if since.is_a?(String)
32
+ since = Time.parse(since)
33
+ end
34
+
35
+ elapsed = Time.now - since
36
+
37
+ message = since.strftime("%Y/%m/%d %H:%M:%S")
38
+ if elapsed <= 60
39
+ message << " (~ #{elapsed.floor}s ago)"
40
+ elsif elapsed <= (60 * 60)
41
+ message << " (~ #{(elapsed / 60).floor}m ago)"
42
+ elsif elapsed <= (60 * 60 * 25)
43
+ message << " (~ #{(elapsed / 60 / 60).floor}h ago)"
44
+ end
45
+ message
46
+ end
47
+
48
+ def spinner(ticks)
49
+ %w(/ - \\ |)[ticks % 4]
50
+ end
51
+
52
+ def launchy(message, url)
53
+ action(message) do
54
+ require("launchy")
55
+ launchy = Launchy.open(url)
56
+ if launchy.respond_to?(:join)
57
+ launchy.join
58
+ end
59
+ end
60
+ end
61
+
62
+ #
63
+ #left justified keyed hash with newlines.
64
+ def styled_hash(hash)
65
+ hash.map{|k,v| "#{k.ljust(15)}=#{v}"}.join("\n")
66
+ end
67
+
68
+ end
69
+ end
@@ -0,0 +1,88 @@
1
+ #
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 Text
18
+
19
+ attr_reader :stdout
20
+ attr_reader :stderr
21
+ attr_reader :stdin
22
+ attr_reader :config
23
+ def initialize(stdout, stderr, stdin, config)
24
+ @stdout, @stderr, @stdin, @config = stdout, stderr, stdin, config
25
+ end
26
+
27
+ def highline
28
+ @highline ||= begin
29
+ require 'highline'
30
+ HighLine.new
31
+ end
32
+ end
33
+
34
+ def msg(message)
35
+ stdout.puts message
36
+ end
37
+
38
+ # Prints a message to stdout. Aliased as +info+ for compatibility with
39
+ # the logger API.
40
+
41
+ def info(message)
42
+ stdout.puts("#{color('INFO:', :green, :bold)} #{message}")
43
+ end
44
+
45
+ # Prints a msg to stderr. Used for warn, error, and fatal.
46
+ def err(message)
47
+ stderr.puts message
48
+ end
49
+
50
+ # Print a warning message
51
+ def warn(message)
52
+ err("#{color('WARNING:', :yellow, :bold)} #{message}")
53
+ end
54
+
55
+ # Print an error message
56
+ def error(message)
57
+ err("#{color('ERROR:', :red, :bold)} #{message}")
58
+ end
59
+
60
+ # Print a message describing a fatal error.
61
+ def fatal(message)
62
+ err("#{color('FATAL:', :red, :bold)} #{message}")
63
+ end
64
+
65
+ def color(string, *colors)
66
+ if color?
67
+ highline.color(string, *colors)
68
+ else
69
+ string
70
+ end
71
+ end
72
+
73
+ # Should colored output be used ?. When output is not to a
74
+ # terminal, colored output is never used
75
+ def color?
76
+ stdout.tty?
77
+ end
78
+
79
+ def list(*args)
80
+ highline.list(*args)
81
+ end
82
+
83
+ def pretty_print(data)
84
+ stdout.puts data
85
+ end
86
+
87
+ end
88
+ end
@@ -0,0 +1 @@
1
+ require(File.join(File.dirname(__FILE__), "megam", "api"))
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "megam/api/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "megam_api"
7
+ s.version = Megam::API::VERSION
8
+ s.authors = ["Kishorekumar Neelamegam, Thomas Alrin, Subash Sethurajan"]
9
+ s.email = ["nkishore@megam.co.in","alrin@megam.co.in","subash.avc@gmail.com"]
10
+ s.homepage = "http://github.com/indykish/megam_api"
11
+ s.license = "Apache V2"
12
+ sextra_rdoc_files = ["README.md", "LICENSE" ]
13
+ s.summary = %q{Ruby Client for the Megam Cloud}
14
+ s.description = %q{Ruby Client for the Megam Cloud. Performs REST based HTTP calls to api.megam.co http://github.com/indykish/megam_play.git}
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+ s.add_runtime_dependency 'excon'
20
+ s.add_runtime_dependency 'highline'
21
+ s.add_runtime_dependency 'yajl-ruby'
22
+ s.add_runtime_dependency 'mixlib-config'
23
+ s.add_runtime_dependency 'mixlib-log'
24
+ s.add_development_dependency 'minitest'
25
+ s.add_development_dependency 'rake'
26
+ end
@@ -0,0 +1,46 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/test_helper")
2
+
3
+ class TestAccounts < MiniTest::Unit::TestCase
4
+
5
+ $admin = "admin-tom"
6
+ $normal = "normal-tom"
7
+
8
+ def test_get_accounts_good
9
+ response =megams.get_accounts(sandbox_email)
10
+ response.body.to_s
11
+ assert_equal(200, response.status)
12
+ end
13
+
14
+
15
+ def test_get_accounts_bad
16
+ assert_raises(Megam::API::Errors::NotFound) do
17
+ response =megams.get_accounts(sandbox_email+"_bad")
18
+ response.body.to_s
19
+ end
20
+ end
21
+
22
+ def test_post_accounts_admin
23
+ response =megams.post_accounts(
24
+ {:id => random_id, :email => sandbox_email, :api_key => sandbox_apikey, :authority => $admin})
25
+ response.body.to_s
26
+ assert_equal(201, response.status)
27
+ end
28
+
29
+ def test_post_accounts_normal
30
+ response =megams.post_accounts(
31
+ {:id => random_id, :email => sandbox_email, :api_key => sandbox_apikey, :authority => $normal})
32
+ response.body.to_s
33
+ assert_equal(201, response.status)
34
+ end
35
+
36
+ def test_post_accounts_normal_bad
37
+ assert_raises(ArgumentError) do
38
+ response =megam.post_accounts(
39
+ {:id => random_id, :emailo => sandbox_email,
40
+ :apik_key => sandbox_apikey, :authority => $admin})
41
+ response.body.to_s
42
+ end
43
+ end
44
+
45
+ end
46
+
@@ -0,0 +1,23 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/test_helper")
2
+
3
+ class TestApps < MiniTest::Unit::TestCase
4
+
5
+ def test_post_appdefns
6
+
7
+ tmp_hash = {
8
+ "node_name" => "black1.megam.co",
9
+ "appdefns" => {"timetokill" => "test1", "metered" => "test1", "logging" => "test1", "runtime_exec" => "test"}
10
+ }
11
+
12
+ puts "======================> POST APPDEFNS TEMP HASH <============================================= "
13
+ puts tmp_hash
14
+ response = megams.post_appdefn(tmp_hash)
15
+ assert_equal(201, response.status)
16
+ end
17
+
18
+ def test_get_appdefns
19
+ response = megams.get_appdefn("black1.megam.co")
20
+ assert_equal(200, response.status)
21
+ end
22
+
23
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/test_helper")
2
+
3
+ class TestApps < MiniTest::Unit::TestCase
4
+
5
+ def test_post_appreqs
6
+
7
+ tmp_hash = {
8
+ "req_type" => "NStart",
9
+ "node_name" => "black1.megam.co",
10
+ "appdefns_id" => "12455",
11
+ "lc_apply" => "APPly",
12
+ "lc_additional" => "ADDition",
13
+ "lc_when" => "When"
14
+ }
15
+
16
+ puts "======================> POST APPREQS TEMP HASH <============================================= "
17
+ puts tmp_hash
18
+ response = megams.post_appreq(tmp_hash)
19
+ assert_equal(201, response.status)
20
+ end
21
+ #=begin
22
+ def test_get_appreqs
23
+ response = megams.get_appreq("black1.megam.co")
24
+ assert_equal(200, response.status)
25
+ end
26
+ #=end
27
+
28
+ end
@@ -0,0 +1,23 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/test_helper")
2
+
3
+ class TestApps < MiniTest::Unit::TestCase
4
+
5
+ def test_post_boltdefns
6
+
7
+ tmp_hash = {
8
+ "node_name" => "black1.megam.co",
9
+ "boltdefns" => {"username" => "new", "apikey" => "new", "store_name" => "", "url" => "", "prime" => "", "timetokill" => "", "metered" => "", "logging" => "", "runtime_exec" => ""}
10
+ }
11
+
12
+ puts "======================> POST APPDEFNS TEMP HASH <============================================= "
13
+ puts tmp_hash
14
+ response = megams.post_boltdefn(tmp_hash)
15
+ assert_equal(201, response.status)
16
+ end
17
+
18
+ def test_get_boltdefns
19
+ response = megams.get_boltdefn("black1.megam.co")
20
+ assert_equal(200, response.status)
21
+ end
22
+
23
+ end