reubenmiller / go-c8y-cli

go c8y cli tool for interacting with the Cumulocity IoT API
https://goc8ycli.netlify.app/
MIT License
36 stars 8 forks source link

Funny output from `api` command #346

Closed bjan-sag closed 8 months ago

bjan-sag commented 8 months ago

When trying to get health of the sslmanagement service, I get a malformed response:

$ c8y api GET '/service/sslmanagement/health'
"liveness"
"readiness"

With debug:

```console $ c8y rest --debug GET '/service/sslmanagement/health' 2024-02-20T14:35:00.878+0100 DEBUG Configuring core modules 2024-02-20T14:35:00.879+0100 DEBUG Creating c8y client 2024-02-20T14:35:00.879+0100 DEBUG using explicit proxy [localhost:8111] 2024-02-20T14:35:00.879+0100 INFO Binding authorization environment variables 2024-02-20T14:35:00.879+0100 DEBUG timeout: 1m0s 2024-02-20T14:35:00.879+0100 INFO activityLog path: /home/bjan/.cumulocity/activitylog/c8y.activitylog.2024-02-20.json 2024-02-20T14:35:00.879+0100 INFO Loaded session: /home/bjan/.cumulocity/eulatest.json 2024-02-20T14:35:00.879+0100 DEBUG command str: api 2024-02-20T14:35:00.879+0100 INFO command: c8y rest --debug GET /service/sslmanagement/health 2024-02-20T14:35:00.879+0100 DEBUG output format: table 2024-02-20T14:35:00.881+0100 DEBUG starting worker: 1 2024-02-20T14:35:00.881+0100 INFO Max jobs: 0 2024-02-20T14:35:00.881+0100 DEBUG checking job iterator: 1 2024-02-20T14:35:00.881+0100 DEBUG Input line: %!s() 2024-02-20T14:35:00.881+0100 DEBUG adding job: 1 2024-02-20T14:35:00.882+0100 DEBUG checking job iterator: 2 2024-02-20T14:35:00.882+0100 DEBUG finished adding jobs. lastJobID=1 2024-02-20T14:35:00.882+0100 INFO worker 1: started job 1 2024-02-20T14:35:00.882+0100 INFO Current username: management/bartosz 2024-02-20T14:35:00.882+0100 DEBUG Overriding common options provided in the context. dryRun=false 2024-02-20T14:35:00.882+0100 INFO Headers: map[Accept:[application/json] Authorization:[Basic redacted] User-Agent:[go-client] X-Application:[go-client]] 2024-02-20T14:35:00.882+0100 INFO Sending request: GET http://eu-latest.cumulocity.com/service/sslmanagement/health 2024-02-20T14:35:00.882+0100 INFO GET http://eu-latest.cumulocity.com/service/sslmanagement/health 2024-02-20T14:35:00.968+0100 INFO Status code: 200 2024-02-20T14:35:00.968+0100 INFO Response time: 86ms 2024-02-20T14:35:00.968+0100 INFO Response Content-Type: application/json 2024-02-20T14:35:00.968+0100 DEBUG Response Headers: map[Cache-Control:[no-cache, no-store, max-age=0, must-revalidate] Connection:[keep-alive] Content-Type:[application/json] Date:[Tue, 20 Feb 2024 13:35:00 GMT] Expires:[0] Pragma:[no-cache] X-Content-Type-Options:[nosniff] X-Frame-Options:[DENY] X-Xss-Protection:[1; mode=block]] 2024-02-20T14:35:00.969+0100 INFO Response Length: 0.5KB (uncompressed) 2024-02-20T14:35:00.969+0100 DEBUG Array properties: [groups] 2024-02-20T14:35:00.969+0100 DEBUG Data property: groups 2024-02-20T14:35:00.969+0100 INFO Unfiltered array size. len=2 2024-02-20T14:35:00.969+0100 INFO View mode: auto 2024-02-20T14:35:00.969+0100 DEBUG Looking for definitions in: [/etc/go-c8y-cli/views /home/bjan/.config/c8y/views] 2024-02-20T14:35:00.969+0100 DEBUG Current view path: /etc/go-c8y-cli/views 2024-02-20T14:35:00.970+0100 DEBUG Found view definition: activitylog.json 2024-02-20T14:35:00.970+0100 DEBUG Found view definition: alarms.json 2024-02-20T14:35:00.971+0100 DEBUG Found view definition: applicationReference.json 2024-02-20T14:35:00.971+0100 DEBUG Found view definition: applications.json 2024-02-20T14:35:00.971+0100 DEBUG Found view definition: auditRecord.json 2024-02-20T14:35:00.971+0100 DEBUG Found view definition: bulkOperation.json 2024-02-20T14:35:00.971+0100 DEBUG Found view definition: configuration.json 2024-02-20T14:35:00.971+0100 DEBUG Found view definition: deviceCredentials.json 2024-02-20T14:35:00.971+0100 DEBUG Found view definition: deviceGroup.json 2024-02-20T14:35:00.972+0100 DEBUG Found view definition: devicecertificates.json 2024-02-20T14:35:00.972+0100 DEBUG Found view definition: deviceprofile.json 2024-02-20T14:35:00.972+0100 DEBUG Found view definition: devices.json 2024-02-20T14:35:00.972+0100 DEBUG Found view definition: events.json 2024-02-20T14:35:00.972+0100 DEBUG Found view definition: firmware.json 2024-02-20T14:35:00.972+0100 DEBUG Found view definition: identity.json 2024-02-20T14:35:00.972+0100 DEBUG Found view definition: inbuilt.json 2024-02-20T14:35:00.972+0100 DEBUG Found view definition: inventoryRole.json 2024-02-20T14:35:00.972+0100 DEBUG Found view definition: managedObject.json 2024-02-20T14:35:00.973+0100 DEBUG Found view definition: managedObjectReference.json 2024-02-20T14:35:00.973+0100 DEBUG Found view definition: measurement.json 2024-02-20T14:35:00.973+0100 DEBUG Found view definition: notification2.json 2024-02-20T14:35:00.973+0100 DEBUG Found view definition: operations.json 2024-02-20T14:35:00.973+0100 DEBUG Found view definition: options.json 2024-02-20T14:35:00.973+0100 DEBUG Found view definition: rententionRule.json 2024-02-20T14:35:00.973+0100 DEBUG Found view definition: role.json 2024-02-20T14:35:00.973+0100 DEBUG Found view definition: services.json 2024-02-20T14:35:00.973+0100 DEBUG Found view definition: settings.json 2024-02-20T14:35:00.974+0100 DEBUG Found view definition: smartGroup.json 2024-02-20T14:35:00.974+0100 DEBUG Found view definition: software.json 2024-02-20T14:35:00.974+0100 DEBUG Found view definition: statistics.json 2024-02-20T14:35:00.974+0100 DEBUG Found view definition: tenant.json 2024-02-20T14:35:00.975+0100 DEBUG Found view definition: tenantStatistics.json 2024-02-20T14:35:00.975+0100 DEBUG Found view definition: user.json 2024-02-20T14:35:00.975+0100 DEBUG Found view definition: userGroup.json 2024-02-20T14:35:00.975+0100 DEBUG Loaded definitions: 55 2024-02-20T14:35:00.975+0100 DEBUG Current view path: /home/bjan/.config/c8y/views 2024-02-20T14:35:00.975+0100 DEBUG Skipping view path because it does not exist. path=/home/bjan/.config/c8y/views, error=stat /home/bjan/.config/c8y/views: no such file or directory 2024-02-20T14:35:00.976+0100 DEBUG No matching view found 2024-02-20T14:35:00.976+0100 INFO No matching view detected. defaulting to '**' "liveness" "readiness" 2024-02-20T14:35:00.976+0100 INFO worker 1: finished job 1 in 94ms 2024-02-20T14:35:00.976+0100 DEBUG job successful ```

Meanwhile curl works fine:

$ curl -H 'Authorization:Basic redacted' localhost:8111/service/sslmanagement/health | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   521    0   521    0     0   7661      0 --:--:-- --:--:-- --:--:--  7661
{
  "status": "UP",
  "components": {
    "diskSpace": {
      "status": "UP",
      "details": {
        "total": 161036111872,
        "free": 114176151552,
        "threshold": 10485760,
        "exists": true
      }
    },
    "heapMemory": {
      "status": "UP",
      "details": {
        "init": 16777216,
        "used": 59712136,
        "committed": 81485824,
        "max": 259522560,
        "threshold": 12976128
      }
    },
    "livenessState": {
      "status": "UP"
    },
    "nonHeapMemory": {
      "status": "UP",
      "details": {
        "init": 7667712,
        "used": 119038344,
        "committed": 124125184,
        "max": -1,
        "threshold": -1
      }
    },
    "ping": {
      "status": "UP"
    },
    "readinessState": {
      "status": "UP"
    }
  },
  "groups": [
    "liveness",
    "readiness"
  ]
}

Version info:

$ c8y version
| branch     | version     |
|------------|-------------|
| HEAD       | 2.39.2      |
reubenmiller commented 8 months ago

@bjan-sag You're seeing this output because go-c8y-cli is trying to be smart and guess where the "interesting parts" of the API response is, by default this is array items.

To disable this "auto detection" just use the global --raw flag.

This should do what you want:

c8y api GET '/service/sslmanagement/health' --raw
bjan-sag commented 8 months ago

Ah, this works - thanks! I tried other views, and output=json alone doesn't help either. I find this default behavior very surprising. Would be great if this smart behavior was opt-in instead of default, but that's personal preference :)

reubenmiller commented 8 months ago

Ah, this works - thanks! I tried other views, and output=json alone doesn't help either. I find this default behavior very surprising. Would be great if this smart behavior was opt-in instead of default, but that's personal preference :)

For the api command being "less smart" might actually be a more sensible default...but for the other commands it is definitely better to trim down what is shown (e.g. show array items of .managedObjects[] instead of the base object).

But I suspect looking at creating your own extension could be interesting to you...

For example, this api endpoint definition (to create a new subcommand group and command)

file: myextextion/api/microservices.yaml

# yaml-language-server: $schema=https://raw.githubusercontent.com/reubenmiller/go-c8y-cli/feat/extensions-manager/tools/schema/extensionCommands.json
---
version: v1
group:
  name: microservices
  description: Cumulocity IoT Microservices health

commands:
  - name: health
    description: Get a microservice health status
    method: GET
    path: /service/{id}/health
    collectionProperty: "-"
    pathParameters:
      - name: id
        type: microservicename
        description: Microservice

Then you can call the command using (with tab completion)

c8y myextension microservices health --id cloud-remote-access

You can even add your own custom views to it:

$ c8y devmgmt ms health --id cloud-remote-access
| status     | livenessStatus | heapUsed       |
|------------|----------------|----------------|
| UP         | UP             | 151554000      |
reubenmiller commented 8 months ago

Or if you just want a light weight alternative, just use an alias which sets the --raw value by default for the c8y api call:

c8y alias set apir 'api --raw'
c8y apir /service/cloud-remote-access/health
bjan-sag commented 8 months ago

For the api command being "less smart" might actually be a more sensible default...but for the other commands it is definitely better to trim down what is shown (e.g. show array items of .managedObjects[] instead of the base object).

I agree - you need a balance here to keep the tool ergonomic.

Thanks for providing the examples, they're really cool. I'll set up an alias for now :)