diff --git a/api/schemas/environments.json b/api/schemas/environments.json index 3b99869d1..58931180a 100644 --- a/api/schemas/environments.json +++ b/api/schemas/environments.json @@ -1,39 +1,61 @@ { "$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" } - } + }, + "config_version": { "type": "string" }, + "environment_timeout": { "type": ["integer", "string"] } }, - "required": ["modulepath", "manifest"] + "required": ["modulepath", "manifest", "environment_timeout", "config_version"], + "oneOf": [ + { + "title": "numeric timeout", + "properties": { + "environment_timeout": { + "type": "integer", + "minimum": 0 + } + } + }, + { + "title": "unlimited timeout", + "properties": { + "environment_timeout": { + "type": "string", + "enum": ["unlimited"] + } + } + } + ] } }, "required": ["settings"] } } } }, "required": ["search_paths", "environments"] } diff --git a/lib/puppet/network/http/api/v2/environments.rb b/lib/puppet/network/http/api/v2/environments.rb index 6331be857..51a4e04f2 100644 --- a/lib/puppet/network/http/api/v2/environments.rb +++ b/lib/puppet/network/http/api/v2/environments.rb @@ -1,21 +1,35 @@ require 'json' class Puppet::Network::HTTP::API::V2::Environments def initialize(env_loader) @env_loader = env_loader end def call(request, response) response.respond_with(200, "application/json", JSON.dump({ "search_paths" => @env_loader.search_paths, "environments" => Hash[@env_loader.list.collect do |env| [env.name, { "settings" => { "modulepath" => env.full_modulepath, - "manifest" => env.manifest + "manifest" => env.manifest, + "environment_timeout" => timeout(env), + "config_version" => env.config_version || '', } }] end] })) end + + private + + def timeout(env) + ttl = @env_loader.get_conf(env.name).environment_timeout + if ttl == 1.0 / 0.0 # INFINITY + "unlimited" + else + ttl + end + end + end diff --git a/spec/unit/network/http/api/v2/environments_spec.rb b/spec/unit/network/http/api/v2/environments_spec.rb index 6c6d7a581..993e55011 100644 --- a/spec/unit/network/http/api/v2/environments_spec.rb +++ b/spec/unit/network/http/api/v2/environments_spec.rb @@ -1,42 +1,63 @@ require 'spec_helper' require 'puppet/node/environment' require 'puppet/network/http' require 'matchers/json' describe Puppet::Network::HTTP::API::V2::Environments do include JSONMatchers 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 handler.call(Puppet::Network::HTTP::Request.from_hash(:headers => { 'accept' => 'application/json' }), response) expect(response.code).to eq(200) expect(response.type).to eq("application/json") expect(JSON.parse(response.body)).to eq({ "search_paths" => loader.search_paths, "environments" => { "production" => { "settings" => { "modulepath" => [File.expand_path("/first"), File.expand_path("/second")], - "manifest" => File.expand_path("/manifests") + "manifest" => File.expand_path("/manifests"), + "environment_timeout" => 0, + "config_version" => "" } } } }) end - it "the response conforms to the environments schema" do + it "the response conforms to the environments schema for unlimited timeout" do + conf_stub = stub 'conf_stub' + conf_stub.expects(:environment_timeout).returns(1.0 / 0.0) environment = Puppet::Node::Environment.create(:production, []) - handler = Puppet::Network::HTTP::API::V2::Environments.new(Puppet::Environments::Static.new(environment)) + env_loader = Puppet::Environments::Static.new(environment) + env_loader.expects(:get_conf).with(:production).returns(conf_stub) + handler = Puppet::Network::HTTP::API::V2::Environments.new(env_loader) 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 + + it "the response conforms to the environments schema for integer timeout" do + conf_stub = stub 'conf_stub' + conf_stub.expects(:environment_timeout).returns(1) + environment = Puppet::Node::Environment.create(:production, []) + env_loader = Puppet::Environments::Static.new(environment) + env_loader.expects(:get_conf).with(:production).returns(conf_stub) + handler = Puppet::Network::HTTP::API::V2::Environments.new(env_loader) + 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 + end