Triofox offers a RESTful API (Representational State Transfer Application Programming Interface) that you can use to automate most tasks via simple HTTP requests (Hypertext Transfer Protocol), such as posts, gets, deletes, etc.
You can use any programming or scripting language to perform HTTP requests and the server responses are usually JSON (Javascript Object Notation) or XML (Extensible Markup Language) objects.
The actual API documentation contains the full reference along with the mandatory "Hello World," but in this article, I will show you a Powershell example using two back-to-back API calls with simple response parsing. If you understand this example, you will be able to use any of the documented API functions, because most of the requests require an authentication cookie to be obtained first and then passed in the HTTP Header of any subsequent calls. Most calls also require a JSON object to be passed in the HTTP body, and the example in this article shows you how to do both of these tasks. Triofox offers an alternate Powershell API that you can call directly without having to make extra HTTP requests, but the REST API is more complete, up-to-date, and more flexible for programming with different languages.
Here is an example that imports an AD user from your Active Directory. Before you execute the PowerShell codes, please make sure you already integrated your AD with Triofox. It performs the following tasks in succession:
- Logs in as an admin user and obtains an authentication cookie for future requests
- Obtain storage information
- Obtain Tenant ID from the admin user account
- Execute import AD user API call
The code should be self-explanatory since many comments have been added on the lines starting with a pound sign (#). Please customize the login credentials and an AD user UPN where indicated.
To implement the script, simply copy the entire code between the equal sign lines below into a notepad text file and name it "importADuser.ps1".
Here is the full API documentation.
# Enter your Admin user login ID
# And new user UPN
#
$triofoxadmin="<your cluster admin>"
$triofoxadminpassword="<your cluster admin password>"
$newaduserUPN="<new AD user UPN>"
$tenantid="" #For Triofox, leave this value empty
$ReqBody1 = @"
{
"Username": "$triofoxadmin",
"Password": "$triofoxadminpassword"
}
"@;
$response1 = Invoke-RestMethod "http://localhost/namespace/n.svc/japiusers/login" -Method Post -ContentType "application/json" -Body $ReqBody1;
#$response1|get-member
$cookie = $response1.JAPIUserLoginResult.Cookie;
if ($cookie -eq $null)
{
Write-Host "Failed to login as $triofoxadmin"
Write-Host $response1
exit
}
$tenantid=$adminuser.DomainId
Write-Host "Tenant ID: "$tenantid
$ReqBody2 = @"
{
"TenantId": "$tenantid"
}
"@;
$headers2 = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers2.Add("x-glad-token", $cookie);
$response2 = Invoke-RestMethod "http://localhost/namespace/n.svc/japigettenantstorage" -Method Post -ContentType "application/json" -Headers $headers2 -Body $ReqBody2;
$StorageDescriptorBytes = [System.Text.Encoding]::Unicode.GetBytes($response2.Context)
$encodedStorageDescriptor = [Convert]::ToBase64String($StorageDescriptorBytes);
$ReqBody3 = @"
{
"UserUPN": "$newaduserUPN",
"StoragePlan": 1,
"StorageConfigure": "$encodedStorageDescriptor"
}
"@;
$response3 = Invoke-RestMethod "http://localhost/namespace/n.svc/japiimportuserbyupn" -Method Post -ContentType "application/json" -Headers $headers2 -Body $ReqBody3;
Write-Host $response3;
#====================================================
Sample 2: Upload a file
#UPLOAD FILE AND SHARE - API POWERSHELL EXAMPLE
#Step 1: First we log in as the user and obtain an authentication cookie
#Here we prepare the XML object body for the authentication call. Change the values between the username and password XML fields below
$ReqBody1 = @"
<?xml version="1.0" encoding="UTF-8"?>
<JsonLogin xmlns="http://tempuri.org/">
<username>azeem@located6j.com</username>
<password>test</password>
</JsonLogin>
"@;
#The following code will obtain an authentication cookie and save it in the $cookie variable. Change the IP address below to your own IP or domain name
$response1 = Invoke-RestMethod "http://10.0.0.164/namespace/n.svc/jsonlogin" -Method Post -ContentType "application/xml" -Body $ReqBody1;
$cookie = $response1.JsonLoginResult.Cookie;
#Write-Host "Logged in... got cookie " $cookie;
#--------------------------------------------------------------
#Step 2: Upload a file
#We pass the cookie obtained above in the Header of the next call
$headers2 = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers2.Add("x-glad-token", $cookie);
$headers2.Add("x-glad-altpath", "/gladr.zip");
$errorCode = "";
#PUT the file to proxiedupload.up
try {
Invoke-RestMethod -Uri "http://10.0.0.164/storage/proxiedupload.up" -Method Put -InFile "C:\TMP\gladr.zip" -Headers $headers2;
} catch {
$errorCode = $_.Exception.Response.StatusCode.value__;
Write-Host "Error:" $_.Exception.Response.StatusDescription;
}
#--------------------------------------------------------------
#Step 3:Create a public link for the file
if($errorCode -eq ""){
$headers3 = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers3.Add("x-glad-token", $cookie);
$ReqBody3 = @"
<?xml version="1.0" encoding="UTF-8"?>
<EnableFilePublicLink xmlns="http://tempuri.org/">
<FileUri>/gladr.zip</FileUri>
</EnableFilePublicLink>
"@;
$response3 = Invoke-RestMethod -Uri "http://10.0.0.164/namespace/n.svc/enablefilepubliclink" -Method POST -ContentType "application/xml" -Body $ReqBody3 -Headers $headers3
if($response3.Success -eq "true"){
Write-Host "Uploaded File Successfully";
Write-Host "Public Link: " $response3.Context;
}
}else{
Write-Host "Upload failed!";
}
#====================================================
Comments
0 comments
Please sign in to leave a comment.