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 | |
| 19 | def get_service_list(self): |
| 20 | """ |
| 21 | :return: |
| 22 | """ |
| 23 | |
| 24 | url = self.vcpecommon.sdc_service_list_url |
| 25 | self.logger.info(url) |
| 26 | r = requests.get(url, headers=self.vcpecommon.sdc_get_request_headers, auth=self.vcpecommon.sdc_get_request_userpass) |
| 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: |
| 37 | if service['name'].startswith('demoVCPE'): |
| 38 | self.logger.debug('service name = %s, url = %s' % (service['name'], service['toscaModelURL'])) |
| 39 | self.download_file(self.vcpecommon.sdc_url_prefix + service['toscaModelURL']) |
| 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) |
| 51 | response = requests.get(url, headers=self.vcpecommon.sdc_get_request_headers, auth=self.vcpecommon.sdc_get_request_userpass) |
| 52 | filename = 'csar/' + self.get_filename_from_cd(response.headers.get('Content-Disposition')) |
| 53 | open(filename, 'wb').write(response.content) |
| 54 | |