Skip to content

Commit 99d0db9

Browse files
committed
parsing the plusAPIs
1 parent d11d7a5 commit 99d0db9

File tree

4 files changed

+225
-55
lines changed

4 files changed

+225
-55
lines changed

internal/datasource/config/configfakes/fake_config_parser.go

Lines changed: 76 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/datasource/config/nginx_config_parser.go

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type ConfigParser interface {
5757
Parse(ctx context.Context, instance *mpi.Instance) (*model.NginxConfigContext, error)
5858
FindStubStatusAPI(ctx context.Context, nginxConfigContext *model.NginxConfigContext) *model.APIDetails
5959
FindPlusAPI(ctx context.Context, nginxConfigContext *model.NginxConfigContext) *model.APIDetails
60+
FindAllPlusAPIs(ctx context.Context, nginxConfigContext *model.NginxConfigContext) []*model.APIDetails
6061
}
6162

6263
var _ ConfigParser = (*NginxConfigParser)(nil)
@@ -144,6 +145,17 @@ func (ncp *NginxConfigParser) FindPlusAPI(
144145
}
145146
}
146147

148+
func (ncp *NginxConfigParser) FindAllPlusAPIs(
149+
ctx context.Context, nginxConfigContext *model.NginxConfigContext,
150+
) []*model.APIDetails {
151+
// This function returns the list populated by createNginxConfigContext/Parse
152+
if nginxConfigContext.PlusAPIs == nil {
153+
return []*model.APIDetails{}
154+
}
155+
156+
return nginxConfigContext.PlusAPIs
157+
}
158+
147159
//nolint:gocognit,gocyclo,revive,cyclop // cognitive complexity is 51, cyclomatic complexity is 24
148160
func (ncp *NginxConfigParser) createNginxConfigContext(
149161
ctx context.Context,
@@ -695,6 +707,17 @@ func (ncp *NginxConfigParser) apiDetailsFromLocationDirective(
695707
if locChild.Directive != plusAPIDirective && locChild.Directive != stubStatusAPIDirective {
696708
continue
697709
}
710+
711+
isWriteEnabled := false
712+
if locChild.Directive == plusAPIDirective {
713+
for _, arg := range locChild.Args {
714+
if arg == "write=on" {
715+
isWriteEnabled = true
716+
slog.DebugContext(ctx, "Found NGINX Plus API with write=on", "location", current.Args[0])
717+
break
718+
}
719+
}
720+
}
698721

699722
addresses := ncp.parseAddressFromServerDirective(parent)
700723
path := ncp.parsePathFromLocationDirective(current)
@@ -703,7 +726,7 @@ func (ncp *NginxConfigParser) apiDetailsFromLocationDirective(
703726
for _, address := range addresses {
704727
details = append(
705728
details,
706-
ncp.createAPIDetails(locationDirectiveName, address, path, caCertLocation, isSSL),
729+
ncp.createAPIDetails(locationDirectiveName, address, path, caCertLocation, isSSL, isWriteEnabled),
707730
)
708731
}
709732
}
@@ -714,6 +737,7 @@ func (ncp *NginxConfigParser) apiDetailsFromLocationDirective(
714737

715738
func (ncp *NginxConfigParser) createAPIDetails(
716739
locationDirectiveName, address, path, caCertLocation string, isSSL bool,
740+
isWriteEnabled bool,
717741
) (details *model.APIDetails) {
718742
if strings.HasPrefix(address, "unix:") {
719743
format := unixStubStatusFormat
@@ -723,18 +747,20 @@ func (ncp *NginxConfigParser) createAPIDetails(
723747
}
724748

725749
details = &model.APIDetails{
726-
URL: fmt.Sprintf(format, path),
727-
Listen: address,
728-
Location: path,
729-
Ca: caCertLocation,
750+
URL: fmt.Sprintf(format, path),
751+
Listen: address,
752+
Location: path,
753+
Ca: caCertLocation,
754+
WriteEnabled: isWriteEnabled,
730755
}
731756
} else {
732757
details = &model.APIDetails{
733758
URL: fmt.Sprintf("%s://%s%s", map[bool]string{true: "https", false: "http"}[isSSL],
734759
address, path),
735-
Listen: address,
736-
Location: path,
737-
Ca: caCertLocation,
760+
Listen: address,
761+
Location: path,
762+
Ca: caCertLocation,
763+
WriteEnabled: isWriteEnabled,
738764
}
739765
}
740766

0 commit comments

Comments
 (0)