foreman_google 3.0.8 → 3.1.1

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 (67) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascripts/foreman_google/locale/cs_CZ/foreman_google.js +7 -1
  3. data/app/assets/javascripts/foreman_google/locale/de/foreman_google.js +7 -1
  4. data/app/assets/javascripts/foreman_google/locale/el/foreman_google.js +7 -1
  5. data/app/assets/javascripts/foreman_google/locale/en/foreman_google.js +7 -1
  6. data/app/assets/javascripts/foreman_google/locale/en_GB/foreman_google.js +7 -1
  7. data/app/assets/javascripts/foreman_google/locale/es/foreman_google.js +7 -1
  8. data/app/assets/javascripts/foreman_google/locale/fr/foreman_google.js +8 -2
  9. data/app/assets/javascripts/foreman_google/locale/it/foreman_google.js +7 -1
  10. data/app/assets/javascripts/foreman_google/locale/ja/foreman_google.js +8 -2
  11. data/app/assets/javascripts/foreman_google/locale/ka/foreman_google.js +7 -1
  12. data/app/assets/javascripts/foreman_google/locale/ko/foreman_google.js +8 -2
  13. data/app/assets/javascripts/foreman_google/locale/pl/foreman_google.js +7 -1
  14. data/app/assets/javascripts/foreman_google/locale/pt_BR/foreman_google.js +7 -1
  15. data/app/assets/javascripts/foreman_google/locale/ru/foreman_google.js +7 -1
  16. data/app/assets/javascripts/foreman_google/locale/sv_SE/foreman_google.js +7 -1
  17. data/app/assets/javascripts/foreman_google/locale/zh_CN/foreman_google.js +8 -2
  18. data/app/assets/javascripts/foreman_google/locale/zh_TW/foreman_google.js +7 -1
  19. data/app/lib/google_cloud_compute/compute_attributes.rb +3 -1
  20. data/app/models/foreman_google/gce.rb +2 -4
  21. data/app/models/foreman_google/google_compute.rb +2 -2
  22. data/lib/foreman_google/engine.rb +1 -1
  23. data/lib/foreman_google/version.rb +1 -1
  24. data/locale/cs_CZ/LC_MESSAGES/foreman_google.mo +0 -0
  25. data/locale/cs_CZ/foreman_google.po +7 -1
  26. data/locale/de/LC_MESSAGES/foreman_google.mo +0 -0
  27. data/locale/de/foreman_google.po +7 -1
  28. data/locale/el/LC_MESSAGES/foreman_google.mo +0 -0
  29. data/locale/el/foreman_google.po +7 -1
  30. data/locale/en/LC_MESSAGES/foreman_google.mo +0 -0
  31. data/locale/en/foreman_google.po +7 -1
  32. data/locale/en_GB/LC_MESSAGES/foreman_google.mo +0 -0
  33. data/locale/en_GB/foreman_google.po +7 -1
  34. data/locale/es/LC_MESSAGES/foreman_google.mo +0 -0
  35. data/locale/es/foreman_google.po +7 -1
  36. data/locale/foreman_google.pot +12 -4
  37. data/locale/fr/LC_MESSAGES/foreman_google.mo +0 -0
  38. data/locale/fr/foreman_google.po +9 -3
  39. data/locale/it/LC_MESSAGES/foreman_google.mo +0 -0
  40. data/locale/it/foreman_google.po +7 -1
  41. data/locale/ja/LC_MESSAGES/foreman_google.mo +0 -0
  42. data/locale/ja/foreman_google.po +9 -3
  43. data/locale/ka/LC_MESSAGES/foreman_google.mo +0 -0
  44. data/locale/ka/foreman_google.po +7 -1
  45. data/locale/ko/LC_MESSAGES/foreman_google.mo +0 -0
  46. data/locale/ko/foreman_google.po +9 -3
  47. data/locale/pl/LC_MESSAGES/foreman_google.mo +0 -0
  48. data/locale/pl/foreman_google.po +7 -1
  49. data/locale/pt_BR/LC_MESSAGES/foreman_google.mo +0 -0
  50. data/locale/pt_BR/foreman_google.po +7 -1
  51. data/locale/ru/LC_MESSAGES/foreman_google.mo +0 -0
  52. data/locale/ru/foreman_google.po +7 -1
  53. data/locale/sv_SE/LC_MESSAGES/foreman_google.mo +0 -0
  54. data/locale/sv_SE/foreman_google.po +7 -1
  55. data/locale/zh_CN/LC_MESSAGES/foreman_google.mo +0 -0
  56. data/locale/zh_CN/foreman_google.po +9 -3
  57. data/locale/zh_TW/LC_MESSAGES/foreman_google.mo +0 -0
  58. data/locale/zh_TW/foreman_google.po +7 -1
  59. data/test/controllers/foreman_google/api/v2/compute_resources_extensions_test.rb +71 -0
  60. data/test/models/concerns/foreman_google/host_managed_extensions_test.rb +46 -0
  61. data/test/models/foreman_google/gce_test.rb +275 -3
  62. data/test/models/foreman_google/google_compute_test.rb +193 -4
  63. data/test/unit/foreman_google/google_compute_adapter_test.rb +59 -7
  64. data/test/unit/google_cloud_compute/compute_attributes_test.rb +235 -0
  65. data/test/unit/google_cloud_compute/compute_collection_test.rb +78 -0
  66. data/test/unit/google_extensions/attached_disk_test.rb +35 -0
  67. metadata +10 -2
@@ -0,0 +1,235 @@
1
+ require 'test_google_helper'
2
+
3
+ module GoogleCloudCompute
4
+ class ComputeAttributesTest < GoogleTestCase
5
+ let(:client) { mock('GoogleAdapter') }
6
+ subject { GoogleCloudCompute::ComputeAttributes.new(client) }
7
+
8
+ setup do
9
+ client.stubs(:project_id).returns('my-project')
10
+ end
11
+
12
+ describe '#for_new' do
13
+ describe 'name handling' do
14
+ it 'generates a default name with foreman- prefix when name is nil' do
15
+ result = subject.for_new({})
16
+ assert_match(/\Aforeman-\d+\z/, result[:name])
17
+ assert_equal result[:name], result[:hostname]
18
+ end
19
+
20
+ it 'parameterizes the provided name' do
21
+ result = subject.for_new(name: 'My VM Name')
22
+ assert_equal 'my-vm-name', result[:name]
23
+ assert_equal 'my-vm-name', result[:hostname]
24
+ end
25
+ end
26
+
27
+ describe 'network handling' do
28
+ it 'defaults to default network' do
29
+ result = subject.for_new({})
30
+ assert_equal 'default', result[:network]
31
+ end
32
+
33
+ it 'uses custom network name' do
34
+ result = subject.for_new(network: 'custom-net')
35
+ assert_equal 'custom-net', result[:network]
36
+ end
37
+
38
+ it 'builds full URL without external IP' do
39
+ result = subject.for_new(network: 'default')
40
+ expected_url = 'https://compute.googleapis.com/compute/v1/projects/my-project/global/networks/default'
41
+ assert_equal expected_url, result[:network_interfaces][0][:network]
42
+ end
43
+
44
+ it 'adds NAT access config with external IP' do
45
+ result = subject.for_new(associate_external_ip: '1')
46
+ nic = result[:network_interfaces][0]
47
+ assert_equal 'global/networks/default', nic[:network]
48
+ assert_equal [{ name: 'External NAT', type: 'ONE_TO_ONE_NAT' }], nic[:access_configs]
49
+ end
50
+
51
+ it 'uses custom network interfaces with external IP' do
52
+ result = subject.for_new(
53
+ associate_external_ip: '1',
54
+ network_interfaces: [{ network: 'global/networks/my-net' }]
55
+ )
56
+ nic = result[:network_interfaces][0]
57
+ assert_equal 'global/networks/my-net', nic[:network]
58
+ assert_equal [{ name: 'External NAT', type: 'ONE_TO_ONE_NAT' }], nic[:access_configs]
59
+ end
60
+ end
61
+
62
+ describe 'associate_external_ip boolean casting' do
63
+ it 'casts string 1 to true' do
64
+ result = subject.for_new(associate_external_ip: '1')
65
+ assert result[:associate_external_ip]
66
+ end
67
+
68
+ it 'casts string 0 to false' do
69
+ result = subject.for_new(associate_external_ip: '0')
70
+ assert_not result[:associate_external_ip]
71
+ end
72
+
73
+ it 'defaults to false when absent' do
74
+ result = subject.for_new({})
75
+ assert_not result[:associate_external_ip]
76
+ end
77
+ end
78
+
79
+ describe 'volume construction' do
80
+ it 'creates a default 20 GB disk when no volumes' do
81
+ result = subject.for_new({})
82
+ assert_equal 1, result[:volumes].length
83
+ assert_equal 20, result[:volumes].first.disk_size_gb
84
+ end
85
+
86
+ it 'creates volumes with custom size' do
87
+ result = subject.for_new(volumes: [{ size_gb: '50' }])
88
+ assert_equal 50, result[:volumes].first.disk_size_gb
89
+ end
90
+
91
+ it 'sets source image on first volume when image_id is provided' do
92
+ image = OpenStruct.new(id: 1, self_link: 'projects/my-project/global/images/centos-7')
93
+ client.stubs(:image).with(1).returns(image)
94
+
95
+ result = subject.for_new(
96
+ volumes: [{ size_gb: '20' }, { size_gb: '30' }],
97
+ image_id: '1'
98
+ )
99
+
100
+ assert_equal 'projects/my-project/global/images/centos-7', result[:volumes][0].source
101
+ assert_empty result[:volumes][1].source
102
+ end
103
+
104
+ it 'names disks sequentially' do
105
+ result = subject.for_new(name: 'test-vm', volumes: [{ size_gb: '20' }, { size_gb: '30' }])
106
+ assert_equal 'test-vm-disk1', result[:volumes][0].device_name
107
+ assert_equal 'test-vm-disk2', result[:volumes][1].device_name
108
+ end
109
+
110
+ it 'accepts disk_size_gb as an alternative key' do
111
+ result = subject.for_new(volumes: [{ disk_size_gb: '40' }])
112
+ assert_equal 40, result[:volumes].first.disk_size_gb
113
+ end
114
+ end
115
+
116
+ describe 'metadata construction' do
117
+ it 'includes ssh-keys' do
118
+ result = subject.for_new(username: 'gce_user', public_key: 'ssh-rsa AAAA')
119
+ ssh_item = result[:metadata][:items].find { |i| i[:key] == 'ssh-keys' }
120
+ assert_equal 'gce_user:ssh-rsa AAAA', ssh_item[:value]
121
+ end
122
+
123
+ it 'includes user-data when present' do
124
+ result = subject.for_new(username: 'user', public_key: 'key', user_data: '#cloud-config')
125
+ ud_item = result[:metadata][:items].find { |i| i[:key] == 'user-data' }
126
+ assert_equal '#cloud-config', ud_item[:value]
127
+ end
128
+
129
+ it 'omits user-data when blank' do
130
+ result = subject.for_new(username: 'user', public_key: 'key')
131
+ keys = result[:metadata][:items].map { |i| i[:key] }
132
+ assert_not_includes keys, 'user-data'
133
+ end
134
+ end
135
+
136
+ it 'passes through machine_type and image_id' do
137
+ result = subject.for_new(machine_type: 'e2-micro', image_id: '42')
138
+ assert_equal 'e2-micro', result[:machine_type]
139
+ assert_equal '42', result[:image_id]
140
+ end
141
+ end
142
+
143
+ describe '#for_create' do
144
+ let(:instance) do
145
+ vol1 = Google::Cloud::Compute::V1::AttachedDisk.new(device_name: 'vm-disk1')
146
+ vol2 = Google::Cloud::Compute::V1::AttachedDisk.new(device_name: 'vm-disk2')
147
+ OpenStruct.new(
148
+ name: 'test-vm',
149
+ zone: 'us-east1-b',
150
+ machine_type: 'e2-micro',
151
+ volumes: [vol1, vol2],
152
+ network_interfaces: [{ network: 'global/networks/default' }],
153
+ metadata: { items: [{ key: 'ssh-keys', value: 'user:key' }] }
154
+ )
155
+ end
156
+
157
+ it 'builds machine_type URL from zone' do
158
+ result = subject.for_create(instance)
159
+ assert_equal 'zones/us-east1-b/machineTypes/e2-micro', result[:machine_type]
160
+ end
161
+
162
+ it 'builds disk source paths' do
163
+ result = subject.for_create(instance)
164
+ assert_equal 'zones/us-east1-b/disks/vm-disk1', result[:disks][0][:source]
165
+ assert_equal 'zones/us-east1-b/disks/vm-disk2', result[:disks][1][:source]
166
+ end
167
+
168
+ it 'marks first disk as boot' do
169
+ result = subject.for_create(instance)
170
+ assert result[:disks][0][:boot]
171
+ assert_not result[:disks][1][:boot]
172
+ end
173
+
174
+ it 'includes network_interfaces and metadata' do
175
+ result = subject.for_create(instance)
176
+ assert_equal instance.network_interfaces, result[:network_interfaces]
177
+ assert_equal instance.metadata, result[:metadata]
178
+ end
179
+
180
+ it 'includes name' do
181
+ result = subject.for_create(instance)
182
+ assert_equal 'test-vm', result[:name]
183
+ end
184
+ end
185
+
186
+ describe '#for_instance' do
187
+ let(:nics) do
188
+ [OpenStruct.new(network: 'projects/my-project/global/networks/custom', network_i_p: '10.0.0.1')]
189
+ end
190
+ let(:instance) do
191
+ OpenStruct.new(
192
+ name: 'existing-vm',
193
+ creation_timestamp: Time.zone.parse('2024-01-15 10:00:00'),
194
+ zone: 'projects/my-project/zones/us-west1-a',
195
+ machine_type: 'zones/us-west1-a/machineTypes/n1-standard-1',
196
+ network_interfaces: nics,
197
+ disks: [OpenStruct.new(device_name: 'disk1')],
198
+ metadata: { items: [] }
199
+ )
200
+ end
201
+
202
+ it 'extracts zone_name from full zone URL' do
203
+ result = subject.for_instance(instance)
204
+ assert_equal 'us-west1-a', result[:zone_name]
205
+ end
206
+
207
+ it 'extracts network name from full URL' do
208
+ result = subject.for_instance(instance)
209
+ assert_equal 'custom', result[:network]
210
+ end
211
+
212
+ it 'converts creation_timestamp to DateTime' do
213
+ result = subject.for_instance(instance)
214
+ assert_kind_of DateTime, result[:creation_timestamp]
215
+ end
216
+
217
+ it 'passes through name and hostname' do
218
+ result = subject.for_instance(instance)
219
+ assert_equal 'existing-vm', result[:name]
220
+ assert_equal 'existing-vm', result[:hostname]
221
+ end
222
+
223
+ it 'passes through disks as volumes and metadata' do
224
+ result = subject.for_instance(instance)
225
+ assert_equal instance.disks, result[:volumes]
226
+ assert_equal instance.metadata, result[:metadata]
227
+ end
228
+
229
+ it 'passes through machine_type' do
230
+ result = subject.for_instance(instance)
231
+ assert_equal instance.machine_type, result[:machine_type]
232
+ end
233
+ end
234
+ end
235
+ end
@@ -0,0 +1,78 @@
1
+ require 'test_google_helper'
2
+
3
+ module GoogleCloudCompute
4
+ class ComputeCollectionTest < GoogleTestCase
5
+ let(:client) { mock('GoogleAdapter') }
6
+ let(:zone) { 'us-east1-b' }
7
+
8
+ setup do
9
+ client.stubs(:project_id).returns('project_id')
10
+ end
11
+
12
+ describe 'with instances' do
13
+ let(:instances) do
14
+ nics = [OpenStruct.new(access_configs: [OpenStruct.new(nat_i_p: '1.2.3.4')],
15
+ network: 'projects/project_id/global/networks/default', network_i_p: '10.0.0.1')]
16
+ [
17
+ OpenStruct.new(id: 1, name: 'vm-1', network_interfaces: nics,
18
+ creation_timestamp: Time.zone.now, zone: zone, machine_type: 'machineTypes/e2-micro'),
19
+ OpenStruct.new(id: 2, name: 'vm-2', network_interfaces: nics,
20
+ creation_timestamp: Time.zone.now, zone: zone, machine_type: 'machineTypes/e2-small'),
21
+ ]
22
+ end
23
+
24
+ setup do
25
+ client.stubs(:instances).with(zone).returns(instances)
26
+ end
27
+
28
+ it 'wraps instances as GoogleCompute objects' do
29
+ collection = GoogleCloudCompute::ComputeCollection.new(client, zone)
30
+ collection.each do |vm|
31
+ assert_kind_of ForemanGoogle::GoogleCompute, vm
32
+ end
33
+ end
34
+
35
+ it 'iterates via each' do
36
+ collection = GoogleCloudCompute::ComputeCollection.new(client, zone)
37
+ names = collection.map(&:name)
38
+ assert_equal %w[vm-1 vm-2], names
39
+ end
40
+
41
+ it 'returns all VMs via all' do
42
+ collection = GoogleCloudCompute::ComputeCollection.new(client, zone)
43
+ assert_equal 2, collection.all.length
44
+ end
45
+
46
+ it 'is Enumerable' do
47
+ collection = GoogleCloudCompute::ComputeCollection.new(client, zone)
48
+ assert_kind_of Enumerable, collection
49
+ assert_equal 2, collection.count
50
+ end
51
+ end
52
+
53
+ describe 'with empty instance list' do
54
+ setup do
55
+ client.stubs(:instances).with(zone).returns([])
56
+ end
57
+
58
+ it 'returns empty collection' do
59
+ collection = GoogleCloudCompute::ComputeCollection.new(client, zone)
60
+ assert_empty collection.all
61
+ end
62
+
63
+ it 'iterates zero times' do
64
+ collection = GoogleCloudCompute::ComputeCollection.new(client, zone)
65
+ count = 0
66
+ collection.each { count += 1 }
67
+ assert_equal 0, count
68
+ end
69
+ end
70
+
71
+ describe 'with attrs' do
72
+ it 'passes attrs to client.instances' do
73
+ client.expects(:instances).with(zone, filter: 'name = "test"').returns([])
74
+ GoogleCloudCompute::ComputeCollection.new(client, zone, filter: 'name = "test"')
75
+ end
76
+ end
77
+ end
78
+ end
@@ -12,6 +12,41 @@ module GoogleExtensions
12
12
  disk = Google::Cloud::Compute::V1::AttachedDisk.new
13
13
  assert_empty disk.insert_attrs[:source_image]
14
14
  end
15
+
16
+ it 'includes device_name as name' do
17
+ disk = Google::Cloud::Compute::V1::AttachedDisk.new(device_name: 'boot-disk', disk_size_gb: 30)
18
+ attrs = disk.insert_attrs
19
+ assert_equal 'boot-disk', attrs[:name]
20
+ assert_equal 30, attrs[:size_gb]
21
+ end
22
+ end
23
+
24
+ describe '#size_gb' do
25
+ it 'returns disk_size_gb' do
26
+ disk = Google::Cloud::Compute::V1::AttachedDisk.new(disk_size_gb: 50)
27
+ assert_equal 50, disk.size_gb
28
+ end
29
+ end
30
+
31
+ describe '#persisted?' do
32
+ it 'returns nil' do
33
+ disk = Google::Cloud::Compute::V1::AttachedDisk.new
34
+ assert_nil disk.persisted?
35
+ end
36
+ end
37
+
38
+ describe '#id' do
39
+ it 'returns nil' do
40
+ disk = Google::Cloud::Compute::V1::AttachedDisk.new
41
+ assert_nil disk.id
42
+ end
43
+ end
44
+
45
+ describe '#_delete' do
46
+ it 'returns nil' do
47
+ disk = Google::Cloud::Compute::V1::AttachedDisk.new
48
+ assert_nil disk._delete
49
+ end
15
50
  end
16
51
  end
17
52
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_google
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.8
4
+ version: 3.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - The Foreman Team
@@ -141,6 +141,7 @@ files:
141
141
  - locale/zh_TW/LC_MESSAGES/foreman_google.mo
142
142
  - locale/zh_TW/foreman_google.po
143
143
  - package.json
144
+ - test/controllers/foreman_google/api/v2/compute_resources_extensions_test.rb
144
145
  - test/factories/gce.rb
145
146
  - test/fixtures/disks_delete.json
146
147
  - test/fixtures/disks_get.json
@@ -160,10 +161,13 @@ files:
160
161
  - test/fixtures/operation_error.json
161
162
  - test/fixtures/operation_get.json
162
163
  - test/fixtures/zones.json
164
+ - test/models/concerns/foreman_google/host_managed_extensions_test.rb
163
165
  - test/models/foreman_google/gce_test.rb
164
166
  - test/models/foreman_google/google_compute_test.rb
165
167
  - test/test_google_helper.rb
166
168
  - test/unit/foreman_google/google_compute_adapter_test.rb
169
+ - test/unit/google_cloud_compute/compute_attributes_test.rb
170
+ - test/unit/google_cloud_compute/compute_collection_test.rb
167
171
  - test/unit/google_extensions/attached_disk_test.rb
168
172
  - webpack/global_index.js
169
173
  - webpack/global_test_setup.js
@@ -196,10 +200,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
196
200
  - !ruby/object:Gem::Version
197
201
  version: '0'
198
202
  requirements: []
199
- rubygems_version: 4.0.6
203
+ rubygems_version: 4.0.20
200
204
  specification_version: 4
201
205
  summary: Google Compute Engine plugin for the Foreman
202
206
  test_files:
207
+ - test/controllers/foreman_google/api/v2/compute_resources_extensions_test.rb
203
208
  - test/factories/gce.rb
204
209
  - test/fixtures/disks_delete.json
205
210
  - test/fixtures/disks_get.json
@@ -219,8 +224,11 @@ test_files:
219
224
  - test/fixtures/operation_error.json
220
225
  - test/fixtures/operation_get.json
221
226
  - test/fixtures/zones.json
227
+ - test/models/concerns/foreman_google/host_managed_extensions_test.rb
222
228
  - test/models/foreman_google/gce_test.rb
223
229
  - test/models/foreman_google/google_compute_test.rb
224
230
  - test/test_google_helper.rb
225
231
  - test/unit/foreman_google/google_compute_adapter_test.rb
232
+ - test/unit/google_cloud_compute/compute_attributes_test.rb
233
+ - test/unit/google_cloud_compute/compute_collection_test.rb
226
234
  - test/unit/google_extensions/attached_disk_test.rb