ONTAP Rest API Discussions

CIFS sessions files API discrepancies

wildcatz4life
1,510 Views

I am working on a script to close all open files within a specific folder. I started down the path of using the REST API, but was unable to get the information needed to run the /protocols/cifs/session/files/{node.uuid}/{svm.uuid}/{identifier}/{connection.identifier}/{session.identifier} command.

 

According to https://library.netapp.com/ecmdocs/ECMLP2884821/html/#/NAS/cifs_open_file_collection_get You should get the following response: 

# The response:

{

  "records": [

                  {

                      "node": {

                        "uuid": "9ea96c65-4411-11ec-b358-005056bbdd2c",

                        "name": "node1"

                      },

                      "svm": {

                        "uuid": "62234287-4412-11ec-b358-005056bbdd2c",

                        "name": "svm1"

                      },

                      "file": {

                        "identifier": 24,

                        "type": "regular"

                      },

                      "connection": {

                        "identifier": 127465,

                        "count": 1

                      },

                      "session_id": 1827054073828868097,

                      "open_mode": "w",

                      "volume": {

                        "name": "vol1",

                        "uuid": "7a5ef257-52de-11e8-95f4-005056952000"

                      },

                      "share": {

                        "name": "root",

                        "mode": "rwd"

                      },

                      "path": "a\\b\\c\\f1",

                      "continuously_available": "no",

                      "reconnected": "No",

                      "range_lock_count": 0

                },

                {

                    "node": {

                      "uuid": "9ea96c65-4411-11ec-b358-005056bbdd2c",

                      "name": "node2"

                    },

                    "svm": {

                      "uuid": "62234287-4412-11ec-b358-005056bbdd2c",

                      "name": "svm2"

                    },

                    "file": {

                      "identifier": 14,

                      "type": "regular"

                    },

                    "connection": {

                      "identifier": 127465,

                      "count": 1

                    },

                    "session_id": 1827054073828868097,

                    "open_mode": "r",

                    "volume": {

                      "name": "vol2",

                      "uuid": "7a5ef257-52de-11e8-95f4-005056952000"

                    },

                    "share": {

                      "name": "root",

                      "mode": "rwd"

                    },

                    "path": "a\\b\\c\\f2",

                    "continuously_available": "no",

                    "reconnected": "No",

                    "range_lock_count": 0

                    }

            ],

            "num_records": 2

  }

 

However, the response I get is missing all the information after session_id:

 

{

    "records": [

        {

            "node": {

                "uuid": "XXX",

                "name": "XXX",

                "_links": {

                    "self": {

                        "href": "/api/cluster/nodes/XXX"

                    }

                }

            },

            "svm": {

                "name": "XXX",

                "uuid": "XXX"

            },

            "identifier": XXX,

            "connection": {

                "identifier": XXX

            },

            "session": {

                "identifier": XXX

            },

            "_links": {

                "self": {

                    "href": "/api/protocols/cifs/session/files/XXX/XXX/XXX/XXX/XXX"

                }

            }

        }

    ],

    "num_records": 1,

    "_links": {

        "self": {

            "href": "/api/protocols/cifs/session/files?"

        }

    }

}

 

My current work around has been to use private cli command:

https://xxx/api/private/cli/cifs/session/file?fields=node,connection_id,session_id,file_id,path

 

The problem with the private cli command is that I need the svm Uuid and that is not a viable field. Though I do know the SVM of the folder in question, so that won't change, but wanted to make the script a bit more modular to handle any folder/file path combo.

5 REPLIES 5

wildcatz4life
1,410 Views

Current workaround is to use the fields parameter to select the additional fields as the documentation say when using GET commands: GET calls on collections usually return only name and UUID by default. If you want to retrieve additional properties, you need to specify them using the "fields" query parameter.

 

curl --insecure --user xxx:xxx GET https://xxx/api/protocols/cifs/session/files?fields=path,volume,share&return_timeout=15&return_records=true -H "accept: application/json"

christybiskit
1,055 Views

wildcatz4life, this is great information, thank you. Did you get your code working to close open files? I need to get open files filtered on a specific path. I get no results with this code (I did verify that the file at the path was open at the time of the script execution). If I remove the "path" body element I get the expected response for all open files on "share.name". So something is wrong with my "path" filter. Any ideas?

 

$body = @{
"return_timeout" = "15"
"return_records" = "true"
"share.name" = "myshare"
"path" = "myfiles\\myfolder1\\mydll.dll"
}
$headers = @{
"accept" = "application/json"
}
$response = Invoke-WebRequest -Uri "https://mynetapp/api/protocols/cifs/session/files?fields=path,volume,share" -Credential (Get-Credential) -Body $body -Headers $headers
$response.AllElements

 

wildcatz4life
935 Views

I just took the response for all open files, and then parsed through them with a for loop and an if statement comparing the path and share name.

christybiskit
927 Views

ah, ok. Any chance you have that code nearby? I am a little green in using/parsing JSON files.

wildcatz4life
924 Views

this is written in powershell:

 

$searchFilepath = 'XX\XX'
$shareName = 'XXXX'

# Create paramaters for RestAPI call
$Params = @{
"URI" = "https://$svmIP/api/protocols/cifs/session/files?fields=path,volume,share,identifier&return_timeout=60&return_records=true"
"Method" = 'GET'
"Credential" = $credential
}

$responseJSON = Invoke-RestMethod @Params

#Loop through each response give from API call searching for path defined above
foreach ($record in $responseJSON.records){


if($record.path -like $searchFilepath -And $record.share.name -like $shareName){
#write-host "Node " $record.node.uuid ", " SVM $record.svm.uuid ", " FILE ID $record.identifier ", " Connection ID $record.connection.identifier", " Session ID $record.session.identifier

write-host "File closed: " $record.path

$nodeUUID = $record.node.uuid
$svmUUID = $record.svm.uuid
$fileID = $record.identifier
$connectionID = $record.connection.identifier
$sessionID = $record.session.identifier

#close file DELETE /protocols/cifs/session/files/{node.uuid}/{svm.uuid}/{file.identifier}/{connection.identifier}/{session_id}
$CloseParams = @{
"URI" = "https://$svmIP/api/protocols/cifs/session/files/$nodeUUID/$svmUUID/$fileID/$connectionID/$sessionID"
"Method" = 'DELETE'
"Credential" = $credential
}
#write-host $CloseParams.URI
Invoke-RestMethod @CloseParams

}
}

Public