Support of user and project mgmt in sol005 client
[osm/osmclient.git] / osmclient / sol005 / sdncontroller.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 """
18 OSM SDN controller API handling
19 """
20
21 from osmclient.common import utils
22 from osmclient.common.exceptions import ClientException
23 from osmclient.common.exceptions import NotFound
24 import json
25
26
27 class SdnController(object):
28 def __init__(self, http=None, client=None):
29 self._http = http
30 self._client = client
31 self._apiName = '/admin'
32 self._apiVersion = '/v1'
33 self._apiResource = '/sdns'
34 self._apiBase = '{}{}{}'.format(self._apiName,
35 self._apiVersion, self._apiResource)
36
37 def create(self, name, sdn_controller):
38 http_code, resp = self._http.post_cmd(endpoint=self._apiBase,
39 postfields_dict=sdn_controller)
40 #print 'HTTP CODE: {}'.format(http_code)
41 #print 'RESP: {}'.format(resp)
42 if http_code in (200, 201, 202, 204):
43 if resp:
44 resp = json.loads(resp)
45 if not resp or 'id' not in resp:
46 raise ClientException('unexpected response from server - {}'.format(
47 resp))
48 print(resp['id'])
49 else:
50 msg = ""
51 if resp:
52 try:
53 msg = json.loads(resp)
54 except ValueError:
55 msg = resp
56 raise ClientException("failed to create SDN controller {} - {}".format(name, msg))
57
58 def update(self, name, sdn_controller):
59 sdnc = self.get(name)
60 http_code, resp = self._http.put_cmd(endpoint='{}/{}'.format(self._apiBase,sdnc['_id']),
61 postfields_dict=sdn_controller)
62 #print 'HTTP CODE: {}'.format(http_code)
63 #print 'RESP: {}'.format(resp)
64 if http_code in (200, 201, 202, 204):
65 if resp:
66 resp = json.loads(resp)
67 if not resp or 'id' not in resp:
68 raise ClientException('unexpected response from server - {}'.format(
69 resp))
70 print(resp['id'])
71 else:
72 msg = ""
73 if resp:
74 try:
75 msg = json.loads(resp)
76 except ValueError:
77 msg = resp
78 raise ClientException("failed to update SDN controller {} - {}".format(name, msg))
79
80 def delete(self, name, force=False):
81 sdn_controller = self.get(name)
82 querystring = ''
83 if force:
84 querystring = '?FORCE=True'
85 http_code, resp = self._http.delete_cmd('{}/{}{}'.format(self._apiBase,
86 sdn_controller['_id'], querystring))
87 #print 'HTTP CODE: {}'.format(http_code)
88 #print 'RESP: {}'.format(resp)
89 if http_code == 202:
90 print('Deletion in progress')
91 elif http_code == 204:
92 print('Deleted')
93 elif resp and 'result' in resp:
94 print('Deleted')
95 else:
96 msg = ""
97 if resp:
98 try:
99 msg = json.loads(resp)
100 except ValueError:
101 msg = resp
102 raise ClientException("failed to delete SDN controller {} - {}".format(name, msg))
103
104 def list(self, filter=None):
105 """Returns a list of SDN controllers
106 """
107 filter_string = ''
108 if filter:
109 filter_string = '?{}'.format(filter)
110 resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string))
111 #print 'RESP: {}'.format(resp)
112 if resp:
113 return resp
114 return list()
115
116 def get(self, name):
117 """Returns an SDN controller based on name or id
118 """
119 if utils.validate_uuid4(name):
120 for sdnc in self.list():
121 if name == sdnc['_id']:
122 return sdnc
123 else:
124 for sdnc in self.list():
125 if name == sdnc['name']:
126 return sdnc
127 raise NotFound("SDN controller {} not found".format(name))
128
129