def upload_userdata( endpoint, file_path, app_scope): restclient = RestClient(endpoint, credentials_file='api_key.json', verify=False) #file_path = # source file contains hangul with UTF-8 encoding #app_scope = 'APJDemo' # name of scope VRF independent # upload user annotation file req_payload = [tetpyclient.MultiPartOption(key='X-Tetration-Oper', val='add')] resp = restclient.upload(file_path, '/assets/cmdb/upload/' + app_scope, req_payload) #print(resp, resp.text) #file_path = 'output.csv' #resp = restclient.download(file_path, '/assets/cmdb/download/' + app_scope) with open(file_path) as inf: # activate annotation with column names # hdr = ['VM_Name', 'VM_Guest_OS', 'VM_Host', 'VM_Datastore', 'VM_Network', "VM_OWNER"] hdr = inf.readline().strip().split(',') print(hdr[1:]) resp = restclient.put('/assets/cmdb/annotations/' + app_scope, json_body=json.dumps(hdr[1:])) print(resp, resp.text)
def main(): module = AnsibleModule( argument_spec=dict( api_key=dict(type='str', required=True), api_secret=dict(type='str', required=True), host=dict(type='str', required=True), api_version=dict(type='str', default='v1'), name=dict(type='str', required=True), method=dict(type='str', required=True, choices=['delete', 'get', 'post', 'put']), payload=dict(type='dict', required=False), params=dict(type='dict', required=False), ), # we can't predict if the proposed API call will make a change to the system supports_check_mode=False ) # if tetpyclient is not available, our only option is to fail try: import json import tetpyclient from tetpyclient import RestClient except ImportError: module.fail_json(msg="Some module dependencies are missing.") method = module.params['method'] api_name = '/openapi/' + module.params['api_version'] + '/' + module.params['name'] req_payload = module.params['payload'] restclient = RestClient( module.params['host'], api_key=module.params['api_key'], api_secret=module.params['api_secret'], verify=False ) # Do our best to provide "changed" status accurately, but it's not possible # as different Tetration APIs react differently to operations like creating # an element that already exists. changed = False if method == 'get': response = restclient.get(api_name, params=module.params['params']) elif method == 'delete': response = restclient.delete(api_name) changed = True if response.status_code / 100 == 2 else False elif method == 'post': response = restclient.post(api_name, json_body=json.dumps(req_payload)) changed = True if response.status_code / 100 == 2 else False elif method == 'put': response = restclient.put(api_name, json_body=json.dumps(req_payload)) changed = True if response.status_code / 100 == 2 else False # Put status_code in the return JSON. If the status_code is not 200, we # add the text that came from the REST call and the payload to make # debugging easier. result = {} result['status_code'] = response.status_code result['ok'] = response.ok result['reason'] = response.reason if int(response.status_code) / 100 == 2: result['json'] = response.json() else: result['text'] = response.text module.exit_json(changed=changed, **result)
def scope_action(hostname, api_key, api_secret, scope_name, filters, parent_scope_name, commit, validate_certs): result = {"ansible_facts": {}} api_endpoint = 'https://{0}'.format(hostname) restclient = RestClient(api_endpoint, api_key=api_key, api_secret=api_secret, verify=validate_certs) # Getting the scopes to do parent_id lookup and check if scope already exists resp = restclient.get('/openapi/v1/app_scopes') if not resp.status_code == 200: return ( 1, "Error {0}: {1} during connection attempt to {2}/openapi/v1/app_scopes. \n" .format(resp.status_code, resp.reason, api_endpoint)) cluster_scopes = json.loads(resp.content) # De-capitalize and hyphenate any field keywords in the filters for i, filter in enumerate(filters): if filter['field'] == 'Address Type': newfield = filter['field'].lower().replace(' ', '_') filters[i]['field'] = newfield if filter['field'] == 'Address': filters[i]['field'] = 'ip' filters[i]['type'] = 'subnet' # Check if scope exists, and filter is the same for scope in cluster_scopes: if scope_name == scope['short_name'] and scope['query']['filters'][1][ 'filters'] == filters: result['changed'] = False return (0, result) #Parent ID lookup cluster_scope_dict = {x['short_name']: x['id'] for x in cluster_scopes} for cluster_scope in cluster_scope_dict.keys(): if cluster_scope == parent_scope_name: parent_scope_id = cluster_scope_dict[cluster_scope] #Build the json payload payload = json.dumps( dict(short_name=scope_name, short_query=dict(type='and', filters=filters), parent_app_scope_id=parent_scope_id)) #If scope already present on the cluster do a PUT, otherwise POST if scope_name in cluster_scope_dict: resp = restclient.put('/openapi/v1/app_scopes/{0}'.format( cluster_scope_dict[scope_name]), json_body=payload) else: resp = restclient.post('/openapi/v1/app_scopes', json_body=payload) if not resp.status_code == 200: return (1, "Error {0}: {1} posting scope to the cluster. \n{2}".format( resp.status_code, resp.reason, resp.json())) #If commit flag set, commit the change at the parent scope if commit: resp = restclient.post( '/openapi/v1/app_scopes/commit_dirty?root_app_scope_id={0}'.format( parent_scope_id), json_body=json.dumps({'sync': True})) if not resp.status_code == 200: return ( 1, "Error {0}: {1} committing scope query change. \n{2}".format( resp.status_code, resp.reason, resp.json())) result["ansible_facts"] = resp.json() result['changed'] = True return (0, result)