#    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 posixpath
from functools import wraps

from muranoclient.common import base


def normalize_path(f):
    @wraps(f)
    def f_normalize_path(*args, **kwargs):
        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


def verify_path(f):
    @wraps(f)
    def f_verify_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 '/'")

    return f_verify_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

    @verify_path
    @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)

    @verify_path
    @normalize_path
    def post(self, environment_id, path, data, session_id):
        headers = {'X-Configuration-Session': session_id}

        return self._create('/environments/{0}/services/{1}'.
                            format(environment_id, path), data,
                            headers=headers)

    @verify_path
    @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)

    @verify_path
    @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)

@@ -784,296 +784,257 @@
     
-args = list(args)%0A        if len(args) %3E= 3:%0A            args%5B2%5D = args%5B2%5D%5B1:%5D%0A        else:%0A            kwargs%5B'path'%5D = kwargs%5B'path'%5D%5B1:%5D%0A%0A        return f(*args, **kwargs)%0A%0A    return f_normalize_path%0A%0A%0Adef verify_path(f):%0A    @wraps(f)%0A    def f_verify_path(*args, **kw
+path = args%5B2%5D if len(args) %3E= 3 else kwargs%5B'path'%5D%0A%0A        #path formally is just absolute unix path%0A        if not posixpath.isabs(path):%0A            raise ValueError(%22Parameter 'path' should start with '/'%22)%0A%0A        # args = list(
 args)
-:
 %0A       
  pat
@@ -1025,39 +1025,24 @@
 rgs)%0A       
- path = args%5B2%5D
  if len(args
@@ -1051,187 +1051,139 @@
 %3E= 3
- else kwargs%5B'path'%5D%0A%0A        #path formally is just absolute unix path%0A        if not posixpath.isabs(path):%0A            raise ValueError(%22Parameter 'path' should start with '/'%22
+:%0A            args%5B2%5D = args%5B2%5D%5B1:%5D%0A        else:%0A            kwargs%5B'path'%5D = kwargs%5B'path'%5D%5B1:%5D%0A%0A        return f(*args, **kwargs
 )%0A%0A 
@@ -1194,22 +1194,25 @@
 eturn f_
-verify
+normalize
 _path%0A%0A%0A
@@ -1456,33 +1456,16 @@
 ervice%0A%0A
-    @verify_path%0A
     @nor
@@ -1793,33 +1793,16 @@
 aders)%0A%0A
-    @verify_path%0A
     @nor
@@ -2104,33 +2104,16 @@
 aders)%0A%0A
-    @verify_path%0A
     @nor
@@ -2418,25 +2418,8 @@
 s)%0A%0A
-    @verify_path%0A
     

Fix service creation failure