1 # Copyright 2017 ZTE Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
18 from lcm.pub.exceptions import NSLCMException
19 from lcm.pub.utils.restcall import call_req, rest_no_auth
20 from lcm.pub.config.config import SDC_BASE_URL, SDC_USER, SDC_PASSWD
22 logger = logging.getLogger(__name__)
24 ASSETTYPE_RESOURCES = "resources"
25 ASSETTYPE_SERVICES = "services"
27 def call_sdc(resource, method, content=''):
28 return call_req(base_url=SDC_BASE_URL,
31 auth_type=rest_no_auth,
36 def get_artifacts(asset_type):
37 resource = "/sdc/v1/catalog/{assetType}"
38 resource = resource.format(assetType=asset_type)
39 ret = call_sdc(resource, "GET")
41 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
42 raise NSLCMException("Failed to query artifacts(%s) from sdc." % asset_type)
43 return json.JSONDecoder().decode(ret[1])
45 def get_artifact(asset_type, csar_id):
46 artifacts = get_artifacts(asset_type)
47 for artifact in artifacts:
48 if artifact["uuid"] == csar_id:
50 raise NSLCMException("Failed to query artifact(%s,%s) from sdc." % (asset_type, csar_id))
52 def delete_artifact(asset_type, asset_id, artifact_id):
53 resource = "/sdc/v1/catalog/{assetType}/{uuid}/artifacts/{artifactUUID}"
54 resource = resource.format(assetType=asset_type, uuid=asset_id, artifactUUID=artifact_id)
55 ret = call_sdc(resource, "DELETE")
57 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
58 raise NSLCMException("Failed to delete artifacts(%s) from sdc." % artifact_id)
59 return json.JSONDecoder().decode(ret[1])
61 def download_artifacts(download_url, local_path):
62 ret = call_req(base_url=download_url,
65 auth_type=rest_no_auth,