def __init__(self, profile_id=None, name=None, externalReference=None, comment=None, vendor_id=None, model_id=None, microserviceUris=[], templateUris=[], attachedManagedEntities=[], customer_id=None): """ Initialize. Parameters ---------- profile_id: Integer Profile id name: String Profile Name externalReference: String Configurable id for Profile comment: String Configurable id for Profile vendor_id: Integer Manufacture ID model_id: Integer Model ID login: String Login microserviceUris: List List of Microservices you want to attach templateUris: List List of Templates you want to attach attachedManagedEntities: List List of Managed Entities you want to attach customer_id: Integer Customer id which you want to attach a created configuration profile Returns ------- None """ MSA_API.__init__(self) self.api_path = "/conf-profile" self.profile_id = profile_id self.name = name self.externalReference = externalReference self.comment = comment self.vendor_id = vendor_id self.model_id = model_id self.microserviceUris = microserviceUris self.templateUris = templateUris self.attachedManagedEntities = attachedManagedEntities self.customer_id = customer_id if profile_id: self.read()
def __init__(self): """ Initialize. Parameters ---------- None Returns ------- None """ MSA_API.__init__(self) self.api_path = "/conf-backup"
def default_hostname_fixture(): """ Fixure to use default hostname and port for API endpoint """ with patch('requests.post') as mock_post: mock_post.return_value.json.return_value = {'token': '12345qwert'} api = MSA_API() return api
def environment_variable_fixture(): """ Fixture to test environment variables for API endpoint """ os.environ['MSA_SDK_API_HOSTNAME'] = "environ_hostname" os.environ['MSA_SDK_API_PORT'] = "2222" with patch('requests.post') as mock_post: mock_post.return_value.json.return_value = {'token': '12345qwert'} api = MSA_API() return api
def api_fixture(tmpdir): """ API Fixtures """ f_name = tmpdir.mkdir('test').join('vars_ctx_file') api_info = 'OTHER_VALUE-1=foobar-1\n' api_info += 'UBI_WILDFLY_JNDI_ADDRESS=test_hostname\n' api_info += 'UBI_WILDFLY_JNDI_PORT=1111\n' api_info += 'OTHER_VALUE-2=foobar-2\n' api_info += 'OTHER_VALUE-4=foobar-3\n' api_info += 'OTHER_VALUE-5=foobar-4\n' api_info += 'OTHER_VALUE-6=foobar-5\n' with open(f_name, 'w+') as t_file: t_file.write(api_info) with patch('requests.post') as mock_post: mock_post.return_value.json.return_value = {'token': '12345qwert'} with patch('msa_sdk.constants.VARS_CTX_FILE', f_name): api = MSA_API() return api
from msa_sdk.variables import Variables from msa_sdk.msa_api import MSA_API dev_var = Variables() dev_var.add('id', var_type='Integer') dev_var.add('src_ip', var_type='String') dev_var.add('dst_port', var_type='Integer') # port code from # https://github.com/openmsa/Workflows/blob/master/Tutorials/php/Simple_Firewall/Add_filter_Rule_add_rule.php ret = MSA_API.process_content('ENDED', 'port/IP blocked', context, True) print(ret)
def get_status_loop(self, rs_id, api_ver, stop=120): counter = 0 while counter < stop: status = self.get_resource_status(rs_id, api_ver) if status == 'Succeeded': break else: time.sleep(2) counter = counter + 2 if __name__ == "__main__": if int(context["scale_lvl"]) <= 0: ret = MSA_API.process_content('ENDED', f'Scale-in completed. Skipping.', context, True) print(ret) exit() try: one = AZConnection(client_id=context['client_id'], client_secret=context['client_secret'], subscription_id=context['subscription_id'], tenant_id=context['tenant_id'], resource_group=context['resource_group'], location=context['location']) vm_id_list = [] except ConnectionError:
objects_list = IpamOrderObject.command_objects_instances(ms_ipam_vrf) #Collect cusromer sites object_id for future use vrf_list = list() for vrf in objects_list: vrf_object = IpamOrderObject.command_objects_instances_by_id(ms_ipam_vrf, vrf)[ms_ipam_vrf][vrf] try: if vrf_object['tenant'] == customer_name: vrf_list.append(vrf) except: pass Orchestration.update_asynchronous_task_details(*async_update_list, 'Retrieving customer details... OK') success_comment = 'Customer {} exists in IPAM system. It has {} sites and {} devices'.format(customer_name, len(sites_list), len(devices_list)) context['customer_details'] = { "name": customer_name, "object_id": tenant_object, "customer_id": customer_id, "sites_list": sites_list, "devices_list": devices_list } if len(vrf_list) > 0: context['customer_details']['vrf'] = vrf_list[0].replace(' ', '_') #Finish the task correctlly print(MSA_API.process_content('ENDED', success_comment , context, True))
from msa_sdk.lookup import Lookup from msa_sdk.device import Device from msa_sdk.order import Order from msa_sdk.orchestration import Orchestration from msa_sdk.variables import Variables from msa_sdk.msa_api import MSA_API dev_var = Variables() context = Variables.task_call() Orchestration = Orchestration(context['UBIQUBEID']) async_update_list = (context['PROCESSINSTANCEID'], context['TASKID'], context['EXECNUMBER']) if int(context["scale_lvl"]) <= 0: ret = MSA_API.process_content('ENDED', f'Scale-in completed. Skipping.', context, True) print(ret) exit() # (1) prepare data for k8s_pre_requirements microservice k8s_pre_requirements_data = {"object_id": "123"} k8s_pre_requirements_ms = { "k8s_pre_requirements": { "123": k8s_pre_requirements_data } } device_list = context['vm_id_list_new'] # deploy required packages for me in device_list:
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)
from msa_sdk.variables import Variables from msa_sdk.msa_api import MSA_API from msa_sdk.order import Order dev_var = Variables() context = Variables.task_call() # (4) cryptomap try: order = Order(context["left_device_id"]) order.command_execute("CREATE", context["sdwan_ipsec_conf_crypto_map_left"]) except Exception as e: ret = MSA_API.process_content('FAILED', f'ERROR: {str(e)}', context, True) print(ret) try: order = Order(context["right_device_id"]) order.command_execute('CREATE', context["sdwan_ipsec_conf_crypto_map_right"]) except Exception as e: ret = MSA_API.process_content('FAILED', f'ERROR: {str(e)}', context, True) print(ret) ret = MSA_API.process_content('ENDED', f'IPsec Crypto_map Created.', context, True) print(ret)
Also, any new variables should be stored in context which are used across Service Instance The variables stored in context can be used across all the Tasks and Processes of a particular Service Update context array [add/update/delete variables] as per requirement ENTER YOUR CODE HERE ''' context = Variables.task_call(dev_var) context['var_name2'] = int(context['var_name2']) + 1 ''' Format of the Task response : JSON format : {"wo_status":"status","wo_comment":"comment","wo_newparams":{json_body}} wo_status : ENDED [Green color] or FAILED [Red color] or WARNING [Orange color] -> While the Task is Running [means no response returned yet], task status is RUNNING [Blue color] -> When status is returned as FAILED, the Orchestration Engine stops the Process Execution from this Task wo_comment : Appropriate Comment to display as per the success/failure of the Task wo_newparams : json_body parameters returned from this Task Function process_content() takes care of Creating a Json response from inputs This function definiton can be found at : http://[YOUR_MSA_URL]/msa_sdk/msa_api.html#msa_sdk.msa_api.MSA_API.process_content NOTE : For 'wo_newparams', always pass "context" [whether wo_status is ENDED/FAILED/WARNING to preserve it across Service Instance] -> Last argument "true" mentions whether the json_response to be Logged in the logfile : /opt/jboss/latest/logs/process.log -> If not passed, it's "false" The response "ret" should be echoed from the Task "print(ret)" which is read by Orchestration Engine In case of FAILURE/WARNING, the Task can be Terminated by calling "exit" as per Logic ''' ret = MSA_API.process_content('ENDED', 'Task OK', context, True) print(ret)
# (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": list(context['vms'][2].values())[0]['internal'], "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') 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) Orchestration.update_asynchronous_task_details( *async_update_list, f'Master Initiated: k8s-node-1') # (2) get params from master first # k8s_get_token k8s_get_token = {'k8s_get_token': {'': {'object_id': 'kubeamd'}}} try: order = Order(str(master_id)) order.command_execute('IMPORT', k8s_get_token) data_1 = json.loads(order.content) data_1 = json.loads(data_1['message']) except Exception as e: ret = MSA_API.process_content('FAILED', f'ERROR: {str(e)}', context, True)
from msa_sdk.variables import Variables from msa_sdk.msa_api import MSA_API dev_var = Variables() context = Variables.task_call() ret = MSA_API.process_content('ENDED', f'SEE YOU', context, True) print(ret)
"object_id": "123", "k8s_m_1_ip": list(context['vms'][0].values())[0]['internal'], "k8s_m_2_ip": list(context['vms'][1].values())[0]['internal'], } k8s_ha_proxy_ms = {"k8s_ha_proxy": {"123": k8s_ha_proxy_data}} # (2) get short ME id me_id = context['proxy_host'][3:] # (3) install package on certain me Orchestration.update_asynchronous_task_details( *async_update_list, f'haproxy installation: {Device(device_id=me_id).name}') try: order = Order(me_id) order.command_execute('CREATE', k8s_package_management_ms, timeout=300) except Exception as e: ret = MSA_API.process_content('FAILED', f'ERROR: {str(e)}', context, True) print(ret) time.sleep(10) # (4) configure and start proxy try: order = Order(me_id) order.command_execute('CREATE', k8s_ha_proxy_ms) except Exception as e: ret = MSA_API.process_content('FAILED', f'ERROR: {str(e)}', context, True) print(ret) ret = MSA_API.process_content('ENDED', f'Loadbalancer prepared.', context, True) print(ret)
time.sleep(3) #Flag to know when microwave station becoms available is_available = False Orchestration.update_asynchronous_task_details( *async_update_list, 'Check microwave station availability. IP is {}... '.format( context['ip_address'])) #String to check availability ping_string = "ping -c 1 -W 1 {} | grep -oP '(0|100)%\s+?packet\s+?loss'".format( context['ip_address']) while not is_available: if os.popen(ping_string).read().startswith('0%'): is_available = True else: time.sleep(10) Orchestration.update_asynchronous_task_details( *async_update_list, 'Check microwave station availability. IP is {}... OK'.format( context['ip_address'])) #Finish the task correctlly result = MSA_API.process_content( 'ENDED', 'Station with IP {} is available now.'.format(context['ip_address']), context, True) print(result)
from msa_sdk.variables import Variables from msa_sdk.msa_api import MSA_API dev_var = Variables() context = Variables.task_call() if __name__ == "__main__": ret = MSA_API.process_content('ENDED', 'WF INSTANCE REMOVED', context, True) print(ret)
from msa_sdk.variables import Variables from msa_sdk.msa_api import MSA_API from msa_sdk.order import Order dev_var = Variables() dev_var.add('id', var_type='Integer') dev_var.add('device_id', var_type='Device') context = Variables.task_call(dev_var) short_device_id = context['device_id'][-3:] context['short_device_id'] = short_device_id ms_sdwan_ipsec_start = {context['id']: {"object_id": context['id']}} context['ms_sdwan_ipsec_start'] = ms_sdwan_ipsec_start try: order = Order(context['short_device_id']) order.command_execute('CREATE', {"sdwan_ipsec_start": ms_sdwan_ipsec_start}) except Exception as e: ret = MSA_API.process_content('FAILED', f'ERROR: {str(e)}', context, True) ret = MSA_API.process_content('ENDED', f'Take IPsec control.', context, True) print(ret)
from msa_sdk.variables import Variables from msa_sdk.msa_api import MSA_API from msa_sdk.order import Order dev_var = Variables() context = Variables.task_call() # (1) loopback try: '''check if loopback already exists''' # order = Order(left_device_id) # order.command_execute('IMPORT', {"sdwan_loopback": context["id"]}) order = Order(context["left_device_id"]) order.command_execute("CREATE", context["sdwan_loopback_left"]) except Exception as e: ret = MSA_API.process_content('FAILED', f'ERROR: {str(e)}', context, True) print(ret) try: order = Order(context["right_device_id"]) order.command_execute('CREATE', context["sdwan_loopback_right"]) except Exception as e: ret = MSA_API.process_content('FAILED', f'ERROR: {str(e)}', context, True) print(ret) ret = MSA_API.process_content('ENDED', f'Loopbacks Created.', context, True) print(ret)
import random import time from msa_sdk.variables import Variables from msa_sdk.msa_api import MSA_API from msa_sdk.order import Order context = Variables.task_call() device_id_list = context["device_id_list"] ms_vars_dict_list = context["ms_vars_dict_list"] def rand_int(): return ''.join(random.choice('0123456789') for i in range(8)) for device in device_id_list.values(): for ms_var_dict in ms_vars_dict_list.values(): Order(device).command_execute('CREATE', {'k8s_pods': { rand_int(): ms_var_dict }}) time.sleep(3 * int(context['packet_count'])) ret = MSA_API.process_content('ENDED', f'PODs have been created', context, True) print(ret)
"threat_extraction": "true" } gateway = {"Gateway": {object_id: micro_service_vars_array}} # call the CREATE for simple_firewall MS for each device order = Order(devicelongid) order.command_execute('CREATE', gateway) # convert dict object into json content = json.loads(order.content) # check if the response is OK if order.response.ok: # ret = MSA_API.process_content('ENDED', # f'STATUS: {content["status"]}, \ # MESSAGE: {content["message"]}', # context, True) ret = MSA_API.process_content( 'ENDED', f'STATUS: {content["status"]}, \ MESSAGE: Gateway {object_id}', context, True) else: ret = MSA_API.process_content( 'FAILED', f'Gateway Creation failed \ - {order.content}', context, True) context[ 'service_id_display'] = f'{context["SERVICEINSTANCEID"]} - Firewall {context["device"]}' print(ret)
# connect to Azure and retrieve current rules try: one = AZUpdateSecGr(client_id=context['client_id'], client_secret=context['client_secret'], subscription_id=context['subscription_id'], tenant_id=context['tenant_id'], resource_group=context['resource_group'], location=context['location']) # get current SG rules r1 = one.sec_group_show(context['new_sec_gr_name'], context['new_sec_rule_name']) 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() # prepare data retrieved from user inputs if context['sec_rule_action'] == 'add': if "sourceAddressPrefix" in r1["properties"].keys(): r1["properties"]["sourceAddressPrefixes"] = [] # move existed rule to sourceAddressPrefixES: r1["properties"]["sourceAddressPrefixes"].append(
context = Variables.task_call() ms_vars_dict_list = [] device_ip_list = context["device_ip_list"] ''' Generate data in format. ms_vars_dict = {"namespace": , "pod_name": , "container_name": , "remote_ip": } ''' for ip in device_ip_list.values(): ms_vars_dict = { "namespace": context['namespace'], "pod_name": ip.replace(".", "-"), "container_name": ip.replace(".", "-"), "remote_ip": ip, "packet_size": context['packet_count'], "packet_count": context['packet_size'] } ms_vars_dict_list.append(ms_vars_dict) context['ms_vars_dict_list'] = ms_vars_dict_list ret = MSA_API.process_content('ENDED', f'DATA {context["ms_vars_dict_list"]}', context, True) print(ret)
''' example curl http://49.135.32.214:9999/restconf/wdm2/add/1/1 -X POST Visit http://[YOUR_MSA_URL]/msa_sdk/ to see what you can import. ''' from msa_sdk.variables import Variables from msa_sdk.msa_api import MSA_API import requests dev_var = Variables() context = Variables.task_call(dev_var) #x = requests.post('https://w3schools.com/python/demopage.htm') x = requests.post('http://49.135.32.214:9999/restconf/wdm2/add/1/1') ok = "OK" in x.text if ok: ret = MSA_API.process_content('ENDED', f'{x.text}', context, True) else: ret = MSA_API.process_content('FAILED', f'{x.text}', context, True) print(ret)
nsd_name = uuid_gen if 'nsd_name' in context: name = context.get('nsd_name') if name: nsd_name = name + '_' + uuid_gen TOSCA_meta = 'TOSCA.meta' filename = '/opt/fmc_repository/Datafiles/NFV/NSD/' + nsd_name + '/TOSCA-Metadata/' + TOSCA_meta #nsd_sol0001_schema content TOSCA_meta_contents = """ TOSCA-Meta-File-Version: 1.0 CSAR-Version: 1.1 Created-By: OASIS TOSCA TC Entry-Definitions: Definitions/""" + nsd_name + '.yaml' #create file in http server directory. if not os.path.exists(os.path.dirname(filename)): try: os.makedirs(os.path.dirname(filename)) except OSError as exc: # Guard against race condition if exc.errno != errno.EEXIST: raise with open(filename, "w") as file: file.write(TOSCA_meta_contents) file.close() MSA_API.task_success('NSD TOSCA Sol001 meta was created successfully.', context, True)
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) # syncronize first k8_pods_list = {'k8_pods_list': {'': {}}} order.command_execute('IMPORT', k8_pods_list) order.command_objects_instances('k8_pods_list') ms_instances = json.loads(order.content) pods_list = json.loads(order.content) for pod in pods_list: if get_pod_ns(pod) == context['user_namespace']: info_message( f'Removing service pods: {target_device_name} {pod}...') order.command_execute('DELETE', {'k8_pods_list': pod}) ret = MSA_API.process_content('ENDED', f'Service PODs removed, User APPs removed.', context, True) print(ret)
Orchestration.update_asynchronous_task_details( *async_update_list, f'Retrieving security groups...') sec_group_id = one.get_sec_group(context['subscription_id'], context['resource_group']) Orchestration.update_asynchronous_task_details( *async_update_list, f'Retrieving subnets...') subnet_id = one.get_subnet(context['subscription_id'], context['resource_group']) 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() # get last vm index try: temp_list = [] for vm in vms: temp_list.append(list(vm.keys())[0]) temp_list.reverse()
device_list = search.content device_list = json.loads(device_list) worker_id_list = [] master = context['vm_name'] + '-1' backup = context['vm_name'] + '-2' # add all vms that are not k8s masters to the list for me in device_list: if me['name'] != (master and backup): worker_id_list.append(me['id']) # (4) kubeadm loop on workers Orchestration.update_asynchronous_task_details(*async_update_list, f'Worker nodes processing...') for me in worker_id_list: try: order = Order(str(me)) order.command_execute('CREATE', context['k8s_join_worker'], timeout=300) except Exception as e: ret = MSA_API.process_content('FAILED', f'ERROR: {str(e)}', context, True) print(ret) Orchestration.update_asynchronous_task_details(*async_update_list, f'Worker Initiated: {me}') ret = MSA_API.process_content('ENDED', 'K8S cluster ready - Enjoy!', context, True) print(ret)
client_secret=context['client_secret'], subscription_id=context['subscription_id'], tenant_id=context['tenant_id'], resource_group=context['resource_group'], location=context['location']) subnet = one.subnet_create(virt_net_name=context['virt_net_name'], subnet_name=context['subnet_name'], ip_prefix=context['subnet_ip_prefix']) snet_id = subnet['id'] one.get_status_loop(snet_id, '2020-05-01') status = one.get_resource_status(snet_id, '2020-05-01') 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() context['snet_id'] = snet_id ret = MSA_API.process_content('ENDED', f'{context["virt_net_name"]} = {status}', context, True) print(ret)
def __init__(self): """Initialize.""" MSA_API.__init__(self) self.api_path = "/customer"