blob: 1a202b4cfb5f5381e7fa3372dbac7868204f2556 [file] [log] [blame]
Yang Xuc52ed6e2019-04-29 00:20:52 -04001#! /usr/bin/python
2
3import sys
4import logging
5import requests
6import json
7from vcpecommon import *
8
9
10class 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 Xu0e319ef2019-04-30 14:28:07 -040019 def download_vcpe_service_template(self):
Yang Xuc52ed6e2019-04-29 00:20:52 -040020 """
21 :return:
22 """
23
24 url = self.vcpecommon.sdc_service_list_url
25 self.logger.info(url)
Yang Xu0e319ef2019-04-30 14:28:07 -040026 r = requests.get(url, headers=self.vcpecommon.sdc_be_request_headers, auth=self.vcpecommon.sdc_be_request_userpass)
Yang Xuc52ed6e2019-04-29 00:20:52 -040027 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 Xu0e319ef2019-04-30 14:28:07 -040037 if service['name'].startswith('demoVCPE') or service['name'].startswith('vCPEResCust'):
Yang Xuc52ed6e2019-04-29 00:20:52 -040038 self.logger.debug('service name = %s, url = %s' % (service['name'], service['toscaModelURL']))
Yang Xu0e319ef2019-04-30 14:28:07 -040039 self.download_file(self.vcpecommon.sdc_be_url_prefix + service['toscaModelURL'])
Yang Xuc52ed6e2019-04-29 00:20:52 -040040
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 Xu0e319ef2019-04-30 14:28:07 -040051 response = requests.get(url, headers=self.vcpecommon.sdc_be_request_headers, auth=self.vcpecommon.sdc_be_request_userpass)
Yang Xuc52ed6e2019-04-29 00:20:52 -040052 filename = 'csar/' + self.get_filename_from_cd(response.headers.get('Content-Disposition'))
53 open(filename, 'wb').write(response.content)
54
Yang Xu0e319ef2019-04-30 14:28:07 -040055 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