Standardize Formatting
[osm/osmclient.git] / osmclient / v1 / client.py
1 # Copyright 2017 Sandvine
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 v1 client API
19 """
20
21 from osmclient.v1 import vnf
22 from osmclient.v1 import vnfd
23 from osmclient.v1 import ns
24 from osmclient.v1 import nsd
25 from osmclient.v1 import vim
26 from osmclient.v1 import package
27 from osmclient.v1 import vca
28 from osmclient.v1 import utils
29 from osmclient.common import http
30 from osmclient.common import package_tool
31
32
33 class Client(object):
34 def __init__(
35 self,
36 host=None,
37 so_port=8008,
38 so_project="default",
39 ro_host=None,
40 ro_port=9090,
41 upload_port=8443,
42 **kwargs
43 ):
44 self._user = "admin"
45 self._password = "admin"
46
47 if len(host.split(":")) > 1:
48 # backwards compatible, port provided as part of host
49 self._host = host.split(":")[0]
50 self._so_port = host.split(":")[1]
51 else:
52 self._host = host
53 self._so_port = so_port
54
55 self._so_project = so_project
56
57 http_client = http.Http("https://{}:{}/".format(self._host, self._so_port))
58 http_client.set_http_header(
59 ["Accept: application/vnd.yand.data+json", "Content-Type: application/json"]
60 )
61
62 self._so_version = self.get_so_version(http_client)
63
64 if ro_host is None:
65 ro_host = host
66 ro_http_client = http.Http("http://{}:{}/".format(ro_host, ro_port))
67 ro_http_client.set_http_header(
68 ["Accept: application/vnd.yand.data+json", "Content-Type: application/json"]
69 )
70
71 upload_client_url = "https://{}:{}/composer/upload?api_server={}{}".format(
72 self._host,
73 upload_port,
74 "https://localhost&upload_server=https://",
75 self._host,
76 )
77
78 if self._so_version == "v3":
79 upload_client_url = (
80 "https://{}:{}/composer/upload?api_server={}{}&project_name={}".format(
81 self._host,
82 upload_port,
83 "https://localhost&upload_server=https://",
84 self._host,
85 self._so_project,
86 )
87 )
88
89 upload_client = http.Http(upload_client_url)
90
91 self.vnf = vnf.Vnf(http_client, client=self, **kwargs)
92 self.vnfd = vnfd.Vnfd(http_client, client=self, **kwargs)
93 self.ns = ns.Ns(http=http_client, client=self, **kwargs)
94 self.nsd = nsd.Nsd(http_client, client=self, **kwargs)
95 self.vim = vim.Vim(
96 http=http_client, ro_http=ro_http_client, client=self, **kwargs
97 )
98 self.package = package.Package(
99 http=http_client, upload_http=upload_client, client=self, **kwargs
100 )
101 self.vca = vca.Vca(http_client, client=self, **kwargs)
102 self.utils = utils.Utils(http_client, **kwargs)
103 self.package_tool = package_tool.PackageTool(client=self)
104
105 @property
106 def so_rbac_project_path(self):
107 if self._so_version == "v3":
108 return "project/{}/".format(self._so_project)
109 else:
110 return ""
111
112 def get_so_version(self, http_client):
113 try:
114 resp = http_client.get_cmd("api/operational/version")
115 if not resp or "rw-base:version" not in resp:
116 return "v2"
117
118 if resp["rw-base:version"]["version"].split(".")[0] == "5":
119 # SO Version 5.x.x.x.x translates to OSM V3
120 return "v3"
121 return "v2"
122 except Exception:
123 return "v2"