Skip to content

Commit

Permalink
disable datahub prompt when querying high performance api (#238)
Browse files Browse the repository at this point in the history
* feat(gen): support semantic method (to mimic other REST methods in cli handling)

* fix: disable prompts for datahub query
  • Loading branch information
reubenmiller authored Mar 21, 2023
1 parent 18d3ebf commit 1dc5b9b
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 7 deletions.
1 change: 1 addition & 0 deletions api/spec/json/datahub.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{
"name": "query",
"method": "POST",
"semanticMethod": "GET",
"path": "service/datahub/sql",
"accept": "application/json",
"description": "Execute a SQL query and retrieve the results",
Expand Down
11 changes: 11 additions & 0 deletions api/spec/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,17 @@
"DELETE"
]
},
"semanticMethod": {
"type": "string",
"title": "Semantic REST method",
"description": "How the call should be handled internally. This will override how the request is processed in respect to confirmation, create/update/delete modes etc.",
"enum": [
"GET",
"POST",
"PUT",
"DELETE"
]
},
"description": {
"type": "string",
"title": "Description of the endpoint"
Expand Down
1 change: 1 addition & 0 deletions api/spec/yaml/datahub.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ information:
endpoints:
- name: query
method: POST
semanticMethod: GET
path: service/datahub/sql
accept: application/json
description: Execute a SQL query and retrieve the results
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/datahub/query/query.auto.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions pkg/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
AnnotationValueFromPipelineData = "valueFromPipeline.data"
AnnotationValueCollectionProperty = "collectionProperty"
AnnotationValueDeprecated = "deprecatedNotice"
AnnotationValueSemanticMethod = "semanticMethod"
)

// Option adds flags to a given command
Expand Down Expand Up @@ -268,6 +269,34 @@ func GetDeprecationNoticeFromAnnotation(cmd *cobra.Command) (value string) {
return
}

// WithSemanticMethod sets a semantic REST method which may be different to the actual REST method used
// useful to be more descriptive about how the action should behave (e.g. prompting).
func WithSemanticMethod(v string) Option {
return func(cmd *cobra.Command) *cobra.Command {
if v != "" {
if cmd.Annotations == nil {
cmd.Annotations = map[string]string{}
}
cmd.Annotations[AnnotationValueSemanticMethod] = v
}
return cmd
}
}

// GetSemanticMethodFromAnnotation returns semantic REST method related to the action from the annotations
func GetSemanticMethodFromAnnotation(cmd *cobra.Command) (value string) {
if cmd == nil {
return
}
if cmd.Annotations == nil {
return
}
if v, ok := cmd.Annotations[AnnotationValueSemanticMethod]; ok {
value = v
}
return
}

// GetPipeOptionsFromAnnotation returns the pipeline options stored in the annotations
func GetPipeOptionsFromAnnotation(cmd *cobra.Command) (options *PipelineOptions, err error) {
options = &PipelineOptions{}
Expand Down
9 changes: 8 additions & 1 deletion pkg/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type BatchOptions struct {
Delay time.Duration
DelayBefore time.Duration
AbortOnErrorCount int
SemanticMethod string

PostActions []flags.Action

Expand Down Expand Up @@ -114,6 +115,7 @@ func (w *Worker) getBatchOptions(cmd *cobra.Command) (*BatchOptions, error) {
TotalWorkers: w.config.GetWorkers(),
Delay: w.config.WorkerDelay(),
DelayBefore: w.config.WorkerDelayBefore(),
SemanticMethod: flags.GetSemanticMethodFromAnnotation(cmd),
}

if v, err := cmd.Flags().GetInt("count"); err == nil {
Expand Down Expand Up @@ -263,7 +265,12 @@ func (w *Worker) runBatched(requestIterator *requestiterator.RequestIterator, co
w.logger.Debugf("adding job: %d", jobID)

if request != nil {
shouldConfirm = w.config.ShouldConfirm(request.Method)
if batchOptions.SemanticMethod != "" {
// Use a custom method which controls how the request should be handled but is not the actual request
shouldConfirm = w.config.ShouldConfirm(batchOptions.SemanticMethod)
} else {
shouldConfirm = w.config.ShouldConfirm(request.Method)
}
}

// confirm action
Expand Down
17 changes: 12 additions & 5 deletions scripts/build-cli/New-C8yApiGoCommand.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,13 @@
#
# Pre run validation (disable some commands without switch flags)
#
$PreRunFunction = switch ($Specification.method) {
$FunctionalMethod = if ($Specification.semanticMethod) {
$Specification.semanticMethod
} else {
$Specification.method
}

$PreRunFunction = switch ($FunctionalMethod) {
"POST" { "f.CreateModeEnabled()" }
"PUT" { "f.UpdateModeEnabled()" }
"DELETE" { "f.DeleteModeEnabled()" }
Expand Down Expand Up @@ -624,12 +630,13 @@ $($Examples -join "`n`n")
)
$(
if ($collectionProperty) {
"flags.WithCollectionProperty(`"$collectionProperty`"),"
"flags.WithCollectionProperty(`"$collectionProperty`"),`n"
}
)
$(
if ($DeprecationNotice) {
"flags.WithDeprecationNotice(`"$DeprecationNotice`"),"
"flags.WithDeprecationNotice(`"$DeprecationNotice`"),`n"
}
if ($Specification.semanticMethod) {
"flags.WithSemanticMethod(`"$($Specification.semanticMethod)`"),`n"
}
)
)
Expand Down

0 comments on commit 1dc5b9b

Please sign in to comment.