Skip to content

Commit

Permalink
API client can retrieve env variables for a revision
Browse files Browse the repository at this point in the history
[finishes #162994516]

Signed-off-by: Mona Mohebbi <[email protected]>
  • Loading branch information
cwlbraa authored and monamohebbi committed Jan 28, 2019
1 parent 20a30c1 commit 3281fb5
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 4 deletions.
12 changes: 12 additions & 0 deletions app/controllers/v3/app_revisions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'actions/app_revisions_update'
require 'presenters/v3/revision_presenter'
require 'controllers/v3/mixins/app_sub_resource'
require 'presenters/v3/revision_environment_variables_presenter'

class AppRevisionsController < ApplicationController
include AppSubResource
Expand Down Expand Up @@ -51,4 +52,15 @@ def update

render status: :ok, json: Presenters::V3::RevisionPresenter.new(revision)
end

def show_environment_variables
app, space, org = AppFetcher.new.fetch(hashed_params[:guid])
app_not_found! unless app && permission_queryer.can_read_from_space?(space.guid, org.guid)
unauthorized! unless permission_queryer.can_read_secrets_in_space?(space.guid, org.guid)

revision = RevisionModel.find(guid: hashed_params[:revision_guid])
resource_not_found!(:revision) unless revision && revision.app_guid == app.guid

render status: :ok, json: Presenters::V3::RevisionEnvironmentVariablesPresenter.new(revision)
end
end
10 changes: 6 additions & 4 deletions app/presenters/v3/app_environment_variables_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ def to_hash
links: build_links
}

if !app.environment_variables.nil?
app.environment_variables.each do |key, value|
result[:var][key.to_sym] = value
end
env_vars&.each do |key, value|
result[:var][key.to_sym] = value
end

result
end

private

def env_vars
app&.environment_variables
end

def build_links
url_builder = VCAP::CloudController::Presenters::ApiUrlBuilder.new

Expand Down
45 changes: 45 additions & 0 deletions app/presenters/v3/revision_environment_variables_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'presenters/v3/app_environment_variables_presenter'
require 'presenters/v3/base_presenter'

module VCAP::CloudController
module Presenters
module V3
class RevisionEnvironmentVariablesPresenter
attr_reader :revision

def initialize(revision)
@revision = revision
end

def to_hash
result = {
var: {},
links: build_links
}

env_vars&.each do |key, value|
result[:var][key.to_sym] = value
end

result
end

private

def env_vars
revision&.environment_variables
end

def build_links
url_builder = VCAP::CloudController::Presenters::ApiUrlBuilder.new

{
self: { href: url_builder.build_url(path: "/v3/apps/#{revision.app.guid}/revisions/#{revision.guid}/environment_variables") },
revision: { href: url_builder.build_url(path: "/v3/apps/#{revision.app.guid}/revisions/#{revision.guid}") },
app: { href: url_builder.build_url(path: "/v3/apps/#{revision.app.guid}") }
}
end
end
end
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
# app revisions
get '/apps/:guid/revisions', to: 'app_revisions#index'
get '/apps/:guid/revisions/:revision_guid', to: 'app_revisions#show'
get '/apps/:guid/revisions/:revision_guid/environment_variables', to: 'app_revisions#show_environment_variables'
patch '/apps/:guid/revisions/:revision_guid', to: 'app_revisions#update'

# environment variables
Expand Down
28 changes: 28 additions & 0 deletions spec/request/revisions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,32 @@
)
end
end

describe 'GET /v3/apps/:guid/revision/:revguid/environment_variables' do
let!(:revision2) { VCAP::CloudController::RevisionModel.make(
app: app_model,
version: 43,
environment_variables: { 'key' => 'value' },
)
}

it 'gets the environment variables for the revision' do
get "/v3/apps/#{app_model.reload.guid}/revisions/#{revision2.guid}/environment_variables", nil, user_header
expect(last_response.status).to eq(200), last_response.body

parsed_response = MultiJson.load(last_response.body)
expect(parsed_response).to be_a_response_like(
{
'var' => {
'key' => 'value'
},
'links' => {
'self' => { 'href' => "#{link_prefix}/v3/apps/#{app_model.guid}/revisions/#{revision2.guid}/environment_variables" },
'revision' => { 'href' => "#{link_prefix}/v3/apps/#{app_model.guid}/revisions/#{revision2.guid}" },
'app' => { 'href' => "#{link_prefix}/v3/apps/#{app_model.guid}" },
}
}
)
end
end
end
1 change: 1 addition & 0 deletions spec/support/fakes/blueprints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ module VCAP::CloudController
end

RevisionModel.blueprint do
app { AppModel.make }
end

TestModel.blueprint do
Expand Down
56 changes: 56 additions & 0 deletions spec/unit/controllers/v3/app_revisions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,60 @@
end
end
end

describe '#show_environment_variables' do
let!(:droplet) { VCAP::CloudController::DropletModel.make }
let!(:app_model) { VCAP::CloudController::AppModel.make(droplet: droplet) }
let!(:space) { app_model.space }
let(:user) { VCAP::CloudController::User.make }
let(:revision) { VCAP::CloudController::RevisionModel.make(
app: app_model,
version: 808,
droplet_guid: droplet.guid,
environment_variables: { 'key' => 'value' },
)
}

before do
set_current_user(user)
allow_user_read_access_for(user, spaces: [space])
allow_user_secret_access(user, space: space)
end

it 'returns 200 and shows environment_variables' do
get :show_environment_variables, params: { guid: app_model.guid, revision_guid: revision.guid }

expect(response.status).to eq(200)
expect(parsed_body['var']).to eq({ 'key' => 'value' })
end

context 'when retrieving env variables for revision that do not exist' do
it '404s' do
get :show_environment_variables, params: { guid: app_model.guid, revision_guid: 'nonsense' }
expect(response.status).to eq(404)
end
end

context 'when access to the space is restricted' do
before do
disallow_user_read_access(user, space: space)
end

it '404s' do
get :show_environment_variables, params: { guid: app_model.guid, revision_guid: 'nonsense' }
expect(response.status).to eq(404)
end
end

context 'when access is restricted' do
before do
disallow_user_secret_access(user, space: space)
end

it '403s' do
get :show_environment_variables, params: { guid: app_model.guid, revision_guid: 'nonsense' }
expect(response.status).to eq(403)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'spec_helper'
require 'presenters/v3/revision_environment_variables_presenter'

module VCAP::CloudController::Presenters::V3
RSpec.describe RevisionEnvironmentVariablesPresenter do
let(:revision) do
VCAP::CloudController::RevisionModel.make(
environment_variables: { 'CUSTOM_ENV_VAR' => 'hello' },
)
end

subject(:presenter) { RevisionEnvironmentVariablesPresenter.new(revision) }

describe '#to_hash' do
let(:result) { presenter.to_hash }

it 'presents the app environment variables as json' do
expect(result).to eq({
var: {
CUSTOM_ENV_VAR: 'hello'
},
links: {
self: {
href: "#{link_prefix}/v3/apps/#{revision.app.guid}/revisions/#{revision.guid}/environment_variables",
},
revision: {
href: "#{link_prefix}/v3/apps/#{revision.app.guid}/revisions/#{revision.guid}",
},
app: {
href: "#{link_prefix}/v3/apps/#{revision.app.guid}",
},
}
})
end
end
end
end

0 comments on commit 3281fb5

Please sign in to comment.