Files
deb-python-fuelclient/fuelclient/objects/base.py
Alexandr Notchenko 33cd0ea3ee Fuelclient major refactoring
* package structure
 * some implementation ideas
 * a lot of working functions
 * fixed Fuelclient tests
 * fixed action validation
 * fixed formatting and pep8
 * fixed copyright
 * fixed unnecessary request during deployment

 Implements: blueprint fuelclient-refactoring

Change-Id: Ice7f96273afb5d96e7970acfc45c61bab620aaed
2014-04-10 15:23:25 +04:00

63 lines
1.7 KiB
Python

# Copyright 2014 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.
from fuelclient.cli.serializers import Serializer
from fuelclient.client import APIClient
class BaseObject(object):
class_api_path = None
instance_api_path = None
connection = APIClient
def __init__(self, obj_id, **kwargs):
self.connection = APIClient
self.serializer = Serializer(**kwargs)
self.id = obj_id
self._data = None
@classmethod
def init_with_data(cls, data):
instance = cls(data["id"])
instance._data = data
return instance
@classmethod
def get_by_ids(cls, ids):
return map(cls, ids)
def update(self):
self._data = self.connection.get_request(
self.instance_api_path.format(self.id))
def get_fresh_data(self):
self.update()
return self.data
@property
def data(self):
if self._data is None:
return self.get_fresh_data()
else:
return self._data
@classmethod
def get_all_data(cls):
return cls.connection.get_request(cls.class_api_path)
@classmethod
def get_all(cls):
return map(cls.init_with_data, cls.get_all_data())