1 # Copyright 2017 Sandvine
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may
6 # not use this file except in compliance with the License. You may obtain
7 # a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 # License for the specific language governing permissions and limitations
18 OSM package API handling
21 from osmclient
.common
.exceptions
import ClientException
22 from osmclient
.common
.exceptions
import NotFound
23 from osmclient
.common
import utils
26 class Package(object):
27 def __init__(self
, http
=None, upload_http
=None, client
=None):
30 self
._upload
_http
= upload_http
32 def _wait_for_package(self
, pkg_type
):
33 if 'vnfd' in pkg_type
['type']:
34 get_method
= self
._client
.vnfd
.get
35 elif 'nsd' in pkg_type
['type']:
36 get_method
= self
._client
.nsd
.get
38 raise ClientException("no valid package type found")
40 # helper method to check if pkg exists
41 def check_exists(func
):
48 return utils
.wait_for_value(lambda:
50 get_method(pkg_type
['name'])))
52 def get_key_val_from_pkg(self
, descriptor_file
):
53 return utils
.get_key_val_from_pkg(descriptor_file
)
55 def wait_for_upload(self
, filename
):
56 """wait(block) for an upload to succeed.
57 The filename passed is assumed to be a descriptor tarball.
59 pkg_type
= utils
.get_key_val_from_pkg(filename
)
62 raise ClientException("Cannot determine package type")
64 if not self
._wait
_for
_package
(pkg_type
):
65 raise ClientException("package {} failed to upload"
68 def upload(self
, filename
):
69 resp
= self
._upload
_http
.post_cmd(formfile
=('package', filename
))
70 if not resp
or 'transaction_id' not in resp
:
71 raise ClientException("failed to upload package")