Commit 426b1fbf authored by ale's avatar ale

Internal refactor of the webappdb client

This will make it easier in the future to replace the implementation
with one that is resource-aware (as input to lookup() is no longer
just a list of 'sites').
parent ce419030
Pipeline #11201 passed with stages
in 1 minute and 55 seconds
......@@ -51,34 +51,21 @@ func (tx *wdbTX) GetUser(ctx context.Context, name string) (*as.RawUser, error)
}
// Find all web resources.
var sites []string
sitemap := make(map[string]*as.Resource)
var sites []*as.Resource
for _, r := range user.Resources {
if r.Type == as.ResourceTypeWebsite || r.Type == as.ResourceTypeDomain {
sites = append(sites, r.Name)
sitemap[r.Name] = r
sites = append(sites, r)
}
}
var cmsinfo map[string][]*webappdb.App
if len(sitemap) == 0 {
goto skip
}
cmsinfo, err = tx.lookup(ctx, sites)
if err != nil {
log.Printf("error looking up webappdb: %v", err)
goto skip
}
for key, values := range cmsinfo {
r, ok := sitemap[key]
if !ok || r.Website == nil {
continue
if len(sites) > 0 {
if err = tx.lookup(ctx, sites); err != nil {
// This error is non-fatal, we just do not
// augment the resources with web app info.
log.Printf("error looking up webappdb: %v", err)
}
r.Website.CMSInfo = values
}
skip:
return user, nil
}
......@@ -87,23 +74,44 @@ func (tx *wdbTX) GetResource(ctx context.Context, id as.ResourceID) (*as.RawReso
if err != nil || rsrc == nil {
return rsrc, err
}
if rsrc.Type != as.ResourceTypeWebsite && rsrc.Type != as.ResourceTypeDomain {
return rsrc, nil
}
if tmp, err := tx.lookup(ctx, []string{rsrc.Name}); err == nil {
rsrc.Website.CMSInfo = tmp[rsrc.Name]
if rsrc.Type == as.ResourceTypeWebsite || rsrc.Type == as.ResourceTypeDomain {
if err := tx.lookup(ctx, []*as.Resource{&rsrc.Resource}); err != nil {
// This error is non-fatal, we just do not
// augment the resources with web app info.
log.Printf("error looking up webappdb: %v", err)
}
}
return rsrc, nil
}
func (tx *wdbTX) lookup(ctx context.Context, sites []string) (map[string][]*webappdb.App, error) {
// Lookup resources in the webapp DB, modifying them in-place with any
// eventual CMSInfo data returned.
func (tx *wdbTX) lookup(ctx context.Context, resources []*as.Resource) error {
// Build a list of site names, and a site name -> resource map
// so we can modify the original objects with our results.
sites := make([]string, 0, len(resources))
rsrcMap := make(map[string]*as.Resource)
for _, r := range resources {
rsrcMap[r.Name] = r
sites = append(sites, r.Name)
}
var resp webappdb.FindAppsBySiteResponse
if err := tx.dbbe.Call(ctx, "", "/api/search/by_site", &webappdb.FindAppsBySiteRequest{
Sites: sites,
}, &resp); err != nil {
return nil, err
return err
}
return resp.Apps, nil
for key, values := range resp.Apps {
r, ok := rsrcMap[key]
if !ok || r.Website == nil {
continue
}
r.Website.CMSInfo = values
}
return nil
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment