|
| 1 | +Using the API |
| 2 | +============= |
| 3 | + |
| 4 | +Authorization / Configuration |
| 5 | +----------------------------- |
| 6 | + |
| 7 | +- Use :class:`Client <gcloud.dns.client.Client>` objects to configure |
| 8 | + your applications. |
| 9 | + |
| 10 | +- :class:`Client <gcloud.dns.client.Client>` objects hold both a ``project`` |
| 11 | + and an authenticated connection to the PubSub service. |
| 12 | + |
| 13 | +- The authentication credentials can be implicitly determined from the |
| 14 | + environment or directly via |
| 15 | + :meth:`from_service_account_json <gcloud.dns.client.Client.from_service_account_json>` |
| 16 | + and |
| 17 | + :meth:`from_service_account_p12 <gcloud.dns.client.Client.from_service_account_p12>`. |
| 18 | + |
| 19 | +- After setting ``GOOGLE_APPLICATION_CREDENTIALS`` and ``GCLOUD_PROJECT`` |
| 20 | + environment variables, create an instance of |
| 21 | + :class:`Client <gcloud.dns.client.Client>`. |
| 22 | + |
| 23 | + .. doctest:: |
| 24 | + |
| 25 | + >>> from gcloud import dns |
| 26 | + >>> client = dns.Client() |
| 27 | + |
| 28 | +- Override the credentials inferred from the environment by passing explicit |
| 29 | + ``credentials`` to one of the alternative ``classmethod`` factories, |
| 30 | + `:meth:gcloud.dns.client.Client.from_service_account_json`: |
| 31 | + |
| 32 | + .. doctest:: |
| 33 | + |
| 34 | + >>> from gcloud import dns |
| 35 | + >>> client = dns.Client.from_service_account_json('/path/to/creds.json') |
| 36 | + |
| 37 | + or `:meth:gcloud.dns.client.Client.from_service_account_p12`: |
| 38 | + |
| 39 | + .. doctest:: |
| 40 | + |
| 41 | + >>> from gcloud import dns |
| 42 | + >>> client = dns.Client.from_service_account_p12('/path/to/creds.p12', 'jrandom@example.com') |
| 43 | + |
| 44 | + |
| 45 | +Projects |
| 46 | +-------- |
| 47 | + |
| 48 | +A project is the top-level container in the ``DNS`` API: it is tied |
| 49 | +closely to billing, and can provide default access control across all its |
| 50 | +datasets. If no ``project`` is passed to the client container, the library |
| 51 | +attempts to infer a project using the environment (including explicit |
| 52 | +environment variables, GAE, and GCE). |
| 53 | + |
| 54 | +To override the project inferred from the environment, pass an explicit |
| 55 | +``project`` to the constructor, or to either of the alternative |
| 56 | +``classmethod`` factories: |
| 57 | + |
| 58 | + .. doctest:: |
| 59 | + |
| 60 | + >>> from gcloud import dns |
| 61 | + >>> client = dns.Client(project='PROJECT_ID') |
| 62 | + |
| 63 | +Project Quotas |
| 64 | +-------------- |
| 65 | + |
| 66 | +Query the quotas for a given project: |
| 67 | + |
| 68 | + .. doctest:: |
| 69 | + |
| 70 | + >>> from gcloud import dns |
| 71 | + >>> client = dns.Client(project='PROJECT_ID') |
| 72 | + >>> quotas = client.quotas() # API request |
| 73 | + >>> for key, value in sorted(quotas.items()): |
| 74 | + ... print('%s: %s' % (key, value)) |
| 75 | + managedZones: 10000 |
| 76 | + resourceRecordsPerRrset: 100 |
| 77 | + rrsetsPerManagedZone: 10000 |
| 78 | + rrsetAdditionsPerChange: 100 |
| 79 | + rrsetDeletionsPerChange: 100 |
| 80 | + totalRrdataSizePerChange: 10000 |
| 81 | + |
| 82 | +.. note:: |
| 83 | + |
| 84 | + See https://github.com/GoogleCloudPlatform/gcloud-python/issues/1136 |
| 85 | + for whether to expose quotas. |
| 86 | + |
| 87 | +Project ACLs |
| 88 | +~~~~~~~~~~~~ |
| 89 | + |
| 90 | +Each project has an access control list granting reader / writer / owner |
| 91 | +permission to one or more entities. This list cannot be queried or set |
| 92 | +via the API: it must be managed using the Google Developer Console. |
| 93 | + |
| 94 | + |
| 95 | +Managed Zones |
| 96 | +------------- |
| 97 | + |
| 98 | +A "managed zone" is the container for DNS records for the same DNS name |
| 99 | +suffix and has a set of name servers that accept and responds to queries: |
| 100 | + |
| 101 | + .. doctest:: |
| 102 | + |
| 103 | + >>> from gcloud import dns |
| 104 | + >>> client = dns.Client(project='PROJECT_ID') |
| 105 | + >>> zone = client.zone('acme-co', description='Acme Company zone', |
| 106 | + ... dns_name='example.com') |
| 107 | + |
| 108 | + >>> zone.exists() # API request |
| 109 | + False |
| 110 | + >>> zone.create() # API request |
| 111 | + >>> zone.exists() # API request |
| 112 | + True |
| 113 | + |
| 114 | +List the zones for a given project: |
| 115 | + |
| 116 | + .. doctest:: |
| 117 | + |
| 118 | + >>> from gcloud import dns |
| 119 | + >>> client = dns.Client(project='PROJECT_ID') |
| 120 | + >>> zones = client.list_zones() # API request |
| 121 | + >>> [zone.name for zone in zones] |
| 122 | + ['acme-co'>] |
| 123 | + |
| 124 | + |
| 125 | +Resource Record Sets |
| 126 | +-------------------- |
| 127 | + |
| 128 | +Each managed zone exposes a read-only set of resource records: |
| 129 | + |
| 130 | + .. doctest:: |
| 131 | + |
| 132 | + >>> from gcloud import dns |
| 133 | + >>> client = dns.Client(project='PROJECT_ID') |
| 134 | + >>> zone = client.zone('acme-co') |
| 135 | + >>> records, page_token = zone.list_resources() # API request |
| 136 | + >>> [(record.name, record.type, record.ttl, record.rrdatas) for record in records] |
| 137 | + [('example.com.', 'SOA', 21600, ['ns-cloud1.googlecomains.com dns-admin.google.com 1 21600 3600 1209600 300')] |
| 138 | + |
| 139 | +.. note:: |
| 140 | + |
| 141 | + The ``page_token`` returned from ``zone.list_resources()`` will be |
| 142 | + an opaque string if there are more resources than can be returned in a |
| 143 | + single request. To enumerate them all, repeat calling |
| 144 | + ``zone.list_resources()``, passing the ``page_token``, until the token |
| 145 | + is None. |
| 146 | + |
| 147 | +Change requests |
| 148 | +--------------- |
| 149 | + |
| 150 | +Update the resource record set for a zone by creating a change request |
| 151 | +bundling additions to or deletions from the set. |
| 152 | + |
| 153 | + .. doctest:: |
| 154 | + |
| 155 | + >>> import time |
| 156 | + >>> from gcloud import dns |
| 157 | + >>> client = dns.Client(project='PROJECT_ID') |
| 158 | + >>> zone = client.zone('acme-co') |
| 159 | + >>> record = dns.ResourceRecord(name='www.example.com', type='CNAME') |
| 160 | + >>> change = zone.change_request(additions=[record]) |
| 161 | + >>> change.begin() # API request |
| 162 | + >>> while change.status != 'done': |
| 163 | + ... print('Waiting for change to complete') |
| 164 | + ... time.sleep(60) |
| 165 | + ... change.reload() # API request |
| 166 | + |
| 167 | + |
| 168 | +List changes made to the resource record set for a given zone: |
| 169 | + |
| 170 | + .. doctest:: |
| 171 | + |
| 172 | + >>> from gcloud import dns |
| 173 | + >>> client = dns.Client(project='PROJECT_ID') |
| 174 | + >>> zone = client.zone('acme-co') |
| 175 | + >>> changes = [] |
| 176 | + >>> changes, page_token = zone.list_changes() # API request |
| 177 | + |
| 178 | +.. note:: |
| 179 | + |
| 180 | + The ``page_token`` returned from ``zone.list_changes()`` will be |
| 181 | + an opaque string if there are more hcanges than can be returned in a |
| 182 | + single request. To enumerate them all, repeat calling |
| 183 | + ``zone.list_hcanges()``, passing the ``page_token``, until the token |
| 184 | + is None. |
0 commit comments