Publish NIFI pattern via REST?

I have several nifi servers for which I would like to be able to create POST templates through the REST interface from a script

The "/ controller / templates" endpoint appears to be the proper REST endpoint to support POST connection of an arbitrary template to my Nifi installation. enter image description here The "snippetId" field is what confuses me, how do I determine the "ID of the fragment whose contents will contain the template"? Does anyone have an example of how I can upload the "test.xml" template to my server without using the user interface?

+5
source share
6 answers

The documentation provided is somewhat confusing, and the solution I developed was obtained from the nifi api deploy groovy script at https://github.com/aperepel/nifi-api-deploy

Ultimately, to directly create a POST template, you can use the following in Python requests

requests.post("%s/nifi-api/controller/templates"%(url,), files={"template":open(filename, 'rb')}) 

Where file name is the name of your template and url is the path to your nifi instance. I did not understand this directly in curl, but it will hopefully get people with a similar question to start!

Edit: Please note that you also cannot load a template with the same name as an existing template. Before reloading, be sure to delete the existing template. Using the unravel library to parse the XML template, the following script works just fine:

 import untangle, sys, requests def deploy_template(filename, url): p = untangle.parse(filename) new_template_name=p.template.name.cdata r=requests.get("%s/nifi-api/controller/templates"%(url,), headers={"Accept":"application/json"}) for each in r.json()["templates"]: if each["name"]==new_template_name: requests.delete(each["uri"]) requests.post("%s/nifi-api/controller/templates"%(url,), files={"template":open(filename, 'rb')}) if __name__=="__main__": deploy_template(sys.argv[1], sys.argv[2]) 
+4
source

The API has moved to 1.0:

POST /process-groups/{id}/templates/upload

An example using the Python query library:

 res = requests.post( "{hostname}/nifi-api/process-groups/{destination_process_group}/templates/upload".format( **args ), files={"template": open( file_path, 'rb')} ) 
+4
source

If you want to add a template to NiFi via cURL, you can use the following command:

 curl -iv -F template=@my _nifi_template.xml -X POST http://nifi-host:nifi-port/nifi-api/controller/templates 

This will add the template to the NiFi instance with the same name as the template when it was generated.

And -iv is optional - it's just there for debugging purposes.

+2
source

The documentation can be confusing because this endpoint is overloaded, and the documentation tool only generates a document for one of them (see NIFI-1113 ). There is an email flow that takes into account importing the template using curl, so between the above response and the email flow, I hope you can find an approach that works for you.

+1
source

I have implemented a full Python client for this in NiPyApi
Key features for templates:

  [ "list_all_templates", "get_template_by_name", "deploy_template", "upload_template", "create_pg_snippet", "create_template", "delete_template", "export_template", 'get_template' ] 

The client currently supports NiFi-1.1.2 - 1.7.1 and NiFi-Registry (which is much better than templates for deploying streams)

+1
source

You can use Nifi Api to download the template. To do this, follow these two steps:

1. Get a token from Nifi Api:

 token=$(curl -k -X POST --negotiate -u : https://nifi_hostname:port/nifi-api/access/kerberos) 

2. Download the template file using the token:

 curl -k -F template=@template _file.xml -X POST https://nifi_hostname:port/nifi-api/process-groups/Process_group_id/templates/upload -H "Authorization: Bearer $token" 
0
source

All Articles