knife-google 1.3.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/.gitignore +1 -0
- data/.travis.yml +15 -3
- data/CHANGELOG.md +4 -6
- data/Gemfile +3 -9
- data/README.md +208 -355
- data/RELEASE_NOTES.md +8 -17
- data/Rakefile +8 -49
- data/knife-google.gemspec +20 -17
- data/lib/chef/knife/cloud/google_service.rb +491 -0
- data/lib/chef/knife/cloud/google_service_helpers.rb +62 -0
- data/lib/chef/knife/cloud/google_service_options.rb +58 -0
- data/lib/chef/knife/google_disk_create.rb +40 -44
- data/lib/chef/knife/google_disk_delete.rb +22 -40
- data/lib/chef/knife/google_disk_list.rb +57 -51
- data/lib/chef/knife/google_project_quotas.rb +59 -0
- data/lib/chef/knife/google_region_list.rb +43 -102
- data/lib/chef/knife/google_region_quotas.rb +77 -0
- data/lib/chef/knife/google_server_create.rb +224 -505
- data/lib/chef/knife/google_server_delete.rb +20 -78
- data/lib/chef/knife/google_server_list.rb +42 -53
- data/lib/chef/knife/google_server_show.rb +44 -0
- data/lib/chef/knife/google_zone_list.rb +39 -50
- data/lib/knife-google/version.rb +3 -2
- data/spec/cloud/google_service_helpers_spec.rb +120 -0
- data/spec/cloud/google_service_spec.rb +832 -0
- data/spec/google_disk_create_spec.rb +72 -0
- data/spec/google_disk_delete_spec.rb +64 -0
- data/spec/google_disk_list_spec.rb +93 -0
- data/spec/google_project_quotas_spec.rb +63 -0
- data/spec/google_region_list_spec.rb +65 -0
- data/spec/google_region_quotas_spec.rb +108 -0
- data/spec/google_server_create_spec.rb +177 -0
- data/spec/google_server_delete_spec.rb +39 -0
- data/spec/google_server_list_spec.rb +77 -0
- data/spec/google_server_show_spec.rb +60 -0
- data/spec/google_zone_list_spec.rb +59 -0
- metadata +91 -114
- data/CONTRIB.md +0 -64
- data/lib/chef/knife/google_base.rb +0 -76
- data/lib/chef/knife/google_project_list.rb +0 -178
- data/lib/chef/knife/google_setup.rb +0 -31
- data/lib/google/compute.rb +0 -47
- data/lib/google/compute/client.rb +0 -216
- data/lib/google/compute/config.rb +0 -23
- data/lib/google/compute/creatable_resource_collection.rb +0 -55
- data/lib/google/compute/deletable_resource_collection.rb +0 -51
- data/lib/google/compute/disk.rb +0 -38
- data/lib/google/compute/exception.rb +0 -30
- data/lib/google/compute/firewall.rb +0 -65
- data/lib/google/compute/global_operation.rb +0 -60
- data/lib/google/compute/image.rb +0 -29
- data/lib/google/compute/listable_resource_collection.rb +0 -33
- data/lib/google/compute/machine_type.rb +0 -36
- data/lib/google/compute/mixins/utils.rb +0 -58
- data/lib/google/compute/network.rb +0 -29
- data/lib/google/compute/project.rb +0 -76
- data/lib/google/compute/region.rb +0 -31
- data/lib/google/compute/region_operation.rb +0 -62
- data/lib/google/compute/resource.rb +0 -81
- data/lib/google/compute/resource_collection.rb +0 -78
- data/lib/google/compute/server.rb +0 -88
- data/lib/google/compute/server/attached_disk.rb +0 -39
- data/lib/google/compute/server/network_interface.rb +0 -38
- data/lib/google/compute/server/network_interface/access_config.rb +0 -35
- data/lib/google/compute/server/serial_port_output.rb +0 -31
- data/lib/google/compute/snapshot.rb +0 -30
- data/lib/google/compute/version.rb +0 -19
- data/lib/google/compute/zone.rb +0 -34
- data/lib/google/compute/zone_operation.rb +0 -62
- data/spec/chef/knife/google_base_spec.rb +0 -46
- data/spec/chef/knife/google_disk_create_spec.rb +0 -37
- data/spec/chef/knife/google_disk_delete_spec.rb +0 -64
- data/spec/chef/knife/google_disk_list_spec.rb +0 -36
- data/spec/chef/knife/google_region_list_spec.rb +0 -32
- data/spec/chef/knife/google_server_create_spec.rb +0 -138
- data/spec/chef/knife/google_server_delete_spec.rb +0 -127
- data/spec/chef/knife/google_server_list_spec.rb +0 -39
- data/spec/chef/knife/google_setup_spec.rb +0 -24
- data/spec/chef/knife/google_zone_list_spec.rb +0 -32
- data/spec/data/client.json +0 -14
- data/spec/data/compute-v1.json +0 -6734
- data/spec/data/disk.json +0 -14
- data/spec/data/firewall.json +0 -13
- data/spec/data/global_operation.json +0 -36
- data/spec/data/image.json +0 -12
- data/spec/data/machine_type.json +0 -24
- data/spec/data/network.json +0 -10
- data/spec/data/project.json +0 -21
- data/spec/data/region.json +0 -23
- data/spec/data/serial_port_output.json +0 -5
- data/spec/data/server.json +0 -46
- data/spec/data/snapshot.json +0 -12
- data/spec/data/zone.json +0 -22
- data/spec/data/zone_operation.json +0 -36
- data/spec/google/compute/disk_spec.rb +0 -115
- data/spec/google/compute/firewall_spec.rb +0 -129
- data/spec/google/compute/global_operation_spec.rb +0 -62
- data/spec/google/compute/image_spec.rb +0 -75
- data/spec/google/compute/machine_type_spec.rb +0 -53
- data/spec/google/compute/network_spec.rb +0 -68
- data/spec/google/compute/project_spec.rb +0 -71
- data/spec/google/compute/region_spec.rb +0 -51
- data/spec/google/compute/server_spec.rb +0 -118
- data/spec/google/compute/snapshot_spec.rb +0 -57
- data/spec/google/compute/zone_operation_spec.rb +0 -62
- data/spec/google/compute/zone_spec.rb +0 -51
- data/spec/spec_helper.rb +0 -45
- data/spec/support/mocks.rb +0 -62
- data/spec/support/resource_examples.rb +0 -70
- data/spec/support/spec_google_base.rb +0 -60
@@ -1,4 +1,8 @@
|
|
1
|
-
#
|
1
|
+
#
|
2
|
+
# Author:: Paul Rossman (<paulrossman@google.com>)
|
3
|
+
# Author:: Chef Partner Engineering (<partnereng@chef.io>)
|
4
|
+
# Copyright:: Copyright 2015-2016 Google Inc., Chef Software, Inc.
|
5
|
+
# License:: Apache License, Version 2.0
|
2
6
|
#
|
3
7
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
8
|
# you may not use this file except in compliance with the License.
|
@@ -12,88 +16,26 @@
|
|
12
16
|
# See the License for the specific language governing permissions and
|
13
17
|
# limitations under the License.
|
14
18
|
|
15
|
-
require
|
19
|
+
require "chef/knife"
|
20
|
+
require "chef/knife/cloud/server/delete_options"
|
21
|
+
require "chef/knife/cloud/server/delete_command"
|
22
|
+
require "chef/knife/cloud/google_service"
|
23
|
+
require "chef/knife/cloud/google_service_helpers"
|
24
|
+
require "chef/knife/cloud/google_service_options"
|
16
25
|
|
17
26
|
class Chef
|
18
27
|
class Knife
|
19
|
-
class
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
require 'chef/api_client'
|
25
|
-
require 'google/compute'
|
26
|
-
end
|
27
|
-
|
28
|
-
banner "knife google server delete SERVER [SERVER] -Z ZONE (options)"
|
29
|
-
|
30
|
-
attr_reader :instances
|
31
|
-
|
32
|
-
option :zone,
|
33
|
-
:short => "-Z ZONE",
|
34
|
-
:long => "--gce-zone ZONE",
|
35
|
-
:description => "The Zone for this server"
|
28
|
+
class Cloud
|
29
|
+
class GoogleServerDelete < ServerDeleteCommand
|
30
|
+
include ServerDeleteOptions
|
31
|
+
include GoogleServiceHelpers
|
32
|
+
include GoogleServiceOptions
|
36
33
|
|
37
|
-
|
38
|
-
:short => "-P",
|
39
|
-
:long => "--purge",
|
40
|
-
:boolean => true,
|
41
|
-
:default => false,
|
42
|
-
:description => "Destroy corresponding node and client on the Chef Server, in addition to destroying the GCE server itself. Assumes node and client have the same name as the server (if not, add the '--node-name' option)."
|
43
|
-
|
44
|
-
option :chef_node_name,
|
45
|
-
:short => "-N NAME",
|
46
|
-
:long => "--node-name NAME",
|
47
|
-
: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."
|
48
|
-
|
49
|
-
# Taken from knife-ec2 plugin, for rational , check the following link
|
50
|
-
# https://github.com/opscode/knife-ec2/blob/master/lib/chef/knife/ec2_server_delete.rb#L48
|
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
|
-
begin
|
63
|
-
zone = client.zones.get(config[:zone] || Chef::Config[:knife][:gce_zone]).self_link
|
64
|
-
rescue Google::Compute::ResourceNotFound
|
65
|
-
ui.error("Zone '#{config[:zone] || Chef::Config[:knife][:gce_zone]}' not found")
|
66
|
-
exit 1
|
67
|
-
rescue Google::Compute::ParameterValidation
|
68
|
-
ui.error("Must specify zone in knife config file or in command line as an option. Try --help.")
|
69
|
-
exit 1
|
70
|
-
end
|
34
|
+
banner "knife google server delete INSTANCE_NAME [INSTANCE_NAME] (options)"
|
71
35
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
instance = client.instances.get(:name=>instance_name, :zone=>selflink2name(zone))
|
76
|
-
@instances << instance
|
77
|
-
msg_pair("Name", instance.name)
|
78
|
-
msg_pair("Machine Type", selflink2name(instance.machine_type))
|
79
|
-
msg_pair("Zone", selflink2name(instance.zone))
|
80
|
-
msg_pair("Tags", instance.tags.has_key?("items") ? instance.tags["items"].join(',') : "None")
|
81
|
-
msg_pair("Public IP Address", public_ips(instance).join(','))
|
82
|
-
msg_pair("Private IP Address", private_ips(instance).join(','))
|
83
|
-
puts "\n"
|
84
|
-
ui.warn("Persistent disks attached to this instance are not deleted with this operation")
|
85
|
-
ui.confirm("Do you really want to delete server '#{selflink2name(zone)}:#{instance.name}'")
|
86
|
-
client.instances.delete(:instance=>instance.name, :zone=>selflink2name(zone))
|
87
|
-
ui.warn("Deleted server '#{selflink2name(zone)}:#{instance.name}'")
|
88
|
-
if config[:purge]
|
89
|
-
destroy_item(Chef::Node, instance.name, "node")
|
90
|
-
destroy_item(Chef::ApiClient, instance.name, "client")
|
91
|
-
else
|
92
|
-
ui.warn("Corresponding node and client for the #{instance.name} server were not deleted and remain registered with the Chef Server")
|
93
|
-
end
|
94
|
-
rescue
|
95
|
-
ui.error("Could not locate server '#{selflink2name(zone)}:#{instance_name}'.")
|
96
|
-
end
|
36
|
+
def validate_params!
|
37
|
+
check_for_missing_config_values!
|
38
|
+
super
|
97
39
|
end
|
98
40
|
end
|
99
41
|
end
|
@@ -1,4 +1,8 @@
|
|
1
|
-
#
|
1
|
+
#
|
2
|
+
# Author:: Paul Rossman (<paulrossman@google.com>)
|
3
|
+
# Author:: Chef Partner Engineering (<partnereng@chef.io>)
|
4
|
+
# Copyright:: Copyright 2015-2016 Google Inc., Chef Software, Inc.
|
5
|
+
# License:: Apache License, Version 2.0
|
2
6
|
#
|
3
7
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
8
|
# you may not use this file except in compliance with the License.
|
@@ -11,66 +15,51 @@
|
|
11
15
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
16
|
# See the License for the specific language governing permissions and
|
13
17
|
# limitations under the License.
|
14
|
-
#
|
15
|
-
require 'chef/knife/google_base'
|
16
|
-
|
17
|
-
class Chef
|
18
|
-
class Knife
|
19
|
-
class GoogleServerList < Knife
|
20
18
|
|
21
|
-
|
19
|
+
require "chef/knife"
|
20
|
+
require "chef/knife/cloud/server/list_command"
|
21
|
+
require "chef/knife/cloud/server/list_options"
|
22
|
+
require "chef/knife/cloud/google_service"
|
23
|
+
require "chef/knife/cloud/google_service_helpers"
|
24
|
+
require "chef/knife/cloud/google_service_options"
|
22
25
|
|
23
|
-
|
26
|
+
class Chef::Knife::Cloud
|
27
|
+
class GoogleServerList < ServerListCommand
|
28
|
+
include GoogleServiceHelpers
|
29
|
+
include GoogleServiceOptions
|
24
30
|
|
25
|
-
|
26
|
-
:short => "-Z ZONE",
|
27
|
-
:long => "--gce-zone ZONE",
|
28
|
-
:description => "The Zone for this server"
|
31
|
+
banner "knife google server list"
|
29
32
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
zone = client.zones.get(config[:zone] || Chef::Config[:knife][:gce_zone])
|
35
|
-
rescue Google::Compute::ResourceNotFound
|
36
|
-
ui.error("Zone '#{config[:zone] || Chef::Config[:knife][:gce_zone] }' not found.")
|
37
|
-
exit 1
|
38
|
-
rescue Google::Compute::ParameterValidation
|
39
|
-
ui.error("Must specify zone in knife config file or in command line as an option. Try --help.")
|
40
|
-
exit 1
|
41
|
-
end
|
33
|
+
def validate_params!
|
34
|
+
check_for_missing_config_values!
|
35
|
+
super
|
36
|
+
end
|
42
37
|
|
43
|
-
|
44
|
-
|
38
|
+
def before_exec_command
|
39
|
+
@columns_with_info = [
|
40
|
+
{ label: "Instance Name", key: "name" },
|
41
|
+
{ label: "Status", key: "status", value_callback: method(:format_status_value) },
|
42
|
+
{ label: "Machine Type", key: "machine_type" },
|
43
|
+
{ label: "Internal IP", key: "private_ip" },
|
44
|
+
{ label: "External IP", key: "public_ip" },
|
45
|
+
{ label: "Network", key: "network" },
|
46
|
+
]
|
45
47
|
|
46
|
-
|
48
|
+
@sort_by_field = "name"
|
49
|
+
end
|
47
50
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
when 'stopping', 'stopped', 'terminated'
|
59
|
-
ui.color(status, :red)
|
60
|
-
when 'requested', 'provisioning', 'staging'
|
61
|
-
ui.color(status, :yellow)
|
62
|
-
else
|
63
|
-
ui.color(status, :green)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
51
|
+
def format_status_value(status)
|
52
|
+
status = status.downcase
|
53
|
+
status_color = case status
|
54
|
+
when "stopping", "stopped", "terminated"
|
55
|
+
:red
|
56
|
+
when "requested", "provisioning", "staging"
|
57
|
+
:yellow
|
58
|
+
else
|
59
|
+
:green
|
60
|
+
end
|
67
61
|
|
68
|
-
|
69
|
-
puts ui.list(instance_list, :uneven_columns_across, output_column_count)
|
70
|
-
else
|
71
|
-
puts "No servers found in #{zone.name} zone."
|
72
|
-
end
|
73
|
-
end
|
62
|
+
ui.color(status, status_color)
|
74
63
|
end
|
75
64
|
end
|
76
65
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Chef Partner Engineering (<partnereng@chef.io>)
|
3
|
+
# Copyright:: Copyright 2016 Chef Software, Inc.
|
4
|
+
# License:: Apache License, Version 2.0
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
|
18
|
+
require "chef/knife"
|
19
|
+
require "chef/knife/cloud/server/show_options"
|
20
|
+
require "chef/knife/cloud/server/show_command"
|
21
|
+
require "chef/knife/cloud/google_service"
|
22
|
+
require "chef/knife/cloud/google_service_helpers"
|
23
|
+
require "chef/knife/cloud/google_service_options"
|
24
|
+
|
25
|
+
class Chef
|
26
|
+
class Knife
|
27
|
+
class Cloud
|
28
|
+
class GoogleServerShow < ServerShowCommand
|
29
|
+
include ServerShowOptions
|
30
|
+
include GoogleServiceHelpers
|
31
|
+
include GoogleServiceOptions
|
32
|
+
|
33
|
+
banner "knife google server show INSTANCE_NAME (options)"
|
34
|
+
|
35
|
+
def validate_params!
|
36
|
+
check_for_missing_config_values!
|
37
|
+
raise "You must supply an instance name to display" if @name_args.empty?
|
38
|
+
raise "You may only supply one instance name" if @name_args.size > 1
|
39
|
+
super
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -1,4 +1,8 @@
|
|
1
|
-
#
|
1
|
+
#
|
2
|
+
# Author:: Paul Rossman (<paulrossman@google.com>)
|
3
|
+
# Author:: Chef Partner Engineering (<partnereng@chef.io>)
|
4
|
+
# Copyright:: Copyright 2015-2016 Google Inc., Chef Software, Inc.
|
5
|
+
# License:: Apache License, Version 2.0
|
2
6
|
#
|
3
7
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
8
|
# you may not use this file except in compliance with the License.
|
@@ -11,62 +15,47 @@
|
|
11
15
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
16
|
# See the License for the specific language governing permissions and
|
13
17
|
# limitations under the License.
|
14
|
-
#
|
15
|
-
require 'chef/knife/google_base'
|
16
|
-
require 'time'
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
19
|
+
require "chef/knife"
|
20
|
+
require "chef/knife/cloud/list_resource_command"
|
21
|
+
require "chef/knife/cloud/google_service"
|
22
|
+
require "chef/knife/cloud/google_service_helpers"
|
23
|
+
require "chef/knife/cloud/google_service_options"
|
21
24
|
|
22
|
-
|
25
|
+
class Chef::Knife::Cloud
|
26
|
+
class GoogleZoneList < ResourceListCommand
|
27
|
+
include GoogleServiceHelpers
|
28
|
+
include GoogleServiceOptions
|
23
29
|
|
24
|
-
|
30
|
+
banner "knife google zone list"
|
25
31
|
|
26
|
-
|
27
|
-
|
32
|
+
def validate_params!
|
33
|
+
check_for_missing_config_values!
|
34
|
+
super
|
35
|
+
end
|
28
36
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
37
|
+
def before_exec_command
|
38
|
+
@columns_with_info = [
|
39
|
+
{ label: "Zone", key: "name" },
|
40
|
+
{ label: "Status", key: "status", value_callback: method(:format_status_value) },
|
41
|
+
]
|
34
42
|
|
35
|
-
|
43
|
+
@sort_by_field = "name"
|
44
|
+
end
|
45
|
+
|
46
|
+
def format_status_value(status)
|
47
|
+
status = status.downcase
|
48
|
+
status_color = if status == "up"
|
49
|
+
:green
|
50
|
+
else
|
51
|
+
:red
|
52
|
+
end
|
53
|
+
|
54
|
+
ui.color(status, status_color)
|
55
|
+
end
|
36
56
|
|
37
|
-
|
38
|
-
|
39
|
-
zone_list << begin
|
40
|
-
status = zone.status.downcase
|
41
|
-
case status
|
42
|
-
when 'up'
|
43
|
-
ui.color(status, :green)
|
44
|
-
else
|
45
|
-
ui.color(status, :red)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
deprecation_state = "-"
|
49
|
-
if zone.deprecated.respond_to?('state')
|
50
|
-
deprecation_state = zone.deprecated.state
|
51
|
-
end
|
52
|
-
zone_list << deprecation_state
|
53
|
-
unless zone.maintenance_windows.nil?
|
54
|
-
maintenance_window = zone.maintenance_windows.map do |mw|
|
55
|
-
begin_time = Time.parse(mw["beginTime"])
|
56
|
-
end_time = Time.parse(mw["endTime"])
|
57
|
-
if (Time.now >= begin_time) and (Time.now <= end_time)
|
58
|
-
ui.color("#{begin_time} to #{end_time}",:red)
|
59
|
-
else
|
60
|
-
ui.color("#{begin_time} to #{end_time}",:green)
|
61
|
-
end
|
62
|
-
end.join(",")
|
63
|
-
zone_list << maintenance_window
|
64
|
-
else
|
65
|
-
zone_list << "-"
|
66
|
-
end
|
67
|
-
end
|
68
|
-
ui.info(ui.list(zone_list, :uneven_columns_across, output_column_count))
|
69
|
-
end
|
57
|
+
def query_resource
|
58
|
+
service.list_zones
|
70
59
|
end
|
71
60
|
end
|
72
61
|
end
|
data/lib/knife-google/version.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright
|
1
|
+
# Copyright 2015 Google Inc. All Rights Reserved.
|
2
2
|
#
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
4
|
# you may not use this file except in compliance with the License.
|
@@ -14,6 +14,7 @@
|
|
14
14
|
#
|
15
15
|
module Knife
|
16
16
|
module Google
|
17
|
-
VERSION = "
|
17
|
+
VERSION = "2.0.0".freeze
|
18
|
+
MAJOR, MINOR, TINY = VERSION.split(".")
|
18
19
|
end
|
19
20
|
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Chef Partner Engineering (<partnereng@chef.io>)
|
3
|
+
# Copyright:: Copyright (c) 2016 Chef Software, Inc.
|
4
|
+
# License:: Apache License, Version 2.0
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
|
19
|
+
require "chef/knife/cloud/google_service"
|
20
|
+
require "chef/knife/cloud/google_service_helpers"
|
21
|
+
|
22
|
+
class Tester
|
23
|
+
include Chef::Knife::Cloud::GoogleServiceHelpers
|
24
|
+
end
|
25
|
+
|
26
|
+
describe Chef::Knife::Cloud::GoogleServiceHelpers do
|
27
|
+
let(:tester) { Tester.new }
|
28
|
+
|
29
|
+
describe '#create_service_instance' do
|
30
|
+
it "creates a GoogleService instance" do
|
31
|
+
expect(tester).to receive(:locate_config_value).with(:gce_project).and_return("test_project")
|
32
|
+
expect(tester).to receive(:locate_config_value).with(:gce_zone).and_return("test_zone")
|
33
|
+
expect(tester).to receive(:locate_config_value).with(:request_timeout).and_return(123)
|
34
|
+
expect(tester).to receive(:locate_config_value).with(:request_refresh_rate).and_return(321)
|
35
|
+
expect(tester).to receive(:locate_config_value).with(:max_pages).and_return(456)
|
36
|
+
expect(tester).to receive(:locate_config_value).with(:max_page_size).and_return(654)
|
37
|
+
|
38
|
+
expect(Chef::Knife::Cloud::GoogleService).to receive(:new).with(
|
39
|
+
project: "test_project",
|
40
|
+
zone: "test_zone",
|
41
|
+
wait_time: 123,
|
42
|
+
refresh_rate: 321,
|
43
|
+
max_pages: 456,
|
44
|
+
max_page_size: 654
|
45
|
+
).and_return("service_object")
|
46
|
+
|
47
|
+
expect(tester.create_service_instance).to eq("service_object")
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe '#check_for_missing_config_values' do
|
52
|
+
it "does not raise an exception if all parameters are present" do
|
53
|
+
expect(tester).to receive(:locate_config_value).with(:gce_project).and_return("project")
|
54
|
+
expect(tester).to receive(:locate_config_value).with(:gce_zone).and_return("zone")
|
55
|
+
expect(tester).to receive(:locate_config_value).with(:key1).and_return("value1")
|
56
|
+
expect(tester).to receive(:locate_config_value).with(:key2).and_return("value2")
|
57
|
+
|
58
|
+
expect { tester.check_for_missing_config_values!(:key1, :key2) }.not_to raise_error
|
59
|
+
end
|
60
|
+
|
61
|
+
it "raises an exception if a parameter is missing" do
|
62
|
+
ui = double("ui")
|
63
|
+
expect(tester).to receive(:ui).and_return(ui)
|
64
|
+
expect(tester).to receive(:locate_config_value).with(:gce_project).and_return("project")
|
65
|
+
expect(tester).to receive(:locate_config_value).with(:gce_zone).and_return(nil)
|
66
|
+
expect(tester).to receive(:locate_config_value).with(:key1).and_return("value1")
|
67
|
+
expect(tester).to receive(:locate_config_value).with(:key2).and_return(nil)
|
68
|
+
expect(ui).to receive(:error).with("The following required parameters are missing: gce_zone, key2")
|
69
|
+
expect { tester.check_for_missing_config_values!(:key1, :key2) }.to raise_error(RuntimeError)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe '#private_ip_for' do
|
74
|
+
it "returns the IP address if it exists" do
|
75
|
+
network_interface = double("network_interface", network_ip: "1.2.3.4")
|
76
|
+
server = double("server", network_interfaces: [network_interface])
|
77
|
+
|
78
|
+
expect(tester.private_ip_for(server)).to eq("1.2.3.4")
|
79
|
+
end
|
80
|
+
|
81
|
+
it "returns 'unknown' if the IP cannot be found" do
|
82
|
+
server = double("server")
|
83
|
+
|
84
|
+
expect(server).to receive(:network_interfaces).and_raise(NoMethodError)
|
85
|
+
expect(tester.private_ip_for(server)).to eq("unknown")
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
describe '#public_ip_for' do
|
90
|
+
it "returns the IP address if it exists" do
|
91
|
+
access_config = double("access_config", nat_ip: "4.3.2.1")
|
92
|
+
network_interface = double("network_interface", access_configs: [access_config])
|
93
|
+
server = double("server", network_interfaces: [network_interface])
|
94
|
+
|
95
|
+
expect(tester.public_ip_for(server)).to eq("4.3.2.1")
|
96
|
+
end
|
97
|
+
|
98
|
+
it "returns 'unknown' if the IP cannot be found" do
|
99
|
+
network_interface = double("network_interface")
|
100
|
+
server = double("server", network_interfaces: [network_interface])
|
101
|
+
|
102
|
+
expect(network_interface).to receive(:access_configs).and_raise(NoMethodError)
|
103
|
+
expect(tester.public_ip_for(server)).to eq("unknown")
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
describe '#valid_disk_size?' do
|
108
|
+
it "returns true if the disk is between 10 and 10,000" do
|
109
|
+
expect(tester.valid_disk_size?(50)).to eq(true)
|
110
|
+
end
|
111
|
+
|
112
|
+
it "returns false if the disk is less than 10" do
|
113
|
+
expect(tester.valid_disk_size?(5)).to eq(false)
|
114
|
+
end
|
115
|
+
|
116
|
+
it "returns false if the disk is greater than 10,000" do
|
117
|
+
expect(tester.valid_disk_size?(20_000)).to eq(false)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|