Ejemplo n.º 1
0
def test_read_by_reference(mock_post):
    """
    Read Device by reference
    """

    mock_post.return_value.json.return_value = {'token': '12345qwert'}

    with patch('requests.get') as mock_call_get:
        mock_call_get.return_value.text = device_info()
        with patch('msa_sdk.msa_api.host_port') as mock_host_port:
            mock_host_port.return_value = ('api_hostname', '8080')
            device = Device()

        assert _is_valid_json(device.read('DEV_REF'))

        assert device.path == '/device/reference/DEV_REF'
        assert device.device_id == 21594
        assert device.name == "Linux self MSA"
        assert device.manufacturer_id == 14020601
        assert device.model_id == 14020601
        assert device.management_address == '127.0.0.1'
        assert device.management_port == '22'
        assert device.management_interface == ''
        assert device.login == 'root'
        assert device.password == '$ubiqube'
        assert device.password_admin == ''
        assert not device.log_enabled
        assert not device.mail_alerting
        assert not device.reporting
        assert device.use_nat
        assert device.snmp_community == ''
        mock_call_get.assert_called_once()
Ejemplo n.º 2
0
def device_fixture():
    """Device fixture."""
    with patch('requests.post') as mock_post:
        mock_post.return_value.json.return_value = {'token': '12345qwert'}

        with patch('msa_sdk.msa_api.host_port') as mock_host_port:
            mock_host_port.return_value = ('api_hostname', '8080')
            device = Device(10,
                            "MyDevice",
                            11,
                            13,
                            "ncroot",
                            "pswd",
                            "adm_pswd",
                            "mng_addres",
                            "Dexternal",
                            management_port="22")
        device.log_response = False
    return device
Ejemplo n.º 3
0
def test_read_by_invalid_id(mock_post):
    """
    Read Device by id
    """

    mock_post.return_value.json.return_value = {'token': '12345qwert'}

    fail_return = {"errorCode": 500, "message": "Not found"}

    return_message = {
        "wo_status": "FAIL",
        "wo_comment": "Read device",
        "wo_newparams": "Not found"
    }
    with patch('requests.get') as mock_call_get:
        mock_call_get.return_value = MagicMock(ok=False)
        mock_call_get.return_value.json.return_value = fail_return
        with patch('msa_sdk.msa_api.host_port') as mock_host_port:
            mock_host_port.return_value = ('api_hostname', '8080')
            device = Device(device_id=21594)
        mock_call_get.assert_called_once()
        assert device.content == json.dumps(return_message)
Ejemplo n.º 4
0
from msa_sdk.device import Device
from msa_sdk.variables import Variables
from msa_sdk.msa_api import MSA_API

dev_var = Variables()

dev_var.add('customer_id', var_type='Customer')
dev_var.add('managed_device_name')
dev_var.add('manufacturer_id', var_type='Integer')
dev_var.add('model_id', var_type='Integer')
dev_var.add('device_ip_address', var_type='IP Address', def_value='10.30.19.41')
dev_var.add('login')
dev_var.add('password', var_type='Password')
dev_var.add('password_admin', var_type='Password')

context = Variables.task_call(dev_var)

new_device = Device(context['customer_id'], context['managed_device_name'], context['manufacturer_id'],context['model_id'], context['login'], context['password'], context['password_admin'],context['device_ip_address'])
new_device.create()
context['device_id'] = new_device.device_id

print(new_device.process_content('ENDED', 'Task OK', context, True))
Ejemplo n.º 5
0
device_list = search.content
device_list = json.loads(device_list)

for me in device_list:
    if me['name'] == context['vm_name'] + '-1':
        master_id = me['id']
    elif me['name'] == context['vm_name'] + '-2':
        backup_id = me['id']
    elif me['name'] == context['vm_name'] + '-3':
        lb_id = me['id']

# (1) kubeadm init on master first
# stub here - actually k8s master nodes should be selected according to the user input
k8s_kubeadm_init_data = {
    "object_id": "123",
    "lb_ip": Device(device_id=str(lb_id)).management_address,
    "lb_port": "6443",
}
k8s_kubeadm_init_ms = {"k8s_kubeadm_init": {"123": k8s_kubeadm_init_data}}
Orchestration.update_asynchronous_task_details(*async_update_list,
                                               f'k8s-node-1: Provisioning')
'''
# open when master selection option available
try:
    order = Order(str(master_id))
    order.command_execute('CREATE', k8s_kubeadm_init_ms, timeout=400)
except Exception as e:
    ret = MSA_API.process_content('FAILED',
                                  f'ERROR: {str(e)}',
                                  context, True)
    print(ret)
def check_pod_name(text):
    # ip address like pattern for container name
    # ex. 10_10_10_10, 192_168_0_100
    return bool(re.search('(\d{1,3}_){3}\d{1,3}', text))


if __name__ == "__main__":
    ########################################################################################################
    #                                         MATH ALGORITHM                                               #
    ########################################################################################################

    info_message('Processing metrics...')
    cost_summary = {}

    for device in device_id_list:
        d_name = Device(device_id=device).name
        info_message(f'Processing metrics from: {d_name}...')
        cost_summary[device] = float()
        order = Order(device)
        order.command_objects_instances('logs')

        for ms_item in json.loads(order.content):
            if check_pod_name(ms_item):
                order.command_objects_instances_by_id('logs', ms_item)
                cost = json.loads(order.content)['logs'][ms_item]['min']
                cost_summary[device] += float(cost)

    target_device_id = min(cost_summary, key=cost_summary.get)
    target_device_name = Device(device_id=target_device_id).name

    info_message(f'Best offer from: {target_device_name}.', 5)
dev_var = Variables()
dev_var.add('subtenant', var_type='Customer')
dev_var.add('user_namespace', var_type='String')
dev_var.add('service_namespace', var_type='String')
dev_var.add('pkt_size', var_type='Integer')
dev_var.add('pkt_count', var_type='Integer')
dev_var.add('app_name', var_type='String')
context = Variables.task_call(dev_var)

if __name__ == "__main__":

    device_id_list = []
    device_ip_list = []

    search = Lookup()
    search.look_list_device_by_customer_ref(context['subtenant'])
    device_list = search.content
    device_list = json.loads(device_list)

    for device in device_list:
        device_id_list.append(device['id'])
        device_ip_list.append(
            Device(device_id=device['id']).management_address)

    context['device_ip_list'] = device_ip_list
    context['device_id_list'] = device_id_list

    ret = MSA_API.process_content(
        'ENDED', f'Workflow Instance Created. Data retrieved.', context, True)
    print(ret)
        return response.json()
      
if __name__ == "__main__":

    # Next block creates ME
    # manufacturer/model = 14020601 > Linux/Generic
    
    cust_id = context["UBIQUBEID"][4:]
    
    vm_id_list = []
    for vm in context['vms']:
        entity = Device(customer_id=cust_id,
                        name=list(vm.keys())[0],
                        manufacturer_id="14020601",
                        model_id="19112020",
                        login=context['vm_username'],
                        password=context['vm_secret'],
                        password_admin=context['vm_secret'],
                        management_address=list(vm.values())[0]['external'])

        entity.create()
        time.sleep(2)
        vm_id_list.append(str(entity.device_id))
        # assign deployment setting
        entity.profile_switch(context['dpl_linux'], context['dpl_linux'])
        time.sleep(2)
        entity.activate()

    Orchestration.update_asynchronous_task_details(*async_update_list,
                                                   f'MEs created - Activating...')
    
Ejemplo n.º 9
0
from msa_sdk.variables import Variables
from msa_sdk.msa_api import MSA_API
from msa_sdk.lookup import Lookup
from msa_sdk.device import Device

dev_var = Variables()
dev_var.add('subtenant', var_type='Customer')
dev_var.add('namespace', var_type='String')
dev_var.add('packet_size', var_type='Integer')
dev_var.add('packet_count', var_type='Integer')
dev_var.add('app_name', var_type='String')
context = Variables.task_call(dev_var)

device_id_list = []
device_ip_list = []

search = Lookup()
search.look_list_device_by_customer_ref(context['subtenant'])
device_list = search.content.decode()
device_list = json.loads(device_list)

for device in device_list:
    device_id_list.append(device['id'])
    device_ip_list.append(Device(device_id=device['id']).management_address)
    
context['device_ip_list'] = device_ip_list
context['device_id_list'] = device_id_list

ret = MSA_API.process_content('ENDED', f'workflow initialised {context["device_ip_list"]}', context, True)
print(ret)
Ejemplo n.º 10
0
import json
from msa_sdk.lookup import Lookup
from msa_sdk.device import Device
from msa_sdk.variables import Variables
from msa_sdk.msa_api import MSA_API

dev_var = Variables()
context = Variables.task_call()

# prepare device list, by id
cust_ref = context["UBIQUBEID"]
search = Lookup()
search.look_list_device_by_customer_ref(cust_ref)
device_list = search.content
device_list = json.loads(device_list)

me_list_deleted = []
try:
    for me in device_list:
        Device(device_id=me['id']).delete()
        me_list_deleted.append(me['name'])
    if len(me_list_deleted) == 0:
        me_list_deleted.append('...nothing to delete...')
    ret = MSA_API.process_content(
        'ENDED', f'Next devices were deleted: {me_list_deleted}', context,
        True)
    print(ret)
except Exception as e:
    ret = MSA_API.process_content('FAILED', f'ERROR: {str(e)}', context, True)
    print(ret)
Ejemplo n.º 11
0
import collections
import inspect
import json
from inspect import signature

from msa_sdk import util
from msa_sdk.conf_profile import ConfProfile
from msa_sdk.customer import Customer
from msa_sdk.device import Device
from msa_sdk.lookup import Lookup
from msa_sdk.orchestration import Orchestration
from msa_sdk.order import Order
from msa_sdk.repository import Repository
from msa_sdk.variables import Variables

device = Device()
lookup = Lookup()
order = Order(1)
orchestration = Orchestration(1)
repository = Repository()
customer = Customer()
conf_profile = ConfProfile()
variable = Variables()

output_doc = collections.defaultdict(dict)  # type: dict


def get_members(cls_name, obj):
    """Extract members."""
    output_doc[cls_name] = {"methods": list()}
    for i in inspect.getmembers(obj, predicate=inspect.ismethod):
Ejemplo n.º 12
0
    Orchestration.update_asynchronous_task_details(*async_update_list, text)
    time.sleep(timeout)


def get_pod_ns(pod_name):
    order.command_objects_instances_by_id('k8_pods_list', pod_name)
    response = json.loads(order.content)
    return response['k8_pods_list'][pod_name]['namespace']


if __name__ == "__main__":

    # remove metrics pods
    info_message('Removing service pods...')
    for device in device_id_list:
        d_name = Device(device_id=device).name
        info_message(f'Removing service pods: {d_name}...')
        order = Order(device)
        # syncronize first
        k8_pods_list = {'k8_pods_list': {'': {}}}
        order.command_execute('IMPORT', k8_pods_list)
        order.command_objects_instances('k8_pods_list')
        pods_list = json.loads(order.content)
        for pod in pods_list:
            if get_pod_ns(pod) == context['service_namespace']:
                info_message(f'Removing service pods: {d_name} {pod}...')
                order.command_execute('DELETE', {'k8_pods_list': pod})

    # remove all applications from user_namespace
    info_message(f'Removing user apps: {target_device_name}...')
    order = Order(target_device_id)
Ejemplo n.º 13
0
#Variables to finish the task properlly
fail_comment = str()
success_comment = str()
fail_string = f'{{"wo_status": "FAIL", "wo_comment": "{fail_comment}"}}'
success_string = f'{{"wo_status": "ENDED", "wo_comment": "{success_comment}"}}'

#Create Orchestration object to update GUI dynamically
Orchestration = Orchestration(context['UBIQUBEID'])
async_update_list = (context['PROCESSINSTANCEID'], context['TASKID'], context['EXECNUMBER'])


#IPAM order object
IpamOrderObject = Order(ipam_device_id)

#Create CE device object
CeDeviceObject = Device(device_id = ce_device_id,
                        device_external = ce_device_external_reference)
pretty_formatted_bar = list('------------------------')
Orchestration.update_asynchronous_task_details(*async_update_list, 'Provisioning of CE device... [{}]'.format(''.join(pretty_formatted_bar)))

#If the device mgmt interface is REST-based, add required configuration variables
if 'rest' in ce_local_context['interface'].lower():
    for variable, value in ce_local_context['msa_specific']['rest_headers'].items():
        CeDeviceObject.create_configuration_variable(name = variable, value = value)

#Provision device
CeDeviceObject.initial_provisioning()

#Wait until provisioning is done
while CeDeviceObject.provision_status()['status'] != 'OK':
    pretty_formatted_bar.insert(0,'*')
    Orchestration.update_asynchronous_task_details(*async_update_list, 'Provisioning of CE device... [{}]'.format(''.join(pretty_formatted_bar)))
Ejemplo n.º 14
0
                                      context, True)
        print(ret)

    if 'token' in data_5['k8s_create_service_account'].keys():
        k8s_token = data_5['k8s_create_service_account']['token']['token']
    else:
        ret = MSA_API.process_content('WARNING',
                                      f'Token not found: {k8s_token}',
                                      context, True)
        print(ret)
        exit()

    me_id = context['proxy_host'][3:]
    # same ip address as haproxy ip address
    try:
        k8s_api_ip = Device(device_id=me_id).management_address
        cust_id = context["UBIQUBEID"][4:]

        entity = Device(customer_id=cust_id,
                        name="k8s-api",
                        manufacturer_id="20060101",
                        model_id="20060101",
                        login="******",
                        password="******",
                        password_admin="admin",
                        management_address=k8s_api_ip)

        entity.create()
    except Exception as e:
        ret = MSA_API.process_content('WARNING',
                                      f'Can\'t call device {me_id} : {str(e)}',
                                                                   ' OK. Device is {}'.format(site, site_device_list[0]))
time.sleep(3)

# Retrieve CE details and create MSA ME
Orchestration.update_asynchronous_task_details(*async_update_list, 'Create new MSA ME for CE device {}... '.format(site_device_list[0]))

ce_device = IpamOrderObject.command_objects_instances_by_id(ms_ipam_device, 
                                                            site_device_list[0])[ms_ipam_device][site_device_list[0]]
ce_device_local_context = json.loads(json.dumps(xmltodict.parse(ce_device['local_context_data'])))['local_context_data']

CeDeviceObject = Device(customer_id = re.match('^\D+?(\d+?)$',context['UBIQUBEID']).group(1), 
                        name = ce_device['object_id'], 
                        device_external = re.sub(r'[\.\[\]-]', "_", ce_device['object_id']),
                        manufacturer_id = ce_device_local_context['msa_specific']['manufacture_id'],
                        password_admin = ce_device_local_context['enable_password'],
                        model_id = ce_device_local_context['msa_specific']['model_id'],
                        login = ce_device_local_context['username'], 
                        password = ce_device_local_context['password'], 
                        management_address = ce_device['primary_ip'].split('/')[0],
                        management_port = ce_device_local_context['port']
                        )

CeDeviceObject.create()
Orchestration.update_asynchronous_task_details(*async_update_list, 'Create new MSA ME for CE device {}... OK'.format(site_device_list[0]))
time.sleep(3)

#Put useful variables to context
context['ce_device_details'] = dict()
context['ce_device_details']['local_context']       = ce_device_local_context
context['ce_device_details']['device_id']           = CeDeviceObject.device_id
context['ce_device_details']['ce_device_name']		= ce_device['object_id']
Ejemplo n.º 16
0
            Orchestration.update_asynchronous_task_details(
                *async_update_list, f'Removing from Azure {ip_name}...')
            time.sleep(1)
            r = one.del_status(one.delete_ip, ip_name, timeout=400)
            Orchestration.update_asynchronous_task_details(
                *async_update_list, f'{r}...')
            time.sleep(10)

    except ConnectionError:
        ret = MSA_API.process_content('WARNING',
                                      f'Connection Error - Check Internet',
                                      context, True)
        print(ret)
        exit()
    except HTTPError:
        ret = MSA_API.process_content('WARNING', f'HTTP Error - Check API',
                                      context, True)
        print(ret)
        exit()

    # (2) remove from MSA
    for me in msa_rm_vm:
        time.sleep(2)
        Orchestration.update_asynchronous_task_details(
            *async_update_list, f'Removing from MSA {me}...')
        Device(device_id=me).delete()

    ret = MSA_API.process_content('ENDED', 'Scale-in completed.', context,
                                  True)
    print(ret)
Ejemplo n.º 17
0
counter = 0
while ce_device_id is None or counter < len(devices):
    if devices[counter]['name'] == ce_device_name:
        ce_device_id = devices[counter]['id']
    counter += 1

Orchestration.update_asynchronous_task_details(
    *async_update_list,
    'Retrieve information about CE device on the site... OK')
time.sleep(3)

Orchestration.update_asynchronous_task_details(*async_update_list,
                                               'Remove CE managed entity... ')
#Create CE device object
CeDeviceObject = Device(device_id=ce_device_id)
CeDeviceObject.delete()
Orchestration.update_asynchronous_task_details(
    *async_update_list, 'Remove CE managed entity... OK')
time.sleep(3)

#Update IPAM
Orchestration.update_asynchronous_task_details(*async_update_list,
                                               'Update IPAM... ')
ms_dict = {
    ms_ipam_device: {
        ce_device_name: {
            'object_id': ce_device_name,
            'status': context['status'],
            'site': device_object['site'],
            'role': device_object['role'],
Ejemplo n.º 18
0
 def __init__(self, device_id):
     """Initialize."""
     Device.__init__(self, device_id=device_id)
     self.api_path = '/ordercommand'
     self.read()
from msa_sdk.device import Device
from msa_sdk.variables import Variables

context = Variables.task_call()

new_device = Device(device_id=context['device_id'])
new_device.read()
new_device.ping(new_device.management_address)

print(
    new_device.process_content(
        'ENDED',
        'Pinging IP: ' + new_device.management_address + ' successfully',
        context, True))
success_comment = str()
fail_string = f'{{"wo_status": "FAIL", "wo_comment": "{fail_comment}"}}'
success_string = f'{{"wo_status": "ENDED", "wo_comment": "{success_comment}"}}'

#Create Orchestration object to update GUI dynamically
Orchestration = Orchestration(context['UBIQUBEID'])
async_update_list = (context['PROCESSINSTANCEID'], context['TASKID'],
                     context['EXECNUMBER'])

#Create microwave station object
StationDeviceObject = Device(
    customer_id=re.match('^\D+?(\d+?)$', context['UBIQUBEID']).group(1),
    name='{}[{}]'.format(context['hostname'], context['ip_address']),
    device_external=re.sub(r'[\.\[\]-]', "_", context['hostname']),
    manufacturer_id=nec_ipasolink_manufacture_id,
    password_admin=nec_ipasolink_default_password,
    model_id=nec_ipasolink_model_id,
    login=nec_ipasolink_default_username,
    password=nec_ipasolink_default_password,
    management_address=context['ip_address'],
    management_port='22')

StationDeviceObject.create()

pretty_formatted_bar = list('-' * 12)
Orchestration.update_asynchronous_task_details(
    *async_update_list,
    'Creating managed entity for the station... [{}]'.format(
        ''.join(pretty_formatted_bar)))

#Provision device
from msa_sdk.variables import Variables
from msa_sdk.msa_api import MSA_API
from msa_sdk.device import Device

dev_var = Variables()
dev_var.add('id', var_type='integer')
dev_var.add('site_left', var_type='Device')
dev_var.add('site_right', var_type='Device')
dev_var.add('leftsubnet', var_type='string')
dev_var.add('rightsubnet', var_type='string')
dev_var.add('secret', var_type='string')
context = Variables.task_call(dev_var)

left_device_id = context['site_left'][-3:]
right_device_id = context['site_right'][-3:]

left_device = Device(device_id=left_device_id)
right_device = Device(device_id=right_device_id)

context['left_device_id'] = left_device_id
context['right_device_id'] = right_device_id
context['left_device_ip'] = left_device.management_address
context['right_device_ip'] = right_device.management_address

ret = MSA_API.process_content(
    'ENDED', f'IPsec data retrieved \
                               site-A: {context}, \
                               site-Z: {context}', context, True)

print(ret)
Ejemplo n.º 22
0
from msa_sdk.device import Device
from msa_sdk.variables import Variables

context = Variables.task_call()

new_device = Device(device_id=context['device_id'])
new_device.initial_provisioning()

print(
    new_device.process_content(
        'ENDED',
        'Device: ' + str(new_device.device_id) + ' provisioned successfully',
        context, True))
Ejemplo n.º 23
0
        one = MSAConnect(context['msa_fqdn'], context['msa_user'],
                         context['msa_pass'])
        if isinstance(one.token, tuple):
            ret = MSA_API.process_content(
                'WARNING', f'Can\'t get MSA token {one.token[1]}', context,
                True)
            print(ret)
            exit()
    except HTTPError as http_err:
        ret = MSA_API.process_content('WARNING',
                                      f'HTTP error occured: {http_err}',
                                      context, True)
        print(ret)
        exit()

    entity = Device(device_id=context['k8s_device'][3:])

    try:
        one.set_params(str(entity.device_id), 'KUBE_TOKEN',
                       context["k8s_token"])
        one.set_params(str(entity.device_id), 'KUBE_AUTH_METHOD', 'KUBERNETES')
        one.set_params(str(entity.device_id), 'KUBE_HTTP_PROTOCOL', 'https')
        one.set_params(str(entity.device_id), 'KUBE_PORT', context["k8s_port"])
    except Exception as e:
        ret = MSA_API.process_content(
            'WARNING',
            f'Can\'t set custom params {context["k8s_device"]} : {str(e)}',
            context, True)
        print(ret)
        exit()