Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable environments not available in fs #147 #156

Merged
merged 1 commit into from
May 22, 2023
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
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