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
21 logger = logging.getLogger(__name__)
23 ASSETTYPE_RESOURCES = "resources"
24 ASSETTYPE_SERVICES = "services"
26 def call_sdc(resource, method, content=''):
27 base_url = "TODO: SDC Base URL"
28 sdc_user = "TODO: sdc user"
29 sdc_passwd = "TODO: sdc passwd"
30 sdc_auth_type = "TODO: sdc auth type"
31 return call_req(base_url, sdc_user, sdc_passwd, sdc_auth_type, resource, method, content)
33 def get_artifacts(asset_type):
34 resource = "/sdc/v1/catalog/{assetType}"
35 resource = resource.format(assetType=asset_type)
36 ret = call_sdc(resource, "GET")
38 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
39 raise NSLCMException("Failed to query artifacts(%s) from sdc." % asset_type)
40 return json.JSONDecoder().decode(ret[1])
42 def get_artifact(asset_type, csar_id):
43 artifacts = get_artifacts(asset_type)
44 for artifact in artifacts:
45 if artifact["uuid"] == csar_id:
47 raise NSLCMException("Failed to query artifact(%s,%s) from sdc." % (asset_type, csar_id))
49 def delete_artifact(asset_type, asset_id, artifact_id):
50 resource = "/sdc/v1/catalog/{assetType}/{uuid}/artifacts/{artifactUUID}"
51 resource = resource.format(assetType=asset_type, uuid=asset_id, artifactUUID=artifact_id)
52 ret = call_sdc(resource, "DELETE")
54 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
55 raise NSLCMException("Failed to delete artifacts(%s) from sdc." % artifact_id)
56 return json.JSONDecoder().decode(ret[1])
58 def download_artifacts(download_url, local_path):
59 sdc_user = "TODO: sdc user"
60 sdc_passwd = "TODO: sdc passwd"
61 sdc_auth_type = "TODO: sdc auth type"
62 ret = call_req(download_url, sdc_user, sdc_passwd, sdc_auth_type, "", "GET")