Adding new functions to deploy and delete
The patch addes new functions to deploy artifacts to a repo and
delete this one from the repo.
* To deploy artifact below paramters should be defined in a state:
test_deploy:
artifactory.deploy_artifact:
- user: user
- password: password
- host: artifactory.host.name
- proto: https
- port: 443
- source_file: /file/to/deploy.log
- endpoint: /repo-local/dir_if_needed/
* To delete artifact or directory the below paramets should be defined.
CAUTION: The directory will be delete recursively.
test_delete:
artifactory.delete_artifact:
- user: user
- password: password
- host: artifactory.host.name
- proto: https
- port: 443
- item_to_delete: /repo-local/file_or_dir_to_be_deleted
Change-Id: I92b1a1cf751f00844311f7bada9baf0a17b02874
diff --git a/_modules/artifactory.py b/_modules/artifactory.py
index 6987ada..0e53b6f 100644
--- a/_modules/artifactory.py
+++ b/_modules/artifactory.py
@@ -7,6 +7,7 @@
import json
import logging
import requests
+import os
from collections import OrderedDict
@@ -20,6 +21,18 @@
def _api_call(endpoint, data=None, headers=None, method='GET',
**connection_args):
+ if endpoint.startswith('/api') == False:
+ endpoint = '/api' + endpoint
+
+ return _rest_call(endpoint=endpoint,
+ data=data,
+ headers=headers,
+ method=method,
+ **connection_args)
+
+def _rest_call(endpoint, data=None, headers=None, method='GET',
+ **connection_args):
+
log.debug('Got connection args: {}'.format(connection_args))
# Set default values if empty
@@ -34,7 +47,6 @@
'url',
'{proto}://{host}:{port}/artifactory'.format(**connection_args)
)
- api_url = base_url + '/api'
username = connection_args.get('user', 'admin')
password = connection_args.get('password', 'password')
@@ -49,10 +61,10 @@
if(data and method == 'GET'):
method = 'POST'
- endpoint_url = api_url + endpoint
+ endpoint_url = base_url + endpoint
log.debug('Doing {0} request to {1}'.format(method, endpoint_url))
- # API call request
+ # REST API call request
resp = api_connection.request(
method=method,
url=endpoint_url,
@@ -60,7 +72,7 @@
headers=headers
)
- if resp.status_code == requests.codes.ok:
+ if resp.ok:
return True, resp.text
else:
errors = json.loads(resp.text).get('errors')
@@ -71,7 +83,6 @@
log.error('%(status)s:%(message)s' % json.loads(resp.text))
return False, json.loads(resp.text)
-
def get_license(**kwargs):
endpoint = '/system/license'
@@ -249,3 +260,24 @@
headers={'Content-Type': 'application/json'},
**kwargs
)
+
+def deploy_artifact(source_file, endpoint, **kwargs):
+
+ endpoint = endpoint + "/" + os.path.basename(source_file)
+ with open(source_file, 'rb') as input_file:
+ result, status = _rest_call(
+ endpoint=endpoint,
+ method='PUT',
+ data=input_file,
+ **kwargs
+ )
+ return result, json.loads(status)
+
+def delete_artifact(item_to_delete, **kwargs):
+
+ return _rest_call(
+ endpoint=item_to_delete,
+ method='DELETE',
+ **kwargs
+ )
+