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.
Files changed (111) hide show
  1. checksums.yaml +5 -13
  2. data/.gitignore +1 -0
  3. data/.travis.yml +15 -3
  4. data/CHANGELOG.md +4 -6
  5. data/Gemfile +3 -9
  6. data/README.md +208 -355
  7. data/RELEASE_NOTES.md +8 -17
  8. data/Rakefile +8 -49
  9. data/knife-google.gemspec +20 -17
  10. data/lib/chef/knife/cloud/google_service.rb +491 -0
  11. data/lib/chef/knife/cloud/google_service_helpers.rb +62 -0
  12. data/lib/chef/knife/cloud/google_service_options.rb +58 -0
  13. data/lib/chef/knife/google_disk_create.rb +40 -44
  14. data/lib/chef/knife/google_disk_delete.rb +22 -40
  15. data/lib/chef/knife/google_disk_list.rb +57 -51
  16. data/lib/chef/knife/google_project_quotas.rb +59 -0
  17. data/lib/chef/knife/google_region_list.rb +43 -102
  18. data/lib/chef/knife/google_region_quotas.rb +77 -0
  19. data/lib/chef/knife/google_server_create.rb +224 -505
  20. data/lib/chef/knife/google_server_delete.rb +20 -78
  21. data/lib/chef/knife/google_server_list.rb +42 -53
  22. data/lib/chef/knife/google_server_show.rb +44 -0
  23. data/lib/chef/knife/google_zone_list.rb +39 -50
  24. data/lib/knife-google/version.rb +3 -2
  25. data/spec/cloud/google_service_helpers_spec.rb +120 -0
  26. data/spec/cloud/google_service_spec.rb +832 -0
  27. data/spec/google_disk_create_spec.rb +72 -0
  28. data/spec/google_disk_delete_spec.rb +64 -0
  29. data/spec/google_disk_list_spec.rb +93 -0
  30. data/spec/google_project_quotas_spec.rb +63 -0
  31. data/spec/google_region_list_spec.rb +65 -0
  32. data/spec/google_region_quotas_spec.rb +108 -0
  33. data/spec/google_server_create_spec.rb +177 -0
  34. data/spec/google_server_delete_spec.rb +39 -0
  35. data/spec/google_server_list_spec.rb +77 -0
  36. data/spec/google_server_show_spec.rb +60 -0
  37. data/spec/google_zone_list_spec.rb +59 -0
  38. metadata +91 -114
  39. data/CONTRIB.md +0 -64
  40. data/lib/chef/knife/google_base.rb +0 -76
  41. data/lib/chef/knife/google_project_list.rb +0 -178
  42. data/lib/chef/knife/google_setup.rb +0 -31
  43. data/lib/google/compute.rb +0 -47
  44. data/lib/google/compute/client.rb +0 -216
  45. data/lib/google/compute/config.rb +0 -23
  46. data/lib/google/compute/creatable_resource_collection.rb +0 -55
  47. data/lib/google/compute/deletable_resource_collection.rb +0 -51
  48. data/lib/google/compute/disk.rb +0 -38
  49. data/lib/google/compute/exception.rb +0 -30
  50. data/lib/google/compute/firewall.rb +0 -65
  51. data/lib/google/compute/global_operation.rb +0 -60
  52. data/lib/google/compute/image.rb +0 -29
  53. data/lib/google/compute/listable_resource_collection.rb +0 -33
  54. data/lib/google/compute/machine_type.rb +0 -36
  55. data/lib/google/compute/mixins/utils.rb +0 -58
  56. data/lib/google/compute/network.rb +0 -29
  57. data/lib/google/compute/project.rb +0 -76
  58. data/lib/google/compute/region.rb +0 -31
  59. data/lib/google/compute/region_operation.rb +0 -62
  60. data/lib/google/compute/resource.rb +0 -81
  61. data/lib/google/compute/resource_collection.rb +0 -78
  62. data/lib/google/compute/server.rb +0 -88
  63. data/lib/google/compute/server/attached_disk.rb +0 -39
  64. data/lib/google/compute/server/network_interface.rb +0 -38
  65. data/lib/google/compute/server/network_interface/access_config.rb +0 -35
  66. data/lib/google/compute/server/serial_port_output.rb +0 -31
  67. data/lib/google/compute/snapshot.rb +0 -30
  68. data/lib/google/compute/version.rb +0 -19
  69. data/lib/google/compute/zone.rb +0 -34
  70. data/lib/google/compute/zone_operation.rb +0 -62
  71. data/spec/chef/knife/google_base_spec.rb +0 -46
  72. data/spec/chef/knife/google_disk_create_spec.rb +0 -37
  73. data/spec/chef/knife/google_disk_delete_spec.rb +0 -64
  74. data/spec/chef/knife/google_disk_list_spec.rb +0 -36
  75. data/spec/chef/knife/google_region_list_spec.rb +0 -32
  76. data/spec/chef/knife/google_server_create_spec.rb +0 -138
  77. data/spec/chef/knife/google_server_delete_spec.rb +0 -127
  78. data/spec/chef/knife/google_server_list_spec.rb +0 -39
  79. data/spec/chef/knife/google_setup_spec.rb +0 -24
  80. data/spec/chef/knife/google_zone_list_spec.rb +0 -32
  81. data/spec/data/client.json +0 -14
  82. data/spec/data/compute-v1.json +0 -6734
  83. data/spec/data/disk.json +0 -14
  84. data/spec/data/firewall.json +0 -13
  85. data/spec/data/global_operation.json +0 -36
  86. data/spec/data/image.json +0 -12
  87. data/spec/data/machine_type.json +0 -24
  88. data/spec/data/network.json +0 -10
  89. data/spec/data/project.json +0 -21
  90. data/spec/data/region.json +0 -23
  91. data/spec/data/serial_port_output.json +0 -5
  92. data/spec/data/server.json +0 -46
  93. data/spec/data/snapshot.json +0 -12
  94. data/spec/data/zone.json +0 -22
  95. data/spec/data/zone_operation.json +0 -36
  96. data/spec/google/compute/disk_spec.rb +0 -115
  97. data/spec/google/compute/firewall_spec.rb +0 -129
  98. data/spec/google/compute/global_operation_spec.rb +0 -62
  99. data/spec/google/compute/image_spec.rb +0 -75
  100. data/spec/google/compute/machine_type_spec.rb +0 -53
  101. data/spec/google/compute/network_spec.rb +0 -68
  102. data/spec/google/compute/project_spec.rb +0 -71
  103. data/spec/google/compute/region_spec.rb +0 -51
  104. data/spec/google/compute/server_spec.rb +0 -118
  105. data/spec/google/compute/snapshot_spec.rb +0 -57
  106. data/spec/google/compute/zone_operation_spec.rb +0 -62
  107. data/spec/google/compute/zone_spec.rb +0 -51
  108. data/spec/spec_helper.rb +0 -45
  109. data/spec/support/mocks.rb +0 -62
  110. data/spec/support/resource_examples.rb +0 -70
  111. data/spec/support/spec_google_base.rb +0 -60
@@ -1,4 +1,8 @@
1
- # Copyright 2013 Google Inc. All Rights Reserved.
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 'chef/knife/google_base'
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 GoogleServerDelete < Knife
20
-
21
- include Knife::GoogleBase
22
-
23
- deps do
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
- option :purge,
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
- @instances = []
73
- @name_args.each do |instance_name|
74
- begin
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
- # Copyright 2013 Google Inc. All Rights Reserved.
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
- include Knife::GoogleBase
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
- banner "knife google server list -Z ZONE (options)"
26
+ class Chef::Knife::Cloud
27
+ class GoogleServerList < ServerListCommand
28
+ include GoogleServiceHelpers
29
+ include GoogleServiceOptions
24
30
 
25
- option :zone,
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
- def run
31
- $stdout.sync = true
32
-
33
- begin
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
- instance_label = ['name', 'type', 'public ip', 'private ip', 'disks', 'zone', 'status']
44
- instance_list = (instance_label.map {|label| ui.color(label, :bold)}).flatten.compact
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
- output_column_count = instance_list.length
48
+ @sort_by_field = "name"
49
+ end
47
50
 
48
- client.instances.list(:zone=>zone.name).each do |instance|
49
- instance_list << instance.name
50
- instance_list << selflink2name(instance.machine_type.to_s)
51
- instance_list << public_ips(instance).join(',')
52
- instance_list << private_ips(instance).join(',')
53
- instance_list << disks(instance).join(',')
54
- instance_list << selflink2name(instance.zone.to_s)
55
- instance_list << begin
56
- status = instance.status.downcase
57
- case status
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
- if instance_list.count > 8 # This condition checks if there are any servers available. The first 8 values are the Labels.
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
- # Copyright 2013 Google Inc. All Rights Reserved.
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
- class Chef
19
- class Knife
20
- class GoogleZoneList < Knife
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
- include Knife::GoogleBase
25
+ class Chef::Knife::Cloud
26
+ class GoogleZoneList < ResourceListCommand
27
+ include GoogleServiceHelpers
28
+ include GoogleServiceOptions
23
29
 
24
- banner "knife google zone list (options)"
30
+ banner "knife google zone list"
25
31
 
26
- def run
27
- $stdout.sync = true
32
+ def validate_params!
33
+ check_for_missing_config_values!
34
+ super
35
+ end
28
36
 
29
- zone_list = [
30
- ui.color("name", :bold),
31
- ui.color('status', :bold),
32
- ui.color('deprecation', :bold),
33
- ui.color('maintainance window', :bold)].flatten.compact
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
- output_column_count = zone_list.length
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
- client.zones.list.each do |zone|
38
- zone_list << zone.name
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
@@ -1,4 +1,4 @@
1
- # Copyright 2013 Google Inc. All Rights Reserved.
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 = "1.3.1"
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