1 # Copyright 2018 Telefonica
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
21 from osmclient
.common
.exceptions
import NotFound
22 from osmclient
.common
.exceptions
import OsmHttpException
23 from osmclient
.common
import utils
30 def __init__(self
, http
=None, client
=None):
33 self
._logger
= logging
.getLogger('osmclient')
34 self
._apiName
= '/pdu'
35 self
._apiVersion
= '/v1'
36 self
._apiResource
= '/pdu_descriptors'
37 self
._apiBase
= '{}{}{}'.format(self
._apiName
,
38 self
._apiVersion
, self
._apiResource
)
40 def list(self
, filter=None):
41 self
._logger
.debug("")
42 self
._client
.get_token()
45 filter_string
= '?{}'.format(filter)
46 _
, resp
= self
._http
.get2_cmd('{}{}'.format(self
._apiBase
,filter_string
))
48 return json
.loads(resp
)
52 self
._logger
.debug("")
53 self
._client
.get_token()
54 if utils
.validate_uuid4(name
):
55 for pdud
in self
.list():
56 if name
== pdud
['_id']:
59 for pdud
in self
.list():
60 if 'name' in pdud
and name
== pdud
['name']:
62 raise NotFound("pdud {} not found".format(name
))
64 def get_individual(self
, name
):
65 self
._logger
.debug("")
67 # It is redundant, since the previous one already gets the whole pdudInfo
68 # The only difference is that a different primitive is exercised
69 _
, resp
= self
._http
.get2_cmd('{}/{}'.format(self
._apiBase
, pdud
['_id']))
70 #print(yaml.safe_dump(resp))
72 return json
.loads(resp
)
73 raise NotFound("pdu {} not found".format(name
))
75 def delete(self
, name
, force
=False):
76 self
._logger
.debug("")
80 querystring
= '?FORCE=True'
81 http_code
, resp
= self
._http
.delete_cmd('{}/{}{}'.format(self
._apiBase
,
82 pdud
['_id'], querystring
))
83 #print('HTTP CODE: {}'.format(http_code))
84 #print('RESP: {}'.format(resp))
86 print('Deletion in progress')
87 elif http_code
== 204:
93 msg
= json
.loads(resp
)
96 raise OsmHttpException("failed to delete pdu {} - {}".format(name
, msg
))
98 def create(self
, pdu
, update_endpoint
=None):
99 self
._logger
.debug("")
100 self
._client
.get_token()
101 headers
= self
._client
._headers
102 headers
['Content-Type'] = 'application/yaml'
103 http_header
= ['{}: {}'.format(key
,val
)
104 for (key
,val
) in list(headers
.items())]
105 self
._http
.set_http_header(http_header
)
107 http_code
, resp
= self
._http
.put_cmd(endpoint
=update_endpoint
, postfields_dict
=pdu
)
109 endpoint
= self
._apiBase
110 #endpoint = '{}{}'.format(self._apiBase,ow_string)
111 http_code
, resp
= self
._http
.post_cmd(endpoint
=endpoint
, postfields_dict
=pdu
)
112 #print('HTTP CODE: {}'.format(http_code))
113 #print('RESP: {}'.format(resp))
114 #if http_code in (200, 201, 202, 204):
116 resp
= json
.loads(resp
)
117 if not resp
or 'id' not in resp
:
118 raise OsmHttpException('unexpected response from server: '.format(
122 # msg = "Error {}".format(http_code)
125 # msg = "{} - {}".format(msg, json.loads(resp))
127 # msg = "{} - {}".format(msg, resp)
128 # raise ClientException("failed to create/update pdu - {}".format(msg))
130 def update(self
, name
, filename
):
131 self
._logger
.debug("")
132 pdud
= self
.get(name
)
133 endpoint
= '{}/{}'.format(self
._apiBase
, pdud
['_id'])
134 self
.create(filename
=filename
, update_endpoint
=endpoint
)