|
| 1 | +# coding: utf-8 |
| 2 | + |
| 3 | +# (C) Copyright IBM Corp. 2020. |
| 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 | + |
| 17 | +""" |
| 18 | +DNS Zones |
| 19 | +""" |
| 20 | + |
| 21 | +from datetime import datetime |
| 22 | +from enum import Enum |
| 23 | +from typing import Dict, List |
| 24 | +import json |
| 25 | + |
| 26 | +from ibm_cloud_sdk_core import BaseService, DetailedResponse |
| 27 | +from ibm_cloud_sdk_core.authenticators.authenticator import Authenticator |
| 28 | +from ibm_cloud_sdk_core.get_authenticator import get_authenticator_from_environment |
| 29 | +from ibm_cloud_sdk_core.utils import datetime_to_string, string_to_datetime |
| 30 | + |
| 31 | +from .common import get_sdk_headers |
| 32 | + |
| 33 | +############################################################################## |
| 34 | +# Service |
| 35 | +############################################################################## |
| 36 | + |
| 37 | +class DnsZonesV1(BaseService): |
| 38 | + """The DNS Zones V1 service.""" |
| 39 | + |
| 40 | + DEFAULT_SERVICE_URL = 'https://api.dns-svcs.cloud.ibm.com/v1' |
| 41 | + DEFAULT_SERVICE_NAME = 'dns_zones' |
| 42 | + |
| 43 | + @classmethod |
| 44 | + def new_instance(cls, |
| 45 | + service_name: str = DEFAULT_SERVICE_NAME, |
| 46 | + ) -> 'DnsZonesV1': |
| 47 | + """ |
| 48 | + Return a new client for the DNS Zones service using the specified |
| 49 | + parameters and external configuration. |
| 50 | + """ |
| 51 | + authenticator = get_authenticator_from_environment(service_name) |
| 52 | + service = cls( |
| 53 | + authenticator |
| 54 | + ) |
| 55 | + service.configure_service(service_name) |
| 56 | + return service |
| 57 | + |
| 58 | + def __init__(self, |
| 59 | + authenticator: Authenticator = None, |
| 60 | + ) -> None: |
| 61 | + """ |
| 62 | + Construct a new client for the DNS Zones service. |
| 63 | +
|
| 64 | + :param Authenticator authenticator: The authenticator specifies the authentication mechanism. |
| 65 | + Get up to date information from https://github.com/IBM/python-sdk-core/blob/master/README.md |
| 66 | + about initializing the authenticator of your choice. |
| 67 | + """ |
| 68 | + BaseService.__init__(self, |
| 69 | + service_url=self.DEFAULT_SERVICE_URL, |
| 70 | + authenticator=authenticator) |
| 71 | + |
| 72 | + |
| 73 | + ######################### |
| 74 | + # DNS Zones |
| 75 | + ######################### |
| 76 | + |
| 77 | + |
| 78 | + def list_dnszones(self, instance_id: str, *, x_correlation_id: str = None, offset: int = None, limit: int = None, vpc_id: str = None, **kwargs) -> DetailedResponse: |
| 79 | + """ |
| 80 | + List DNS zones. |
| 81 | +
|
| 82 | + List the DNS zones for a given service instance. |
| 83 | +
|
| 84 | + :param str instance_id: The unique identifier of a service instance. |
| 85 | + :param str x_correlation_id: (optional) Uniquely identifying a request. |
| 86 | + :param int offset: (optional) Specify how many DNS zones to skip over, the |
| 87 | + default value is 0. |
| 88 | + :param int limit: (optional) Specify how many DNS zones are returned, the |
| 89 | + default value is 10. |
| 90 | + :param str vpc_id: (optional) Specify the VPC instance id. |
| 91 | + :param dict headers: A `dict` containing the request headers |
| 92 | + :return: A `DetailedResponse` containing the result, headers and HTTP status code. |
| 93 | + :rtype: DetailedResponse with `dict` result representing a `ListDnszones` object |
| 94 | + """ |
| 95 | + |
| 96 | + if instance_id is None: |
| 97 | + raise ValueError('instance_id must be provided') |
| 98 | + headers = { |
| 99 | + 'X-Correlation-ID': x_correlation_id |
| 100 | + } |
| 101 | + sdk_headers = get_sdk_headers(service_name=self.DEFAULT_SERVICE_NAME, service_version='V1', operation_id='list_dnszones') |
| 102 | + headers.update(sdk_headers) |
| 103 | + |
| 104 | + params = { |
| 105 | + 'offset': offset, |
| 106 | + 'limit': limit, |
| 107 | + 'vpc_id': vpc_id |
| 108 | + } |
| 109 | + |
| 110 | + if 'headers' in kwargs: |
| 111 | + headers.update(kwargs.get('headers')) |
| 112 | + |
| 113 | + url = '/instances/{0}/dnszones'.format(*self.encode_path_vars(instance_id)) |
| 114 | + request = self.prepare_request(method='GET', |
| 115 | + url=url, |
| 116 | + headers=headers, |
| 117 | + params=params) |
| 118 | + |
| 119 | + response = self.send(request) |
| 120 | + return response |
| 121 | + |
| 122 | + |
| 123 | + def create_dnszone(self, instance_id: str, *, name: str = None, description: str = None, label: str = None, x_correlation_id: str = None, **kwargs) -> DetailedResponse: |
| 124 | + """ |
| 125 | + Create a DNS zone. |
| 126 | +
|
| 127 | + Create a DNS zone for a given service instance. |
| 128 | +
|
| 129 | + :param str instance_id: The unique identifier of a service instance. |
| 130 | + :param str name: (optional) Name of DNS zone. |
| 131 | + :param str description: (optional) The text describing the purpose of a DNS |
| 132 | + zone. |
| 133 | + :param str label: (optional) The label of a DNS zone. |
| 134 | + :param str x_correlation_id: (optional) Uniquely identifying a request. |
| 135 | + :param dict headers: A `dict` containing the request headers |
| 136 | + :return: A `DetailedResponse` containing the result, headers and HTTP status code. |
| 137 | + :rtype: DetailedResponse with `dict` result representing a `Dnszone` object |
| 138 | + """ |
| 139 | + |
| 140 | + if instance_id is None: |
| 141 | + raise ValueError('instance_id must be provided') |
| 142 | + headers = { |
| 143 | + 'X-Correlation-ID': x_correlation_id |
| 144 | + } |
| 145 | + sdk_headers = get_sdk_headers(service_name=self.DEFAULT_SERVICE_NAME, service_version='V1', operation_id='create_dnszone') |
| 146 | + headers.update(sdk_headers) |
| 147 | + |
| 148 | + data = { |
| 149 | + 'name': name, |
| 150 | + 'description': description, |
| 151 | + 'label': label |
| 152 | + } |
| 153 | + data = {k: v for (k, v) in data.items() if v is not None} |
| 154 | + data = json.dumps(data) |
| 155 | + headers['content-type'] = 'application/json' |
| 156 | + |
| 157 | + if 'headers' in kwargs: |
| 158 | + headers.update(kwargs.get('headers')) |
| 159 | + |
| 160 | + url = '/instances/{0}/dnszones'.format(*self.encode_path_vars(instance_id)) |
| 161 | + request = self.prepare_request(method='POST', |
| 162 | + url=url, |
| 163 | + headers=headers, |
| 164 | + data=data) |
| 165 | + |
| 166 | + response = self.send(request) |
| 167 | + return response |
| 168 | + |
| 169 | + |
| 170 | + def delete_dnszone(self, instance_id: str, dnszone_id: str, *, x_correlation_id: str = None, **kwargs) -> DetailedResponse: |
| 171 | + """ |
| 172 | + Delete a DNS zone. |
| 173 | +
|
| 174 | + Delete a DNS zone. |
| 175 | +
|
| 176 | + :param str instance_id: The unique identifier of a service instance. |
| 177 | + :param str dnszone_id: The unique identifier of a DNS zone. |
| 178 | + :param str x_correlation_id: (optional) Uniquely identifying a request. |
| 179 | + :param dict headers: A `dict` containing the request headers |
| 180 | + :return: A `DetailedResponse` containing the result, headers and HTTP status code. |
| 181 | + :rtype: DetailedResponse |
| 182 | + """ |
| 183 | + |
| 184 | + if instance_id is None: |
| 185 | + raise ValueError('instance_id must be provided') |
| 186 | + if dnszone_id is None: |
| 187 | + raise ValueError('dnszone_id must be provided') |
| 188 | + headers = { |
| 189 | + 'X-Correlation-ID': x_correlation_id |
| 190 | + } |
| 191 | + sdk_headers = get_sdk_headers(service_name=self.DEFAULT_SERVICE_NAME, service_version='V1', operation_id='delete_dnszone') |
| 192 | + headers.update(sdk_headers) |
| 193 | + |
| 194 | + if 'headers' in kwargs: |
| 195 | + headers.update(kwargs.get('headers')) |
| 196 | + |
| 197 | + url = '/instances/{0}/dnszones/{1}'.format(*self.encode_path_vars(instance_id, dnszone_id)) |
| 198 | + request = self.prepare_request(method='DELETE', |
| 199 | + url=url, |
| 200 | + headers=headers) |
| 201 | + |
| 202 | + response = self.send(request) |
| 203 | + return response |
| 204 | + |
| 205 | + |
| 206 | + def get_dnszone(self, instance_id: str, dnszone_id: str, *, x_correlation_id: str = None, **kwargs) -> DetailedResponse: |
| 207 | + """ |
| 208 | + Get a DNS zone. |
| 209 | +
|
| 210 | + Get details of a DNS zone. |
| 211 | +
|
| 212 | + :param str instance_id: The unique identifier of a service instance. |
| 213 | + :param str dnszone_id: The unique identifier of a DNS zone. |
| 214 | + :param str x_correlation_id: (optional) Uniquely identifying a request. |
| 215 | + :param dict headers: A `dict` containing the request headers |
| 216 | + :return: A `DetailedResponse` containing the result, headers and HTTP status code. |
| 217 | + :rtype: DetailedResponse with `dict` result representing a `Dnszone` object |
| 218 | + """ |
| 219 | + |
| 220 | + if instance_id is None: |
| 221 | + raise ValueError('instance_id must be provided') |
| 222 | + if dnszone_id is None: |
| 223 | + raise ValueError('dnszone_id must be provided') |
| 224 | + headers = { |
| 225 | + 'X-Correlation-ID': x_correlation_id |
| 226 | + } |
| 227 | + sdk_headers = get_sdk_headers(service_name=self.DEFAULT_SERVICE_NAME, service_version='V1', operation_id='get_dnszone') |
| 228 | + headers.update(sdk_headers) |
| 229 | + |
| 230 | + if 'headers' in kwargs: |
| 231 | + headers.update(kwargs.get('headers')) |
| 232 | + |
| 233 | + url = '/instances/{0}/dnszones/{1}'.format(*self.encode_path_vars(instance_id, dnszone_id)) |
| 234 | + request = self.prepare_request(method='GET', |
| 235 | + url=url, |
| 236 | + headers=headers) |
| 237 | + |
| 238 | + response = self.send(request) |
| 239 | + return response |
| 240 | + |
| 241 | + |
| 242 | + def update_dnszone(self, instance_id: str, dnszone_id: str, *, description: str = None, label: str = None, x_correlation_id: str = None, **kwargs) -> DetailedResponse: |
| 243 | + """ |
| 244 | + Update the properties of a DNS zone. |
| 245 | +
|
| 246 | + Update the properties of a DNS zone. |
| 247 | +
|
| 248 | + :param str instance_id: The unique identifier of a service instance. |
| 249 | + :param str dnszone_id: The unique identifier of a DNS zone. |
| 250 | + :param str description: (optional) The text describing the purpose of a DNS |
| 251 | + zone. |
| 252 | + :param str label: (optional) The label of a DNS zone. |
| 253 | + :param str x_correlation_id: (optional) Uniquely identifying a request. |
| 254 | + :param dict headers: A `dict` containing the request headers |
| 255 | + :return: A `DetailedResponse` containing the result, headers and HTTP status code. |
| 256 | + :rtype: DetailedResponse with `dict` result representing a `Dnszone` object |
| 257 | + """ |
| 258 | + |
| 259 | + if instance_id is None: |
| 260 | + raise ValueError('instance_id must be provided') |
| 261 | + if dnszone_id is None: |
| 262 | + raise ValueError('dnszone_id must be provided') |
| 263 | + headers = { |
| 264 | + 'X-Correlation-ID': x_correlation_id |
| 265 | + } |
| 266 | + sdk_headers = get_sdk_headers(service_name=self.DEFAULT_SERVICE_NAME, service_version='V1', operation_id='update_dnszone') |
| 267 | + headers.update(sdk_headers) |
| 268 | + |
| 269 | + data = { |
| 270 | + 'description': description, |
| 271 | + 'label': label |
| 272 | + } |
| 273 | + data = {k: v for (k, v) in data.items() if v is not None} |
| 274 | + data = json.dumps(data) |
| 275 | + headers['content-type'] = 'application/json' |
| 276 | + |
| 277 | + if 'headers' in kwargs: |
| 278 | + headers.update(kwargs.get('headers')) |
| 279 | + |
| 280 | + url = '/instances/{0}/dnszones/{1}'.format(*self.encode_path_vars(instance_id, dnszone_id)) |
| 281 | + request = self.prepare_request(method='PATCH', |
| 282 | + url=url, |
| 283 | + headers=headers, |
| 284 | + data=data) |
| 285 | + |
| 286 | + response = self.send(request) |
| 287 | + return response |
0 commit comments