Code Coverage

Cobertura Coverage Report > osmclient.sol005 >

pdud.py

Trend

File Coverage summary

NameClassesLinesConditionals
pdud.py
100%
1/1
16%
15/93
100%
0/0

Coverage Breakdown by Class

NameLinesConditionals
pdud.py
16%
15/93
N/A

Source

osmclient/sol005/pdud.py
1 # Copyright 2018 Telefonica
2 #
3 # All Rights Reserved.
4 #
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
8 #
9 #         http://www.apache.org/licenses/LICENSE-2.0
10 #
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
15 #    under the License.
16
17 1 """
18 OSM pdud API handling
19 """
20
21 1 from osmclient.common.exceptions import NotFound
22 1 from osmclient.common.exceptions import ClientException
23 1 from osmclient.common import utils
24 1 import json
25 1 import logging
26
27
28 1 class Pdu(object):
29 1     def __init__(self, http=None, client=None):
30 0         self._http = http
31 0         self._client = client
32 0         self._logger = logging.getLogger("osmclient")
33 0         self._apiName = "/pdu"
34 0         self._apiVersion = "/v1"
35 0         self._apiResource = "/pdu_descriptors"
36 0         self._apiBase = "{}{}{}".format(
37             self._apiName, self._apiVersion, self._apiResource
38         )
39
40 1     def _get_vim_account(self, vim_account):
41 0         vim = self._client.vim.get(vim_account)
42 0         if vim is None:
43 0             raise NotFound("cannot find vim account '{}'".format(vim_account))
44 0         return vim
45
46 1     def list(self, filter=None):
47 0         self._logger.debug("")
48 0         self._client.get_token()
49 0         filter_string = ""
50 0         if filter:
51 0             filter_string = "?{}".format(filter)
52 0         _, resp = self._http.get2_cmd("{}{}".format(self._apiBase, filter_string))
53 0         if resp:
54 0             return json.loads(resp)
55 0         return list()
56
57 1     def get(self, name):
58 0         self._logger.debug("")
59 0         self._client.get_token()
60 0         if utils.validate_uuid4(name):
61 0             for pdud in self.list():
62 0                 if name == pdud["_id"]:
63 0                     return pdud
64         else:
65 0             for pdud in self.list():
66 0                 if "name" in pdud and name == pdud["name"]:
67 0                     return pdud
68 0         raise NotFound("pdud {} not found".format(name))
69
70 1     def get_individual(self, name):
71 0         self._logger.debug("")
72 0         pdud = self.get(name)
73         # It is redundant, since the previous one already gets the whole pdudInfo
74         # The only difference is that a different primitive is exercised
75 0         try:
76 0             _, resp = self._http.get2_cmd("{}/{}".format(self._apiBase, pdud["_id"]))
77 0         except NotFound:
78 0             raise NotFound("pdu '{}' not found".format(name))
79         # print(yaml.safe_dump(resp))
80 0         if resp:
81 0             return json.loads(resp)
82 0         raise NotFound("pdu '{}' not found".format(name))
83
84 1     def delete(self, name, force=False):
85 0         self._logger.debug("")
86 0         pdud = self.get(name)
87 0         querystring = ""
88 0         if force:
89 0             querystring = "?FORCE=True"
90 0         http_code, resp = self._http.delete_cmd(
91             "{}/{}{}".format(self._apiBase, pdud["_id"], querystring)
92         )
93         # print('HTTP CODE: {}'.format(http_code))
94         # print('RESP: {}'.format(resp))
95 0         if http_code == 202:
96 0             print("Deletion in progress")
97 0         elif http_code == 204:
98 0             print("Deleted")
99         else:
100 0             msg = resp or ""
101             # if resp:
102             #     try:
103             #         msg = json.loads(resp)
104             #     except ValueError:
105             #         msg = resp
106 0             raise ClientException("failed to delete pdu {} - {}".format(name, msg))
107
108 1     def create(self, pdu, update_endpoint=None):
109 0         self._logger.debug("")
110
111 0         if pdu["vim_accounts"]:
112 0             vim_account_list = []
113 0             for vim_account in pdu["vim_accounts"]:
114 0                 vim = self._get_vim_account(vim_account)
115 0                 vim_account_list.append(vim["_id"])
116 0             pdu["vim_accounts"] = vim_account_list
117
118 0         self._client.get_token()
119 0         headers = self._client._headers
120 0         headers["Content-Type"] = "application/yaml"
121 0         self._http.set_http_header(headers)
122 0         if update_endpoint:
123 0             http_code, resp = self._http.patch_cmd(
124                 endpoint=update_endpoint, postfields_dict=pdu
125             )
126         else:
127 0             endpoint = self._apiBase
128             # endpoint = '{}{}'.format(self._apiBase,ow_string)
129 0             http_code, resp = self._http.post_cmd(
130                 endpoint=endpoint, postfields_dict=pdu
131             )
132 0         if http_code in (200, 201, 202):
133 0             if resp:
134 0                 resp = json.loads(resp)
135 0             if not resp or "id" not in resp:
136 0                 raise ClientException(
137                     "unexpected response from server: {}".format(resp)
138                 )
139 0             print(resp["id"])
140 0         elif http_code == 204:
141 0             print("Updated")
142
143 1     def update(self, name, pdu):
144 0         self._logger.debug("")
145 0         pdud = self.get(name)
146 0         endpoint = "{}/{}".format(self._apiBase, pdud["_id"])
147 0         self.create(pdu=pdu, update_endpoint=endpoint)