Skip to content

Commit

Permalink
(PUP-1678) List environment settings instead of modules
Browse files Browse the repository at this point in the history
In talking with consumers of the environment information it turns out
that listing the modules isn't the desired feature. Instead they really
want the settings of the environment (modulepath, manifest). Other
settings may be added later.

This has the added benefit that listing the environments is now a much
cheaper operations since it doesn't need to load the module data.
  • Loading branch information
zaphod42 committed Feb 21, 2014
1 parent e4822b8 commit c7e3697
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 60 deletions.
16 changes: 11 additions & 5 deletions api/docs/http_environments.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Get

Get the list of known environments.

GET /v2/environments
GET /v2.0/environments

### Parameters

Expand All @@ -19,7 +19,7 @@ None

Note: module lists shortened for readability.

GET /v2/environments
GET /v2.0/environments

HTTP 200 OK
Content-Type: application/json
Expand All @@ -28,10 +28,16 @@ Note: module lists shortened for readability.
"search_paths": ["/etc/puppet/environments"]
"environments": {
"production": {
"modules": {
"a-module": { "version": "1.3.5" }
"a-different-module": { "version": "2.4.6" }
"settings": {
"modulepath": ["/first/module/directory", "/second/module/directory"],
"manifest": ["/location/of/manifests"]
}
}
}
}

Schema
------

A environments response body adheres to the {file:api/schemas/environments.json
api/schemas/environments.json} schema.
69 changes: 34 additions & 35 deletions api/schemas/environments.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Environment Enumeration",
"description": "An enumeration of environments and their modules",
"type": "object",
"properties": {
"search_paths": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1,
"description": "An array of the paths where the master looked for environments."
},
"environments": {
"type": "object",
"patternProperties": {
"^[a-z0-9_]+$": {
"type": "object",
"properties": {
"modules" : {
"type": "object",
"patternProperties": {
"^[a-z0-9_\-]+$": {
"type": "object",
"properties": {
"version": { "type": "string" }
},
"required": ["version"]
}
}
}
},
"required": ["modules"]
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Environment Enumeration",
"description": "An enumeration of environments and their settings",
"type": "object",
"properties": {
"search_paths": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1,
"description": "An array of the paths where the master looked for environments."
},
"environments": {
"type": "object",
"patternProperties": {
"^[a-z0-9_]+$": {
"type": "object",
"properties": {
"settings" : {
"type": "object",
"properties": {
"manifest": { "type": "string" },
"modulepath": {
"type": "array",
"items": { "type": "string" }
}
},
"required": ["modulepath", "manifest"]
}
},
"required": ["settings"]
}
},
"required": ["search_paths", "environments"]
}
}
},
"required": ["search_paths", "environments"]
}
9 changes: 4 additions & 5 deletions lib/puppet/network/http/api/v2/environments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ def call(request, response)
"search_paths" => @env_loader.search_paths,
"environments" => Hash[@env_loader.list.collect do |env|
[env.name, {
"modules" => Hash[env.modules.collect do |mod|
[mod.name, {
"version" => mod.version
}]
end]
"settings" => {
"modulepath" => env.full_modulepath,
"manifest" => env.manifest
}
}]
end]
}))
Expand Down
21 changes: 6 additions & 15 deletions spec/unit/network/http/api/v2/environments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
describe Puppet::Network::HTTP::API::V2::Environments do
include JSONMatchers

it "responds with all of the available environments environments" do
environment = FakeEnvironment.create(:production, [], '')
it "responds with all of the available environments" do
environment = Puppet::Node::Environment.create(:production, ["/first", "/second"], '/manifests')
loader = Puppet::Environments::Static.new(environment)
handler = Puppet::Network::HTTP::API::V2::Environments.new(loader)
response = Puppet::Network::HTTP::MemoryResponse.new
Expand All @@ -21,31 +21,22 @@
"search_paths" => loader.search_paths,
"environments" => {
"production" => {
"modules" => {
"testing" => {
"version" => "1.2.3"
}
"settings" => {
"modulepath" => ["/first", "/second"],
"manifest" => "/manifests"
}
}
}
})
end

it "the response conforms to the environments schema" do
environment = FakeEnvironment.create(:production, [], '')
environment = Puppet::Node::Environment.create(:production, [], '')
handler = Puppet::Network::HTTP::API::V2::Environments.new(Puppet::Environments::Static.new(environment))
response = Puppet::Network::HTTP::MemoryResponse.new

handler.call(Puppet::Network::HTTP::Request.from_hash(:headers => { 'accept' => 'application/json' }), response)

expect(response.body).to validate_against('api/schemas/environments.json')
end

class FakeEnvironment < Puppet::Node::Environment
def modules
fake_module = Puppet::Module.new('testing', '/somewhere/on/disk', self)
fake_module.version = "1.2.3"
[fake_module]
end
end
end

0 comments on commit c7e3697

Please sign in to comment.