Skip to content

Commit 66dbfe5

Browse files
authored
✨ Support list all application identities. (#832)
Add: /applications/:id/identities to list all identities (including defaults) for all kinds. Inject default creds in addon adapter Application() method. Required by: - konveyor/tackle2-addon-analyzer#154 - konveyor/tackle2-addon-discovery#19 Incidental: Small refactor/re-org of Application binding for consistency. --------- Signed-off-by: Jeff Ortel <[email protected]>
1 parent 14de92c commit 66dbfe5

File tree

6 files changed

+158
-70
lines changed

6 files changed

+158
-70
lines changed

addon/task.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ func (h *Task) Application() (r *api.Application, err error) {
3939
return
4040
}
4141
r, err = h.richClient.Application.Get(appRef.ID)
42+
if err != nil {
43+
return
44+
}
45+
r.Identities = []api.Ref{}
46+
identities, err := h.richClient.Application.Identity(appRef.ID).List()
47+
if err != nil {
48+
return
49+
}
50+
for _, identity := range identities {
51+
r.Identities = append(
52+
r.Identities,
53+
api.Ref{
54+
ID: identity.ID,
55+
Name: identity.Name,
56+
})
57+
}
4258
return
4359
}
4460

api/identity.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ const (
1717
IdentitiesRoot = "/identities"
1818
IdentityRoot = IdentitiesRoot + "/:" + ID
1919
//
20-
AppIdentitiesRoot = ApplicationRoot + "/identities/:" + Kind
20+
AppIdentitiesRoot = ApplicationRoot + "/identities"
21+
AppIdentitiesKindRoot = AppIdentitiesRoot + "/:" + Kind
2122
)
2223

2324
// Params.
@@ -40,6 +41,7 @@ func (h IdentityHandler) AddRoutes(e *gin.Engine) {
4041
routeGroup.DELETE(IdentityRoot, h.Delete)
4142
//
4243
routeGroup.GET(AppIdentitiesRoot, h.AppList)
44+
routeGroup.GET(AppIdentitiesKindRoot, h.AppList)
4345
}
4446

4547
// Get godoc
@@ -142,7 +144,10 @@ func (h IdentityHandler) AppList(ctx *gin.Context) {
142144
db := h.DB(ctx)
143145
db = db.Joins("JOIN ApplicationIdentity j ON j.IdentityID = Identity.ID")
144146
db = db.Where("j.ApplicationID", id)
145-
err := db.Find(&direct, "kind", kind).Error
147+
if kind != "" {
148+
db = db.Where("kind", kind)
149+
}
150+
err := db.Find(&direct).Error
146151
if err != nil {
147152
_ = ctx.Error(err)
148153
return
@@ -157,18 +162,24 @@ func (h IdentityHandler) AppList(ctx *gin.Context) {
157162
return
158163
}
159164
r.With(m)
160-
mp[r.Kind] = r
165+
mp[m.Kind] = r
161166
}
162167
db = h.DB(ctx)
163168
var indirect []model.Identity
164169
db = db.Where("default", true)
165-
err = db.Find(&indirect, "kind", kind).Error
170+
if kind != "" {
171+
db = db.Where("kind", kind)
172+
}
173+
err = db.Find(&indirect).Error
166174
if err != nil {
167175
_ = ctx.Error(err)
168176
return
169177
}
170178
for i := range indirect {
171179
m := &indirect[i]
180+
if _, found := mp[m.Kind]; found {
181+
continue
182+
}
172183
r := Identity{}
173184
err := h.Decrypt(ctx, m)
174185
if err != nil {

binding/application.go

Lines changed: 105 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,54 @@ func (h *Application) Bucket(id uint) (b *BucketContent) {
6161
return
6262
}
6363

64-
// FindIdentity by kind.
65-
func (h *Application) FindIdentity(id uint, kind string) (r *api.Identity, found bool, err error) {
66-
list := []api.Identity{}
67-
p := Param{
68-
Key: api.Decrypted,
69-
Value: "1",
64+
// Tags returns the tags API.
65+
func (h *Application) Tags(id uint) (tg AppTags) {
66+
tg = AppTags{
67+
client: h.client,
68+
appId: id,
7069
}
71-
path := Path(api.AppIdentitiesRoot).Inject(Params{api.ID: id, api.Kind: kind})
72-
err = h.client.Get(path, &list, p)
73-
if err != nil {
74-
return
70+
return
71+
}
72+
73+
// Facts returns the facts API.
74+
func (h *Application) Facts(id uint) (f AppFacts) {
75+
f = AppFacts{
76+
client: h.client,
77+
appId: id,
7578
}
76-
for i := range list {
77-
r = &list[i]
78-
found = true
79-
return
79+
return
80+
}
81+
82+
// Analysis returns the analysis API.
83+
func (h *Application) Analysis(id uint) (a Analysis) {
84+
a = Analysis{
85+
client: h.client,
86+
appId: id,
8087
}
8188
return
8289
}
8390

84-
// Tags returns the tags API.
85-
func (h *Application) Tags(id uint) (tg AppTags) {
86-
tg = AppTags{
91+
// Manifest returns the manifest API.
92+
func (h *Application) Manifest(id uint) (f AppManifest) {
93+
f = AppManifest{
94+
client: h.client,
95+
appId: id,
96+
}
97+
return
98+
}
99+
100+
// Identity returns the identity API.
101+
func (h *Application) Identity(id uint) (f AppIdentity) {
102+
f = AppIdentity{
103+
client: h.client,
104+
appId: id,
105+
}
106+
return
107+
}
108+
109+
// Assessment returns the assessment API.
110+
func (h *Application) Assessment(id uint) (f AppAssessment) {
111+
f = AppAssessment{
87112
client: h.client,
88113
appId: id,
89114
}
@@ -193,15 +218,6 @@ func (h *AppTags) unique(ids []uint) (u []uint) {
193218
return
194219
}
195220

196-
// Facts returns the tags API.
197-
func (h *Application) Facts(id uint) (f AppFacts) {
198-
f = AppFacts{
199-
client: h.client,
200-
appId: id,
201-
}
202-
return
203-
}
204-
205221
// AppFacts sub-resource API.
206222
// Provides association management of facts.
207223
type AppFacts struct {
@@ -273,31 +289,7 @@ func (h *AppFacts) Replace(facts api.Map) (err error) {
273289
return
274290
}
275291

276-
// Analysis returns the analysis API.
277-
func (h *Application) Analysis(id uint) (a Analysis) {
278-
a = Analysis{
279-
client: h.client,
280-
appId: id,
281-
}
282-
return
283-
}
284-
285-
// Create an Application Assessment.
286-
func (h *Application) CreateAssesment(id uint, r *api.Assessment) (err error) {
287-
path := Path(api.AppAssessmentsRoot).Inject(Params{api.ID: id})
288-
err = h.client.Post(path, &r)
289-
return
290-
}
291-
292-
// Get Application Assessments.
293-
func (h *Application) GetAssesments(id uint) (list []api.Assessment, err error) {
294-
list = []api.Assessment{}
295-
path := Path(api.AppAssessmentsRoot).Inject(Params{api.ID: id})
296-
err = h.client.Get(path, &list)
297-
return
298-
}
299-
300-
// Analysis API.
292+
// Analysis sub-resource API.
301293
type Analysis struct {
302294
client *Client
303295
appId uint
@@ -339,15 +331,7 @@ func (h *Analysis) Create(manifest, encoding string) (r *api.Analysis, err error
339331
return
340332
}
341333

342-
// Manifest returns the tags API.
343-
func (h *Application) Manifest(id uint) (f AppManifest) {
344-
f = AppManifest{
345-
client: h.client,
346-
appId: id,
347-
}
348-
return
349-
}
350-
334+
// AppManifest sub-resource API.
351335
type AppManifest struct {
352336
client *Client
353337
appId uint
@@ -363,3 +347,64 @@ func (h *AppManifest) Get(param ...Param) (r *api.Manifest, err error) {
363347
err = h.client.Get(path, r, param...)
364348
return
365349
}
350+
351+
// AppIdentity sub-resource API.
352+
type AppIdentity struct {
353+
client *Client
354+
appId uint
355+
}
356+
357+
// List identities.
358+
func (h AppIdentity) List() (list []api.Identity, err error) {
359+
p := Param{
360+
Key: api.Decrypted,
361+
Value: "1",
362+
}
363+
path := Path(api.AppIdentitiesRoot).Inject(Params{api.ID: h.appId})
364+
err = h.client.Get(path, &list, p)
365+
if err != nil {
366+
return
367+
}
368+
return
369+
}
370+
371+
// Find by kind.
372+
func (h AppIdentity) Find(kind string) (r *api.Identity, found bool, err error) {
373+
list := []api.Identity{}
374+
p := Param{
375+
Key: api.Decrypted,
376+
Value: "1",
377+
}
378+
path := Path(api.AppIdentitiesKindRoot).Inject(Params{api.ID: h.appId, api.Kind: kind})
379+
err = h.client.Get(path, &list, p)
380+
if err != nil {
381+
return
382+
}
383+
for i := range list {
384+
r = &list[i]
385+
found = true
386+
return
387+
}
388+
return
389+
}
390+
391+
// AppAssessment sub-resource API.
392+
type AppAssessment struct {
393+
client *Client
394+
appId uint
395+
}
396+
397+
// Create an Assessment.
398+
func (h AppAssessment) Create(r *api.Assessment) (err error) {
399+
path := Path(api.AppAssessmentsRoot).Inject(Params{api.ID: h.appId})
400+
err = h.client.Post(path, &r)
401+
return
402+
}
403+
404+
// List Assessments.
405+
func (h AppAssessment) List() (list []api.Assessment, err error) {
406+
list = []api.Assessment{}
407+
path := Path(api.AppAssessmentsRoot).Inject(Params{api.ID: h.appId})
408+
err = h.client.Get(path, &list)
409+
return
410+
}

hack/add/identity.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ host="${HOST:-localhost:8080}"
55
id="${1:-0}" # 0=system-assigned.
66
name="${2:-Test}"
77
kind="${3:-source}"
8-
def="${4:-0}"
8+
def="${4:-false}"
99

1010
# create identity.
1111
curl -X POST ${host}/identities \

test/api/application/identity_test.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ func TestFindIdentity(t *testing.T) {
3737
defer func() {
3838
_ = RichClient.Identity.Delete(indirect.ID)
3939
}()
40+
indirect2 := &api.Identity{
41+
Kind: "Test",
42+
Name: "indirect-shadowed",
43+
Default: true,
44+
}
45+
err = RichClient.Identity.Create(indirect2)
46+
assert.Must(t, err)
47+
defer func() {
48+
_ = RichClient.Identity.Delete(indirect2.ID)
49+
}()
4050
application := &api.Application{
4151
Name: t.Name(),
4252
Identities: []api.Ref{{ID: direct.ID}},
@@ -47,7 +57,7 @@ func TestFindIdentity(t *testing.T) {
4757
_ = Application.Delete(application.ID)
4858
}()
4959
// Find direct.
50-
identity, found, err := Application.FindIdentity(application.ID, direct.Kind)
60+
identity, found, err := Application.Identity(application.ID).Find(direct.Kind)
5161
assert.Must(t, err)
5262
if found {
5363
if identity.ID != direct.ID {
@@ -57,7 +67,7 @@ func TestFindIdentity(t *testing.T) {
5767
t.Errorf("direct not found")
5868
}
5969
// Find indirect.
60-
identity, found, err = Application.FindIdentity(application.ID, indirect.Kind)
70+
identity, found, err = Application.Identity(application.ID).Find(indirect.Kind)
6171
assert.Must(t, err)
6272
if found {
6373
if identity.ID != indirect.ID {
@@ -67,9 +77,15 @@ func TestFindIdentity(t *testing.T) {
6777
t.Errorf("indirect not found")
6878
}
6979
// Not find indirect.
70-
_, found, err = Application.FindIdentity(application.ID, "None")
80+
_, found, err = Application.Identity(application.ID).Find("None")
7181
assert.Must(t, err)
7282
if found {
7383
t.Errorf("not found expected")
7484
}
85+
// List
86+
list, err := Application.Identity(application.ID).List()
87+
assert.Must(t, err)
88+
if len(list) != 2 {
89+
t.Errorf("list expected: 1, actual: %d", len(list))
90+
}
7591
}

test/api/assessment/api_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestAssessmentCRUD(t *testing.T) {
2222
app := api.Application{Name: r.Application.Name}
2323
assert.Must(t, RichClient.Application.Create(&app))
2424
r.Application.ID = app.ID
25-
err := RichClient.Application.CreateAssesment(app.ID, &r)
25+
err := RichClient.Application.Assessment(app.ID).Create(&r)
2626
if err != nil {
2727
t.Errorf(err.Error())
2828
}
@@ -39,7 +39,7 @@ func TestAssessmentCRUD(t *testing.T) {
3939
}
4040

4141
// Get via parent object Application.
42-
gotList, err := RichClient.Application.GetAssesments(r.Application.ID)
42+
gotList, err := RichClient.Application.Assessment(r.Application.ID).List()
4343
if err != nil {
4444
t.Errorf(err.Error())
4545
}

0 commit comments

Comments
 (0)