From 992d3cea85a2e4c4cba2febbeffdbe39c569c016 Mon Sep 17 00:00:00 2001 From: Shaygun <17057583+shaygun@users.noreply.github.com> Date: Thu, 26 Nov 2026 16:58:24 +0000 Subject: [PATCH] feat: choose the provider when ordering a VM noderings_vm ordered the cheapest offer from any provider. A new hosting_provider parameter limits it to one provider, given by its ID, storefront slug or name (any case). A name that more than one provider uses fails instead of guessing. The parameter is not named provider because Puppet reserves that name. provider_id keeps working and takes precedence. The API core is regenerated from the noderings cookbook. Signed-off-by: Shaygun <17057583+shaygun@users.noreply.github.com> --- README.md | 7 +++ REFERENCE.md | 11 +++- lib/puppet/provider/noderings_vm/api.rb | 2 +- lib/puppet/type/noderings_vm.rb | 12 ++++- lib/puppet_x/noderings/client.rb | 4 ++ lib/puppet_x/noderings/compute.rb | 59 ++++++++++++++++++--- spec/support/fake_noderings.rb | 36 ++++++++++++- spec/unit/puppet/provider/providers_spec.rb | 28 ++++++++++ 8 files changed, 147 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 4664677..86786f3 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,13 @@ noderings_vm { 'web-1': } ``` +Without `hosting_provider` the provider can change between orders. Set it to +keep one provider: it takes the provider's ID, its storefront slug (such as +`acme-hosting`) or its name (`Acme Hosting`, any case). A name that more than +one provider uses fails; give the ID or slug then. The parameter is not called +`provider` because Puppet reserves that name. `provider_id` still works and +takes precedence. + On an existing VM, Puppet keeps `tags`, `firewall_ruleset` (an empty string detaches it) and `power_state` as declared. The plan is never changed. A different `distro` fails the run unless `allow_reinstall => true`, because diff --git a/REFERENCE.md b/REFERENCE.md index cbe2955..77f9e33 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -456,6 +456,7 @@ The following parameters are available in the `noderings_vm` type. * [`billing_type`](#-noderings_vm--billing_type) * [`city`](#-noderings_vm--city) * [`country`](#-noderings_vm--country) +* [`hosting_provider`](#-noderings_vm--hosting_provider) * [`name`](#-noderings_vm--name) * [`password`](#-noderings_vm--password) * [`plan`](#-noderings_vm--plan) @@ -491,6 +492,14 @@ City of the offer, such as Ashburn. Country of the offer, such as United States. +##### `hosting_provider` + +Only consider offers from this marketplace provider, given by its ID, its +storefront slug (such as acme-hosting) or its name (Acme Hosting, any case). +Left unset, the cheapest matching offer from any provider is ordered. A name +that more than one provider uses fails; give the ID or slug then. (Puppet +reserves the name provider for the resource provider.) + ##### `name` namevar @@ -516,7 +525,7 @@ usually discover the appropriate provider for your platform. ##### `provider_id` -Only consider offers from this marketplace provider. +Only consider offers from the marketplace provider with this ID. Takes precedence over hosting_provider. ##### `region_id` diff --git a/lib/puppet/provider/noderings_vm/api.rb b/lib/puppet/provider/noderings_vm/api.rb index ba44f92..0c143a9 100644 --- a/lib/puppet/provider/noderings_vm/api.rb +++ b/lib/puppet/provider/noderings_vm/api.rb @@ -82,7 +82,7 @@ def offer plan_id, region_id, = api('finding an offer') do compute.choose_offer(client, resource[:plan], resource[:country], resource[:city], billing_type: resource[:billing_type].to_s, provider_id: resource[:provider_id], - sort: resource[:sort].to_s) + sort: resource[:sort].to_s, provider: resource[:hosting_provider]) end [plan_id, region_id] end diff --git a/lib/puppet/type/noderings_vm.rb b/lib/puppet/type/noderings_vm.rb index 169850d..37b3bf7 100644 --- a/lib/puppet/type/noderings_vm.rb +++ b/lib/puppet/type/noderings_vm.rb @@ -63,8 +63,18 @@ defaultto(:price) end + newparam(:hosting_provider) do + desc <<~DESC + Only consider offers from this marketplace provider, given by its ID, its + storefront slug (such as acme-hosting) or its name (Acme Hosting, any case). + Left unset, the cheapest matching offer from any provider is ordered. A name + that more than one provider uses fails; give the ID or slug then. (Puppet + reserves the name provider for the resource provider.) + DESC + end + newparam(:provider_id) do - desc 'Only consider offers from this marketplace provider.' + desc 'Only consider offers from the marketplace provider with this ID. Takes precedence over hosting_provider.' end newparam(:plan_id) do diff --git a/lib/puppet_x/noderings/client.rb b/lib/puppet_x/noderings/client.rb index 699de32..c54511d 100644 --- a/lib/puppet_x/noderings/client.rb +++ b/lib/puppet_x/noderings/client.rb @@ -444,6 +444,10 @@ def get_provider(provider_id) get("/v1/marketplace/providers/#{PuppetX::Noderings.escape(provider_id)}")['provider'] end + def get_public_storefront(slug) + get("/v1/public/storefronts/#{PuppetX::Noderings.escape(slug)}")['storefront'] + end + def list_distros get('/v1/distros')['distros'] || [] end diff --git a/lib/puppet_x/noderings/compute.rb b/lib/puppet_x/noderings/compute.rb index ee26112..c302dfd 100644 --- a/lib/puppet_x/noderings/compute.rb +++ b/lib/puppet_x/noderings/compute.rb @@ -53,10 +53,40 @@ def same_place?(region, country, city) region['country'].to_s.casecmp?(country.to_s) && region['city'].to_s.casecmp?(city.to_s) end + SLUG_RE = %r{\A[a-z0-9-]{1,80}\z}.freeze + + # A provider ID, storefront slug or provider name -> [provider_id, name]. + # + # An ID, or a slug that names a public storefront, gives the provider ID. + # Anything else is a provider name, matched without regard to case + # against the offers. + def resolve_provider(client, provider) + value = provider.to_s.strip + return [nil, nil] if value.empty? + return [value, nil] if PuppetX::Noderings.uuid?(value) + + provider_id = SLUG_RE.match?(value) ? storefront_provider_id(client, value) : '' + provider_id.empty? ? [nil, value] : [provider_id, nil] + end + + # The provider ID of the public storefront with this slug, or '' if there is none. + def storefront_provider_id(client, slug) + (client.get_public_storefront(slug) || {})['providerId'].to_s + rescue APIError => e + raise unless e.not_found? + + '' + end + # Returns [offers, located]: offers sorted best first, and how many matched # the location before the capability and IPv6 filters. + # + # provider is a provider ID, storefront slug or provider name (see + # resolve_provider); provider_id takes precedence over it. def find_offers(client, plan_code, country, city, billing_type: 'usage', provider_id: nil, - require_capabilities: [], require_ipv6: false, sort: 'price') + require_capabilities: [], require_ipv6: false, sort: 'price', provider: nil) + provider_name = nil + provider_id, provider_name = resolve_provider(client, provider) if provider_id.to_s.empty? && !provider.to_s.strip.empty? billing = BILLING_TYPES.fetch(billing_type.to_s, '') found = [] if provider_id.to_s.empty? @@ -68,9 +98,7 @@ def find_offers(client, plan_code, country, city, billing_type: 'usage', provide p['ipv6AvailableInFilteredRegion'], p['planPrices']) end else - p = client.get( - "/v1/marketplace/providers/#{PuppetX::Noderings.escape(provider_id)}", - ) + p = client.get_provider(provider_id) || {} info = p['provider'] || {} (p['availablePlans'] || []).each do |pl| next if pl['planCode'] != plan_code || (!billing.empty? && pl['billingType'] != billing) @@ -81,6 +109,14 @@ def find_offers(client, plan_code, country, city, billing_type: 'usage', provide end end end + if provider_name + want = provider_name.downcase + found = found.select { |o| o['provider_name'].to_s.strip.downcase == want } + if found.map { |o| o['provider_id'] }.uniq.length > 1 + raise UsageError, "more than one provider is named #{provider_name}; give its provider ID or " \ + 'storefront slug instead' + end + end located = found.length need = Array(require_capabilities) out = found.select { |o| (need - o['capabilities']).empty? && (!require_ipv6 || o['ipv6']) } @@ -110,8 +146,9 @@ def offer(info, plan_id, region_id, billing, ipv6, prices) } end - def no_offer_message(plan_code, country, city, billing_type, located) + def no_offer_message(plan_code, country, city, billing_type, located, provider = nil) msg = "No offer for plan code #{plan_code} in #{city}, #{country}" + msg += " from provider #{provider}" unless provider.to_s.empty? msg += " with billing type #{billing_type}" unless billing_type.to_s.empty? msg += '.' msg += " #{located} offer(s) matched the location but not the required capabilities or IPv6." if located.positive? @@ -209,14 +246,20 @@ def translate_create_error(err) end # Returns [plan_id, region_id, offer] for the best matching offer. + # + # provider limits the offers to one provider, given by ID, storefront slug or name. def choose_offer(client, plan_code, country, city, billing_type: 'usage', provider_id: nil, - require_capabilities: [], require_ipv6: false, sort: 'price') + require_capabilities: [], require_ipv6: false, sort: 'price', provider: nil) offers, located = Marketplace.find_offers( client, plan_code, country, city, billing_type: billing_type, provider_id: provider_id, - require_capabilities: require_capabilities, require_ipv6: require_ipv6, sort: sort + require_capabilities: require_capabilities, require_ipv6: require_ipv6, sort: sort, + provider: provider ) - raise UsageError, Marketplace.no_offer_message(plan_code, country, city, billing_type, located) if offers.empty? + if offers.empty? + named = provider_id.to_s.empty? ? provider.to_s.strip : provider_id + raise UsageError, Marketplace.no_offer_message(plan_code, country, city, billing_type, located, named) + end best = offers.first [best['plan_id'], best['region_id'], best] diff --git a/spec/support/fake_noderings.rb b/spec/support/fake_noderings.rb index 4cad0e6..0576cd5 100644 --- a/spec/support/fake_noderings.rb +++ b/spec/support/fake_noderings.rb @@ -11,10 +11,21 @@ class FakeNodeRings PLAN_ID = '11111111-1111-1111-1111-111111111111' REGION_ID = '22222222-2222-2222-2222-222222222222' + ACME_ID = '33333333-3333-3333-3333-333333333333' + ACME_PLAN_ID = '44444444-4444-4444-4444-444444444444' + ACME_REGION_ID = '55555555-5555-5555-5555-555555555555' + ACME_OFFER = { + 'organizationId' => ACME_ID, 'organizationName' => 'Acme Hosting', 'planId' => ACME_PLAN_ID, + 'planRegions' => [{ 'country' => 'United States', 'city' => 'Ashburn', 'regionId' => ACME_REGION_ID }], + 'capabilities' => [], 'rating' => 3, + 'planPrices' => [{ 'unitAmount' => '0.012', 'unitName' => 'per compute-hour' }], + }.freeze attr_reader :calls, :ssh_keys, :zones, :records, :rulesets, :rules, :groups, :vms, :created_vm_bodies # Status codes to return before handling the next matching request: { 'POST /v1/virtual-machines' => [502] } attr_reader :failures + # More provider offers to list, such as a second provider with the same name. + attr_reader :extra_providers def initialize @calls = [] @@ -27,6 +38,7 @@ def initialize @vms = {} @created_vm_bodies = [] @failures = Hash.new { |h, k| h[k] = [] } + @extra_providers = [] @routes = [ ['GET', %r{\A/v1/organization\z}, :get_org], ['GET', %r{\A/v1/ssh-keys\z}, :list_keys], @@ -52,6 +64,8 @@ def initialize ['DELETE', %r{\A/v1/firewall/rules/([^/]+)\z}, :delete_rule], ['POST', %r{\A/v1/firewall/rules/([^/]+)/move\z}, :move_rule], ['GET', %r{\A/v1/marketplace/providers\z}, :list_providers], + ['GET', %r{\A/v1/marketplace/providers/([^/]+)\z}, :get_provider], + ['GET', %r{\A/v1/public/storefronts/([^/]+)\z}, :get_storefront], ['GET', %r{\A/v1/marketplace/plans/([^/]+)\z}, :get_plan], ['GET', %r{\A/v1/marketplace/plans\z}, :list_plans], ['GET', %r{\A/v1/marketplace/regions\z}, :list_regions], @@ -137,7 +151,27 @@ def list_providers(query, _body) 'planPrices' => [{ 'unitAmount' => '0.007', 'unitName' => 'per compute-hour' }, { 'unitAmount' => '0.0001', 'unitName' => 'per GB-hour storage' },], }, - ] },] + ACME_OFFER, + ] + @extra_providers },] + end + + def get_provider(_query, _body, provider_id) + return err(404, 5, 'provider not found') unless provider_id == ACME_ID + + [200, { 'provider' => { + 'provider' => { 'organizationId' => ACME_ID, 'organizationName' => 'Acme Hosting', 'rating' => 3 }, + 'availablePlans' => [{ + 'planId' => ACME_PLAN_ID, 'planCode' => 'SC-GC-1C-1G-20', 'billingType' => 'BILLING_TYPE_USAGE', + 'assetType' => 'ASSET_TYPE_VM', 'planPrices' => ACME_OFFER['planPrices'], + 'regions' => [{ 'country' => 'United States', 'city' => 'Ashburn', 'regionId' => ACME_REGION_ID }], + }], + } },] + end + + def get_storefront(_query, _body, slug) + return err(404, 5, 'storefront not found') unless slug == 'acme-hosting' + + [200, { 'storefront' => { 'slug' => slug, 'providerId' => ACME_ID } }] end # -- SSH keys ---------------------------------------------------------------- diff --git a/spec/unit/puppet/provider/providers_spec.rb b/spec/unit/puppet/provider/providers_spec.rb index 02c206a..1f4bc45 100644 --- a/spec/unit/puppet/provider/providers_spec.rb +++ b/spec/unit/puppet/provider/providers_spec.rb @@ -208,6 +208,34 @@ def vm expect(fake.writes).to be_empty end + context 'with hosting_provider' do + [FakeNodeRings::ACME_ID, 'acme-hosting', 'Acme Hosting', ' acme HOSTING '].each do |provider| + it "orders from the provider given as #{provider.inspect}" do + report = apply(resource(:noderings_vm, 'web-1', offer.merge(ssh_keys: ['laptop'], hosting_provider: provider))) + expect(failed(report)).to be_empty + expect(fake.created_vm_bodies.last).to include('planId' => FakeNodeRings::ACME_PLAN_ID, 'regionId' => FakeNodeRings::ACME_REGION_ID) + end + end + + it 'orders the cheapest offer without one' do + apply(resource(:noderings_vm, 'web-1', offer.merge(ssh_keys: ['laptop']))) + expect(fake.created_vm_bodies.last['planId']).to eq FakeNodeRings::PLAN_ID + end + + it 'fails for an unknown provider' do + report = apply(resource(:noderings_vm, 'web-1', offer.merge(ssh_keys: ['laptop'], hosting_provider: 'Nobody Hosting'))) + expect(failed(report).join).to include('from provider Nobody Hosting') + expect(fake.created_vm_bodies).to be_empty + end + + it 'refuses a provider name two providers share' do + fake.extra_providers << FakeNodeRings::ACME_OFFER.merge('organizationId' => '66666666-6666-6666-6666-666666666666') + report = apply(resource(:noderings_vm, 'web-1', offer.merge(ssh_keys: ['laptop'], hosting_provider: 'acme hosting'))) + expect(failed(report).join).to include('more than one provider is named acme hosting') + expect(fake.created_vm_bodies).to be_empty + end + end + it 'converges tags, firewall and power on an existing VM' do apply(resource(:noderings_vm, 'web-1', offer.merge(ssh_keys: ['laptop'], tags: ['old']))) apply_fresh { [resource(:noderings_vm, 'web-1', tags: ['new'], firewall_ruleset: 'web', power_state: 'stopped')] }