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