python-muranoclient/muranoclient/v1/services.py
Igor Yozhikov 59920a3d63 Cherry-pick following change-ids from release-0.2.
I155d42b0b45444fe239cb4ac9bd7e26e5c835aa5
Ie8559212b3850a191e61b7eace489ac369f7bfab
I42d3f577270c8330595c1e006e488c6fc4567d12
Ic75310c28e3d65e8871cc974699146c6767a4c6b
Ibf5ded656f7ae2c80e00aeb4c757bf238b7595aa

Change-Id: Ic260349de80e4ab92239c8eef8e492507bbc40e9
2013-09-03 15:15:02 +04:00

90 lines
2.8 KiB
Python

# Copyright (c) 2013 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import types
import posixpath
from functools import wraps
from muranoclient.common import base
def normalize_path(f):
@wraps(f)
def f_normalize_path(*args, **kwargs):
path = args[2] if len(args) >= 3 else kwargs['path']
#path formally is just absolute unix path
if not posixpath.isabs(path):
raise ValueError("Parameter 'path' should start with '/'")
args = list(args)
if len(args) >= 3:
args[2] = args[2][1:]
else:
kwargs['path'] = kwargs['path'][1:]
return f(*args, **kwargs)
return f_normalize_path
class Service(base.Resource):
def __repr__(self):
return '<Service %s>' % self._info
def data(self, **kwargs):
return self.manager.data(self, **kwargs)
class ServiceManager(base.Manager):
resource_class = Service
@normalize_path
def get(self, environment_id, path, session_id=None):
if session_id:
headers = {'X-Configuration-Session': session_id}
else:
headers = {}
return self._list('/environments/{0}/services/{1}'.
format(environment_id, path), headers=headers)
@normalize_path
def post(self, environment_id, path, data, session_id):
headers = {'X-Configuration-Session': session_id}
result = self._create('/environments/{0}/services/{1}'.
format(environment_id, path), data,
headers=headers, return_raw=True)
if isinstance(result, types.ListType):
return [self.resource_class(self, item) for item in result]
else:
return self.resource_class(self, result)
@normalize_path
def put(self, environment_id, path, data, session_id):
headers = {'X-Configuration-Session': session_id}
return self._update('/environments/{0}/services/{1}'.
format(environment_id, path), data,
headers=headers)
@normalize_path
def delete(self, environment_id, path, session_id):
headers = {'X-Configuration-Session': session_id}
path = '/environments/{0}/services/{1}'.format(environment_id, path)
return self._delete(path, headers=headers)