Skip to content

Commit 92fafcc

Browse files
gloursndeloof
authored andcommitted
add validation for required parameters of provider service when metadata are available
Signed-off-by: Guillaume Lours <[email protected]>
1 parent fee8aee commit 92fafcc

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

pkg/compose/plugins.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ func (s *composeService) runPlugin(ctx context.Context, project *types.Project,
6060
return err
6161
}
6262

63-
cmd := s.setupPluginCommand(ctx, project, service, plugin, command)
63+
cmd, err := s.setupPluginCommand(ctx, project, service, plugin, command)
64+
if err != nil {
65+
return err
66+
}
6467

6568
variables, err := s.executePlugin(ctx, cmd, command, service)
6669
if err != nil {
@@ -164,7 +167,7 @@ func (s *composeService) getPluginBinaryPath(provider string) (path string, err
164167
return path, err
165168
}
166169

167-
func (s *composeService) setupPluginCommand(ctx context.Context, project *types.Project, service types.ServiceConfig, path, command string) *exec.Cmd {
170+
func (s *composeService) setupPluginCommand(ctx context.Context, project *types.Project, service types.ServiceConfig, path, command string) (*exec.Cmd, error) {
168171
cmdOptionsMetadata := s.getPluginMetadata(path, service.Provider.Type)
169172
var currentCommandMetadata CommandMetadata
170173
switch command {
@@ -175,6 +178,9 @@ func (s *composeService) setupPluginCommand(ctx context.Context, project *types.
175178
}
176179
commandMetadataIsEmpty := len(currentCommandMetadata.Parameters) == 0
177180
provider := *service.Provider
181+
if err := currentCommandMetadata.CheckRequiredParameters(provider); !commandMetadataIsEmpty && err != nil {
182+
return nil, err
183+
}
178184

179185
args := []string{"compose", "--project-name", project.Name, command}
180186
for k, v := range provider.Options {
@@ -211,7 +217,7 @@ func (s *composeService) setupPluginCommand(ctx context.Context, project *types.
211217
carrier := propagation.MapCarrier{}
212218
otel.GetTextMapPropagator().Inject(ctx, &carrier)
213219
cmd.Env = append(cmd.Env, types.Mapping(carrier).Values()...)
214-
return cmd
220+
return cmd, nil
215221
}
216222

217223
func (s *composeService) getPluginMetadata(path, command string) ProviderMetadata {
@@ -270,3 +276,14 @@ func (c CommandMetadata) GetParameter(paramName string) (ParameterMetadata, bool
270276
}
271277
return ParameterMetadata{}, false
272278
}
279+
280+
func (c CommandMetadata) CheckRequiredParameters(provider types.ServiceProviderConfig) error {
281+
for _, p := range c.Parameters {
282+
if p.Required {
283+
if _, ok := provider.Options[p.Name]; !ok {
284+
return fmt.Errorf("required parameter %q is missing from provider %q definition", p.Name, provider.Type)
285+
}
286+
}
287+
}
288+
return nil
289+
}

0 commit comments

Comments
 (0)