Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -491,6 +492,14 @@ City of the offer, such as Ashburn.

Country of the offer, such as United States.

##### <a name="-noderings_vm--hosting_provider"></a>`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.)

##### <a name="-noderings_vm--name"></a>`name`

namevar
Expand All @@ -516,7 +525,7 @@ usually discover the appropriate provider for your platform.

##### <a name="-noderings_vm--provider_id"></a>`provider_id`

Only consider offers from this marketplace provider.
Only consider offers from the marketplace provider with this ID. Takes precedence over hosting_provider.

##### <a name="-noderings_vm--region_id"></a>`region_id`

Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/provider/noderings_vm/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion lib/puppet/type/noderings_vm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/puppet_x/noderings/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 51 additions & 8 deletions lib/puppet_x/noderings/compute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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)
Expand All @@ -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']) }
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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]
Expand Down
36 changes: 35 additions & 1 deletion spec/support/fake_noderings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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],
Expand All @@ -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],
Expand Down Expand Up @@ -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 ----------------------------------------------------------------
Expand Down
28 changes: 28 additions & 0 deletions spec/unit/puppet/provider/providers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')] }
Expand Down