Skip to content

Commit

Permalink
Merge pull request #408 from reubenmiller/fix-support-empty-tenant-op…
Browse files Browse the repository at this point in the history
…tion-values

feat(tenantoptions): allow setting tenant option with an explicit empty value
  • Loading branch information
reubenmiller authored Oct 10, 2024
2 parents 200b418 + c1b6da0 commit df99bdc
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 20 deletions.
10 changes: 3 additions & 7 deletions api/spec/json/tenantOptions.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
},
{
"name": "value",
"type": "string",
"type": "stringAny",
"description": "Value of option"
},
{
Expand All @@ -89,8 +89,7 @@
],
"bodyRequiredKeys": [
"category",
"key",
"value"
"key"
]
},
{
Expand Down Expand Up @@ -235,17 +234,14 @@
"body": [
{
"name": "value",
"type": "string",
"type": "stringAny",
"description": "New value"
},
{
"name": "data",
"type": "json",
"description": "Additional properties"
}
],
"bodyRequiredKeys": [
"value"
]
},
{
Expand Down
1 change: 1 addition & 0 deletions api/spec/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@
"uipluginversion",
"queryExpression",
"string",
"stringAny",
"stringStatic",
"inventoryChildType",
"source",
Expand Down
8 changes: 2 additions & 6 deletions api/spec/yaml/tenantOptions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ commands:
description: Key of option

- name: value
type: string
type: stringAny
description: Value of option

- name: data
Expand All @@ -73,7 +73,6 @@ commands:
bodyRequiredKeys:
- "category"
- "key"
- "value"

- name: getTenantOption
description: Get tenant option
Expand Down Expand Up @@ -176,16 +175,13 @@ commands:

body:
- name: value
type: string
type: stringAny
description: New value

- name: data
type: json
description: Additional properties

bodyRequiredKeys:
- value

- name: updateTenantOptionBulk
description: Update multiple tenant options
descriptionLong: Update multiple tenant options in provided category
Expand Down
2 changes: 1 addition & 1 deletion cmd/gen-tests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ func getParameterValue(cmd *cobra.Command, parameter *models.Parameter) (value s
}
// case "applications":
// fallthrough
case "string", "application":
case "string", "application", "stringAny":
v, err := cmd.Flags().GetString(parameter.Name)
if err == nil {
value = v
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/tenantoptions/create/create.auto.go

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

3 changes: 1 addition & 2 deletions pkg/cmd/tenantoptions/update/update.auto.go

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

5 changes: 4 additions & 1 deletion pkg/cmdparser/cmdparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func AddFlag(cmd *CmdOptions, p *models.Parameter, factory *cmdutil.Factory) err
return nil
}
switch p.Type {
case "string", "stringStatic", "devicerequest", "json_custom", "directory", "softwareName", "softwareversionName", "softwareDetails", "firmwareName", "firmwareversionName", "firmwarepatchName", "firmwareDetails", "binaryUploadURL", "inventoryChildType", "subscriptionName", "subscriptionId", "file", "attachment", "fileContents", "fileContentsAsString", "certificatefile":
case "string", "stringAny", "stringStatic", "devicerequest", "json_custom", "directory", "softwareName", "softwareversionName", "softwareDetails", "firmwareName", "firmwareversionName", "firmwarepatchName", "firmwareDetails", "binaryUploadURL", "inventoryChildType", "subscriptionName", "subscriptionId", "file", "attachment", "fileContents", "fileContentsAsString", "certificatefile":
cmd.Command.Flags().StringP(p.Name, p.ShortName, p.Default, p.GetDescription())

case "json":
Expand Down Expand Up @@ -403,6 +403,9 @@ func GetOption(cmd *CmdOptions, p *models.Parameter, factory *cmdutil.Factory, a
case "string", "source", "tenantname", "devicerequest", "subscriptionName", "subscriptionId", "applicationname", "microserviceinstance", "microservicename", "softwareName", "softwareversionName", "firmwareName", "firmwareversionName", "firmwarepatchName", "uipluginversion":
opts = append(opts, flags.WithStringValue(p.Name, targetProp, p.Format))

case "stringAny":
opts = append(opts, flags.WithAnyStringValue(p.Name, targetProp, p.Format))

case "stringStatic":
opts = append(opts, flags.WithStaticStringValue(p.Name, p.Value))
case "integer":
Expand Down
30 changes: 30 additions & 0 deletions pkg/flags/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,36 @@ func WithStringValue(opts ...string) GetOption {
}
}

// WithAnyStringValue adds a string from cli arguments but allows the user to set an empty string
func WithAnyStringValue(opts ...string) GetOption {
return func(cmd *cobra.Command, inputIterators *RequestInputIterators) (string, interface{}, error) {

src, dst, format := UnpackGetterOptions("%s", opts...)

if inputIterators != nil && inputIterators.PipeOptions != nil {
if inputIterators.PipeOptions.Name == src {
inputIterators.PipeOptions.Format = format
return WithPipelineIterator(inputIterators.PipeOptions)(cmd, inputIterators)
}
}

if cmd.Flag(src) == nil {
return "", nil, nil
}

value, err := cmd.Flags().GetString(src)
if err != nil {
return dst, value, err
}

if cmd.Flags().Changed(src) {
return dst, AnyString(applyFormatter(format, value)), err
}

return dst, applyFormatter(format, value), err
}
}

func WithVersion(fallbackSrc string, opts ...string) GetOption {
return func(cmd *cobra.Command, inputIterators *RequestInputIterators) (string, interface{}, error) {

Expand Down
2 changes: 1 addition & 1 deletion scripts/build-cli/New-C8yApiGoCommand.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,7 @@ Function Get-C8yGoArgs {
}
}

"string" {
{ $_ -in "string", "stringAny" } {
$SetFlag = if ($UseOption) {
'cmd.Flags().StringP("{0}", "{1}", "{2}", "{3}")' -f $Name, $OptionName, $Default, $Description
} else {
Expand Down
3 changes: 3 additions & 0 deletions scripts/build-cli/New-C8yApiGoGetValueFromFlag.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
# string
"string" = "flags.WithStringValue(`"${prop}`", `"${queryParam}`"$FormatValue),"

# stringAny string value (allow users to set an empty string value)
"stringAny" = "flags.WithAnyStringValue(`"${prop}`", `"${queryParam}`"$FormatValue),"

# stringStatic
"stringStatic" = "flags.WithStaticStringValue(`"${prop}`", `"$FixedValue`"),"

Expand Down
1 change: 1 addition & 0 deletions scripts/build-powershell/New-C8yPowershellArguments.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
"softwareversionName" { "object[]"; break }
"source" { "object"; break }
"string" { "string"; break }
"stringAny" { "string"; break }
"strings" { "string"; break }
"subscriptionId" { "string"; break }
"subscriptionName" { "string"; break }
Expand Down
29 changes: 29 additions & 0 deletions tests/manual/tenantoptions/tenantoptions_create.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
tests:
It creates a tenant option with an empty string value:
command: >
c8y tenantoptions create --category measurement.series.latestvalue --key c8y_Temperature.T --value "" --dry --dryFormat json |
c8y util show --select body -c=false --output json
exit-code: 0
stdout:
exactly: |
{
"body": {
"category": "measurement.series.latestvalue",
"key": "c8y_Temperature.T",
"value": ""
}
}
It creates a tenant option without a value:
command: >
c8y tenantoptions create --category measurement.series.latestvalue --key c8y_Temperature.T --dry --dryFormat json |
c8y util show --select body -c=false --output json
exit-code: 0
stdout:
exactly: |
{
"body": {
"category": "measurement.series.latestvalue",
"key": "c8y_Temperature.T"
}
}
24 changes: 24 additions & 0 deletions tests/manual/tenantoptions/tenantoptions_update.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
tests:
It creates a tenant option with an empty string value:
command: >
c8y tenantoptions update --category measurement.series.latestvalue --key c8y_Power.V --value "" --dry --dryFormat json |
c8y util show --select body -c=false --output json
exit-code: 0
stdout:
exactly: |
{
"body": {
"value": ""
}
}
It creates a tenant option without a value:
command: >
c8y tenantoptions update --category measurement.series.latestvalue --key c8y_Power.V --dry --dryFormat json |
c8y util show --select body -c=false --output json
exit-code: 0
stdout:
exactly: |
{
"body": {}
}
1 change: 1 addition & 0 deletions tools/schema/extensionCommands.json
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@
"softwareversionName",
"source",
"string",
"stringAny",
"string[]",
"stringcsv[]",
"stringStatic",
Expand Down

0 comments on commit df99bdc

Please sign in to comment.