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

name filter is not exact #128

Closed
baelen-git opened this issue Oct 18, 2023 · 2 comments
Closed

name filter is not exact #128

baelen-git opened this issue Oct 18, 2023 · 2 comments

Comments

@baelen-git
Copy link
Contributor

baelen-git commented Oct 18, 2023

I have 2 SPT's:
OCP-BM
OCP-BM-M6

When I search for OCP-BM the name filter returns 2 SPTs

jump@jumpserver:~/intersight$ ./isctl get  server profiletemplate name OCP-BM -o jsonpath='$[*].Moid'
6520445d697265301fb9fc5d
652ee57677696e301f6f8a49

this is a valid workaround

jump@jumpserver:~/intersight$ ./isctl get  server profiletemplate --filter "Name eq 'OCP-BM'" -o jsonpath='$[*].Moid'
652ee57677696e301f6f8a40
@cgascoig
Copy link
Owner

Actually, notice that the Moid in the second query is not in the results of the first query.

What is going on here is that both queries are finding the same single SPT, the difference is what the JSONPath extracts from the results because of a subtle difference between name XYZ and --filter "Name eq 'XYZ'.

When you use name XYZ isctl knows that the result should be a single object, so it gets returned as a single object:

❯ isctl get ntp policy name isctl-bats -o yaml
AccountMoid: 59c84e4a16267c0001c23428
Ancestors: []
AuthenticatedNtpServers: []
ClassId: ntp.Policy
CreateTime: "2023-10-22T21:57:48.97Z"
Description: ""
DomainGroupMoid: 5b25418d7a7662743465cf72
Enabled: true
ModTime: "2023-10-22T21:57:48.97Z"
Moid: 65359adc6275723101e2ea54
Name: isctl-bats
NtpServers:
- 1.1.1.1
ObjectType: ntp.Policy
Organization:
  ClassId: mo.MoRef
  Moid: 5ddec4226972652d33548943
  ObjectType: organization.Organization
  link: https://www.intersight.com/api/v1/organization/Organizations/5ddec4226972652d33548943
Owners:
- 59c84e4a16267c0001c23428
PermissionResources:
- ClassId: mo.MoRef
  Moid: 5ddec4226972652d33548943
  ObjectType: organization.Organization
  link: https://www.intersight.com/api/v1/organization/Organizations/5ddec4226972652d33548943
Profiles: []
SharedScope: ""
Tags: []
Timezone: Pacific/Niue

Whereas if you use --filter "Name eq 'XYZ' the result will be a list (because it could be 0, 1, or N results) - in this case, notice that it is a list of length 1:

❯ isctl get ntp policy --filter "Name eq 'isctl-bats'" -o yaml
- AccountMoid: 59c84e4a16267c0001c23428
  Ancestors: []
  AuthenticatedNtpServers: []
  ClassId: ntp.Policy
  CreateTime: "2023-10-22T21:57:48.97Z"
  Description: ""
  DomainGroupMoid: 5b25418d7a7662743465cf72
  Enabled: true
  ModTime: "2023-10-22T21:57:48.97Z"
  Moid: 65359adc6275723101e2ea54
  Name: isctl-bats
  NtpServers:
  - 1.1.1.1
  ObjectType: ntp.Policy
  Organization:
    ClassId: mo.MoRef
    Moid: 5ddec4226972652d33548943
    ObjectType: organization.Organization
    link: https://www.intersight.com/api/v1/organization/Organizations/5ddec4226972652d33548943
  Owners:
  - 59c84e4a16267c0001c23428
  PermissionResources:
  - ClassId: mo.MoRef
    Moid: 5ddec4226972652d33548943
    ObjectType: organization.Organization
    link: https://www.intersight.com/api/v1/organization/Organizations/5ddec4226972652d33548943
  Profiles: []
  SharedScope: ""
  Tags: []
  Timezone: Pacific/Niue

The next subtle thing is the way JSONPath works - if you take a look at the JSONPath spec you will see that it uses [] for both the child operator and the subscript operator - this gives the slightly unexpected behaviour that $[*].Moid means both "get the Moid attribute from each object in this list" or "get the Moid attribute from each child of this object".

Going back to the ntp policy example above:

❯ isctl get ntp policy name isctl-bats -o jsonpath='$[*].Moid'
5ddec4226972652d33548943
❯ isctl get ntp policy --filter "Name eq 'isctl-bats'" -o jsonpath='$[*].Moid'
65359adc6275723101e2ea54

Notice that they return different Moids. This is because the first one is actually returning $.Organization.Moid whereas the second is returning $[0].Moid

All of this is to say that when using name XYZ your JSONPath query should really be $.Moid not $[*].Moid:

❯ isctl get ntp policy name isctl-bats -o jsonpath='$.Moid'
65359adc6275723101e2ea54
❯ isctl get ntp policy --filter "Name eq 'isctl-bats'" -o jsonpath='$[*].Moid'
65359adc6275723101e2ea54

I fully appreciate this is quite subtle and a bit confusing - I would definitely welcome any suggestions to simplify this. One option is just to offer something better than JSONPath which is pretty easy to get wrong when you just want to pull out a specific attribute - maybe just use Go templates instead (see #100).

@baelen-git
Copy link
Contributor Author

Thanks for the explanation Chris. Much appreciated, now I understand what is happening

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants