4134f77ba01a727c067b387861e2446479ef4773
[osm/osmclient.git] / osmclient / sol005 / ns.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 ns 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
26
27 class Ns(object):
28
29 def __init__(self, http=None, client=None):
30 self._http = http
31 self._client = client
32 self._apiName = '/nslcm'
33 self._apiVersion = '/v1'
34 self._apiResource = '/ns_instances_content'
35 self._apiBase = '{}{}{}'.format(self._apiName,
36 self._apiVersion, self._apiResource)
37
38 def list(self, filter=None):
39 """Returns a list of NS
40 """
41 filter_string = ''
42 if filter:
43 filter_string = '?{}'.format(filter)
44 resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string))
45 if resp:
46 return resp
47 return list()
48
49 def get(self, name):
50 """Returns an NS based on name or id
51 """
52 if utils.validate_uuid4(name):
53 for ns in self.list():
54 if name == ns['_id']:
55 return ns
56 else:
57 for ns in self.list():
58 if name == ns['name']:
59 return ns
60 raise NotFound("ns {} not found".format(name))
61
62 def get_individual(self, name):
63 ns_id = name
64 if not utils.validate_uuid4(name):
65 for ns in self.list():
66 if name == ns['name']:
67 ns_id = ns['_id']
68 break
69 resp = self._http.get_cmd('{}/{}'.format(self._apiBase, ns_id))
70 #resp = self._http.get_cmd('{}/{}/nsd_content'.format(self._apiBase, ns_id))
71 #print yaml.safe_dump(resp)
72 if resp:
73 return resp
74 raise NotFound("ns {} not found".format(name))
75
76 def delete(self, name):
77 ns = self.get(name)
78 resp = self._http.delete_cmd('{}/{}'.format(self._apiBase,ns['_id']))
79 #print 'RESP: '.format(resp)
80 if resp is None:
81 print 'Deleted'
82 else:
83 raise ClientException("failed to delete ns {}: {}".format(name, resp))
84
85 def create(self, nsd_name, nsr_name, account, config=None,
86 ssh_keys=None, description='default description',
87 admin_status='ENABLED'):
88
89 nsd = self._client.nsd.get(nsd_name)
90
91 datacenter = self._client.vim.get(account)
92 if datacenter is None:
93 raise NotFound("cannot find datacenter account {}".format(account))
94
95 ns = {}
96 ns['nsdId'] = nsd['_id']
97 ns['nsName'] = nsr_name
98 ns['nsDescription'] = description
99 #ns['vimAccountId'] = datacenter['_id']
100 # TODO: Fix it to use datacenter _id
101 ns['vimAccountId'] = datacenter['name']
102 #ns['userdata'] = {}
103 #ns['userdata']['key1']='value1'
104 #ns['userdata']['key2']='value2'
105
106 if ssh_keys is not None:
107 # ssh_keys is comma separate list
108 ssh_keys_format = []
109 for key in ssh_keys.split(','):
110 ssh_keys_format.append({'key-pair-ref': key})
111
112 ns['ssh-authorized-key'] = ssh_keys_format
113
114 #print yaml.safe_dump(ns)
115 try:
116 self._apiResource = '/ns_instances_content'
117 self._apiBase = '{}{}{}'.format(self._apiName,
118 self._apiVersion, self._apiResource)
119 #print resp
120 resp = self._http.post_cmd(endpoint=self._apiBase,
121 postfields_dict=ns)
122 if not resp or 'id' not in resp:
123 raise ClientException('unexpected response from server: '.format(
124 resp))
125 else:
126 print resp['id']
127 except ClientException as exc:
128 message="failed to create ns: {} nsd: {}\nerror:\n{}".format(
129 nsr_name,
130 nsd_name,
131 exc.message)
132 raise ClientException(message)
133