Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(software install): add softwareType field by looking up the software #416

Merged
merged 1 commit into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion api/spec/json/softwareVersions.json
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,14 @@
"type": "string",
"required": false,
"property": "c8y_SoftwareUpdate.0.url",
"description": "Software url. Leave blank to automatically set it if a matching firmware/version is found in the c8y firmware repository"
"description": "Software url. Leave blank to automatically set it if a matching software/version is found in the c8y software repository"
},
{
"name": "softwareType",
"type": "string",
"required": false,
"property": "c8y_SoftwareUpdate.0.softwareType",
"description": "Software type. Leave blank to automatically set it if a matching software/version is found in the c8y software repository"
},
{
"name": "description",
Expand Down
7 changes: 6 additions & 1 deletion api/spec/yaml/softwareVersions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,13 @@ commands:
type: 'string'
required: false
property: c8y_SoftwareUpdate.0.url
description: Software url. Leave blank to automatically set it if a matching firmware/version is found in the c8y firmware repository
description: Software url. Leave blank to automatically set it if a matching software/version is found in the c8y software repository

- name: softwareType
type: string
required: false
property: c8y_SoftwareUpdate.0.softwareType
description: Software type. Leave blank to automatically set it if a matching software/version is found in the c8y software repository

- name: description
type: string
Expand Down
25 changes: 24 additions & 1 deletion pkg/c8yfetcher/c8yfetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ func WithSoftwareByNameFirstMatch(factory *cmdutil.Factory, args []string, opts
}

// WithSoftwareVersionData adds software information (name, version and url)
func WithSoftwareVersionData(factory *cmdutil.Factory, flagSoftware, flagVersion, flagURL string, args []string, opts ...string) flags.GetOption {
func WithSoftwareVersionData(factory *cmdutil.Factory, flagSoftware, flagVersion, flagURL string, flagSoftwareType string, args []string, opts ...string) flags.GetOption {
return func(cmd *cobra.Command, inputIterators *flags.RequestInputIterators) (string, interface{}, error) {
client, err := factory.Client()
if err != nil {
Expand All @@ -731,10 +731,33 @@ func WithSoftwareVersionData(factory *cmdutil.Factory, flagSoftware, flagVersion
url = v[0]
}

softwareType := ""
if v, err := flags.GetFlagStringValues(cmd, flagSoftwareType); err == nil && len(v) > 0 {
softwareType = v[0]
}

if softwareType == "" && software != "" {
// Set software type by looking up the software (if found)
matchingSoftware, _, err := client.Software.GetSoftwareByName(c8y.WithDisabledDryRunContext(context.Background()), software, c8y.NewPaginationOptions(5))
if err != nil {
return "", "", err
}

if len(matchingSoftware.ManagedObjects) > 0 {
if v := matchingSoftware.Items[0].Get("softwareType"); v.Exists() {
softwareType = v.String()
}
}
}

_, dst, _ := flags.UnpackGetterOptions("", opts...)

output := map[string]string{}

if softwareType != "" {
output["softwareType"] = softwareType
}

// If version is empty, then pass the values as is
if version == "" || (software != "" && version != "" && url != "") {
output["name"] = software
Expand Down
6 changes: 4 additions & 2 deletions pkg/cmd/software/versions/install/install.auto.go

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

2 changes: 1 addition & 1 deletion pkg/cmdparser/cmdparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ func GetOption(cmd *CmdOptions, p *models.Parameter, factory *cmdutil.Factory, a
opts = append(opts, c8yfetcher.WithSoftwareByNameFirstMatch(factory, args, p.Name, targetProp, p.Format))

case "softwareDetails":
opts = append(opts, c8yfetcher.WithSoftwareVersionData(factory, p.GetDependentProperty(0, "software"), p.Name, p.GetDependentProperty(1, "url"), args, "", targetProp, p.Format))
opts = append(opts, c8yfetcher.WithSoftwareVersionData(factory, p.GetDependentProperty(0, "software"), p.Name, p.GetDependentProperty(1, "url"), p.GetDependentProperty(2, "softwareType"), args, "", targetProp, p.Format))

case "configurationDetails":
opts = append(opts, c8yfetcher.WithConfigurationFileData(factory, p.Name, p.GetDependentProperty(0, "configurationType"), p.GetDependentProperty(1, "url"), args, "", targetProp, p.Format))
Expand Down
2 changes: 1 addition & 1 deletion scripts/build-cli/New-C8yApiGoGetValueFromFlag.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
"software[]" = "c8yfetcher.WithSoftwareByNameFirstMatch(n.factory, args, `"${prop}`", `"${queryParam}`"$FormatValue),"

"softwareDetails" = @(
"c8yfetcher.WithSoftwareVersionData(n.factory, `"software`", `"version`", `"url`", args, `"`", `"${queryParam}`"$FormatValue),"
"c8yfetcher.WithSoftwareVersionData(n.factory, `"software`", `"version`", `"url`", `"softwareType`", args, `"`", `"${queryParam}`"$FormatValue),"
) -join "`n"

"configurationDetails" = @(
Expand Down
43 changes: 38 additions & 5 deletions tests/manual/software/install/software_versions_install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ config:
tests:
It creates an operation to update software without a version:
command: |
c8y software versions install --device 1 --software "myapp" --description "Installing software" |
c8y software versions install --device 1 --software "userapp1" --description "Installing software" |
c8y util show --select method,pathEncoded,body --compact=false
exit-code: 0
stdout:
Expand All @@ -20,7 +20,7 @@ tests:
"c8y_SoftwareUpdate": [
{
"action": "install",
"name": "myapp",
"name": "userapp1",
"url": "",
"version": ""
}
Expand All @@ -36,7 +36,7 @@ tests:
command: |
c8y software versions install \
--device 1 \
--software "myapp" \
--software "userapp1" \
--description "Installing software" \
--url "https://test.com/binary/package.zip" |
c8y util show --select method,pathEncoded,body --compact=false
Expand All @@ -48,7 +48,7 @@ tests:
"c8y_SoftwareUpdate": [
{
"action": "install",
"name": "myapp",
"name": "userapp1",
"url": "https://test.com/binary/package.zip",
"version": ""
}
Expand Down Expand Up @@ -77,6 +77,7 @@ tests:
{
"action": "install",
"name": "my-app",
"softwareType": "linux-package",
"url": "https://example.com/debian/my-app-1.2.3.deb",
"version": "1.2.3"
}
Expand Down Expand Up @@ -106,6 +107,7 @@ tests:
{
"action": "install",
"name": "my-app",
"softwareType": "linux-package",
"url": "https://test.com/binary/package.zip",
"version": "1.2.3"
}
Expand All @@ -115,4 +117,35 @@ tests:
},
"method": "POST",
"pathEncoded": "/devicecontrol/operations"
}
}

It creates an operation to update software with a version but a custom softwareType:
command: |
c8y software versions install \
--device 1 \
--software "my-app" \
--version "1.2.3" \
--softwareType otherType \
--description "Installing software" \
--url "https://test.com/binary/package.zip" |
c8y util show --select method,pathEncoded,body --compact=false
exit-code: 0
stdout:
exactly: |
{
"body": {
"c8y_SoftwareUpdate": [
{
"action": "install",
"name": "my-app",
"softwareType": "otherType",
"url": "https://test.com/binary/package.zip",
"version": "1.2.3"
}
],
"description": "Installing software",
"deviceId": "1"
},
"method": "POST",
"pathEncoded": "/devicecontrol/operations"
}
7 changes: 5 additions & 2 deletions tests/scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ setup () {
create_firmware_version "iot-linux" "1.0.0" "https://example.com"
create_firmware_patch_version "iot-linux" "1.0.1" "https://example.com/patch1"

create_software "my-app"
create_software "my-app" "linux-package"
create_software_version "my-app" "1.2.3" "https://example.com/debian/my-app-1.2.3.deb"

create_device_profile "profile01"
Expand Down Expand Up @@ -203,8 +203,11 @@ create_configuration () {

create_software () {
local name="$1"
c8y software get -n --id "$name" --silentStatusCodes 404 ||
local software_type="$2"
{
c8y software get -n --id "$name" --silentStatusCodes 404 ||
c8y software create -n --name "$name"
} | c8y software update --softwareType "$software_type"
}

create_software_version () {
Expand Down
7 changes: 6 additions & 1 deletion tools/PSc8y/Public/Install-SoftwareVersion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,16 @@ Get a software package
[object[]]
$Version,

# Software url. Leave blank to automatically set it if a matching firmware/version is found in the c8y firmware repository
# Software url. Leave blank to automatically set it if a matching software/version is found in the c8y software repository
[Parameter()]
[string]
$Url,

# Software type. Leave blank to automatically set it if a matching software/version is found in the c8y software repository
[Parameter()]
[string]
$SoftwareType,

# Operation description
[Parameter()]
[string]
Expand Down