Exemplo n.º 1
0
def delete_terminated_agents(agents_to_delete):
    restclient = RestClient(tetration_url,
                            api_key=tetration_api_key,
                            api_secret=tetration_api_secret,
                            verify=False)

    for agent in agents_to_delete:
        print('INFO: Deleting sensor with uuid: {}'.format(agent['host_uuid']))
        resp = restclient.delete('/openapi/v1/sensors/{}'.format(
            agent['host_uuid']))
        if resp.ok:
            print('INFO: Deleted sensor with uuid: {}'.format(
                agent['host_uuid']))
        else:
            print("ERROR: Failed to Deleted sensor with uuid: {}".format(
                agent['host_uuid']))
            print(resp.text)
Exemplo n.º 2
0
restclient = RestClient(API_ENDPOINT,
                        credentials_file='credentials.json',
                        verify=False)
#GET
appScopes = restclient.get('/app_scopes').json()
#Iterate through JSON response for SearchStright and Print name and short_name, id, and query are also options among other
for s in appScopes:
    if searchString in s["name"]:
        req_payload = {
            "scopeName": s["name"],  # optional
            "filter": {
                "type":
                "and",
                "filters": [
                    {
                        "type": "subnet",
                        "field": "ip",
                        "value": "0.0.0.0/0"
                    },
                ]
            }
        }
        #Iterate through scopes that match the searchString and get the inventory count
        scopeInv = restclient.post('/inventory/count',
                                   json_body=json.dumps(req_payload)).json()
        #If  Inventory count is 0, delete scope and print name and id
        if scopeInv["count"] == 0:
            restclient.delete('/app_scopes/' + s["id"])
            print(s["name"] + ' ID: ' + s["id"] + ' Count:' +
                  str(scopeInv["count"]) + ' Deleted')
Exemplo n.º 3
0
# make sure we get a good response from the request
if resp.status_code != 200:
   print("Unsuccessful request returned code: {} , \
       response: {}".format(resp.status_code,resp.reason))
results = resp.json()

pprint.pprint(results[0])

#---------------------------------------------------------------------------------------

# Delete Application Workspace

# Create the Tetration REST Client 
rc = RestClient(API_ENDPOINT, api_secret=API_SECRET, api_key=API_KEY, verify=False)

# this is already deleted
deleteme = '5d71196e755f0254fbc8615b'

DEL_ENDPOINT = "/openapi/v1/applications/"+deleteme
print(DEL_ENDPOINT)
resp = rc.delete(DEL_ENDPOINT)

# make sure we get a good response from the request
if resp.status_code != 200:
   print("Unsuccessful request returned code: {} , \
       response: {}".format(resp.status_code,resp.reason))
results = resp.json()

pprint.pprint(results.success)
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)
searchFilter = "host-"
req_payload = {
    #ScopeNames Below, you can have multiple, or just one
    "scopeName": "Default",
    "Default:Scope1:Scope2"
    "limit": 2,
    "filter": {
        "type":
        "and",
        "filters": [
            #Hostname is just one of any field you can search on, contains can be any of the boolean values
            {
                "type": "contains",
                "field": "hostname",
                "value": searchFilter
            }
        ]
    }
}
resp = restclient.post('/inventory/search', json_body=json.dumps(req_payload))
print(resp.status_code)
count = 0
if resp.status_code == 200:
    parsed_resp = resp.json()
    for results in parsed_resp["results"]:
        #print(results["host_name"], results["ip"], results["host_uuid"])
        resp2 = restclient.delete('/sensors/' + results["host_uuid"])
        print(str(resp2.status_code), results["host_uuid"])
        count = count + 1
        print(count)