knife-google 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. data/.gitignore +22 -0
  2. data/CONTRIB.md +64 -0
  3. data/Gemfile +11 -0
  4. data/README.md +287 -0
  5. data/Rakefile +53 -0
  6. data/knife-google.gemspec +26 -0
  7. data/lib/chef/knife/google_base.rb +39 -68
  8. data/lib/chef/knife/google_disk_create.rb +60 -0
  9. data/lib/chef/knife/google_disk_delete.rb +60 -0
  10. data/lib/chef/knife/google_disk_list.rb +75 -0
  11. data/lib/chef/knife/google_server_create.rb +273 -184
  12. data/lib/chef/knife/google_server_delete.rb +74 -32
  13. data/lib/chef/knife/google_server_list.rb +45 -64
  14. data/lib/chef/knife/google_setup.rb +31 -0
  15. data/lib/chef/knife/google_zone_list.rb +78 -0
  16. data/lib/google/compute.rb +46 -0
  17. data/lib/google/compute/client.rb +188 -0
  18. data/lib/google/compute/config.rb +23 -0
  19. data/lib/google/compute/creatable_resource_collection.rb +38 -0
  20. data/lib/google/compute/deletable_resource_collection.rb +51 -0
  21. data/lib/google/compute/disk.rb +40 -0
  22. data/lib/google/compute/exception.rb +28 -0
  23. data/lib/google/compute/firewall.rb +65 -0
  24. data/lib/google/compute/global_operation.rb +60 -0
  25. data/lib/google/compute/image.rb +30 -0
  26. data/lib/google/compute/kernel.rb +20 -0
  27. data/lib/google/compute/listable_resource_collection.rb +33 -0
  28. data/lib/google/compute/machine_type.rb +36 -0
  29. data/lib/google/compute/mixins/utils.rb +58 -0
  30. data/lib/google/compute/network.rb +29 -0
  31. data/lib/google/compute/project.rb +76 -0
  32. data/lib/google/compute/resource.rb +81 -0
  33. data/lib/google/compute/resource_collection.rb +78 -0
  34. data/lib/google/compute/server.rb +87 -0
  35. data/lib/google/compute/server/attached_disk.rb +39 -0
  36. data/lib/google/compute/server/network_interface.rb +38 -0
  37. data/lib/google/compute/server/network_interface/access_config.rb +35 -0
  38. data/lib/google/compute/server/serial_port_output.rb +31 -0
  39. data/lib/google/compute/snapshot.rb +30 -0
  40. data/lib/google/compute/version.rb +19 -0
  41. data/lib/google/compute/zone.rb +32 -0
  42. data/lib/google/compute/zone_operation.rb +60 -0
  43. data/lib/knife-google/version.rb +18 -2
  44. data/spec/chef/knife/google_base_spec.rb +46 -0
  45. data/spec/chef/knife/google_disk_create_spec.rb +36 -0
  46. data/spec/chef/knife/google_disk_delete_spec.rb +65 -0
  47. data/spec/chef/knife/google_disk_list_spec.rb +36 -0
  48. data/spec/chef/knife/google_server_create_spec.rb +84 -0
  49. data/spec/chef/knife/google_server_delete_spec.rb +105 -0
  50. data/spec/chef/knife/google_server_list_spec.rb +39 -0
  51. data/spec/chef/knife/google_setup_spec.rb +25 -0
  52. data/spec/chef/knife/google_zone_list_spec.rb +32 -0
  53. data/spec/data/client.json +14 -0
  54. data/spec/data/compute-v1beta14.json +3386 -0
  55. data/spec/data/disk.json +15 -0
  56. data/spec/data/firewall.json +13 -0
  57. data/spec/data/global_operation.json +36 -0
  58. data/spec/data/image.json +12 -0
  59. data/spec/data/kernel.json +15 -0
  60. data/spec/data/machine_type.json +24 -0
  61. data/spec/data/network.json +10 -0
  62. data/spec/data/project.json +21 -0
  63. data/spec/data/serial_port_output.json +5 -0
  64. data/spec/data/server.json +46 -0
  65. data/spec/data/snapshot.json +12 -0
  66. data/spec/data/zone.json +30 -0
  67. data/spec/data/zone_operation.json +36 -0
  68. data/spec/google/compute/disk_spec.rb +105 -0
  69. data/spec/google/compute/firewall_spec.rb +128 -0
  70. data/spec/google/compute/global_operation_spec.rb +62 -0
  71. data/spec/google/compute/image_spec.rb +75 -0
  72. data/spec/google/compute/kernel_spec.rb +49 -0
  73. data/spec/google/compute/machine_type_spec.rb +53 -0
  74. data/spec/google/compute/network_spec.rb +68 -0
  75. data/spec/google/compute/project_spec.rb +71 -0
  76. data/spec/google/compute/server_spec.rb +125 -0
  77. data/spec/google/compute/snapshot_spec.rb +69 -0
  78. data/spec/google/compute/zone_operation_spec.rb +62 -0
  79. data/spec/google/compute/zone_spec.rb +50 -0
  80. data/spec/spec_helper.rb +44 -0
  81. data/spec/support/mocks.rb +62 -0
  82. data/spec/support/resource_examples.rb +70 -0
  83. data/spec/support/spec_google_base.rb +56 -0
  84. metadata +121 -31
  85. data/README.rdoc +0 -96
@@ -0,0 +1,29 @@
1
+ # Copyright 2013 Google Inc. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Google
16
+ module Compute
17
+ class Network < Resource
18
+
19
+ attr_reader :ip_v4_range, :gateway_ipv4
20
+
21
+ def from_hash(data)
22
+ super(data)
23
+ @ip_v4_range = data["IPv4Range"]
24
+ @gateway_ipv4 = data["gatewayIPv4"]
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,76 @@
1
+ # Copyright 2013 Google Inc. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Google
16
+ module Compute
17
+ class Project < Resource
18
+
19
+ attr_reader :common_instance_metadata, :quotas, :external_ip_addresses
20
+
21
+ def from_hash(data)
22
+ super(data)
23
+ @common_instance_metadata = data["commonInstanceMetadata"]
24
+ @quotas = data["quotas"]
25
+ @external_ip_addresses = data["externalIpAddresses"]
26
+ end
27
+
28
+ def set_common_instance_metadata(metadata)
29
+ data = @dispatcher.dispatch(:api_method=>api_resource.set_common_instance_metadata,
30
+ :parameters=>{:project=>name},
31
+ :body_object=>{
32
+ "kind" => "compute#metadata",
33
+ "items" => metadata
34
+ }
35
+ )
36
+ update!
37
+ end
38
+
39
+ def add_common_instance_metadata!(options)
40
+ temp_metadata = @common_instance_metadata["items"]
41
+ options.keys.each do |k|
42
+ if common_instance_metadata["items"].any?{|metadata| metadata["key"] == k}
43
+ raise ParameterValidation, "Key:'#{k}' already exist in common server metadata"
44
+ else
45
+ temp_metadata << {'key'=>k ,'value'=> options[k]}
46
+ end
47
+ end
48
+ set_common_instance_metadata(temp_metadata)
49
+ end
50
+
51
+ def remove_common_instance_metadata!(options)
52
+ temp_metadata = common_instance_metadata["items"]
53
+ options.keys.each do |k|
54
+ unless common_instance_metadata["items"].any?{|metadata| metadata['key'] == k}
55
+ raise ParameterValidation, "Key:'#{k}' does not exist in common server metadata"
56
+ else
57
+ temp_metadata.delete({'key'=>k, 'value'=> options[k]})
58
+ end
59
+ end
60
+ set_common_instance_metadata(temp_metadata)
61
+ end
62
+
63
+ def update_common_instance_metadata!(options)
64
+ temp_metadata = @common_instance_metadata["items"]
65
+ options.keys.each do |k|
66
+ if common_instance_metadata["items"].any?{|metadata| metadata["key"] == k}
67
+ metadata = common_instance_metadata["items"].select{|metadata| metadata["key"] == k}.first
68
+ temp_metadata.delete(metadata)
69
+ end
70
+ temp_metadata << { 'key' => k, 'value'=> options[k]}
71
+ end
72
+ set_common_instance_metadata(temp_metadata)
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,81 @@
1
+ # Copyright 2013 Google Inc. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'google/compute/exception'
16
+ require 'multi_json'
17
+ require 'google/compute/mixins/utils'
18
+
19
+ module Google
20
+ module Compute
21
+ class Resource
22
+ include Utils
23
+
24
+ attr_reader :kind, :id, :creation_timestamp
25
+ attr_reader :name, :description, :self_link, :dispatcher
26
+
27
+ def initialize(resource_data)
28
+ from_hash(resource_data)
29
+ end
30
+
31
+ def to_s
32
+ name
33
+ end
34
+
35
+ def project
36
+ if self.is_a?(Google::Compute::Project)
37
+ name
38
+ else
39
+ self_link=~Regexp.new('/projects/(.*?)/')
40
+ $1
41
+ end
42
+ end
43
+
44
+ def api_resource
45
+ # MacineType => machine_types
46
+ # Servers => instances
47
+ collection_name = self.class.name.split('::').last.snake_case + "s"
48
+ if collection_name == "servers"
49
+ @dispatcher.compute.send("instances")
50
+ else
51
+ @dispatcher.compute.send(collection_name)
52
+ end
53
+ end
54
+
55
+ def type
56
+ kind.split('#').last
57
+ end
58
+
59
+ def self.class_name
60
+ name.split('::').last.downcase
61
+ end
62
+
63
+ def update!
64
+ data= @dispatcher.dispatch(:api_method=>api_resource.get,:parameters=>{type =>name})
65
+ from_hash(data.merge(:dispatcher => @dispatcher))
66
+ end
67
+
68
+ def from_hash(resource_data)
69
+ @kind = resource_data["kind"]
70
+ @name = resource_data["name"]
71
+ @self_link = resource_data["selfLink"]
72
+ @id = resource_data["id"].to_i
73
+ @description = resource_data["description"]
74
+ unless resource_data["creationTimestamp"].nil?
75
+ @creation_timestamp = Time.parse(resource_data["creationTimestamp"])
76
+ end
77
+ @dispatcher = resource_data[:dispatcher]
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,78 @@
1
+ # Copyright 2013 Google Inc. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Google
16
+ module Compute
17
+ class ResourceCollection
18
+
19
+ attr_reader :dispatcher
20
+
21
+ def initialize(options)
22
+ @dispatcher = options[:dispatcher]
23
+ @resource_class= options[:resource_class]
24
+ end
25
+
26
+ def get(options={})
27
+ if options.is_a?(String)
28
+ options = name_to_hash(options)
29
+ elsif options.is_a?(Google::Compute::Resource)
30
+ options = name_to_hash(options.name)
31
+ elsif options.is_a?(Hash) && options.has_key?(:name)
32
+ options.merge!(name_to_hash(options[:name]))
33
+ options.delete(:name)
34
+ elsif options.is_a?(Hash) && options.has_key?(@resource_class.class_name.to_sym)
35
+ else
36
+ raise ParameterValidation, "argument must be :" +
37
+ "name of the resource, or "+
38
+ "a resource object, or " +
39
+ "a hash with a key as resource name, eg ({:disk=>'xxx'}) , or " +
40
+ "a hash with a :name key " +
41
+ "you have passed '#{options.inspect}'"
42
+ end
43
+ if options.is_a?(Hash) && options.has_key?("server")
44
+ options[:instance] = options["server"]
45
+ options.delete("server")
46
+ end
47
+ data = @dispatcher.dispatch(:api_method=>api_resource.get, :parameters=>options)
48
+ @resource_class.new(data.merge(:dispatcher=>@dispatcher))
49
+ end
50
+
51
+ def name_to_hash(resource_name)
52
+ {downcase_first_letter(resource_class_name) => resource_name}
53
+ end
54
+
55
+ def downcase_first_letter(word)
56
+ word.sub(/^[A-Z]/) {|f| f.downcase}
57
+ end
58
+
59
+ def resource_class_name
60
+ name = @resource_class.name.split('::').last
61
+ end
62
+
63
+ def project
64
+ @dispatcher.project
65
+ end
66
+
67
+ def api_resource
68
+ # Servers => instances
69
+ collection_name = resource_class_name.snake_case + "s"
70
+ if collection_name == "servers"
71
+ @dispatcher.compute.send("instances")
72
+ else
73
+ @dispatcher.compute.send(collection_name)
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,87 @@
1
+ # Copyright 2013 Google Inc. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'google/compute/server/serial_port_output'
16
+ require 'google/compute/server/network_interface'
17
+ require 'google/compute/server/attached_disk'
18
+
19
+ module Google
20
+ module Compute
21
+ class Server < Resource
22
+
23
+ attr_reader :tags, :image, :machine_type, :status, :status_message, :zone
24
+ attr_reader :network_interfaces, :disks, :metadata, :service_accounts
25
+
26
+ def from_hash(data)
27
+ super(data)
28
+ @tags = data["tags"]
29
+ @image = data["image"]
30
+ @machine_type = data["machineType"]
31
+ @status = data["status"]
32
+ @status_message = data["statusMessage"]
33
+ @zone = data["zone"]
34
+ @network_interfaces = []
35
+ if data["networkInterfaces"] || data["networkInterfaces"].is_a?(Array)
36
+ data["networkInterfaces"].each do |interface|
37
+ @network_interfaces << NetworkInterface.new(interface)
38
+ end
39
+ end
40
+ @disks = []
41
+ if data["disks"] || data["disks"].is_a?(Array)
42
+ data["disks"].each do |disk_data|
43
+ @disks << AttachedDisk.new(disk_data)
44
+ end
45
+ end
46
+ @metadata = data["metadata"]
47
+ @service_accounts = data["service_accounts"]
48
+ end
49
+
50
+ def serial_port_output
51
+ @serial_port_output ||= begin
52
+ data = @dispatcher.dispatch(:api_method => api_resource.get_serial_port_output,
53
+ :parameters=>{ :project =>project, :zone=>zone,
54
+ :instance => name
55
+ })
56
+ SerialPortOutput.new(data)
57
+ end
58
+ end
59
+
60
+ def add_access_config(options={})
61
+ interface = options.delete(:network_interface)
62
+ interface = interface.name if interface.is_a?(Network::Interface)
63
+ body_object = options[:access_config].to_hash
64
+ data = @dispatcher.dispatch(:api_method => api_resource.add_access_config,
65
+ :parameters=>{ :project => project,
66
+ :instance => name,
67
+ :network_interface => interface
68
+ },
69
+ :body_object => body_object)
70
+ ZoneOperation.new(data.merge!(:dispatcher=>@dispatcher))
71
+ end
72
+
73
+ def delete_access_config(options={})
74
+ interface = options.delete(:network_interface)
75
+ interface = interface.name if interface.is_a?(Network::Interface)
76
+ access_config = options[:access_config].to_hash if options[:access_config].is_a?(AccessConfig)
77
+ data = @dispatcher.dispatch(:api_method => api_resource.delete_access_config,
78
+ :parameters=>{ :project =>project,
79
+ :instance => name,
80
+ :network_interface => interface,
81
+ :access_config => access_configs
82
+ })
83
+ ZoneOperation.new(data.merge!(:dispatcher=>@dispatcher))
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,39 @@
1
+ # Copyright 2013 Google Inc. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ # Google compute engine, attached disk of an instance
16
+
17
+ require 'google/compute/mixins/utils'
18
+
19
+ module Google
20
+ module Compute
21
+
22
+ class AttachedDisk
23
+ include Utils
24
+
25
+ attr_reader :kind, :type, :mode, :source
26
+ attr_reader :device_name, :index, :boot
27
+
28
+ def initialize(data)
29
+ @kind = data["kind"]
30
+ @type = data["type"]
31
+ @mode= data["mode"]
32
+ @source = data["source"]
33
+ @device_name = data["deviceName"]
34
+ @index = data["index"]
35
+ @boot = data["boot"]
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,38 @@
1
+ # Copyright 2013 Google Inc. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'google/compute/server/network_interface/access_config'
16
+ require 'google/compute/mixins/utils'
17
+
18
+ module Google
19
+ module Compute
20
+ class NetworkInterface
21
+ include Utils
22
+
23
+ attr_reader :name, :network, :network_ip, :access_configs
24
+
25
+ def initialize(data)
26
+ @name = data["name"]
27
+ @network = data["network"]
28
+ @network_ip = data["networkIP"]
29
+ @access_configs=[]
30
+ if data["accessConfigs"] && data["accessConfigs"].is_a?(Array)
31
+ data["accessConfigs"].each do |config|
32
+ @access_configs << AccessConfig.new(config)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,35 @@
1
+ # Copyright 2013 Google Inc. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'google/compute/mixins/utils'
16
+
17
+ module Google
18
+ module Compute
19
+ class NetworkInterface
20
+ class AccessConfig
21
+
22
+ include Utils
23
+
24
+ attr_reader :kind, :name, :type, :nat_ip
25
+
26
+ def initialize(data)
27
+ @kind = data["kind"]
28
+ @name = data["name"]
29
+ @type = data["type"]
30
+ @nat_ip = data["natIP"]
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end