From d042d7184be92bfe255a0b7a3141741b2b2e528a Mon Sep 17 00:00:00 2001 From: Nathan McKinley Date: Mon, 9 Jul 2018 21:54:12 +0000 Subject: [PATCH] Switch compute/disk type to be a resource reference instead of string. --- README.md | 2 +- lib/puppet/provider/gcompute_disk/google.rb | 4 +- lib/puppet/type/gcompute_disk.rb | 3 +- .../network/gcompute_disk/success1~name.yaml | 2 +- .../network/gcompute_disk/success1~title.yaml | 2 +- .../network/gcompute_disk/success2~name.yaml | 2 +- .../network/gcompute_disk/success2~title.yaml | 2 +- .../network/gcompute_disk/success3~name.yaml | 2 +- .../network/gcompute_disk/success3~title.yaml | 2 +- spec/gcompute_disk_provider_spec.rb | 224 ++++++++++++++++-- 10 files changed, 220 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 985fc435..3b608620 100644 --- a/README.md +++ b/README.md @@ -1577,7 +1577,7 @@ gcompute_disk { 'id-of-resource': sha256 => string, }, source_snapshot_id => string, - type => string, + type => reference to gcompute_disk_type, users => [ string, ... diff --git a/lib/puppet/provider/gcompute_disk/google.rb b/lib/puppet/provider/gcompute_disk/google.rb index 050f7da4..c27aea8e 100644 --- a/lib/puppet/provider/gcompute_disk/google.rb +++ b/lib/puppet/provider/gcompute_disk/google.rb @@ -32,6 +32,7 @@ require 'google/compute/property/disk_disk_encryption_key' require 'google/compute/property/disk_source_image_encryption_key' require 'google/compute/property/disk_source_snapshot_encryption_key' +require 'google/compute/property/disktype_selflink' require 'google/compute/property/integer' require 'google/compute/property/namevalues' require 'google/compute/property/string' @@ -89,7 +90,8 @@ def self.fetch_to_hash(fetch, resource) licenses: Google::Compute::Property::StringArray.api_munge(fetch['licenses']), size_gb: Google::Compute::Property::Integer.api_munge(fetch['sizeGb']), - type: Google::Compute::Property::String.api_munge(fetch['type']), + type: + Google::Compute::Property::DiskTypeSelfLinkRef.api_munge(fetch['type']), users: Google::Compute::Property::StringArray.api_munge(fetch['users']), name: resource[:name], source_image: resource[:source_image] diff --git a/lib/puppet/type/gcompute_disk.rb b/lib/puppet/type/gcompute_disk.rb index 0f1569bd..9c707730 100644 --- a/lib/puppet/type/gcompute_disk.rb +++ b/lib/puppet/type/gcompute_disk.rb @@ -28,6 +28,7 @@ require 'google/compute/property/disk_disk_encryption_key' require 'google/compute/property/disk_source_image_encryption_key' require 'google/compute/property/disk_source_snapshot_encryption_key' +require 'google/compute/property/disktype_selflink' require 'google/compute/property/integer' require 'google/compute/property/namevalues' require 'google/compute/property/string' @@ -221,7 +222,7 @@ DOC end - newproperty(:type, parent: Google::Compute::Property::String) do + newproperty(:type, parent: Google::Compute::Property::DiskTypeSelfLinkRef) do desc <<-DOC URL of the disk type resource describing which disk type to use to create the disk. Provide this when creating the disk. diff --git a/spec/data/network/gcompute_disk/success1~name.yaml b/spec/data/network/gcompute_disk/success1~name.yaml index f36cc72e..983a66aa 100644 --- a/spec/data/network/gcompute_disk/success1~name.yaml +++ b/spec/data/network/gcompute_disk/success1~name.yaml @@ -54,7 +54,7 @@ sourceSnapshotEncryptionKey: rawKey: test raw_key#0 data sha256: test sha256#0 data sourceSnapshotId: test source_snapshot_id#0 data -type: test type#0 data +type: selflink(resource(disk_type,0)) users: - ww - xx diff --git a/spec/data/network/gcompute_disk/success1~title.yaml b/spec/data/network/gcompute_disk/success1~title.yaml index e0638cbf..cfacc4e4 100644 --- a/spec/data/network/gcompute_disk/success1~title.yaml +++ b/spec/data/network/gcompute_disk/success1~title.yaml @@ -54,7 +54,7 @@ sourceSnapshotEncryptionKey: rawKey: test raw_key#0 data sha256: test sha256#0 data sourceSnapshotId: test source_snapshot_id#0 data -type: test type#0 data +type: selflink(resource(disk_type,0)) users: - ww - xx diff --git a/spec/data/network/gcompute_disk/success2~name.yaml b/spec/data/network/gcompute_disk/success2~name.yaml index de04f5d6..9477d52c 100644 --- a/spec/data/network/gcompute_disk/success2~name.yaml +++ b/spec/data/network/gcompute_disk/success2~name.yaml @@ -55,7 +55,7 @@ sourceSnapshotEncryptionKey: rawKey: test raw_key#1 data sha256: test sha256#1 data sourceSnapshotId: test source_snapshot_id#1 data -type: test type#1 data +type: selflink(resource(disk_type,1)) users: - uu - vv diff --git a/spec/data/network/gcompute_disk/success2~title.yaml b/spec/data/network/gcompute_disk/success2~title.yaml index 404ddf94..7cfc36e2 100644 --- a/spec/data/network/gcompute_disk/success2~title.yaml +++ b/spec/data/network/gcompute_disk/success2~title.yaml @@ -55,7 +55,7 @@ sourceSnapshotEncryptionKey: rawKey: test raw_key#1 data sha256: test sha256#1 data sourceSnapshotId: test source_snapshot_id#1 data -type: test type#1 data +type: selflink(resource(disk_type,1)) users: - uu - vv diff --git a/spec/data/network/gcompute_disk/success3~name.yaml b/spec/data/network/gcompute_disk/success3~name.yaml index 1dc89545..91db51ae 100644 --- a/spec/data/network/gcompute_disk/success3~name.yaml +++ b/spec/data/network/gcompute_disk/success3~name.yaml @@ -57,7 +57,7 @@ sourceSnapshotEncryptionKey: rawKey: test raw_key#2 data sha256: test sha256#2 data sourceSnapshotId: test source_snapshot_id#2 data -type: test type#2 data +type: selflink(resource(disk_type,2)) users: - ss - tt diff --git a/spec/data/network/gcompute_disk/success3~title.yaml b/spec/data/network/gcompute_disk/success3~title.yaml index 5a64455b..6f0eeb29 100644 --- a/spec/data/network/gcompute_disk/success3~title.yaml +++ b/spec/data/network/gcompute_disk/success3~title.yaml @@ -57,7 +57,7 @@ sourceSnapshotEncryptionKey: rawKey: test raw_key#2 data sha256: test sha256#2 data sourceSnapshotId: test source_snapshot_id#2 data -type: test type#2 data +type: selflink(resource(disk_type,2)) users: - ss - tt diff --git a/spec/gcompute_disk_provider_spec.rb b/spec/gcompute_disk_provider_spec.rb index fb636318..34f2f083 100644 --- a/spec/gcompute_disk_provider_spec.rb +++ b/spec/gcompute_disk_provider_spec.rb @@ -63,6 +63,9 @@ expect_network_get_success_zone 1 expect_network_get_success_zone 2 expect_network_get_success_zone 3 + expect_network_get_success_disk_type 1, zone: 'test name#0 data' + expect_network_get_success_disk_type 2, zone: 'test name#1 data' + expect_network_get_success_disk_type 3, zone: 'test name#2 data' end let(:catalog) do @@ -86,6 +89,27 @@ credential => 'cred2', } + gcompute_disk_type { 'resource(disk_type,0)': + name => 'test name#0 data', + zone => 'resource(zone,0)', + project => 'test project#0 data', + credential => 'cred0', + } + + gcompute_disk_type { 'resource(disk_type,1)': + name => 'test name#1 data', + zone => 'resource(zone,1)', + project => 'test project#1 data', + credential => 'cred1', + } + + gcompute_disk_type { 'resource(disk_type,2)': + name => 'test name#2 data', + zone => 'resource(zone,2)', + project => 'test project#2 data', + credential => 'cred2', + } + gcompute_disk { 'title0': ensure => present, description => 'test description#0 data', @@ -109,7 +133,7 @@ raw_key => 'test raw_key#0 data', sha256 => 'test sha256#0 data', }, - type => 'test type#0 data', + type => 'resource(disk_type,0)', zone => 'resource(zone,0)', project => 'test project#0 data', credential => 'cred0', @@ -139,7 +163,7 @@ raw_key => 'test raw_key#1 data', sha256 => 'test sha256#1 data', }, - type => 'test type#1 data', + type => 'resource(disk_type,1)', zone => 'resource(zone,1)', project => 'test project#1 data', credential => 'cred1', @@ -170,7 +194,7 @@ raw_key => 'test raw_key#2 data', sha256 => 'test sha256#2 data', }, - type => 'test type#2 data', + type => 'resource(disk_type,2)', zone => 'resource(zone,2)', project => 'test project#2 data', credential => 'cred2', @@ -221,7 +245,10 @@ is_expected .to have_attributes(source_image: 'test source_image#0 data') end - it { is_expected.to have_attributes(type: 'test type#0 data') } + # TODO(alexstephen): Implement resourceref test. + # it 'type' do + # # Add test code here + # end it { is_expected.to have_attributes(users: %w[ww xx yy zz]) } end @@ -267,7 +294,10 @@ is_expected .to have_attributes(source_image: 'test source_image#1 data') end - it { is_expected.to have_attributes(type: 'test type#1 data') } + # TODO(alexstephen): Implement resourceref test. + # it 'type' do + # # Add test code here + # end it { is_expected.to have_attributes(users: %w[uu vv]) } end @@ -313,7 +343,10 @@ is_expected .to have_attributes(source_image: 'test source_image#2 data') end - it { is_expected.to have_attributes(type: 'test type#2 data') } + # TODO(alexstephen): Implement resourceref test. + # it 'type' do + # # Add test code here + # end it { is_expected.to have_attributes(users: %w[ss tt uu vv]) } end end @@ -340,6 +373,9 @@ expect_network_get_success_zone 1 expect_network_get_success_zone 2 expect_network_get_success_zone 3 + expect_network_get_success_disk_type 1, zone: 'test name#0 data' + expect_network_get_success_disk_type 2, zone: 'test name#1 data' + expect_network_get_success_disk_type 3, zone: 'test name#2 data' end let(:catalog) do @@ -363,6 +399,27 @@ credential => 'cred2', } + gcompute_disk_type { 'resource(disk_type,0)': + name => 'test name#0 data', + zone => 'resource(zone,0)', + project => 'test project#0 data', + credential => 'cred0', + } + + gcompute_disk_type { 'resource(disk_type,1)': + name => 'test name#1 data', + zone => 'resource(zone,1)', + project => 'test project#1 data', + credential => 'cred1', + } + + gcompute_disk_type { 'resource(disk_type,2)': + name => 'test name#2 data', + zone => 'resource(zone,2)', + project => 'test project#2 data', + credential => 'cred2', + } + gcompute_disk { 'title0': ensure => present, description => 'test description#0 data', @@ -387,7 +444,7 @@ raw_key => 'test raw_key#0 data', sha256 => 'test sha256#0 data', }, - type => 'test type#0 data', + type => 'resource(disk_type,0)', zone => 'resource(zone,0)', project => 'test project#0 data', credential => 'cred0', @@ -418,7 +475,7 @@ raw_key => 'test raw_key#1 data', sha256 => 'test sha256#1 data', }, - type => 'test type#1 data', + type => 'resource(disk_type,1)', zone => 'resource(zone,1)', project => 'test project#1 data', credential => 'cred1', @@ -450,7 +507,7 @@ raw_key => 'test raw_key#2 data', sha256 => 'test sha256#2 data', }, - type => 'test type#2 data', + type => 'resource(disk_type,2)', zone => 'resource(zone,2)', project => 'test project#2 data', credential => 'cred2', @@ -501,7 +558,10 @@ is_expected .to have_attributes(source_image: 'test source_image#0 data') end - it { is_expected.to have_attributes(type: 'test type#0 data') } + # TODO(alexstephen): Implement resourceref test. + # it 'type' do + # # Add test code here + # end it { is_expected.to have_attributes(users: %w[ww xx yy zz]) } end @@ -547,7 +607,10 @@ is_expected .to have_attributes(source_image: 'test source_image#1 data') end - it { is_expected.to have_attributes(type: 'test type#1 data') } + # TODO(alexstephen): Implement resourceref test. + # it 'type' do + # # Add test code here + # end it { is_expected.to have_attributes(users: %w[uu vv]) } end @@ -593,7 +656,10 @@ is_expected .to have_attributes(source_image: 'test source_image#2 data') end - it { is_expected.to have_attributes(type: 'test type#2 data') } + # TODO(alexstephen): Implement resourceref test. + # it 'type' do + # # Add test code here + # end it { is_expected.to have_attributes(users: %w[ss tt uu vv]) } end end @@ -663,7 +729,7 @@ 'name' => 'title0', 'sizeGb' => 2_858_499_398, 'sourceImage' => 'test source_image#0 data', - 'type' => 'test type#0 data', + 'type' => 'selflink(resource(disk_type,0))', 'diskEncryptionKey' => { 'rawKey' => 'test raw_key#0 data', 'sha256' => 'test sha256#0 data' @@ -681,6 +747,7 @@ zone: 'test name#0 data' expect_network_get_async 1, name: 'title0', zone: 'test name#0 data' expect_network_get_success_zone 1 + expect_network_get_success_disk_type 1, zone: 'test name#0 data' end subject do @@ -692,6 +759,13 @@ credential => 'cred0', } + gcompute_disk_type { 'resource(disk_type,0)': + name => 'test name#0 data', + zone => 'resource(zone,0)', + project => 'test project#0 data', + credential => 'cred0', + } + gcompute_disk { 'title0': ensure => present, description => 'test description#0 data', @@ -715,7 +789,7 @@ raw_key => 'test raw_key#0 data', sha256 => 'test sha256#0 data', }, - type => 'test type#0 data', + type => 'resource(disk_type,0)', zone => 'resource(zone,0)', project => 'test project#0 data', credential => 'cred0', @@ -754,7 +828,7 @@ 'name' => 'test name#0 data', 'sizeGb' => 2_858_499_398, 'sourceImage' => 'test source_image#0 data', - 'type' => 'test type#0 data', + 'type' => 'selflink(resource(disk_type,0))', 'diskEncryptionKey' => { 'rawKey' => 'test raw_key#0 data', 'sha256' => 'test sha256#0 data' @@ -771,6 +845,7 @@ zone: 'test name#0 data' expect_network_get_async 1, zone: 'test name#0 data' expect_network_get_success_zone 1 + expect_network_get_success_disk_type 1, zone: 'test name#0 data' end subject do @@ -782,6 +857,13 @@ credential => 'cred0', } + gcompute_disk_type { 'resource(disk_type,0)': + name => 'test name#0 data', + zone => 'resource(zone,0)', + project => 'test project#0 data', + credential => 'cred0', + } + gcompute_disk { 'title0': ensure => present, description => 'test description#0 data', @@ -806,7 +888,7 @@ raw_key => 'test raw_key#0 data', sha256 => 'test sha256#0 data', }, - type => 'test type#0 data', + type => 'resource(disk_type,0)', zone => 'resource(zone,0)', project => 'test project#0 data', credential => 'cred0', @@ -1137,6 +1219,106 @@ def load_network_result(file) data end + def expect_network_get_success_disk_type(id, data = {}) + id_data = data.fetch(:name, '').include?('title') ? 'title' : 'name' + body = load_network_result_disk_type("success#{id}~" \ + "#{id_data}.yaml") + .to_json + uri = uri_data_disk_type(id).merge(data) + + request = double('request') + allow(request).to receive(:send).and_return(http_success(body)) + + debug_network "!! GET #{uri}" + expect(Google::Compute::Network::Get).to receive(:new) + .with(self_link_disk_type(uri), + instance_of(Google::FakeAuthorization)) do |args| + debug_network ">> GET #{args}" + request + end + end + + def load_network_result_disk_type(file) + results = File.join(File.dirname(__FILE__), 'data', 'network', + 'gcompute_disk_type', file) + raise "Network result data file #{results}" unless File.exist?(results) + data = YAML.safe_load(File.read(results)) + raise "Invalid network results #{results}" unless data.class <= Hash + data + end + + # Creates variable test data to comply with self_link URI parameters + # Only used for gcompute_disk_type objects + def uri_data_disk_type(id) + { + project: GoogleTests::Constants::DT_PROJECT_DATA[(id - 1) \ + % GoogleTests::Constants::DT_PROJECT_DATA.size], + zone: GoogleTests::Constants::DT_ZONE_DATA[(id - 1) \ + % GoogleTests::Constants::DT_ZONE_DATA.size], + name: GoogleTests::Constants::DT_NAME_DATA[(id - 1) \ + % GoogleTests::Constants::DT_NAME_DATA.size] + } + end + + def self_link_disk_type(data) + URI.join( + 'https://www.googleapis.com/compute/v1/', + expand_variables_disk_type( + 'projects/{{project}}/zones/{{zone}}/diskTypes/{{name}}', + data + ) + ) + end + + def expect_network_get_success_zone(id, data = {}) + id_data = data.fetch(:name, '').include?('title') ? 'title' : 'name' + body = load_network_result_zone("success#{id}~" \ + "#{id_data}.yaml") + .to_json + uri = uri_data_zone(id).merge(data) + + request = double('request') + allow(request).to receive(:send).and_return(http_success(body)) + + debug_network "!! GET #{uri}" + expect(Google::Compute::Network::Get).to receive(:new) + .with(self_link_zone(uri), + instance_of(Google::FakeAuthorization)) do |args| + debug_network ">> GET #{args}" + request + end + end + + def load_network_result_zone(file) + results = File.join(File.dirname(__FILE__), 'data', 'network', + 'gcompute_zone', file) + raise "Network result data file #{results}" unless File.exist?(results) + data = YAML.safe_load(File.read(results)) + raise "Invalid network results #{results}" unless data.class <= Hash + data + end + + # Creates variable test data to comply with self_link URI parameters + # Only used for gcompute_zone objects + def uri_data_zone(id) + { + project: GoogleTests::Constants::Z_PROJECT_DATA[(id - 1) \ + % GoogleTests::Constants::Z_PROJECT_DATA.size], + name: GoogleTests::Constants::Z_NAME_DATA[(id - 1) \ + % GoogleTests::Constants::Z_NAME_DATA.size] + } + end + + def self_link_zone(data) + URI.join( + 'https://www.googleapis.com/compute/v1/', + expand_variables_zone( + 'projects/{{project}}/zones/{{name}}', + data + ) + ) + end + def expect_network_get_success_zone(id, data = {}) id_data = data.fetch(:name, '').include?('title') ? 'title' : 'name' body = load_network_result_zone("success#{id}~" \ @@ -1195,6 +1377,16 @@ def debug_network(message) if ENV['RSPEC_DEBUG'] || ENV['RSPEC_HTTP_VERBOSE'] end + def expand_variables_disk_type(template, data, ext_dat = {}) + Puppet::Type.type(:gcompute_disk_type).provider(:google) + .expand_variables(template, data, ext_dat) + end + + def expand_variables_zone(template, data, ext_dat = {}) + Puppet::Type.type(:gcompute_zone).provider(:google) + .expand_variables(template, data, ext_dat) + end + def expand_variables_zone(template, data, ext_dat = {}) Puppet::Type.type(:gcompute_zone).provider(:google) .expand_variables(template, data, ext_dat)