|
| 1 | +# -------------------------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | +# -------------------------------------------------------------------------------------------- |
| 5 | + |
| 6 | +"""SDK-independent STR operations shared by the existing SQL command handlers.""" |
| 7 | + |
| 8 | +from azure.cli.core.aaz import ( |
| 9 | + AAZBoolArg, AAZBoolType, AAZCommand, AAZHttpOperation, AAZIntArg, |
| 10 | + AAZIntType, AAZObjectType, AAZStrArg, AAZStrType, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +class _ShortTermRetentionPolicy(AAZCommand): |
| 15 | + def _handler(self, command_args): |
| 16 | + # None means omitted in the existing Python handlers, not an explicit JSON null. |
| 17 | + super()._handler({key: value for key, value in command_args.items() if value is not None}) |
| 18 | + |
| 19 | + @classmethod |
| 20 | + def _build_arguments_schema(cls, *args, **kwargs): |
| 21 | + schema = super()._build_arguments_schema(*args, **kwargs) |
| 22 | + schema.resource_group_name = AAZStrArg(required=True) |
| 23 | + schema.parent_name = AAZStrArg(required=True) |
| 24 | + schema.database_name = AAZStrArg(required=True) |
| 25 | + schema.managed = AAZBoolArg(default=False) |
| 26 | + schema.deleted = AAZBoolArg(default=False) |
| 27 | + schema.retention_days = AAZIntArg() |
| 28 | + schema.diffbackup_hours = AAZIntArg() |
| 29 | + schema.lock_immutability = AAZBoolArg() |
| 30 | + return schema |
| 31 | + |
| 32 | + def _output(self, _result=None): |
| 33 | + return self.deserialize_output(self.ctx.vars.instance, client_flatten=True) |
| 34 | + |
| 35 | + |
| 36 | +class ShortTermRetentionPolicyGet(_ShortTermRetentionPolicy): |
| 37 | + def _handler(self, command_args): |
| 38 | + super()._handler(command_args) |
| 39 | + _PolicyGet(ctx=self.ctx)() |
| 40 | + return self._output() |
| 41 | + |
| 42 | + |
| 43 | +class ShortTermRetentionPolicySet(_ShortTermRetentionPolicy): |
| 44 | + AZ_SUPPORT_NO_WAIT = True |
| 45 | + |
| 46 | + def _handler(self, command_args): |
| 47 | + super()._handler(command_args) |
| 48 | + return self.build_lro_poller(self._execute_operations, self._output) |
| 49 | + |
| 50 | + def _execute_operations(self): |
| 51 | + yield _PolicySet(ctx=self.ctx)() |
| 52 | + |
| 53 | + |
| 54 | +class _PolicyOperation(AAZHttpOperation): # pylint: disable=abstract-method |
| 55 | + CLIENT_TYPE = "MgmtClient" |
| 56 | + |
| 57 | + @property |
| 58 | + def url(self): |
| 59 | + parent_type = "managedInstances" if self.ctx.args.managed else "servers" |
| 60 | + child_type = "restorableDroppedDatabases" if self.ctx.args.deleted else "databases" |
| 61 | + return self.client.format_url( |
| 62 | + "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}" |
| 63 | + "/providers/Microsoft.Sql/" + parent_type + "/{parentName}/" + child_type + |
| 64 | + "/{databaseName}/backupShortTermRetentionPolicies/default", |
| 65 | + **self.url_parameters |
| 66 | + ) |
| 67 | + |
| 68 | + @property |
| 69 | + def url_parameters(self): |
| 70 | + return { |
| 71 | + **self.serialize_url_param("subscriptionId", self.ctx.subscription_id, required=True), |
| 72 | + **self.serialize_url_param("resourceGroupName", self.ctx.args.resource_group_name, required=True), |
| 73 | + **self.serialize_url_param("parentName", self.ctx.args.parent_name, required=True), |
| 74 | + **self.serialize_url_param("databaseName", self.ctx.args.database_name, required=True), |
| 75 | + } |
| 76 | + |
| 77 | + @property |
| 78 | + def query_parameters(self): |
| 79 | + return {"api-version": "2026-08-01-preview"} |
| 80 | + |
| 81 | + @property |
| 82 | + def header_parameters(self): |
| 83 | + return {"Accept": "application/json", "Content-Type": "application/json"} |
| 84 | + |
| 85 | + @property |
| 86 | + def error_format(self): |
| 87 | + return "ODataV4Format" |
| 88 | + |
| 89 | + def on_success(self, session): |
| 90 | + self.ctx.set_var( |
| 91 | + "instance", self.deserialize_http_content(session), |
| 92 | + schema_builder=self._build_response_schema, |
| 93 | + ) |
| 94 | + |
| 95 | + @staticmethod |
| 96 | + def _build_response_schema(): |
| 97 | + schema = AAZObjectType() |
| 98 | + schema.id = AAZStrType(flags={"read_only": True}) |
| 99 | + schema.name = AAZStrType(flags={"read_only": True}) |
| 100 | + schema.type = AAZStrType(flags={"read_only": True}) |
| 101 | + schema.properties = AAZObjectType(flags={"client_flatten": True}) |
| 102 | + properties = schema.properties |
| 103 | + properties.retention_days = AAZIntType(serialized_name="retentionDays") |
| 104 | + properties.diff_backup_interval_in_hours = AAZIntType(serialized_name="diffBackupIntervalInHours") |
| 105 | + properties.lock_immutability = AAZBoolType(serialized_name="lockImmutability") |
| 106 | + properties.immutability_status = AAZStrType( |
| 107 | + serialized_name="immutabilityStatus", flags={"read_only": True}, |
| 108 | + ) |
| 109 | + schema.system_data = AAZObjectType(serialized_name="systemData", flags={"read_only": True}) |
| 110 | + system_data = schema.system_data |
| 111 | + system_data.created_by = AAZStrType(serialized_name="createdBy") |
| 112 | + system_data.created_by_type = AAZStrType(serialized_name="createdByType") |
| 113 | + system_data.created_at = AAZStrType(serialized_name="createdAt") |
| 114 | + system_data.last_modified_by = AAZStrType(serialized_name="lastModifiedBy") |
| 115 | + system_data.last_modified_by_type = AAZStrType(serialized_name="lastModifiedByType") |
| 116 | + system_data.last_modified_at = AAZStrType(serialized_name="lastModifiedAt") |
| 117 | + return schema |
| 118 | + |
| 119 | + |
| 120 | +class _PolicyGet(_PolicyOperation): |
| 121 | + @property |
| 122 | + def method(self): |
| 123 | + return "GET" |
| 124 | + |
| 125 | + def __call__(self, *args, **kwargs): |
| 126 | + session = self.client.send_request(request=self.make_request(), stream=False, **kwargs) |
| 127 | + if session.http_response.status_code == 200: |
| 128 | + return self.on_success(session) |
| 129 | + return self.on_error(session.http_response) |
| 130 | + |
| 131 | + |
| 132 | +class _PolicySet(_PolicyOperation): |
| 133 | + @property |
| 134 | + def method(self): |
| 135 | + return "PUT" |
| 136 | + |
| 137 | + @property |
| 138 | + def content(self): |
| 139 | + value, builder = self.new_content_builder(self.ctx.args, typ=AAZObjectType) |
| 140 | + builder.set_prop("properties", AAZObjectType) |
| 141 | + properties = builder.get(".properties") |
| 142 | + properties.set_prop("retentionDays", AAZIntType, ".retention_days") |
| 143 | + if not self.ctx.args.managed: |
| 144 | + properties.set_prop("diffBackupIntervalInHours", AAZIntType, ".diffbackup_hours") |
| 145 | + if not self.ctx.args.deleted: |
| 146 | + properties.set_prop("lockImmutability", AAZBoolType, ".lock_immutability") |
| 147 | + return self.serialize_content(value) |
| 148 | + |
| 149 | + def __call__(self, *args, **kwargs): |
| 150 | + session = self.client.send_request(request=self.make_request(), stream=False, **kwargs) |
| 151 | + if session.http_response.status_code in (200, 202): |
| 152 | + return self.client.build_lro_polling( |
| 153 | + self.ctx.args.no_wait, session, self.on_success, self.on_error, |
| 154 | + lro_options={"final-state-via": "location"}, |
| 155 | + path_format_arguments=self.url_parameters, |
| 156 | + ) |
| 157 | + return self.on_error(session.http_response) |
0 commit comments