/gdata/apps/adminsettings/service.py

http://radioappz.googlecode.com/ · Python · 471 lines · 346 code · 39 blank · 86 comment · 7 complexity · 43773d5ccc7a197f9719dfe7fd23335c MD5 · raw file

  1. #!/usr/bin/python
  2. #
  3. # Copyright (C) 2008 Google, Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Allow Google Apps domain administrators to set domain admin settings.
  17. AdminSettingsService: Set admin settings."""
  18. __author__ = 'jlee@pbu.edu'
  19. import gdata.apps
  20. import gdata.apps.service
  21. import gdata.service
  22. API_VER='2.0'
  23. class AdminSettingsService(gdata.apps.service.PropertyService):
  24. """Client for the Google Apps Admin Settings service."""
  25. def _serviceUrl(self, setting_id, domain=None):
  26. if domain is None:
  27. domain = self.domain
  28. return '/a/feeds/domain/%s/%s/%s' % (API_VER, domain, setting_id)
  29. def genericGet(self, location):
  30. """Generic HTTP Get Wrapper
  31. Args:
  32. location: relative uri to Get
  33. Returns:
  34. A dict containing the result of the get operation."""
  35. uri = self._serviceUrl(location)
  36. try:
  37. return self._GetProperties(uri)
  38. except gdata.service.RequestError, e:
  39. raise AppsForYourDomainException(e.args[0])
  40. def GetDefaultLanguage(self):
  41. """Gets Domain Default Language
  42. Args:
  43. None
  44. Returns:
  45. Default Language as a string. All possible values are listed at:
  46. http://code.google.com/apis/apps/email_settings/developers_guide_protocol.html#GA_email_language_tags"""
  47. result = self.genericGet('general/defaultLanguage')
  48. return result['defaultLanguage']
  49. def UpdateDefaultLanguage(self, defaultLanguage):
  50. """Updates Domain Default Language
  51. Args:
  52. defaultLanguage: Domain Language to set
  53. possible values are at:
  54. http://code.google.com/apis/apps/email_settings/developers_guide_protocol.html#GA_email_language_tags
  55. Returns:
  56. A dict containing the result of the put operation"""
  57. uri = self._serviceUrl('general/defaultLanguage')
  58. properties = {'defaultLanguage': defaultLanguage}
  59. return self._PutProperties(uri, properties)
  60. def GetOrganizationName(self):
  61. """Gets Domain Default Language
  62. Args:
  63. None
  64. Returns:
  65. Organization Name as a string."""
  66. result = self.genericGet('general/organizationName')
  67. return result['organizationName']
  68. def UpdateOrganizationName(self, organizationName):
  69. """Updates Organization Name
  70. Args:
  71. organizationName: Name of organization
  72. Returns:
  73. A dict containing the result of the put operation"""
  74. uri = self._serviceUrl('general/organizationName')
  75. properties = {'organizationName': organizationName}
  76. return self._PutProperties(uri, properties)
  77. def GetMaximumNumberOfUsers(self):
  78. """Gets Maximum Number of Users Allowed
  79. Args:
  80. None
  81. Returns: An integer, the maximum number of users"""
  82. result = self.genericGet('general/maximumNumberOfUsers')
  83. return int(result['maximumNumberOfUsers'])
  84. def GetCurrentNumberOfUsers(self):
  85. """Gets Current Number of Users
  86. Args:
  87. None
  88. Returns: An integer, the current number of users"""
  89. result = self.genericGet('general/currentNumberOfUsers')
  90. return int(result['currentNumberOfUsers'])
  91. def IsDomainVerified(self):
  92. """Is the domain verified
  93. Args:
  94. None
  95. Returns: Boolean, is domain verified"""
  96. result = self.genericGet('accountInformation/isVerified')
  97. if result['isVerified'] == 'true':
  98. return True
  99. else:
  100. return False
  101. def GetSupportPIN(self):
  102. """Gets Support PIN
  103. Args:
  104. None
  105. Returns: A string, the Support PIN"""
  106. result = self.genericGet('accountInformation/supportPIN')
  107. return result['supportPIN']
  108. def GetEdition(self):
  109. """Gets Google Apps Domain Edition
  110. Args:
  111. None
  112. Returns: A string, the domain's edition (premier, education, partner)"""
  113. result = self.genericGet('accountInformation/edition')
  114. return result['edition']
  115. def GetCustomerPIN(self):
  116. """Gets Customer PIN
  117. Args:
  118. None
  119. Returns: A string, the customer PIN"""
  120. result = self.genericGet('accountInformation/customerPIN')
  121. return result['customerPIN']
  122. def GetCreationTime(self):
  123. """Gets Domain Creation Time
  124. Args:
  125. None
  126. Returns: A string, the domain's creation time"""
  127. result = self.genericGet('accountInformation/creationTime')
  128. return result['creationTime']
  129. def GetCountryCode(self):
  130. """Gets Domain Country Code
  131. Args:
  132. None
  133. Returns: A string, the domain's country code. Possible values at:
  134. http://www.iso.org/iso/country_codes/iso_3166_code_lists/english_country_names_and_code_elements.htm"""
  135. result = self.genericGet('accountInformation/countryCode')
  136. return result['countryCode']
  137. def GetAdminSecondaryEmail(self):
  138. """Gets Domain Admin Secondary Email Address
  139. Args:
  140. None
  141. Returns: A string, the secondary email address for domain admin"""
  142. result = self.genericGet('accountInformation/adminSecondaryEmail')
  143. return result['adminSecondaryEmail']
  144. def UpdateAdminSecondaryEmail(self, adminSecondaryEmail):
  145. """Gets Domain Creation Time
  146. Args:
  147. adminSecondaryEmail: string, secondary email address of admin
  148. Returns: A dict containing the result of the put operation"""
  149. uri = self._serviceUrl('accountInformation/adminSecondaryEmail')
  150. properties = {'adminSecondaryEmail': adminSecondaryEmail}
  151. return self._PutProperties(uri, properties)
  152. def GetDomainLogo(self):
  153. """Gets Domain Logo
  154. This function does not make use of the Google Apps Admin Settings API,
  155. it does an HTTP Get of a url specific to the Google Apps domain. It is
  156. included for completeness sake.
  157. Args:
  158. None
  159. Returns: binary image file"""
  160. import urllib
  161. url = 'http://www.google.com/a/cpanel/'+self.domain+'/images/logo.gif'
  162. response = urllib.urlopen(url)
  163. return response.read()
  164. def UpdateDomainLogo(self, logoImage):
  165. """Update Domain's Custom Logo
  166. Args:
  167. logoImage: binary image data
  168. Returns: A dict containing the result of the put operation"""
  169. from base64 import base64encode
  170. uri = self._serviceUrl('appearance/customLogo')
  171. properties = {'logoImage': base64encode(logoImage)}
  172. return self._PutProperties(uri, properties)
  173. def GetCNAMEVerificationStatus(self):
  174. """Gets Domain CNAME Verification Status
  175. Args:
  176. None
  177. Returns: A dict {recordName, verified, verifiedMethod}"""
  178. return self.genericGet('verification/cname')
  179. def UpdateCNAMEVerificationStatus(self, verified):
  180. """Updates CNAME Verification Status
  181. Args:
  182. verified: boolean, True will retry verification process
  183. Returns: A dict containing the result of the put operation"""
  184. uri = self._serviceUrl('verification/cname')
  185. properties = self.GetCNAMEVerificationStatus()
  186. properties['verified'] = verified
  187. return self._PutProperties(uri, properties)
  188. def GetMXVerificationStatus(self):
  189. """Gets Domain MX Verification Status
  190. Args:
  191. None
  192. Returns: A dict {verified, verifiedMethod}"""
  193. return self.genericGet('verification/mx')
  194. def UpdateMXVerificationStatus(self, verified):
  195. """Updates MX Verification Status
  196. Args:
  197. verified: boolean, True will retry verification process
  198. Returns: A dict containing the result of the put operation"""
  199. uri = self._serviceUrl('verification/mx')
  200. properties = self.GetMXVerificationStatus()
  201. properties['verified'] = verified
  202. return self._PutProperties(uri, properties)
  203. def GetSSOSettings(self):
  204. """Gets Domain Single Sign-On Settings
  205. Args:
  206. None
  207. Returns: A dict {samlSignonUri, samlLogoutUri, changePasswordUri, enableSSO, ssoWhitelist, useDomainSpecificIssuer}"""
  208. return self.genericGet('sso/general')
  209. def UpdateSSOSettings(self, enableSSO=None, samlSignonUri=None,
  210. samlLogoutUri=None, changePasswordUri=None,
  211. ssoWhitelist=None, useDomainSpecificIssuer=None):
  212. """Update SSO Settings.
  213. Args:
  214. enableSSO: boolean, SSO Master on/off switch
  215. samlSignonUri: string, SSO Login Page
  216. samlLogoutUri: string, SSO Logout Page
  217. samlPasswordUri: string, SSO Password Change Page
  218. ssoWhitelist: string, Range of IP Addresses which will see SSO
  219. useDomainSpecificIssuer: boolean, Include Google Apps Domain in Issuer
  220. Returns:
  221. A dict containing the result of the update operation.
  222. """
  223. uri = self._serviceUrl('sso/general')
  224. #Get current settings, replace Nones with ''
  225. properties = self.GetSSOSettings()
  226. if properties['samlSignonUri'] == None:
  227. properties['samlSignonUri'] = ''
  228. if properties['samlLogoutUri'] == None:
  229. properties['samlLogoutUri'] = ''
  230. if properties['changePasswordUri'] == None:
  231. properties['changePasswordUri'] = ''
  232. if properties['ssoWhitelist'] == None:
  233. properties['ssoWhitelist'] = ''
  234. #update only the values we were passed
  235. if enableSSO != None:
  236. properties['enableSSO'] = gdata.apps.service._bool2str(enableSSO)
  237. if samlSignonUri != None:
  238. properties['samlSignonUri'] = samlSignonUri
  239. if samlLogoutUri != None:
  240. properties['samlLogoutUri'] = samlLogoutUri
  241. if changePasswordUri != None:
  242. properties['changePasswordUri'] = changePasswordUri
  243. if ssoWhitelist != None:
  244. properties['ssoWhitelist'] = ssoWhitelist
  245. if useDomainSpecificIssuer != None:
  246. properties['useDomainSpecificIssuer'] = gdata.apps.service._bool2str(useDomainSpecificIssuer)
  247. return self._PutProperties(uri, properties)
  248. def GetSSOKey(self):
  249. """Gets Domain Single Sign-On Signing Key
  250. Args:
  251. None
  252. Returns: A dict {modulus, exponent, algorithm, format}"""
  253. return self.genericGet('sso/signingkey')
  254. def UpdateSSOKey(self, signingKey):
  255. """Update SSO Settings.
  256. Args:
  257. signingKey: string, public key to be uploaded
  258. Returns:
  259. A dict containing the result of the update operation."""
  260. uri = self._serviceUrl('sso/signingkey')
  261. properties = {'signingKey': signingKey}
  262. return self._PutProperties(uri, properties)
  263. def IsUserMigrationEnabled(self):
  264. """Is User Migration Enabled
  265. Args:
  266. None
  267. Returns:
  268. boolean, is user migration enabled"""
  269. result = self.genericGet('email/migration')
  270. if result['enableUserMigration'] == 'true':
  271. return True
  272. else:
  273. return False
  274. def UpdateUserMigrationStatus(self, enableUserMigration):
  275. """Update User Migration Status
  276. Args:
  277. enableUserMigration: boolean, user migration enable/disable
  278. Returns:
  279. A dict containing the result of the update operation."""
  280. uri = self._serviceUrl('email/migration')
  281. properties = {'enableUserMigration': enableUserMigration}
  282. return self._PutProperties(uri, properties)
  283. def GetOutboundGatewaySettings(self):
  284. """Get Outbound Gateway Settings
  285. Args:
  286. None
  287. Returns:
  288. A dict {smartHost, smtpMode}"""
  289. uri = self._serviceUrl('email/gateway')
  290. try:
  291. return self._GetProperties(uri)
  292. except gdata.service.RequestError, e:
  293. raise AppsForYourDomainException(e.args[0])
  294. except TypeError:
  295. #if no outbound gateway is set, we get a TypeError,
  296. #catch it and return nothing...
  297. return {'smartHost': None, 'smtpMode': None}
  298. def UpdateOutboundGatewaySettings(self, smartHost=None, smtpMode=None):
  299. """Update Outbound Gateway Settings
  300. Args:
  301. smartHost: string, ip address or hostname of outbound gateway
  302. smtpMode: string, SMTP or SMTP_TLS
  303. Returns:
  304. A dict containing the result of the update operation."""
  305. uri = self._serviceUrl('email/gateway')
  306. #Get current settings, replace Nones with ''
  307. properties = GetOutboundGatewaySettings()
  308. if properties['smartHost'] == None:
  309. properties['smartHost'] = ''
  310. if properties['smtpMode'] == None:
  311. properties['smtpMode'] = ''
  312. #If we were passed new values for smartHost or smtpMode, update them
  313. if smartHost != None:
  314. properties['smartHost'] = smartHost
  315. if smtpMode != None:
  316. properties['smtpMode'] = smtpMode
  317. return self._PutProperties(uri, properties)
  318. def AddEmailRoute(self, routeDestination, routeRewriteTo, routeEnabled, bounceNotifications, accountHandling):
  319. """Adds Domain Email Route
  320. Args:
  321. routeDestination: string, destination ip address or hostname
  322. routeRewriteTo: boolean, rewrite smtp envelop To:
  323. routeEnabled: boolean, enable disable email routing
  324. bounceNotifications: boolean, send bound notificiations to sender
  325. accountHandling: string, which to route, "allAccounts", "provisionedAccounts", "unknownAccounts"
  326. Returns:
  327. A dict containing the result of the update operation."""
  328. uri = self._serviceUrl('emailrouting')
  329. properties = {}
  330. properties['routeDestination'] = routeDestination
  331. properties['routeRewriteTo'] = gdata.apps.service._bool2str(routeRewriteTo)
  332. properties['routeEnabled'] = gdata.apps.service._bool2str(routeEnabled)
  333. properties['bounceNotifications'] = gdata.apps.service._bool2str(bounceNotifications)
  334. properties['accountHandling'] = accountHandling
  335. return self._PostProperties(uri, properties)