def get_campaigns(uri: str, user: str, password: str, api_url: str, __clean_campaign, campaign_id: str = None):
    token = login(uri, user, password)
    if len(token) == 0:
        return

    headers = get_authorized_headers(token)

    endpoint = api_url.format(uri)

    if campaign_id:
        endpoint += "/{}".format(campaign_id)

    get_oscamp_api_request = requests.get(endpoint.format(uri), headers=headers)
    campaigns_dict = get_oscamp_api_request.json()

    if campaign_id:
        logout(uri, token)
        campaign = campaigns_dict.get("data")
        if campaign:
            return print(json.dumps(__clean_campaign(campaign), indent=4))
        else:
            return ""

    campaigns = []
    for campaign in campaigns_dict.get('rows', []):
        campaigns.append(__clean_campaign(campaign))

    logout(uri, token)
    return print(json.dumps(campaigns, indent=4))
Esempio n. 2
0
def add_gateway(uri: str, user: str, password: str, registration_code: str,
                serial_number: str):
    token = login(uri, user, password)
    if len(token) == 0:
        return

    headers = get_authorized_headers(token)

    if not registration_code or not serial_number:
        return print(
            json.dumps(
                {
                    "success":
                    False,
                    "message":
                    "Either serialnumber and registrationcode are required to add a gatrway"
                },
                indent=4))

    body = {
        "registration_code": registration_code,
        "serial_number": serial_number
    }

    req = requests.post(GET_GW_V1_API.format(uri),
                        headers=headers,
                        params=body)
    response = req.json()

    logout(uri, token)
    print(json.dumps(response))
Esempio n. 3
0
def add_device(uri: str,
               user: str,
               password: str,
               gateway_id: int,
               model_id: int,
               serial_number: str,
               name: str,
               notes: str = None):
    token = login(uri, user, password)
    if len(token) == 0:
        return

    headers = get_authorized_headers(token)

    body = {
        "gateway_id": gateway_id,
        "model_id": model_id,
        "serial_number": serial_number,
        "name": name,
        "notes": notes
    }

    req = requests.post(GET_GW_V1_API.format(uri),
                        headers=headers,
                        params=body)
    response = req.json()

    logout(uri, token)
    print(json.dumps(response))
Esempio n. 4
0
def get_oses(uri: str, user: str, password: str, os_id: str = None):
    token = login(uri, user, password)
    if len(token) == 0:
        return

    headers = get_authorized_headers(token)

    endpoint = GET_MODELS_V1_API.format(uri)

    if os_id:
        endpoint += "/{}".format(os_id)

    get_models_api_request = requests.get(endpoint.format(uri),
                                          headers=headers)
    models_dict = get_models_api_request.json()

    if os_id:
        logout(uri, token)
        return print(json.dumps(__clean_os(models_dict.get("data")), indent=4))

    models = []
    for model in models_dict.get('rows', []):
        models.append(__clean_os(model))

    logout(uri, token)
    print(json.dumps(models, indent=4))
Esempio n. 5
0
def get_file_verions(uri: str, user: str, password: str, file_id: str, version_id: str = None):
    token = login(uri, user, password)
    if len(token) == 0:
        return

    headers = get_authorized_headers(token)

    endpoint = (GET_FILES_V1_API + "/{}/versions").format(uri, file_id)

    if version_id:
        endpoint = endpoint + "/{}".format(version_id)

    get_file_request = requests.get(endpoint.format(uri), headers=headers)
    files_dict = get_file_request.json()

    if version_id:
        logout(uri, token)
        return print(json.dumps(__clean_version(files_dict.get("data")), indent=4))

    files = []
    for file in files_dict.get("rows"):
        files.append(__clean_version(file))

    logout(uri, token)
    print(json.dumps(files, indent=4))
Esempio n. 6
0
def get_apps(uri: str, user: str, password: str, app_id: str = None):
    token = login(uri, user, password)
    if len(token) == 0:
        return

    headers = get_authorized_headers(token)

    endpoint = GET_APPS_V1_API.format(uri)

    if app_id:
        endpoint = endpoint + "/{}".format(app_id)

    get_app_request = requests.get(endpoint.format(uri), headers=headers)
    apps_dict = get_app_request.json()

    if app_id:
        logout(uri, token)
        return print(json.dumps(__clean_app(apps_dict.get("data")), indent=4))

    apps = []
    for app in apps_dict.get("rows"):
        apps.append(__clean_app(app))

    logout(uri, token)
    print(json.dumps(apps, indent=4))
Esempio n. 7
0
def get_devices(uri: str,
                user: str,
                password: str,
                device_id: str = None,
                hardware_id: str = None,
                tags: List_string = None):
    if tags is None:
        tags = []
    token = login(uri, user, password)
    if len(token) == 0:
        return

    headers = get_authorized_headers(token)

    endpoint = GET_GW_V1_API.format(uri)

    if device_id:
        endpoint += "/{}".format(device_id)

    params = {}

    if tags or hardware_id:
        filters = []
        if hardware_id:
            filters.append({
                "field": "gateway.hardware_id",
                "operator": "contains",
                "value": hardware_id
            })
        for tag in tags:
            filters.append({"field": "tags", "operator": "eq", "value": tag})
        if len(filters) > 0:
            params = {
                '$filter':
                '{"filters": ' + json.dumps(filters) + ', "logic": "and"}'
            }

    get_devices_api_request = requests.get(endpoint.format(uri),
                                           headers=headers,
                                           params=params)
    devices_dict = get_devices_api_request.json()

    if device_id:
        logout(uri, token)
        device = devices_dict.get("data")
        if device:
            return print(json.dumps(__clean_device(device)))
        else:
            return ""

    devices = []
    for device in devices_dict.get('rows', []):
        devices.append(__clean_device(device))

    logout(uri, token)
    return print(json.dumps(devices, indent=4))
def start_cancel_campaign(uri: str, user: str, password: str, api_url: str, campaign_id: str, cancel: bool = False):
    token = login(uri, user, password)
    if len(token) == 0:
        return

    headers = get_authorized_headers(token)
    operation = "cancel" if cancel else "start"

    endpoint = (api_url + "/{}/" + operation).format(uri, campaign_id)

    interact_oscamp_api_request = requests.post(endpoint.format(uri), headers=headers)
    response = interact_oscamp_api_request.json()

    logout(uri, token)
    print(json.dumps(response, indent=4))
Esempio n. 9
0
def replace_gw(uri: str, user: str, password: str, device_id: str,
               device_serial_number: str, new_gw_id: str):
    token = login(uri, user, password)
    if len(token) == 0:
        return

    url = GET_GW_V1_API + "/{}/replace-gateway".format(uri, device_id)
    headers = get_authorized_headers(token)
    body = {"serial_number": device_serial_number, "new:gateway_id": new_gw_id}

    req = requests.post(url=url, headers=headers, params=body)
    response = req.json()

    logout(uri, token)
    print(json.dumps(response))
Esempio n. 10
0
def decommission(uri: str, user: str, password: str, device_id: str,
                 device_serial_number: str):
    token = login(uri, user, password)
    if len(token) == 0:
        return

    url = GET_GW_V1_API + "/{}/decommission".format(uri, device_id)
    headers = get_authorized_headers(token)
    body = {"serial_number": device_serial_number}

    req = requests.post(url=url, headers=headers, params=body)
    response = req.json()

    logout(uri, token)
    print(json.dumps(response))
Esempio n. 11
0
def add_campaign(uri: str, user: str, password: str, api_url: str, body: dict):
    token = login(uri, user, password)
    if len(token) == 0:
        return

    headers = get_authorized_headers(token)
    endpoint = api_url.format(uri)

    add_oscamp_api_request = requests.post(endpoint.format(uri), headers=headers, json=body)
    res = add_oscamp_api_request.json()
    response = res
    if res.get("data"):
        response = res.get("data")
        response["success"] = res.get("success")

    logout(uri, token)
    print(json.dumps(response, indent=4))
Esempio n. 12
0
def get_geolocation(uri: str, user: str, password: str, device_id: str):
    token = login(uri, user, password)
    if len(token) == 0:
        return

    url = (GET_GW_V1_API +
           "/{}/geolocations?$orderby=created_at DESC&$top=1").format(
               uri, device_id)
    headers = get_authorized_headers(token)

    req = requests.get(url=url, headers=headers)
    response = req.json()

    if response.get('success') and response.get('rows') and len(
            response.get('rows')) > 0:
        ret = response.get('rows')[0]
        del ret['id']
        ret['success'] = True
    else:
        ret = response

    logout(uri, token)
    print(json.dumps(ret, indent=4))
Esempio n. 13
0
def ssh_connect(uri: str, user: str, password: str, gw_id: int):
    token = login(uri, user, password)
    if len(token) == 0:
        return

    headers = get_authorized_headers(token)

    endpoint = GET_GW_V1_API.format(uri) + "/{}/connect-ssh".format(gw_id)

    ssh_connect_api_request = requests.post(endpoint.format(uri),
                                            headers=headers)
    ssh_dict = ssh_connect_api_request.json()

    if ssh_dict.get('success') and ssh_dict.get('data') and ssh_dict.get(
            'data').get('port'):
        data = ssh_dict.get('data')
        host = data.get("host")
        port = data.get("port")
        timeout = data.get("timeout")
        user = data.get("user")

        connection_string = 'ssh -f -L {}:127.0.0.1:{} {}@{} /bin/sleep 600 && ssh [email protected] -o ' \
                            'IdentitiesOnly=yes -p {}'.format(port, port, user, host, port)
        ret = {
            'success': True,
            'connection_string': connection_string,
            'host': host,
            'port': port,
            'user': user,
            'timeout': timeout
        }
    else:
        ret = ssh_dict

    logout(uri, token)
    return print(json.dumps(ret, indent=4))
Esempio n. 14
0
def get_gateways(uri: str,
                 user: str,
                 password: str,
                 unassociated: bool,
                 hardware_id: str = None):
    token = login(uri, user, password)
    if len(token) == 0:
        return

    headers = get_authorized_headers(token)

    endpoint = GET_GW_V1_API.format(uri)

    if unassociated:
        endpoint += "/unassociated"

    params = {}

    if hardware_id:
        params = {
            '$filter':
            '{"filters":[{"field":"hardware_id","operator":"contains","value":"'
            + hardware_id + '"}],"logic":"and"} '
        }

    get_gateways_api_request = requests.get(endpoint.format(uri),
                                            headers=headers,
                                            params=params)
    gateways_dict = get_gateways_api_request.json()

    gateways = []
    for gateway in gateways_dict.get('rows', []):
        gateways.append(__clean_gateway(gateway))

    logout(uri, token)
    return print(json.dumps(gateways, indent=4))