models.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. # Copyright 2014 Baidu, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  4. # except in compliance with the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the
  9. # License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  10. # either express or implied. See the License for the specific language governing permissions
  11. # and limitations under the License.
  12. """
  13. This module provides models for CFC-SDK.
  14. """
  15. import json
  16. DUEDGE_TRIGGER = 'duedge'
  17. DUEROS_TRIGGER = 'dueros'
  18. CRONTAB_TRIGGER = 'cfc-crontab-trigger/v1/'
  19. HTTP_TRIGGER = 'cfc-http-trigger/v1/CFCAPI'
  20. CDN_TRIGGER = 'cdn'
  21. BOS_TRIGGER = 'bos'
  22. class AbstractTriggerDataModel(object):
  23. """Base class for all trigger data."""
  24. def serialize(self):
  25. """
  26. serialize
  27. :return
  28. :rtype dict
  29. """
  30. d = vars(self)
  31. ret = {}
  32. for k in d:
  33. if d[k] is not None:
  34. ret[k] = d[k]
  35. return ret
  36. def get_trigger_source(self):
  37. """
  38. trigger source
  39. :return
  40. :rtype string
  41. """
  42. return ''
  43. def __repr__(self):
  44. return '%s' % json.dumps(self.serialize())
  45. class CrontabTriggerData(AbstractTriggerDataModel):
  46. """
  47. Crontab Trigger Data
  48. :param brn (required) The url path.
  49. :type brn string
  50. :param name (required) The trigger name. 1-30 length.Pattern: ^[a-zA-Z0-9-_]+$
  51. :type name string.The name of the trigger that you are creating or updating
  52. :param schedule_expression (required) Schedule expression.The details see
  53. https://cloud.baidu.com/doc/CFC/s/Zjxl9lbed.
  54. For example, "cron(0 * * * *)" or "rate(10 minutes)".
  55. :type schedule_expression string.
  56. :param enabled.
  57. :type enabled bool. Enables the trigger.
  58. :param custom_input.
  59. :type custom_input json.
  60. """
  61. def __init__(self, brn=None, name=None, schedule_expression=None, enabled=False, custom_input=None):
  62. self.Input = custom_input
  63. self.Brn = brn
  64. self.Name = name
  65. self.ScheduleExpression = schedule_expression
  66. self.UUID = None
  67. if enabled:
  68. self.Enabled = 'Enabled'
  69. else:
  70. self.Enabled = 'Disabled'
  71. def set_status(self, enabled=False):
  72. """
  73. set crontab status
  74. :param enabled.
  75. :type enabled bool. Enables the trigger.
  76. :return
  77. :rtype string
  78. """
  79. if enabled:
  80. self.Enabled = 'Enabled'
  81. else:
  82. self.Enabled = 'Disabled'
  83. def get_trigger_source(self):
  84. return CRONTAB_TRIGGER
  85. class HttpTriggerData(AbstractTriggerDataModel):
  86. """
  87. Http Trigger Data
  88. :param resource_path (required) The url path.
  89. :type resource_path string
  90. :param method (required) The http method. eg "GET,HEAD"
  91. :type method string
  92. :param auth_type (required) Authentication type.
  93. :type auth_type string. eg anonymous | iam
  94. """
  95. def __init__(self, resource_path=None, method=None, auth_type=None):
  96. self.ResourcePath = resource_path
  97. self.Method = method
  98. self.AuthType = auth_type
  99. def get_trigger_source(self):
  100. return HTTP_TRIGGER
  101. class CdnTriggerData(AbstractTriggerDataModel):
  102. """
  103. Cdn Trigger Data
  104. :param event_type (required) Cdn event type. The details see
  105. https://cloud.baidu.com/doc/CFC/s/Kjwvz47o9/#relationconfiguration.
  106. :type event_type string
  107. :param domains. Domain list.
  108. :type domains list of string
  109. :param remark.
  110. :type remark string.
  111. :param status. Enables the trigger.
  112. :type status bool.
  113. """
  114. def __init__(self, event_type=None, domains=None, remark=None, status=False):
  115. self.EventType = event_type
  116. self.Domains = domains
  117. self.Remark = remark
  118. if status:
  119. self.Status = 'enabled'
  120. else:
  121. self.Status = 'disabled'
  122. def set_status(self, enabled=False):
  123. """
  124. set cdn trigger status
  125. :param enabled.
  126. :type enabled bool. Enables the trigger.
  127. :return
  128. :rtype string
  129. """
  130. if enabled:
  131. self.Status = 'enabled'
  132. else:
  133. self.Status = 'disabled'
  134. def get_trigger_source(self):
  135. return CDN_TRIGGER
  136. class BOSTriggerData(AbstractTriggerDataModel):
  137. """
  138. BOS Trigger Data
  139. :param event_type (required) BOS event type. The details see
  140. https://cloud.baidu.com/doc/CFC/s/Kjwvz47o9/#relationconfiguration.
  141. :type event_type list of string
  142. :param resource. For example, /prefix*suffix /my.img /my*img
  143. :type resource string
  144. :param name. The name of the trigger that you are creating or updating
  145. :type name string
  146. :param status. Enables the trigger.
  147. :type status bool.
  148. """
  149. def __init__(self, bucket=None, event_type=None, resource=None, name=None, status=False):
  150. self.Resource = resource
  151. self.EventType = event_type
  152. self.Name = name
  153. self.Bucket = bucket
  154. if status:
  155. self.Status = 'enabled'
  156. else:
  157. self.Status = 'disabled'
  158. def set_status(self, enabled=False):
  159. """
  160. set bos trigger status
  161. :param enabled.
  162. :type enabled bool. Enables the trigger.
  163. :return
  164. :rtype string
  165. """
  166. if enabled:
  167. self.Status = 'enabled'
  168. else:
  169. self.Status = 'disabled'
  170. def get_trigger_source(self):
  171. return BOS_TRIGGER + '/' + self.Bucket