@@ -68,6 +68,14 @@ type (
6868 current * crossplane.Directive , apiType string ) []* model.APIDetails
6969)
7070
71+ type apiCreationParams struct {
72+ locationDirectiveName string
73+ path string
74+ caCertLocation string
75+ isSSL bool
76+ isWriteEnabled bool
77+ }
78+
7179func NewNginxConfigParser (agentConfig * config.Config ) * NginxConfigParser {
7280 return & NginxConfigParser {
7381 agentConfig : agentConfig ,
@@ -148,7 +156,6 @@ func (ncp *NginxConfigParser) FindPlusAPI(
148156func (ncp * NginxConfigParser ) FindAllPlusAPIs (
149157 ctx context.Context , nginxConfigContext * model.NginxConfigContext ,
150158) []* model.APIDetails {
151- // This function returns the list populated by createNginxConfigContext/Parse
152159 if nginxConfigContext .PlusAPIs == nil {
153160 return []* model.APIDetails {}
154161 }
@@ -714,47 +721,55 @@ func (ncp *NginxConfigParser) apiDetailsFromLocationDirective(
714721 addresses := ncp .parseAddressFromServerDirective (parent )
715722 path := ncp .parsePathFromLocationDirective (current )
716723
724+ params := apiCreationParams {
725+ locationDirectiveName : locationDirectiveName ,
726+ path : path ,
727+ caCertLocation : caCertLocation ,
728+ isSSL : isSSL ,
729+ isWriteEnabled : isWriteEnabled ,
730+ }
731+
717732 for _ , locChild := range current .Block {
733+ if locChild .Directive != plusAPIDirective && locChild .Directive != stubStatusAPIDirective {
734+ continue
735+ }
736+
718737 if locChild .Directive == locationDirectiveName {
719- for _ , address := range addresses {
720- details = append (
721- details ,
722- ncp .createAPIDetails (locationDirectiveName , address , path , caCertLocation , isSSL , isWriteEnabled ),
723- )
724- }
738+ details = append (details , ncp .createAPIDetailsForAddresses (
739+ params ,
740+ addresses ,
741+ )... )
725742 }
726743 }
727744
728745 return details
729746}
730747
731- //nolint:revive // isWriteEnabled flag is required for selecting Plus API
732748func (ncp * NginxConfigParser ) createAPIDetails (
733- locationDirectiveName , address , path , caCertLocation string , isSSL bool ,
734- isWriteEnabled bool ,
749+ params apiCreationParams , address string ,
735750) (details * model.APIDetails ) {
736751 if strings .HasPrefix (address , "unix:" ) {
737752 format := unixStubStatusFormat
738753
739- if locationDirectiveName == plusAPIDirective {
754+ if params . locationDirectiveName == plusAPIDirective {
740755 format = unixPlusAPIFormat
741756 }
742757
743758 details = & model.APIDetails {
744- URL : fmt .Sprintf (format , path ),
759+ URL : fmt .Sprintf (format , params . path ),
745760 Listen : address ,
746- Location : path ,
747- Ca : caCertLocation ,
748- WriteEnabled : isWriteEnabled ,
761+ Location : params . path ,
762+ Ca : params . caCertLocation ,
763+ WriteEnabled : params . isWriteEnabled ,
749764 }
750765 } else {
751766 details = & model.APIDetails {
752- URL : fmt .Sprintf ("%s://%s%s" , map [bool ]string {true : "https" , false : "http" }[isSSL ],
753- address , path ),
767+ URL : fmt .Sprintf ("%s://%s%s" , map [bool ]string {true : "https" , false : "http" }[params . isSSL ],
768+ address , params . path ),
754769 Listen : address ,
755- Location : path ,
756- Ca : caCertLocation ,
757- WriteEnabled : isWriteEnabled ,
770+ Location : params . path ,
771+ Ca : params . caCertLocation ,
772+ WriteEnabled : params . isWriteEnabled ,
758773 }
759774 }
760775
@@ -909,6 +924,22 @@ func (ncp *NginxConfigParser) isDuplicateFile(nginxConfigContextFiles []*mpi.Fil
909924 return false
910925}
911926
927+ func (ncp * NginxConfigParser ) createAPIDetailsForAddresses (
928+ params apiCreationParams ,
929+ addresses []string ,
930+ ) (details []* model.APIDetails ) {
931+ for _ , address := range addresses {
932+ details = append (details ,
933+ ncp .createAPIDetails (
934+ params ,
935+ address ,
936+ ),
937+ )
938+ }
939+
940+ return details
941+ }
942+
912943func (ncp * NginxConfigParser ) isPlusAPIWriteEnabled (ctx context.Context ,
913944 directive * crossplane.Directive ,
914945 locationPath string ,
0 commit comments