Post a NIFI template via REST?

梦想与她 提交于 2019-12-05 07:22:36

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

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

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

Where filename is the filename of your template and url is the path to your nifi instance. I haven't figured it out in curl directly but this should hopefully get folks with a similar question started!

Edit: Note that you also can't upload a template with the same name as an existing template. Make sure to delete your existing template before attempting to re-upload. Using the untangle library to parse the XML of the 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])
Stephane

The API has moved in 1.0 to:

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

Example, using Python's requests library:

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

If you want to POST 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 that the template was given when it was generated.

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

The documentation can be confusing because that endpoint is overloaded and the documentation tool only generates doc for one of them (see NIFI-1113). There is an email thread that addresses the import of a template using curl, so between the above answer and the email thread, hopefully you can find the approach that works for you.

I've implemented a full Python client for doing this in NiPyApi
Key functions for templates are:

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

The client supports NiFi-1.1.2 - 1.7.1 currently, and NiFi-Registry (which is a lot better than templates for flow deployment)

You can use Nifi Api to upload a template, to do that 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. Upload 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"
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!