@@ -3,6 +3,7 @@ package listener
33import (
44 "app/base"
55 "app/base/api"
6+ "app/base/candlepin"
67 "app/base/database"
78 "app/base/inventory"
89 "app/base/models"
@@ -50,6 +51,7 @@ const (
5051 RepoPathPattern = "(/content/.*)"
5152 RepoBasearchPlaceholder = "$basearch"
5253 RepoReleaseverPlaceholder = "$releasever"
54+ TemplateRepoPattern = `^https://cert\.console.*/api/pulp-content/(cs-)?[[:xdigit:]]+/templates/`
5355)
5456
5557var (
@@ -63,10 +65,12 @@ var (
6365)
6466
6567var (
66- repoPathRegex = regexp .MustCompile (RepoPathPattern )
67- spacesRegex = regexp .MustCompile (`^\s*$` )
68- httpClient * api.Client
69- metricByErr = map [error ]string {
68+ repoPathRegex = regexp .MustCompile (RepoPathPattern )
69+ spacesRegex = regexp .MustCompile (`^\s*$` )
70+ templateRepoPath = regexp .MustCompile (TemplateRepoPattern )
71+ httpClient * api.Client
72+ candlepinClient = candlepin .CreateCandlepinClient ()
73+ metricByErr = map [error ]string {
7074 ErrNoPackages : ReceivedWarnNoPackages ,
7175 ErrReporter : ReceivedWarnExcludedReporter ,
7276 ErrHostType : ReceivedWarnExcludedHostType ,
@@ -328,12 +332,25 @@ func updateReporterCounter(reporter string) {
328332
329333func hostTemplate (tx * gorm.DB , accountID int , host * Host ) * int64 {
330334 var templateID * int64
331- var err error
332- if host .Reporter == rhsmReporter || host .Reporter == puptooReporter {
333- templateID , err = getTemplate (tx , accountID , host .SystemProfile .Rhsm .Environments )
335+
336+ if hasTemplateRepo (& host .SystemProfile ) {
337+ // check system's env in candlepin
338+ resp , err := callCandlepinEnvironment (base .Context , host .SystemProfile .ConsumerID )
334339 if err != nil {
335340 utils .LogWarn ("inventoryID" , host .ID , "err" , errors .Wrap (err , "Unable to assign templates" ))
336341 }
342+
343+ // get template from candlepin
344+ if resp != nil {
345+ envs := make ([]string , 0 , len (resp .Environments ))
346+ for _ , env := range resp .Environments {
347+ envs = append (envs , env .ID )
348+ }
349+ templateID , err = getTemplate (tx , accountID , envs )
350+ if err != nil {
351+ utils .LogWarn ("inventoryID" , host .ID , "err" , errors .Wrap (err , "Unable to assign templates" ))
352+ }
353+ }
337354 }
338355 return templateID
339356}
@@ -640,6 +657,16 @@ func getRepoPath(systemProfile *inventory.SystemProfile, repo *inventory.YumRepo
640657 return repoPath , nil
641658}
642659
660+ func hasTemplateRepo (systemProfile * inventory.SystemProfile ) bool {
661+ yumRepos := systemProfile .GetYumRepos ()
662+ for _ , r := range yumRepos {
663+ if r .Enabled && templateRepoPath .MatchString (r .BaseURL ) {
664+ return true
665+ }
666+ }
667+ return false
668+ }
669+
643670func processRepos (systemProfile * inventory.SystemProfile ) ([]string , []string ) {
644671 yumRepos := systemProfile .GetYumRepos ()
645672 seen := make (map [string ]bool , len (yumRepos ))
0 commit comments