@@ -742,102 +742,102 @@ func (a *apic) PullAllowlist(ctx context.Context, allowlist *modelscapi.Allowlis
742742 return nil
743743}
744744
745- func (a * apic ) UpdateAllowlists (ctx context.Context , allowlistsLinks []* modelscapi.AllowlistLink , forcePull bool ) error {
746- if len (allowlistsLinks ) == 0 {
745+ func (a * apic ) updateOneAllowlist (ctx context.Context , client * apiclient.ApiClient , link * modelscapi.AllowlistLink ) error {
746+ if log .IsLevelEnabled (log .TraceLevel ) {
747+ log .Tracef ("allowlist body: %+v" , spew .Sdump (link ))
748+ }
749+
750+ if link .Name == nil {
751+ log .Warn ("allowlist has no name" )
747752 return nil
748753 }
749754
750- defaultClient , err := apiclient .NewDefaultClient (a .apiClient .BaseURL , "" , "" , nil )
755+ if link .URL == nil {
756+ log .Warnf ("allowlist %s has no URL" , * link .Name )
757+ return nil
758+ }
759+
760+ if link .ID == nil {
761+ return fmt .Errorf ("allowlist %s has no ID" , * link .Name )
762+ }
763+
764+ description := ""
765+ if link .Description != nil {
766+ description = * link .Description
767+ }
768+
769+ req , err := http .NewRequestWithContext (ctx , http .MethodGet , * link .URL , http .NoBody )
751770 if err != nil {
752- return fmt .Errorf ("while creating default client : %w " , err )
771+ return fmt .Errorf ("while pulling allowlist : %s " , err )
753772 }
754773
755- for _ , link := range allowlistsLinks {
756- if log .IsLevelEnabled (log .TraceLevel ) {
757- log .Tracef ("allowlist body: %+v" , spew .Sdump (link ))
758- }
774+ resp , err := client .GetClient ().Do (req )
775+ if err != nil {
776+ return fmt .Errorf ("while pulling allowlist: %s" , err )
777+ }
778+ defer resp .Body .Close ()
759779
760- if link .Name == nil {
761- log .Warningf ("allowlist has no name" )
762- continue
763- }
780+ scanner := bufio .NewScanner (resp .Body )
781+ items := make ([]* models.AllowlistItem , 0 )
764782
765- if link .URL == nil {
766- log .Warningf ("allowlist %s has no URL" , * link .Name )
767- continue
768- }
783+ for scanner .Scan () {
784+ item := scanner .Text ()
785+ j := & models.AllowlistItem {}
769786
770- if link .ID == nil {
771- log .Warningf ("allowlist %s has no ID" , * link .Name )
772- continue
787+ if err := json .Unmarshal ([]byte (item ), j ); err != nil {
788+ return fmt .Errorf ("while unmarshalling allowlist item: %s" , err )
773789 }
774790
775- description := ""
776- if link .Description != nil {
777- description = * link .Description
778- }
791+ items = append (items , j )
792+ }
779793
780- req , err := http . NewRequestWithContext (ctx , http . MethodGet , * link .URL , http . NoBody )
781- if err != nil {
782- log . Errorf ( "while pulling allowlist: %s" , err )
783- continue
794+ list , err := a . dbClient . GetAllowListByID (ctx , * link .ID , false )
795+ if err != nil {
796+ if ! ent . IsNotFound ( err ) {
797+ return fmt . Errorf ( "while getting allowlist %s: %s" , * link . Name , err )
784798 }
799+ }
785800
786- resp , err := defaultClient .GetClient ().Do (req )
801+ if list == nil {
802+ list , err = a .dbClient .CreateAllowList (ctx , * link .Name , description , * link .ID , true )
787803 if err != nil {
788- log .Errorf ("while pulling allowlist: %s" , err )
789- continue
804+ return fmt .Errorf ("while creating allowlist %s: %s" , * link .Name , err )
790805 }
791- defer resp .Body .Close ()
792-
793- scanner := bufio .NewScanner (resp .Body )
794- items := make ([]* models.AllowlistItem , 0 )
795-
796- for scanner .Scan () {
797- item := scanner .Text ()
798- j := & models.AllowlistItem {}
806+ }
799807
800- if err := json . Unmarshal ([] byte ( item ), j ); err != nil {
801- log . Errorf ( "while unmarshalling allowlist item: %s" , err )
802- continue
803- }
808+ added , err := a . dbClient . ReplaceAllowlist ( ctx , list , items , true )
809+ if err != nil {
810+ return fmt . Errorf ( "while replacing allowlist %s: %s" , * link . Name , err )
811+ }
804812
805- items = append (items , j )
806- }
813+ log .Infof ("added %d values to allowlist %s" , added , list .Name )
807814
808- list , err := a .dbClient .GetAllowListByID (ctx , * link .ID , false )
815+ if list .Name != * link .Name || list .Description != description {
816+ err = a .dbClient .UpdateAllowlistMeta (ctx , * link .ID , * link .Name , description )
809817 if err != nil {
810- if ! ent .IsNotFound (err ) {
811- log .Errorf ("while getting allowlist %s: %s" , * link .Name , err )
812- continue
813- }
818+ return fmt .Errorf ("while updating allowlist meta %s: %s" , * link .Name , err )
814819 }
820+ }
815821
816- if list == nil {
817- list , err = a .dbClient .CreateAllowList (ctx , * link .Name , description , * link .ID , true )
818- if err != nil {
819- log .Errorf ("while creating allowlist %s: %s" , * link .Name , err )
820- continue
821- }
822- }
822+ log .Infof ("Allowlist %s updated" , * link .Name )
823823
824- added , err := a .dbClient .ReplaceAllowlist (ctx , list , items , true )
825- if err != nil {
826- log .Errorf ("while replacing allowlist %s: %s" , * link .Name , err )
827- continue
828- }
824+ return nil
825+ }
829826
830- log .Infof ("added %d values to allowlist %s" , added , list .Name )
827+ func (a * apic ) UpdateAllowlists (ctx context.Context , allowlistsLinks []* modelscapi.AllowlistLink , forcePull bool ) error {
828+ if len (allowlistsLinks ) == 0 {
829+ return nil
830+ }
831831
832- if list .Name != * link .Name || list .Description != description {
833- err = a .dbClient .UpdateAllowlistMeta (ctx , * link .ID , * link .Name , description )
834- if err != nil {
835- log .Errorf ("while updating allowlist meta %s: %s" , * link .Name , err )
836- continue
837- }
838- }
832+ client , err := apiclient .NewDefaultClient (a .apiClient .BaseURL , "" , "" , nil )
833+ if err != nil {
834+ return fmt .Errorf ("while creating default client: %w" , err )
835+ }
839836
840- log .Infof ("Allowlist %s updated" , * link .Name )
837+ for _ , link := range allowlistsLinks {
838+ if err := a .updateOneAllowlist (ctx , client , link ); err != nil {
839+ log .Errorf ("updating allowlists from CAPI: %s" , err )
840+ }
841841 }
842842
843843 return nil
0 commit comments