knife-openstack 0.10.0 → 1.0.0.rc1

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 (45) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +22 -0
  3. data/.travis.yml +6 -0
  4. data/CHANGELOG.md +20 -22
  5. data/Gemfile +11 -1
  6. data/README.md +6 -4
  7. data/Rakefile +33 -0
  8. data/knife-openstack.gemspec +7 -8
  9. data/lib/chef/knife/cloud/openstack_server_create_options.rb +75 -0
  10. data/lib/chef/knife/cloud/openstack_service.rb +62 -0
  11. data/lib/chef/knife/cloud/openstack_service_options.rb +50 -0
  12. data/lib/chef/knife/openstack_flavor_list.rb +37 -51
  13. data/lib/chef/knife/openstack_group_list.rb +38 -45
  14. data/lib/chef/knife/openstack_helpers.rb +30 -0
  15. data/lib/chef/knife/openstack_image_list.rb +42 -59
  16. data/lib/chef/knife/openstack_network_list.rb +25 -21
  17. data/lib/chef/knife/openstack_server_create.rb +166 -452
  18. data/lib/chef/knife/openstack_server_delete.rb +26 -106
  19. data/lib/chef/knife/openstack_server_list.rb +37 -59
  20. data/lib/chef/knife/openstack_server_show.rb +57 -0
  21. data/lib/knife-openstack/version.rb +1 -1
  22. data/spec/functional/flavor_list_func_spec.rb +45 -0
  23. data/spec/functional/group_list_func_spec.rb +67 -0
  24. data/spec/functional/image_list_func_spec.rb +51 -0
  25. data/spec/functional/network_list_func_spec.rb +44 -0
  26. data/spec/functional/server_create_func_spec.rb +118 -0
  27. data/spec/functional/server_delete_func_spec.rb +84 -0
  28. data/spec/functional/server_list_func_spec.rb +95 -0
  29. data/spec/functional/server_show_func_spec.rb +46 -0
  30. data/spec/integration/cleanup.rb +91 -0
  31. data/spec/integration/config/environment.yml.sample +13 -0
  32. data/spec/integration/openstack_spec.rb +618 -0
  33. data/spec/spec_helper.rb +126 -0
  34. data/spec/unit/openstack_flavor_list_spec.rb +30 -0
  35. data/spec/unit/openstack_group_list_spec.rb +43 -0
  36. data/spec/unit/openstack_image_list_spec.rb +32 -0
  37. data/spec/unit/openstack_network_list_spec.rb +39 -0
  38. data/spec/unit/openstack_server_create_spec.rb +344 -182
  39. data/spec/unit/openstack_server_delete_spec.rb +43 -0
  40. data/spec/unit/openstack_server_list_spec.rb +32 -0
  41. data/spec/unit/openstack_server_show_spec.rb +42 -0
  42. data/spec/unit/openstack_service_spec.rb +89 -0
  43. data/spec/unit/validate_spec.rb +55 -0
  44. metadata +95 -51
  45. data/lib/chef/knife/openstack_base.rb +0 -182
@@ -1,106 +1,26 @@
1
- #
2
- # Author:: Seth Chisamore (<schisamo@getchef.com>)
3
- # Author:: Matt Ray (<matt@getchef.com>)
4
- # Copyright:: Copyright (c) 2011-2014 Chef Software, Inc.
5
- # License:: Apache License, Version 2.0
6
- #
7
- # Licensed under the Apache License, Version 2.0 (the "License");
8
- # you may not use this file except in compliance with the License.
9
- # You may obtain a copy of the License at
10
- #
11
- # http://www.apache.org/licenses/LICENSE-2.0
12
- #
13
- # Unless required by applicable law or agreed to in writing, software
14
- # distributed under the License is distributed on an "AS IS" BASIS,
15
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
- # See the License for the specific language governing permissions and
17
- # limitations under the License.
18
- #
19
-
20
- require 'chef/knife/openstack_base'
21
-
22
- # These two are needed for the '--purge' deletion case
23
- require 'chef/node'
24
- require 'chef/api_client'
25
-
26
- class Chef
27
- class Knife
28
- class OpenstackServerDelete < Knife
29
-
30
- include Knife::OpenstackBase
31
-
32
- banner "knife openstack server delete SERVER [SERVER] (options)"
33
-
34
- option :purge,
35
- :short => "-P",
36
- :long => "--purge",
37
- :boolean => true,
38
- :default => false,
39
- :description => "Destroy corresponding node and client on the Chef Server, in addition to destroying the OpenStack node itself. Assumes node and client have the same name as the server (if not, add the '--node-name' option)."
40
-
41
- option :chef_node_name,
42
- :short => "-N NAME",
43
- :long => "--node-name NAME",
44
- :description => "The name of the node and client to delete, if it differs from the server name. Only has meaning when used with the '--purge' option."
45
-
46
- # Extracted from Chef::Knife.delete_object, because it has a
47
- # confirmation step built in... By specifying the '--purge'
48
- # flag (and also explicitly confirming the server destruction!)
49
- # the user is already making their intent known. It is not
50
- # necessary to make them confirm two more times.
51
- def destroy_item(klass, name, type_name)
52
- begin
53
- object = klass.load(name)
54
- object.destroy
55
- ui.warn("Deleted #{type_name} #{name}")
56
- rescue Net::HTTPServerException
57
- ui.warn("Could not find a #{type_name} named #{name} to delete!")
58
- end
59
- end
60
-
61
- def run
62
-
63
- validate!
64
-
65
- @name_args.each do |instance_id|
66
- begin
67
- server = connection.servers.find{|s| s.name == instance_id || s.id == instance_id }
68
-
69
- msg_pair("Instance Name", server.name)
70
- msg_pair("Instance ID", server.id)
71
- msg_pair("Flavor", server.flavor['id'])
72
- msg_pair("Image", server.image['id'])
73
- server.addresses.each do |name,addr|
74
- msg_pair("Network", name)
75
- msg_pair(" IP Address", addr[0]['addr'])
76
- end
77
- msg_pair("Availability Zone", server.availability_zone)
78
-
79
- puts "\n"
80
- confirm("Do you really want to delete this server")
81
-
82
- server.destroy
83
-
84
- ui.warn("Deleted server #{server.id}")
85
-
86
- if config[:purge]
87
- thing_to_delete = config[:chef_node_name] || instance_id
88
- destroy_item(Chef::Node, thing_to_delete, "node")
89
- destroy_item(Chef::ApiClient, thing_to_delete, "client")
90
- else
91
- ui.warn("Corresponding node and client for the #{instance_id} server were not deleted and remain registered with the Chef Server")
92
- end
93
-
94
- rescue NoMethodError
95
- ui.error("Could not locate server '#{instance_id}'.")
96
- rescue Excon::Errors::BadRequest => e
97
- response = Chef::JSONCompat.from_json(e.response.body)
98
- ui.fatal("Unknown server error (#{response['badRequest']['code']}): #{response['badRequest']['message']}")
99
- raise e
100
- end
101
- end
102
- end
103
-
104
- end
105
- end
106
- end
1
+ #
2
+ # Author:: Siddheshwar More (<siddheshwar.more@clogeny.com>)
3
+ # Author:: Kaustubh Deorukhkar (<kaustubh@clogeny.com>)
4
+ # Copyright:: Copyright (c) 2013 Chef Software, Inc.
5
+ #
6
+
7
+ require 'chef/knife/cloud/server/delete_options'
8
+ require 'chef/knife/cloud/server/delete_command'
9
+ require 'chef/knife/cloud/openstack_service'
10
+ require 'chef/knife/cloud/openstack_service_options'
11
+ require 'chef/knife/openstack_helpers'
12
+
13
+ class Chef
14
+ class Knife
15
+ class Cloud
16
+ class OpenstackServerDelete < ServerDeleteCommand
17
+ include ServerDeleteOptions
18
+ include OpenstackServiceOptions
19
+ include OpenstackHelpers
20
+
21
+ banner "knife openstack server delete INSTANCEID [INSTANCEID] (options)"
22
+
23
+ end
24
+ end
25
+ end
26
+ end
@@ -1,7 +1,9 @@
1
1
  #
2
2
  # Author:: Seth Chisamore (<schisamo@getchef.com>)
3
3
  # Author:: Matt Ray (<matt@getchef.com>)
4
- # Copyright:: Copyright (c) 2011-2014 Chef Software, Inc.
4
+ # Author:: Chirag Jog (<chirag@clogeny.com>)
5
+ # Author:: Prabhu Das (<prabhu.das@clogeny.com>)
6
+ # Copyright:: Copyright (c) 2011-2013 Chef Software, Inc.
5
7
  # License:: Apache License, Version 2.0
6
8
  #
7
9
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,73 +19,49 @@
17
19
  # limitations under the License.
18
20
  #
19
21
 
20
- require 'chef/knife/openstack_base'
22
+ require 'chef/knife/cloud/server/list_command'
23
+ require 'chef/knife/openstack_helpers'
24
+ require 'chef/knife/cloud/openstack_service_options'
25
+ require 'chef/knife/cloud/server/list_options'
21
26
 
22
27
  class Chef
23
28
  class Knife
24
- class OpenstackServerList < Knife
29
+ class Cloud
30
+ class OpenstackServerList < ServerListCommand
31
+ include OpenstackHelpers
32
+ include OpenstackServiceOptions
33
+ include ServerListOptions
25
34
 
26
- include Knife::OpenstackBase
35
+ banner "knife openstack server list (options)"
27
36
 
28
- banner "knife openstack server list (options)"
29
-
30
- def run
31
- $stdout.sync = true
37
+ def before_exec_command
38
+ #set columns_with_info map
39
+ @columns_with_info = [
40
+ {:label => 'Name', :key => 'name'},
41
+ {:label => 'Instance ID', :key => 'id'},
42
+ {:label => 'Public IP', :key => 'addresses', :value_callback => method(:get_public_ip_address)},
43
+ {:label => 'Private IP', :key => 'addresses', :value_callback => method(:get_private_ip_address)},
44
+ {:label => 'Flavor', :key => 'flavor', :value_callback => method(:get_id)},
45
+ {:label => 'Image', :key => 'image', :value_callback => method(:get_id)},
46
+ {:label => 'Keypair', :key => 'key_name'},
47
+ {:label => 'State', :key => 'state'},
48
+ {:label => 'Availability Zone', :key => 'availability_zone'}
49
+ ]
50
+ @sort_by_field = "name"
51
+ super
52
+ end
32
53
 
33
- validate!
54
+ def get_public_ip_address (addresses)
55
+ primary_public_ip_address(addresses)
56
+ end
34
57
 
35
- server_list = [
36
- ui.color('Name', :bold),
37
- ui.color('Instance ID', :bold),
38
- ui.color('Zone', :bold),
39
- ui.color('Public IP', :bold),
40
- ui.color('Private IP', :bold),
41
- ui.color('Flavor', :bold),
42
- ui.color('Image', :bold),
43
- ui.color('Keypair', :bold),
44
- ui.color('State', :bold)
45
- ]
58
+ def get_private_ip_address (addresses)
59
+ primary_private_ip_address(addresses)
60
+ end
46
61
 
47
- begin
48
- connection.servers.all.sort_by(&:name).each do |server|
49
- server_list << server.name
50
- server_list << server.id.to_s
51
- server_list << server.availability_zone
52
- if primary_public_ip_address(server.addresses)
53
- server_list << primary_public_ip_address(server.addresses)
54
- else
55
- server_list << ''
56
- end
57
- if primary_private_ip_address(server.addresses)
58
- server_list << primary_private_ip_address(server.addresses)
59
- else
60
- server_list << ''
61
- end
62
- server_list << server.flavor['id'].to_s
63
- if server.image
64
- server_list << server.image['id']
65
- else
66
- server_list << ""
67
- end
68
- server_list << server.key_name
69
- server_list << begin
70
- state = server.state.to_s.downcase
71
- case state
72
- when 'shutting-down','terminated','stopping','stopped','error','shutoff'
73
- ui.color(state, :red)
74
- when 'pending','build','paused','suspended','hard_reboot'
75
- ui.color(state, :yellow)
76
- else
77
- ui.color(state, :green)
78
- end
79
- end
80
- end
81
- rescue Excon::Errors::BadRequest => e
82
- response = Chef::JSONCompat.from_json(e.response.body)
83
- ui.fatal("Unknown server error (#{response['badRequest']['code']}): #{response['badRequest']['message']}")
84
- raise e
62
+ def get_id(value)
63
+ value['id']
85
64
  end
86
- puts ui.list(server_list, :uneven_columns_across, 9)
87
65
 
88
66
  end
89
67
  end
@@ -0,0 +1,57 @@
1
+ #
2
+ # Copyright:: Copyright (c) 2011-2013 Chef Software, Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require 'chef/knife/cloud/server/show_command'
19
+ require 'chef/knife/openstack_helpers'
20
+ require 'chef/knife/cloud/server/show_options'
21
+ require 'chef/knife/cloud/openstack_service'
22
+ require 'chef/knife/cloud/openstack_service_options'
23
+ require 'chef/knife/cloud/exceptions'
24
+
25
+ class Chef
26
+ class Knife
27
+ class Cloud
28
+ class OpenstackServerShow < ServerShowCommand
29
+ include OpenstackHelpers
30
+ include OpenstackServiceOptions
31
+ include ServerShowOptions
32
+
33
+ banner "knife openstack server show (options)"
34
+
35
+ def before_exec_command
36
+ #set columns_with_info map
37
+ @columns_with_info = [
38
+ {:label => 'Instance ID', :key => 'id'},
39
+ {:label => 'Name', :key => 'name'},
40
+ {:label => 'Public IP', :key => 'addresses', :value_callback => method(:primary_public_ip_address)},
41
+ {:label => 'Private IP', :key => 'addresses', :value_callback => method(:primary_private_ip_address)},
42
+ {:label => 'Flavor', :key => 'flavor', :value_callback => method(:get_id)},
43
+ {:label => 'Image', :key => 'image', :value_callback => method(:get_id)},
44
+ {:label => 'Keypair', :key => 'key_name'},
45
+ {:label => 'State', :key => 'state'},
46
+ {:label => 'Availability Zone', :key => 'availability_zone'}
47
+ ]
48
+ super
49
+ end
50
+
51
+ def get_id(value)
52
+ value['id']
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -1,6 +1,6 @@
1
1
  module Knife
2
2
  module OpenStack
3
- VERSION = '0.10.0'
3
+ VERSION = "1.0.0.rc1"
4
4
  MAJOR, MINOR, TINY = VERSION.split('.')
5
5
  end
6
6
  end
@@ -0,0 +1,45 @@
1
+ #
2
+ # Author:: Mukta Aphale (<mukta.aphale@clogeny.com>)
3
+ # Author:: Ameya Varade (<ameya.varade@clogeny.com>)
4
+ # Copyright:: Copyright (c) 2013-2014 Chef Software, Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ require 'spec_helper'
20
+ require 'chef/knife/openstack_flavor_list'
21
+ require 'chef/knife/cloud/openstack_service'
22
+ require 'support/shared_examples_for_command'
23
+
24
+ describe Chef::Knife::Cloud::OpenstackFlavorList do
25
+ let (:instance) {Chef::Knife::Cloud::OpenstackFlavorList.new}
26
+
27
+ context "functionality" do
28
+ before do
29
+ resources = [ TestResource.new({:id => "resource-1", :name => "m1.tiny", :vcpus => "1", :ram => 512, :disk => 0}),
30
+ TestResource.new({:id => "resource-2", :name => "m1-xlarge-bigdisk", :vcpus => "8", :ram => 16384, :disk => 50})
31
+ ]
32
+ allow(instance).to receive(:query_resource).and_return(resources)
33
+ allow(instance).to receive(:puts)
34
+ allow(instance).to receive(:create_service_instance).and_return(Chef::Knife::Cloud::Service.new)
35
+ allow(instance).to receive(:validate!)
36
+ end
37
+
38
+ it "lists formatted list of resources" do
39
+ expect(instance.ui).to receive(:list).with(["Name", "ID", "Virtual CPUs", "RAM", "Disk",
40
+ "m1-xlarge-bigdisk", "resource-2", "8", "16384 MB", "50 GB",
41
+ "m1.tiny", "resource-1", "1", "512 MB", "0 GB"], :uneven_columns_across, 5)
42
+ instance.run
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,67 @@
1
+ #
2
+ # Author:: Mukta Aphale (<mukta.aphale@clogeny.com>)
3
+ # Author:: Prabhu Das (<prabhu.das@clogeny.com>)
4
+ # Author:: Ameya Varade (<ameya.varade@clogeny.com>)
5
+ # Copyright:: Copyright (c) 2013-2014 Chef Software, Inc.
6
+ # License:: Apache License, Version 2.0
7
+ #
8
+ # Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
19
+
20
+ require 'spec_helper'
21
+ require 'chef/knife/openstack_group_list'
22
+ require 'chef/knife/cloud/openstack_service'
23
+
24
+ describe Chef::Knife::Cloud::OpenstackGroupList do
25
+ let (:instance) {Chef::Knife::Cloud::OpenstackGroupList.new}
26
+
27
+ context "functionality" do
28
+ before do
29
+ resources = [ TestResource.new({ "name" => "Unrestricted",
30
+ "description" => "All ports open",
31
+ "security_group_rules" => [TestResource.new({"from_port" => 1,
32
+ "group" => {},
33
+ "ip_protocol" => "tcp",
34
+ "to_port" => 636,
35
+ "parent_group_id" => 14,
36
+ "ip_range" => {"cidr" => "0.0.0.0/0"},
37
+ "id" => 1
38
+ })
39
+ ]
40
+ }),
41
+ TestResource.new({ "name" => "WindowsDomain",
42
+ "description" => "Allows common protocols useful in a Windows domain",
43
+ "security_group_rules" => [TestResource.new({"from_port" => 22,
44
+ "group" => {},
45
+ "ip_protocol" => "tcp",
46
+ "to_port" => 636,
47
+ "parent_group_id" => 14,
48
+ "ip_range" => {"cidr" => "0.0.0.0/0"},
49
+ "id" => 2
50
+ })
51
+ ]
52
+ })
53
+ ]
54
+ allow(instance).to receive(:query_resource).and_return(resources)
55
+ allow(instance).to receive(:puts)
56
+ allow(instance).to receive(:create_service_instance).and_return(Chef::Knife::Cloud::Service.new)
57
+ allow(instance).to receive(:validate!)
58
+ end
59
+
60
+ it "lists formatted list of resources" do
61
+ expect(instance.ui).to receive(:list).with(["Name", "Protocol", "From", "To", "CIDR", "Description",
62
+ "Unrestricted", "tcp", "1", "636", "0.0.0.0/0", "All ports open",
63
+ "WindowsDomain", "tcp", "22", "636", "0.0.0.0/0", "Allows common protocols useful in a Windows domain"], :uneven_columns_across, 6)
64
+ instance.run
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,51 @@
1
+ #
2
+ # Author:: Mukta Aphale (<mukta.aphale@clogeny.com>)
3
+ # Author:: Ameya Varade (<ameya.varade@clogeny.com>)
4
+ # Copyright:: Copyright (c) 2013-2014 Chef Software, Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ require 'spec_helper'
20
+ require 'chef/knife/openstack_image_list'
21
+ require 'chef/knife/cloud/openstack_service'
22
+
23
+ describe Chef::Knife::Cloud::OpenstackImageList do
24
+ let (:instance) {Chef::Knife::Cloud::OpenstackImageList.new}
25
+
26
+ context "functionality" do
27
+ before do
28
+ resources = [ TestResource.new({:id => "resource-1", :name => "image01", :metadata => {} }),
29
+ TestResource.new({:id => "resource-2", :name => "initrd", :metadata => {} })
30
+ ]
31
+ allow(instance).to receive(:query_resource).and_return(resources)
32
+ allow(instance).to receive(:puts)
33
+ allow(instance).to receive(:create_service_instance).and_return(Chef::Knife::Cloud::Service.new)
34
+ allow(instance).to receive(:validate!)
35
+ end
36
+
37
+ it "displays formatted list of images, filtered by default" do
38
+ expect(instance.ui).to receive(:list).with(["Name", "ID", "Snapshot",
39
+ "image01", "resource-1", "no"], :uneven_columns_across, 3)
40
+ instance.run
41
+ end
42
+
43
+ it "lists all images when disable_filter = true" do
44
+ instance.config[:disable_filter] = true
45
+ expect(instance.ui).to receive(:list).with(["Name", "ID", "Snapshot",
46
+ "image01", "resource-1", "no",
47
+ "initrd", "resource-2", "no"], :uneven_columns_across, 3)
48
+ instance.run
49
+ end
50
+ end
51
+ end