Skip to content

Commit 2e857e5

Browse files
add Appgroupname api and uniqueness app group name (#234)
* add app group name api * add uniqueness appgroup name validation * update the commented spec
1 parent 5ca7f4c commit 2e857e5

File tree

7 files changed

+134
-5
lines changed

7 files changed

+134
-5
lines changed

app/controllers/api/v2/infrastructures_controller.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,37 @@ def profile_by_cluster_name
5656
}
5757
end
5858

59+
60+
def profile_by_app_group_name
61+
@app_group = AppGroup.find_by(
62+
name: params[:app_group_name],
63+
)
64+
65+
if @app_group.blank? || !@app_group.available?
66+
render(json: {
67+
success: false,
68+
errors: ['App Group not found'],
69+
code: 404,
70+
}, status: :not_found) && return
71+
end
72+
73+
@helm_infrastructure = @app_group.helm_infrastructure
74+
75+
render json: {
76+
app_group_name: @app_group.name,
77+
app_group_secret: @app_group.secret_key,
78+
capacity: @helm_infrastructure.helm_cluster_template.name,
79+
cluster_name: @helm_infrastructure.cluster_name,
80+
kibana_address: @helm_infrastructure&.kibana_address,
81+
status: @helm_infrastructure.status,
82+
provisioning_status: @helm_infrastructure.provisioning_status,
83+
updated_at: @helm_infrastructure.updated_at.strftime(Figaro.env.timestamp_format),
84+
meta: {
85+
service_names: @helm_infrastructure.default_service_names
86+
},
87+
}
88+
end
89+
5990
def profile_curator
6091
if Figaro.env.es_curator_client_key != params[:client_key]
6192
render(json: {

app/models/app_group.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class AppGroup < ApplicationRecord
22
validates :name, :secret_key, presence: true
3+
validates :name, uniqueness: true
34

45
has_many :barito_apps
56
has_many :app_group_users

config/routes.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
get :profile_by_cluster_name,
3939
to: 'infrastructures#profile_by_cluster_name',
4040
defaults: { format: :json }
41+
get :profile_by_app_group_name,
42+
to: 'infrastructures#profile_by_app_group_name',
43+
defaults: { format: :json }
4144
get :profile_index,
4245
to: 'infrastructures#profile_index',
4346
defaults: { format: :json }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddIndexToAppGroups < ActiveRecord::Migration[5.2]
2+
def change
3+
add_index :app_groups, :name
4+
end
5+
end

db/schema.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2021_07_23_011211) do
13+
ActiveRecord::Schema.define(version: 2021_11_15_000000) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
@@ -60,6 +60,7 @@
6060
t.string "secret_key"
6161
t.integer "log_retention_days", default: 30
6262
t.string "environment", default: "PRODUCTION"
63+
t.index ["name"], name: "index_app_groups_on_name"
6364
t.index ["secret_key"], name: "index_app_groups_on_secret_key"
6465
end
6566

@@ -83,7 +84,7 @@
8384

8485
create_table "cluster_templates", force: :cascade do |t|
8586
t.string "name"
86-
t.jsonb "instances"
87+
t.jsonb "manifest"
8788
t.jsonb "options"
8889
t.datetime "created_at", null: false
8990
t.datetime "updated_at", null: false
@@ -120,6 +121,12 @@
120121
t.index ["name"], name: "index_ext_apps_on_name", unique: true
121122
end
122123

124+
create_table "group_roles", force: :cascade do |t|
125+
t.string "name", null: false
126+
t.datetime "created_at", null: false
127+
t.datetime "updated_at", null: false
128+
end
129+
123130
create_table "group_users", force: :cascade do |t|
124131
t.bigint "group_id"
125132
t.bigint "user_id"
@@ -188,7 +195,7 @@
188195
t.datetime "created_at", null: false
189196
t.datetime "updated_at", null: false
190197
t.integer "cluster_template_id"
191-
t.jsonb "instances", default: {}, null: false
198+
t.jsonb "manifest", default: {}, null: false
192199
t.jsonb "options", default: {}, null: false
193200
t.jsonb "manifests", default: {}, null: false
194201
t.index ["app_group_id"], name: "index_infrastructures_on_app_group_id"

spec/models/app_group_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
require 'rails_helper'
22

33
RSpec.describe AppGroup, type: :model do
4+
context 'uniqueness validation' do
5+
describe ':name' do
6+
subject { create(:app_group) }
7+
8+
it { is_expected.to validate_uniqueness_of(:name) }
9+
end
10+
end
11+
12+
context 'presence validation' do
13+
it 'should check presence of name' do
14+
expect(build(:app_group, name: nil)).not_to be_valid
15+
end
16+
17+
it 'should check presence of secret_key' do
18+
expect(build(:app_group, secret_key: nil)).not_to be_valid
19+
end
20+
end
21+
422
context 'Setup Application' do
523
let(:app_group_props) { build(:app_group) }
624
let(:helm_cluster_template) { create(:helm_cluster_template) }

spec/requests/api/v2/infrastructures_spec.rb

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,72 @@
145145
end
146146
end
147147

148+
describe 'Profile by App Group Name API' do
149+
let(:headers) do
150+
{ 'ACCEPT' => 'application/json', 'HTTP_ACCEPT' => 'application/json' }
151+
end
152+
153+
it 'should return profile information of registered app when supplied cluster name' do
154+
app_group = create(:app_group)
155+
helm_infrastructure = create(
156+
:helm_infrastructure,
157+
app_group: app_group,
158+
status: HelmInfrastructure.statuses[:active]
159+
)
160+
161+
get api_v2_profile_by_app_group_name_path,
162+
params: { access_token: @access_token, app_group_name: app_group.name },
163+
headers: headers
164+
json_response = JSON.parse(response.body)
165+
166+
%w[cluster_name status provisioning_status].
167+
each do |key|
168+
expect(json_response.key?(key)).to eq(true)
169+
expect(json_response[key]).to eq(helm_infrastructure.send(key.to_sym))
170+
end
171+
172+
expect(json_response['app_group_name']).to eq(helm_infrastructure.app_group_name)
173+
expect(json_response['app_group_secret']).to eq(helm_infrastructure.app_group_secret)
174+
expect(json_response['capacity']).to eq(helm_infrastructure.helm_cluster_template.name)
175+
expect(json_response.key?('updated_at')).to eq(true)
176+
expect(json_response['kibana_address']).to eq(helm_infrastructure.kibana_address)
177+
end
178+
179+
it 'should return K8s Kibana if activated' do
180+
app_group = create(:app_group)
181+
helm_infrastructure = create(:helm_infrastructure,
182+
app_group: app_group,
183+
status: HelmInfrastructure.statuses[:active],
184+
cluster_name: 'haza',
185+
)
186+
187+
get api_v2_profile_by_app_group_name_path,
188+
params: { access_token: @access_token, app_group_name: app_group.name },
189+
headers: headers
190+
json_response = JSON.parse(response.body)
191+
192+
expect(json_response['kibana_address']).to eq("#{helm_infrastructure.cluster_name}-kb-http.barito-worker.svc:5601")
193+
expect(json_response['kibana_address']).to eq(helm_infrastructure.kibana_address)
194+
end
195+
196+
context 'when App Group unavailable' do
197+
it 'should return 404' do
198+
error_msg = 'App Group not found'
199+
app_group = create(:app_group)
200+
helm_infrastructure = create(:helm_infrastructure, app_group: app_group)
201+
202+
get api_v2_profile_by_app_group_name_path,
203+
params: { access_token: @access_token, app_group_name: app_group.name },
204+
headers: headers
205+
json_response = JSON.parse(response.body)
206+
207+
expect(json_response['success']).to eq false
208+
expect(json_response['code']).to eq 404
209+
expect(json_response['errors']).to eq [error_msg]
210+
end
211+
end
212+
end
213+
148214
describe 'Profile for Curator' do
149215
let(:headers) do
150216
{ 'ACCEPT' => 'application/json', 'HTTP_ACCEPT' => 'application/json' }
@@ -213,8 +279,6 @@
213279
}
214280
].to_json
215281
end
216-
217-
218282
end
219283

220284
describe 'Profile for Prometheus Exporters' do

0 commit comments

Comments
 (0)