Internet of Things resource management service for OpenStack clouds.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

service.py 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. # coding=utf-8
  2. #
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. from oslo_utils import strutils
  16. from oslo_utils import uuidutils
  17. from iotronic.common import exception
  18. from iotronic.db import api as db_api
  19. from iotronic.objects import base
  20. from iotronic.objects import utils as obj_utils
  21. ACTIONS = ['ServiceEnable', 'ServiceDisable', 'ServiceRestore']
  22. def is_valid_action(action):
  23. if action not in ACTIONS:
  24. raise exception.InvalidServiceAction(action=action)
  25. return True
  26. class Service(base.IotronicObject):
  27. # Version 1.0: Initial version
  28. VERSION = '1.0'
  29. dbapi = db_api.get_instance()
  30. fields = {
  31. 'id': int,
  32. 'uuid': obj_utils.str_or_none,
  33. 'name': obj_utils.str_or_none,
  34. 'project': obj_utils.str_or_none,
  35. 'port': int,
  36. 'protocol': obj_utils.str_or_none,
  37. 'extra': obj_utils.dict_or_none,
  38. }
  39. @staticmethod
  40. def _from_db_object(service, db_service):
  41. """Converts a database entity to a formal object."""
  42. for field in service.fields:
  43. service[field] = db_service[field]
  44. service.obj_reset_changes()
  45. return service
  46. @base.remotable_classmethod
  47. def get(cls, context, service_id):
  48. """Find a service based on its id or uuid and return a Board object.
  49. :param service_id: the id *or* uuid of a service.
  50. :returns: a :class:`Board` object.
  51. """
  52. if strutils.is_int_like(service_id):
  53. return cls.get_by_id(context, service_id)
  54. elif uuidutils.is_uuid_like(service_id):
  55. return cls.get_by_uuid(context, service_id)
  56. else:
  57. raise exception.InvalidIdentity(identity=service_id)
  58. @base.remotable_classmethod
  59. def get_by_id(cls, context, service_id):
  60. """Find a service based on its integer id and return a Board object.
  61. :param service_id: the id of a service.
  62. :returns: a :class:`Board` object.
  63. """
  64. db_service = cls.dbapi.get_service_by_id(service_id)
  65. service = Service._from_db_object(cls(context), db_service)
  66. return service
  67. @base.remotable_classmethod
  68. def get_by_uuid(cls, context, uuid):
  69. """Find a service based on uuid and return a Board object.
  70. :param uuid: the uuid of a service.
  71. :returns: a :class:`Board` object.
  72. """
  73. db_service = cls.dbapi.get_service_by_uuid(uuid)
  74. service = Service._from_db_object(cls(context), db_service)
  75. return service
  76. @base.remotable_classmethod
  77. def get_by_name(cls, context, name):
  78. """Find a service based on name and return a Board object.
  79. :param name: the logical name of a service.
  80. :returns: a :class:`Board` object.
  81. """
  82. db_service = cls.dbapi.get_service_by_name(name)
  83. service = Service._from_db_object(cls(context), db_service)
  84. return service
  85. @base.remotable_classmethod
  86. def list(cls, context, limit=None, marker=None, sort_key=None,
  87. sort_dir=None, filters=None):
  88. """Return a list of Service objects.
  89. :param context: Security context.
  90. :param limit: maximum number of resources to return in a single result.
  91. :param marker: pagination marker for large data sets.
  92. :param sort_key: column to sort results by.
  93. :param sort_dir: direction to sort. "asc" or "desc".
  94. :param filters: Filters to apply.
  95. :returns: a list of :class:`Service` object.
  96. """
  97. db_services = cls.dbapi.get_service_list(filters=filters,
  98. limit=limit,
  99. marker=marker,
  100. sort_key=sort_key,
  101. sort_dir=sort_dir)
  102. return [Service._from_db_object(cls(context), obj)
  103. for obj in db_services]
  104. @base.remotable
  105. def create(self, context=None):
  106. """Create a Service record in the DB.
  107. Column-wise updates will be made based on the result of
  108. self.what_changed(). If target_power_state is provided,
  109. it will be checked against the in-database copy of the
  110. service before updates are made.
  111. :param context: Security context. NOTE: This should only
  112. be used internally by the indirection_api.
  113. Unfortunately, RPC requires context as the first
  114. argument, even though we don't use it.
  115. A context should be set when instantiating the
  116. object, e.g.: Service(context)
  117. """
  118. values = self.obj_get_changes()
  119. db_service = self.dbapi.create_service(values)
  120. self._from_db_object(self, db_service)
  121. @base.remotable
  122. def destroy(self, context=None):
  123. """Delete the Service from the DB.
  124. :param context: Security context. NOTE: This should only
  125. be used internally by the indirection_api.
  126. Unfortunately, RPC requires context as the first
  127. argument, even though we don't use it.
  128. A context should be set when instantiating the
  129. object, e.g.: Service(context)
  130. """
  131. self.dbapi.destroy_service(self.uuid)
  132. self.obj_reset_changes()
  133. @base.remotable
  134. def save(self, context=None):
  135. """Save updates to this Service.
  136. Column-wise updates will be made based on the result of
  137. self.what_changed(). If target_power_state is provided,
  138. it will be checked against the in-database copy of the
  139. service before updates are made.
  140. :param context: Security context. NOTE: This should only
  141. be used internally by the indirection_api.
  142. Unfortunately, RPC requires context as the first
  143. argument, even though we don't use it.
  144. A context should be set when instantiating the
  145. object, e.g.: Service(context)
  146. """
  147. updates = self.obj_get_changes()
  148. self.dbapi.update_service(self.uuid, updates)
  149. self.obj_reset_changes()
  150. @base.remotable
  151. def refresh(self, context=None):
  152. """Refresh the object by re-fetching from the DB.
  153. :param context: Security context. NOTE: This should only
  154. be used internally by the indirection_api.
  155. Unfortunately, RPC requires context as the first
  156. argument, even though we don't use it.
  157. A context should be set when instantiating the
  158. object, e.g.: Service(context)
  159. """
  160. current = self.__class__.get_by_uuid(self._context, self.uuid)
  161. for field in self.fields:
  162. if (hasattr(
  163. self, base.get_attrname(field))
  164. and self[field] != current[field]):
  165. self[field] = current[field]