blob: db0229dfddf520d223146faa082ff99f89a527ae [file] [log] [blame]
Bartek Grzybowskifbbdbec2019-09-25 16:37:05 +02001#!/usr/bin/env python
Yang Xuc52ed6e2019-04-29 00:20:52 -04002
Yang Xuc52ed6e2019-04-29 00:20:52 -04003import logging
4import requests
5import json
Bartek Grzybowskibe1067b2020-03-04 14:01:02 +01006from vcpecommon import * # pylint: disable=W0614
Yang Xuc52ed6e2019-04-29 00:20:52 -04007
8
9class 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 Xu0e319ef2019-04-30 14:28:07 -040018 def download_vcpe_service_template(self):
Yang Xuc52ed6e2019-04-29 00:20:52 -040019 """
20 :return:
21 """
22
23 url = self.vcpecommon.sdc_service_list_url
24 self.logger.info(url)
Brian Freeman0c724152019-09-18 09:30:05 -050025 r = requests.get(url, headers=self.vcpecommon.sdc_be_request_headers, auth=self.vcpecommon.sdc_be_request_userpass, verify=False)
Yang Xuc52ed6e2019-04-29 00:20:52 -040026 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 Xu21b09c92019-06-13 13:19:20 -040036 if (service['name'].startswith('demoVCPE') or service['name'].startswith('vCPEResCust')) and service['distributionStatus'] == 'DISTRIBUTED':
Yang Xuc52ed6e2019-04-29 00:20:52 -040037 self.logger.debug('service name = %s, url = %s' % (service['name'], service['toscaModelURL']))
Yang Xu0e319ef2019-04-30 14:28:07 -040038 self.download_file(self.vcpecommon.sdc_be_url_prefix + service['toscaModelURL'])
Yang Xuc52ed6e2019-04-29 00:20:52 -040039
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 Freeman0c724152019-09-18 09:30:05 -050050 response = requests.get(url, headers=self.vcpecommon.sdc_be_request_headers, auth=self.vcpecommon.sdc_be_request_userpass, verify=False)
Yang Xuc52ed6e2019-04-29 00:20:52 -040051 filename = 'csar/' + self.get_filename_from_cd(response.headers.get('Content-Disposition'))
52 open(filename, 'wb').write(response.content)
53
Yang Xu0e319ef2019-04-30 14:28:07 -040054 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 Freeman0c724152019-09-18 09:30:05 -050061 resp = requests.get(url, headers=self.vcpecommon.sdc_fe_request_headers, auth=self.vcpecommon.sdc_fe_request_userpass, verify=False)
Yang Xu0e319ef2019-04-30 14:28:07 -040062 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 Freeman0c724152019-09-18 09:30:05 -050081 resp = requests.post(url, headers=self.vcpecommon.sdc_fe_request_headers, auth=self.vcpecommon.sdc_fe_request_userpass, json=details, verify=False)
Yang Xu0e319ef2019-04-30 14:28:07 -040082 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