new --force option for xxx-delete commands 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 yaml
25 import json
26
27
28 class SdnController(object):
29 def __init__(self, http=None, client=None):
30 self._http = http
31 self._client = client
32 self._apiName = '/admin'
33 self._apiVersion = '/v1'
34 self._apiResource = '/sdns'
35 self._apiBase = '{}{}{}'.format(self._apiName,
36 self._apiVersion, self._apiResource)
37 def create(self, name, sdn_controller):
38 http_code, resp = self._http.post_cmd(endpoint=self._apiBase,
39 postfields_dict=sdn_controller)
40 if resp:
41 resp = json.loads(resp)
42 #print 'RESP: {}'.format(resp)
43 if not resp or 'id' not in resp:
44 raise ClientException('failed to create SDN controller: '.format(
45 resp))
46 else:
47 print resp['id']
48
49 def update(self, name, sdn_controller):
50 sdnc = self.get(name)
51 http_code, resp = self._http.patch_cmd(endpoint='{}/{}'.format(self._apiBase,sdnc['_id']),
52 postfields_dict=sdn_controller)
53 if resp:
54 resp = json.loads(resp)
55 print 'RESP: {}'.format(resp)
56 if not resp or 'id' not in resp:
57 raise ClientException('failed to update SDN controller: '.format(
58 resp))
59 else:
60 print resp['id']
61
62 def delete(self, name, force=False):
63 sdn_controller = self.get(name)
64 querystring = ''
65 if force:
66 querystring = '?FORCE=True'
67 http_code, resp = self._http.delete_cmd('{}/{}{}'.format(self._apiBase,
68 sdn_controller['_id'], querystring))
69 if resp:
70 resp = json.loads(resp)
71 #print 'RESP: {}'.format(resp)
72 if http_code == 202:
73 print 'Deletion in progress'
74 elif http_code == 204:
75 print 'Deleted'
76 elif 'result' in resp:
77 print 'Deleted'
78 else:
79 raise ClientException("failed to delete vim {} - {}".format(name, resp))
80
81 def list(self, filter=None):
82 """Returns a list of SDN controllers
83 """
84 filter_string = ''
85 if filter:
86 filter_string = '?{}'.format(filter)
87 resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string))
88 #print 'RESP: {}'.format(resp)
89 if resp:
90 return resp
91 return list()
92
93 def get(self, name):
94 """Returns an SDN controller based on name or id
95 """
96 if utils.validate_uuid4(name):
97 for sdnc in self.list():
98 if name == sdnc['_id']:
99 return sdnc
100 else:
101 for sdnc in self.list():
102 if name == sdnc['name']:
103 return sdnc
104 raise NotFound("SDN controller {} not found".format(name))
105
106