Yang Xu | c52ed6e | 2019-04-29 00:20:52 -0400 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | |
| 3 | import sys |
| 4 | import logging |
| 5 | import requests |
| 6 | import json |
| 7 | from vcpecommon import * |
| 8 | |
| 9 | |
| 10 | class SdcUtils: |
| 11 | def __init__(self, vcpecommon): |
| 12 | """ |
| 13 | :param vcpecommon: |
| 14 | """ |
| 15 | self.logger = logging.getLogger(__name__) |
| 16 | self.logger.setLevel(logging.DEBUG) |
| 17 | self.vcpecommon = vcpecommon |
| 18 | |
Yang Xu | 0e319ef | 2019-04-30 14:28:07 -0400 | [diff] [blame] | 19 | def download_vcpe_service_template(self): |
Yang Xu | c52ed6e | 2019-04-29 00:20:52 -0400 | [diff] [blame] | 20 | """ |
| 21 | :return: |
| 22 | """ |
| 23 | |
| 24 | url = self.vcpecommon.sdc_service_list_url |
| 25 | self.logger.info(url) |
Yang Xu | 0e319ef | 2019-04-30 14:28:07 -0400 | [diff] [blame] | 26 | r = requests.get(url, headers=self.vcpecommon.sdc_be_request_headers, auth=self.vcpecommon.sdc_be_request_userpass) |
Yang Xu | c52ed6e | 2019-04-29 00:20:52 -0400 | [diff] [blame] | 27 | self.logger.debug(r) |
| 28 | data = r.json() |
| 29 | |
| 30 | self.logger.debug('---------------------------------------------------------------') |
| 31 | self.logger.debug('------- Creation request submitted to SDC, got response --------') |
| 32 | self.logger.debug('response code = %s' % r.status_code ) |
| 33 | self.logger.debug(json.dumps(data, indent=4, sort_keys=True)) |
| 34 | self.logger.debug('---------------------------------------------------------------') |
| 35 | |
| 36 | for service in data: |
Yang Xu | 0e319ef | 2019-04-30 14:28:07 -0400 | [diff] [blame] | 37 | if service['name'].startswith('demoVCPE') or service['name'].startswith('vCPEResCust'): |
Yang Xu | c52ed6e | 2019-04-29 00:20:52 -0400 | [diff] [blame] | 38 | self.logger.debug('service name = %s, url = %s' % (service['name'], service['toscaModelURL'])) |
Yang Xu | 0e319ef | 2019-04-30 14:28:07 -0400 | [diff] [blame] | 39 | self.download_file(self.vcpecommon.sdc_be_url_prefix + service['toscaModelURL']) |
Yang Xu | c52ed6e | 2019-04-29 00:20:52 -0400 | [diff] [blame] | 40 | |
| 41 | def get_filename_from_cd(self, cd): |
| 42 | """ |
| 43 | cd: in the format of Content-Disposition: attachment; filename="service-Demovcpeinfra-csar.csar" |
| 44 | """ |
| 45 | fname = re.findall('filename="(.+)"', cd) |
| 46 | self.logger.debug('fname = %s' % fname[0]) |
| 47 | return fname[0] |
| 48 | |
| 49 | def download_file(self, url): |
| 50 | self.logger.info(url) |
Yang Xu | 0e319ef | 2019-04-30 14:28:07 -0400 | [diff] [blame] | 51 | response = requests.get(url, headers=self.vcpecommon.sdc_be_request_headers, auth=self.vcpecommon.sdc_be_request_userpass) |
Yang Xu | c52ed6e | 2019-04-29 00:20:52 -0400 | [diff] [blame] | 52 | filename = 'csar/' + self.get_filename_from_cd(response.headers.get('Content-Disposition')) |
| 53 | open(filename, 'wb').write(response.content) |
| 54 | |
Yang Xu | 0e319ef | 2019-04-30 14:28:07 -0400 | [diff] [blame] | 55 | def create_allotted_resource_subcategory(self, newSubcategory): |
| 56 | """ |
| 57 | :param newSubcategory: a new subcategory under Allotted Resource, like BRG |
| 58 | :return: |
| 59 | """ |
| 60 | url = self.vcpecommon.sdc_get_category_list_url |
| 61 | self.logger.info(url) |
| 62 | resp = requests.get(url, headers=self.vcpecommon.sdc_fe_request_headers, auth=self.vcpecommon.sdc_fe_request_userpass) |
| 63 | data = resp.json() |
| 64 | |
| 65 | self.logger.debug('---------------------------------------------------------------') |
| 66 | self.logger.debug('------- Creation request submitted to SDC, got response --------') |
| 67 | self.logger.debug('response code = %s' % resp.status_code ) |
| 68 | self.logger.debug(json.dumps(data, indent=4, sort_keys=True)) |
| 69 | self.logger.debug('---------------------------------------------------------------') |
| 70 | |
| 71 | if data['resourceCategories']: |
| 72 | for category in data['resourceCategories']: |
| 73 | if category['name'] == 'Allotted Resource': |
| 74 | for subcategory in category['subcategories']: |
| 75 | if subcategory['name'] == newSubcategory: |
| 76 | self.logger.debug('Subcategory %s already exists' % newSubcategory) |
| 77 | return |
| 78 | self.logger.debug('Creating a new subcategory %s' % newSubcategory) |
| 79 | url = self.vcpecommon.sdc_create_allotted_resource_subcategory_url |
| 80 | self.logger.info(url) |
| 81 | details = {"name" : newSubcategory} |
| 82 | resp = requests.post(url, headers=self.vcpecommon.sdc_fe_request_headers, auth=self.vcpecommon.sdc_fe_request_userpass, json=details) |
| 83 | self.logger.debug('---------------------------------------------------------------') |
| 84 | self.logger.debug('------- Creation subcategory request submitted to SDC, got response --------') |
| 85 | self.logger.debug('response code = %s' % resp.status_code ) |
| 86 | self.logger.debug('---------------------------------------------------------------') |
| 87 | |
| 88 | |
| 89 | |