Skip to content

Commit

Permalink
Disable environments not available in fs #147 (#156)
Browse files Browse the repository at this point in the history
PuppetDB might report environments that are not actually
available in the file system (e.g. due to short-lived
feature branches). This change displays the environments
in the dropdown, but disables them, so they cannot be
selected.
  • Loading branch information
oneiros authored May 22, 2023
1 parent 345f586 commit dd61e98
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
17 changes: 14 additions & 3 deletions app/models/environment.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
class Environment < HieraModel
attribute :name, :string
attribute :in_use, :boolean, default: false
attribute :available, :boolean, default: false

def self.all
environments_in_use = PuppetDbClient.environments
available_environments = HieraData.environments
unused_environments = available_environments - environments_in_use
environments_in_use.sort.map { |e| new(name: e, in_use: true) } +
unused_environments.sort.map { |e| new(name: e) }
all_environments = {}
environments_in_use.sort.each do |e|
all_environments[e] = { name: e, in_use: true }
end
available_environments.sort.each do |e|
all_environments[e] ||= { name: e }
all_environments[e][:available] = true
end
all_environments.map { |_, attributes| new(**attributes) }
end

def self.find(name)
Expand All @@ -22,6 +29,10 @@ def in_use?
in_use
end

def available?
available
end

def ==(other)
other.is_a?(Environment) && name == other.name
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/environments/_select_environment.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<select name="environment" data-controller="select-navigation" data-action="change->select-navigation#navigate" data-slim-select-target="dropdown">
<option value="" <%= "selected" if @environment.nil? %> data-url="<%= environments_path %>"></option>
<% @environments.each do |environment| %>
<option data-url="<%= environment_nodes_path(environment) %>" <%= "selected" if environment == @environment %>>
<option data-url="<%= environment_nodes_path(environment) %>" <%= "selected" if environment == @environment %> <%= "disabled" unless environment.available? %>>
<%= environment.name %><%= " (unused)" unless environment.in_use? %>
</option>
<% end %>
Expand Down
10 changes: 9 additions & 1 deletion test/models/environment_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'test_helper'

class EnvironmentTest < ActiveSupport::TestCase
test "list the environments" do
test "::all lists the environments" do
expected_environments = %w(
development
eyaml
Expand All @@ -18,6 +18,14 @@ class EnvironmentTest < ActiveSupport::TestCase
assert_equal expected_environments, Environment.all.map(&:name)
end

test "::all correctly marks unavailable environments" do
PuppetDbClient.stub :environments, ["unavailable"] do
environments = Environment.all
unavailable_environment = environments.find { |e| e.name == "unavailable" }
assert_not unavailable_environment.available?
end
end

test "::find correctly sets the `in_use` flag" do
development = Environment.find("development")
assert development.in_use?
Expand Down

0 comments on commit dd61e98

Please sign in to comment.