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

Project Specific environment variables in preview #3559

Merged
merged 4 commits into from Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gemfile.lock
Expand Up @@ -264,7 +264,7 @@ GEM
connection_pool (2.2.1)
crack (0.4.3)
safe_yaml (~> 1.0.0)
crass (1.0.4)
crass (1.0.5)
dalli (2.7.6)
ddtrace (0.14.2)
msgpack
Expand Down Expand Up @@ -344,7 +344,7 @@ GEM
railties (>= 4)
request_store (~> 1.0)
logstash-event (1.2.02)
loofah (2.2.3)
loofah (2.3.1)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mail (2.7.1)
Expand Down
Expand Up @@ -54,7 +54,10 @@ def preview
@project = Project.find(params[:project_id])
end

@groups = SamsonEnv.env_groups(Deploy.new(project: @project), deploy_groups, preview: true)
options = {project_specific: params[:project_specific]}
options.merge!(params[:preview] ? {preview: true, resolve_secrets: true} : {resolve_secrets: false})
sathishavm marked this conversation as resolved.
Show resolved Hide resolved

@groups = SamsonEnv.env_groups(Deploy.new(project: @project), deploy_groups, **options)

respond_to do |format|
format.html
Expand Down
21 changes: 17 additions & 4 deletions plugins/env/app/models/environment_variable.rb
Expand Up @@ -25,7 +25,7 @@ class << self
# preview parameter can be used to not raise an error,
# but return a value with a helpful message
# also used by an external plugin
def env(deploy, deploy_group, preview: false, resolve_secrets: true)
def env(deploy, deploy_group, preview: false, resolve_secrets: true, project_specific: nil)
env = {}

if deploy_group && deploy.project.config_service?
Expand All @@ -36,7 +36,7 @@ def env(deploy, deploy_group, preview: false, resolve_secrets: true)
env.merge! env_vars_from_repo(env_repo_name, deploy.project, deploy_group)
end

env.merge! env_vars_from_db(deploy, deploy_group)
env.merge! env_vars_from_db(deploy, deploy_group, project_specific: project_specific)

resolve_dollar_variables(env)
resolve_secrets(deploy.project, deploy_group, env, preview: preview) if resolve_secrets
Expand Down Expand Up @@ -73,11 +73,24 @@ def config_service_location(project, deploy_group, display:)

private

def env_vars_from_db(deploy, deploy_group)
def env_vars_from_db(deploy, deploy_group, project_specific: nil)
# Project Specific:
# nil => project env + groups env
# true => project env
sathishavm marked this conversation as resolved.
Show resolved Hide resolved
# false => groups env
project_envs =
if project_specific.to_s == "true"
deploy.project.environment_variables
elsif project_specific.to_s == "false"
deploy.project.environment_variable_groups.flat_map(&:environment_variables)
sathishavm marked this conversation as resolved.
Show resolved Hide resolved
else
deploy.project.nested_environment_variables
end

variables =
deploy.environment_variables +
(deploy.stage&.environment_variables || []) +
deploy.project.nested_environment_variables
project_envs
variables.sort_by!(&:priority)
variables.each_with_object({}) do |ev, all|
all[ev.name] = ev.value if !all[ev.name] && ev.matches_scope?(deploy_group)
Expand Down
Expand Up @@ -46,6 +46,17 @@ def self.it_destroys
}
)
end

let!(:other_env_group) do
EnvironmentVariableGroup.create!(
name: "OtherG1",
environment_variables_attributes: {
0 => {name: "X", value: "Y"},
1 => {name: "Y", value: "Z", scope_type_and_id: "DeployGroup-#{deploy_group.id}"}
}
)
end

let(:other_project) do
p = project.dup
p.name = 'xxxxx'
Expand Down Expand Up @@ -128,15 +139,16 @@ def unauthorized_env_group
end

it "calls env with preview" do
EnvironmentVariable.expects(:env).with(anything, anything, preview: true).times(3)
EnvironmentVariable.expects(:env).
with(anything, anything, project_specific: nil, resolve_secrets: false).times(3)
get :preview, params: {group_id: env_group.id}
assert_response :success
end
end

describe "a json GET to #preview" do
it "succeeds" do
get :preview, params: {group_id: env_group.id, project_id: project.id}, format: :json
get :preview, params: {group_id: env_group.id, project_id: project.id, preview: true}, format: :json
assert_response :success
json_response = JSON.parse response.body
json_response['groups'].sort.must_equal [
Expand All @@ -160,6 +172,45 @@ def unauthorized_env_group
get :preview, params: {group_id: env_group.id, project_id: project.id, deploy_group: "pod23"}, format: :json
end
end

describe "project_specific" do
before do
EnvironmentVariable.create!(parent: project, name: 'B', value: 'b')
ProjectEnvironmentVariableGroup.create!(environment_variable_group: other_env_group, project: project)
end
it "renders only project env" do
get :preview, params: {project_id: project.id, project_specific: true}, format: :json
assert_response :success
json_response = JSON.parse response.body
json_response['groups'].sort.must_equal [
[".pod-100", {"B" => "b"}],
[".pod1", {"B" => "b"}],
[".pod2", {"B" => "b"}]
]
end

it "renders only groups env" do
get :preview, params: {project_id: project.id, project_specific: false}, format: :json
assert_response :success
json_response = JSON.parse response.body
json_response['groups'].sort.must_equal [
[".pod-100", {"Y" => "Z", "X" => "Y"}],
[".pod1", {"X" => "Y"}],
[".pod2", {"X" => "Y"}]
]
end

it "renders without project_specific" do
get :preview, params: {project_id: project.id, project_specific: nil}, format: :json
assert_response :success
json_response = JSON.parse response.body
json_response['groups'].sort.must_equal [
[".pod-100", {"B" => "b", "Y" => "Z", "X" => "Y"}],
[".pod1", {"B" => "b", "X" => "Y"}],
[".pod2", {"B" => "b", "X" => "Y"}]
]
end
end
end
end

Expand Down
10 changes: 10 additions & 0 deletions plugins/env/test/models/environment_variable_test.rb
Expand Up @@ -243,6 +243,16 @@ def fake_response(response)
)
end

it "includes only project specific environment variables" do
EnvironmentVariable.env(deploy, nil, project_specific: true).
must_equal("PROJECT" => "PROJECT")
end

it "includes only project groups environment variables" do
EnvironmentVariable.env(deploy, nil, project_specific: false).
must_equal("X" => "Y", "Y" => "Z")
end

describe "secrets" do
before do
create_secret 'global/global/global/foobar'
Expand Down