chef-vpc-toolkit 2.1.0 → 2.2.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.
@@ -1,118 +0,0 @@
1
- require 'uri'
2
- require 'net/http'
3
- require 'net/https'
4
-
5
- module ChefVPCToolkit
6
-
7
- module HttpUtil
8
-
9
- MULTI_PART_BOUNDARY="jtZ!pZ1973um"
10
-
11
- def self.file_upload(url_string, file_data={}, post_data={}, auth_user=nil, auth_password=nil)
12
- url=URI.parse(url_string)
13
- http = Net::HTTP.new(url.host,url.port)
14
- req = Net::HTTP::Post.new(url.path)
15
-
16
- post_arr=[]
17
- post_data.each_pair do |key, value|
18
- post_arr << "--#{MULTI_PART_BOUNDARY}\r\n"
19
- post_arr << "Content-Disposition: form-data; name=\"#{key}\"\r\n"
20
- post_arr << "\r\n"
21
- post_arr << value
22
- post_arr << "\r\n"
23
- end
24
-
25
- file_data.each_pair do |name, file|
26
- post_arr << "--#{MULTI_PART_BOUNDARY}\r\n"
27
- post_arr << "Content-Disposition: form-data; name=\"#{name}\"; filename=\"#{File.basename(file)}\"\r\n"
28
- post_arr << "Content-Type: text/plain\r\n"
29
- post_arr << "\r\n"
30
- post_arr << File.read(file)
31
- post_arr << "\r\n--#{MULTI_PART_BOUNDARY}--\r\n"
32
- end
33
- post_arr << "--#{MULTI_PART_BOUNDARY}--\r\n\r\n"
34
-
35
- req.body=post_arr.join
36
-
37
- if url_string =~ /^https/
38
- http.use_ssl = true
39
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
40
- end
41
- req.basic_auth auth_user, auth_password if auth_user and auth_password
42
- req["Content-Type"] = "multipart/form-data, boundary=#{MULTI_PART_BOUNDARY}"
43
-
44
- response = http.request(req)
45
- case response
46
- when Net::HTTPSuccess
47
- return response.body
48
- else
49
- puts response.body
50
- response.error!
51
- end
52
- end
53
-
54
- def self.post(url_string, post_data, auth_user=nil, auth_password=nil)
55
- url=URI.parse(url_string)
56
- http = Net::HTTP.new(url.host,url.port)
57
- req = Net::HTTP::Post.new(url.path)
58
- if post_data.kind_of?(String) then
59
- req.body=post_data
60
- elsif post_data.kind_of?(Hash) then
61
- req.form_data=post_data
62
- else
63
- raise "Invalid post data type."
64
- end
65
- if url_string =~ /^https/
66
- http.use_ssl = true
67
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
68
- end
69
- req.basic_auth auth_user, auth_password if auth_user and auth_password
70
- response = http.request(req)
71
- case response
72
- when Net::HTTPSuccess
73
- return response.body
74
- else
75
- puts response.body
76
- response.error!
77
- end
78
- end
79
-
80
- def self.get(url_string, auth_user=nil, auth_password=nil)
81
- url=URI.parse(url_string)
82
- http = Net::HTTP.new(url.host,url.port)
83
- req = Net::HTTP::Get.new(url.path)
84
- if url_string =~ /^https/
85
- http.use_ssl = true
86
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
87
- end
88
- req.basic_auth auth_user, auth_password if auth_user and auth_password
89
- response = http.request(req)
90
- case response
91
- when Net::HTTPSuccess
92
- return response.body
93
- else
94
- response.error!
95
- end
96
- end
97
-
98
- def self.delete(url_string, auth_user=nil, auth_password=nil)
99
- url=URI.parse(url_string)
100
- http = Net::HTTP.new(url.host,url.port)
101
- req = Net::HTTP::Delete.new(url.path)
102
- if url_string =~ /^https/
103
- http.use_ssl = true
104
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
105
- end
106
- req.basic_auth auth_user, auth_password if auth_user and auth_password
107
- response = http.request(req)
108
- case response
109
- when Net::HTTPSuccess
110
- return response.body
111
- else
112
- response.error!
113
- end
114
- end
115
-
116
- end
117
-
118
- end
@@ -1,129 +0,0 @@
1
- require File.dirname(__FILE__) + '/test_helper'
2
-
3
- require 'tempfile'
4
-
5
- module ChefVPCToolkit
6
-
7
- class CloudServersVPCTest < Test::Unit::TestCase
8
-
9
- def test_os_types
10
-
11
- hash=CloudServersVPC.server_group_hash(SERVER_GROUP_XML)
12
- os_types=CloudServersVPC.os_types(hash)
13
-
14
- assert_equal 2, os_types.size
15
- assert_equal "rhel", os_types["login1"]
16
- assert_equal "ubuntu", os_types["test1"]
17
-
18
- end
19
-
20
- def test_server_names
21
-
22
- hash=CloudServersVPC.server_group_hash(SERVER_GROUP_XML)
23
- names=CloudServersVPC.server_names(hash)
24
-
25
- assert_equal 2, names.size
26
- assert names.include?("login1")
27
- assert names.include?("test1")
28
-
29
- end
30
-
31
- def test_print_server_group
32
-
33
- hash=CloudServersVPC.server_group_hash(SERVER_GROUP_XML)
34
- tmp = Tempfile.open('chef-cloud-toolkit')
35
- begin
36
- $stdout = tmp
37
- CloudServersVPC.print_server_group(hash)
38
- tmp.flush
39
- output=IO.read(tmp.path)
40
- $stdout = STDOUT
41
- assert output =~ /login1/
42
- assert output =~ /test1/
43
- assert output =~ /184.106.205.120/
44
- ensure
45
- $stdout = STDOUT
46
- end
47
-
48
- end
49
-
50
- def test_most_recent_server_group_hash
51
-
52
- tmp_dir=TmpDir.new_tmp_dir
53
- File.open("#{tmp_dir}/5.xml", 'w') do |f|
54
- f.write(SERVER_GROUP_XML)
55
- end
56
-
57
- hash=CloudServersVPC.most_recent_server_group_hash(File.join(tmp_dir, '*.xml'))
58
-
59
- assert_equal "mydomain.net", hash["domain-name"]
60
- assert_equal "1759", hash["id"]
61
- assert_equal 2, hash["servers"].size
62
-
63
- end
64
-
65
- def test_server_group_xml_for_id
66
-
67
- tmp_dir=TmpDir.new_tmp_dir
68
- File.open("#{tmp_dir}/5.xml", 'w') do |f|
69
- f.write(SERVER_GROUP_XML)
70
- end
71
-
72
- configs={
73
- "cloud_servers_vpc_url" => "http://localhost/",
74
- "cloud_servers_vpc_username" => "admin",
75
- "cloud_servers_vpc_password" => "test123"
76
- }
77
- HttpUtil.stubs(:get).returns(SERVER_GROUP_XML)
78
- xml=CloudServersVPC.server_group_xml_for_id(configs, File.join(tmp_dir, '*.xml'))
79
- assert_not_nil xml
80
- xml=CloudServersVPC.server_group_xml_for_id(configs, File.join(tmp_dir, '*.xml'),"1759")
81
- assert_not_nil xml
82
-
83
- end
84
-
85
- def test_load_public_key
86
-
87
- key=CloudServersVPC.load_public_key
88
- assert_not_nil key
89
-
90
- end
91
-
92
- def test_rebuild
93
-
94
- response={}
95
- response.stubs(:code).returns('200')
96
- HttpUtil.stubs(:post).returns(response)
97
-
98
- hash=CloudServersVPC.server_group_hash(SERVER_GROUP_XML)
99
-
100
- assert_raises(RuntimeError) do
101
- CloudServersVPC.rebuild(hash, "login1")
102
- end
103
-
104
- assert "200", CloudServersVPC.rebuild(hash, "test1").code
105
-
106
- end
107
-
108
- def test_create_client
109
-
110
- response={}
111
- response.stubs(:code).returns('200')
112
- HttpUtil.stubs(:post).returns(response)
113
-
114
- hash=CloudServersVPC.server_group_hash(SERVER_GROUP_XML)
115
-
116
- assert "200", CloudServersVPC.create_client(hash, "test1").code
117
-
118
- end
119
-
120
- def test_vpn_server_name
121
-
122
- hash=CloudServersVPC.server_group_hash(SERVER_GROUP_XML)
123
- assert_equal "login1", CloudServersVPC.vpn_server_name(hash)
124
-
125
- end
126
-
127
- end
128
-
129
- end