OpenStack Compute (Nova) Client
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.

floating_ip_dns.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # Copyright 2011 Andrew Bogott for The Wikimedia Foundation
  2. # All Rights Reserved.
  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. import urllib
  16. from novaclient import base
  17. def _quote_domain(domain):
  18. """Special quoting rule for placing domain names on a url line.
  19. Domain names tend to have .'s in them. Urllib doesn't quote dots,
  20. but Routes tends to choke on them, so we need an extra level of
  21. by-hand quoting here.
  22. """
  23. return urllib.quote(domain.replace('.', '%2E'))
  24. class FloatingIPDNSDomain(base.Resource):
  25. def delete(self):
  26. self.manager.delete(self.domain)
  27. def create(self):
  28. if self.scope == 'public':
  29. self.manager.create_public(self.domain, self.project)
  30. else:
  31. self.manager.create_private(self.domain, self.availability_zone)
  32. def get(self):
  33. entries = self.manager.domains()
  34. for entry in entries:
  35. if entry.get('domain') == self.domain:
  36. return entry
  37. return None
  38. class FloatingIPDNSDomainManager(base.Manager):
  39. resource_class = FloatingIPDNSDomain
  40. def domains(self):
  41. """Return the list of available dns domains."""
  42. return self._list("/os-floating-ip-dns", "domain_entries")
  43. def create_private(self, fqdomain, availability_zone):
  44. """Add or modify a private DNS domain."""
  45. body = {'domain_entry':
  46. {'scope': 'private',
  47. 'availability_zone': availability_zone}}
  48. return self._update('/os-floating-ip-dns/%s' % _quote_domain(fqdomain),
  49. body,
  50. 'domain_entry')
  51. def create_public(self, fqdomain, project):
  52. """Add or modify a public DNS domain."""
  53. body = {'domain_entry':
  54. {'scope': 'public',
  55. 'project': project}}
  56. return self._update('/os-floating-ip-dns/%s' % _quote_domain(fqdomain),
  57. body,
  58. 'domain_entry')
  59. def delete(self, fqdomain):
  60. """Delete the specified domain"""
  61. self._delete("/os-floating-ip-dns/%s" % _quote_domain(fqdomain))
  62. class FloatingIPDNSEntry(base.Resource):
  63. def delete(self):
  64. self.manager.delete(self.name, self.domain)
  65. def create(self):
  66. self.manager.create(self.domain, self.name,
  67. self.ip, self.dns_type)
  68. def get(self):
  69. return self.manager.get(self.domain, self.name)
  70. class FloatingIPDNSEntryManager(base.Manager):
  71. resource_class = FloatingIPDNSEntry
  72. def get(self, domain, name):
  73. """Return a list of entries for the given domain and ip or name."""
  74. return self._get("/os-floating-ip-dns/%s/entries/%s" %
  75. (_quote_domain(domain), name),
  76. "dns_entry")
  77. def get_for_ip(self, domain, ip):
  78. """Return a list of entries for the given domain and ip or name."""
  79. qparams = {'ip': ip}
  80. params = "?%s" % urllib.urlencode(qparams)
  81. return self._list("/os-floating-ip-dns/%s/entries%s" %
  82. (_quote_domain(domain), params),
  83. "dns_entries")
  84. def create(self, domain, name, ip, dns_type):
  85. """Add a new DNS entry."""
  86. body = {'dns_entry':
  87. {'ip': ip,
  88. 'dns_type': dns_type}}
  89. return self._update("/os-floating-ip-dns/%s/entries/%s" %
  90. (_quote_domain(domain), name),
  91. body,
  92. "dns_entry")
  93. def modify_ip(self, domain, name, ip):
  94. """Add a new DNS entry."""
  95. body = {'dns_entry':
  96. {'ip': ip,
  97. 'dns_type': 'A'}}
  98. return self._update("/os-floating-ip-dns/%s/entries/%s" %
  99. (_quote_domain(domain), name),
  100. body,
  101. "dns_entry")
  102. def delete(self, domain, name):
  103. """Delete entry specified by name and domain."""
  104. self._delete("/os-floating-ip-dns/%s/entries/%s" %
  105. (_quote_domain(domain), name))