Skip to content

Commit

Permalink
[Reservations] Creating Reservations commands in azue-cli-extensions (#…
Browse files Browse the repository at this point in the history
…5408)

* Initial

* update

* Update

* Update name

* Update CODEOWNERS

* Update service_name.json

* fix linter

Co-authored-by: kai ru <[email protected]>
  • Loading branch information
gaoyp830 and kairu-ms authored Oct 4, 2022
1 parent 4568de9 commit a1e0008
Show file tree
Hide file tree
Showing 70 changed files with 19,090 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,5 @@
/src/nginx/ @liftr-nginx

/src/elastic-san/ @calvinhzy

/src/reservation/ @gaoyp830 @rkapso @msft-adrianma @sornaks @juhee0202
8 changes: 8 additions & 0 deletions src/reservation/HISTORY.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.. :changelog:
Release History
===============

0.1.0
++++++
* Initial release.
117 changes: 117 additions & 0 deletions src/reservation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Azure CLI Reservation Extension #
This is the extension for reservation feature

### How to use ###
Install this extension using the below CLI command
```
az extension add --name reservation
```

### Included Features
#### Manage Azure Reservations.

##### Get catalog of available reservation.
```
az reservations catalog show --subscription-id {subId} \
[--location {location}] \
[--offer-id {offerId}] \
[--plan-id {planId}] \
[--publisher-id {publisherId}] \
[--reserved-resource-type {AVS, AppService, AzureDataExplorer, AzureFiles, BlockBlob, CosmosDb, DataFactory, Databricks, DedicatedHost, ManagedDisk, MariaDb, MySql, NetAppStorage, PostgreSql, RedHat, RedHatOsa, RedisCache, SapHana, SqlAzureHybridBenefit, SqlDataWarehouse, SqlDatabases, SqlEdge, SuseLinux, VMwareCloudSimple, VirtualMachineSoftware, VirtualMachines}]
```

##### List all reservations within a reservation order.
```
az reservations reservation list \
--reservation-order-id {orderId}
```

##### Get history of a reservation.
```
az reservations reservation list-history \
--reservation-id {reservationId} \
--reservation-order-id {orderId}
```

##### Merge two reservations.
```
az reservations reservation merge \
--reservation-id-1 {reservationId1} \
--reservation-id-2 {reservationId2} \
--reservation-order-id {orderId}
```

##### Get details of a reservation.
```
az reservations reservation show \
--reservation-id {reservationId} \
--reservation-order-id {orderId}
```

##### Split a reservation.
```
az reservations reservation split \
--quantity-1 {quantity1} \
--quantity-2 {quantity2} \
--reservation-id {reservationId} \
--reservation-order-id {orderId}
```

##### Updates the applied scopes of the reservation.
```
az reservations reservation update --applied-scope-type {Shared, Single} \
--reservation-id {reservationId} \
--reservation-order-id {orderId} \
[--applied-scopes] \
[--instance-flexibility {Off, On}] \
```

##### Calculate price for placing a reservation order.
```
az reservations reservation-order calculate
--applied-scope-type {Shared, Single} \
--billing-scope {billingScope} \
--display-name {name} \
--quantity {quantity} \
--reserved-resource-type {AVS, AppService, AzureDataExplorer, AzureFiles, BlockBlob, CosmosDb, DataFactory, Databricks, DedicatedHost, ManagedDisk, MariaDb, MySql, NetAppStorage, PostgreSql, RedHat, RedHatOsa, RedisCache, SapHana, SqlAzureHybridBenefit, SqlDataWarehouse, SqlDatabases, SqlEdge, SuseLinux, VMwareCloudSimple, VirtualMachineSoftware, VirtualMachines} \
--sku {skuName} \
--term {P1Y, P3Y, P5Y} \
[--applied-scope {scopeId}] \
[--billing-plan {Monthly, Upfront}] \
[--instance-flexibility {On, Off}] \
[--location {location}] \
[--renew {false, true}]
```

##### List of all the reservation orders that the user has access to in the current tenant.
```
az reservations reservation list
```

##### Purchase reservation order and create resource under the specified URI.
```
az reservations reservation-order purchase
--reservation-order-id {orderId} \
--applied-scope-type {Shared, Single} \
--billing-scope {billingScope} \
--display-name {name} \
--quantity {quantity} \
--reserved-resource-type {AVS, AppService, AzureDataExplorer, AzureFiles, BlockBlob, CosmosDb, DataFactory, Databricks, DedicatedHost, ManagedDisk, MariaDb, MySql, NetAppStorage, PostgreSql, RedHat, RedHatOsa, RedisCache, SapHana, SqlAzureHybridBenefit, SqlDataWarehouse, SqlDatabases, SqlEdge, SuseLinux, VMwareCloudSimple, VirtualMachineSoftware, VirtualMachines} \
--sku {skuName} \
--term {P1Y, P3Y, P5Y} \
[--applied-scope {scopeId}] \
[--billing-plan {Monthly, Upfront}] \
[--instance-flexibility {On, Off}] \
[--location {location}] \
[--renew {false, true}]
```

##### Get the details of the reservation order.
```
az reservations reservation-order show --reservation-order-id {orderId}
```

##### Get applicable reservations that are applied to this subscription.
```
az reservations reservation-order-id list --subscription-id {subId}
```
34 changes: 34 additions & 0 deletions src/reservation/azext_reservation/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from azure.cli.core import AzCommandsLoader

from azext_reservation._client_factory import reservation_mgmt_client_factory
from ._exception_handler import reservations_exception_handler


class ReservationsCommandsLoader(AzCommandsLoader):

def __init__(self, cli_ctx=None):
from azure.cli.core.commands import CliCommandType
from azure.cli.core.profiles import ResourceType
reservations_custom = CliCommandType(operations_tmpl='azext_reservation.custom#{}',
client_factory=reservation_mgmt_client_factory,
exception_handler=reservations_exception_handler)
super().__init__(cli_ctx=cli_ctx,
custom_command_type=reservations_custom,
resource_type=ResourceType.MGMT_RESERVATIONS)

def load_command_table(self, args):
from azext_reservation.commands import load_command_table
load_command_table(self, args)
return self.command_table

def load_arguments(self, command):
from azext_reservation._params import load_arguments
load_arguments(self, command)


COMMAND_LOADER_CLS = ReservationsCommandsLoader
22 changes: 22 additions & 0 deletions src/reservation/azext_reservation/_client_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------


def cf_reservations(cli_ctx, **_):
from azure.cli.core.commands.client_factory import get_mgmt_service_client
from .vendored_sdks.reservations import AzureReservationAPI
return get_mgmt_service_client(cli_ctx, AzureReservationAPI, subscription_bound=False)


def reservation_mgmt_client_factory(cli_ctx, kwargs):
return cf_reservations(cli_ctx, **kwargs).reservation


def reservation_order_mgmt_client_factory(cli_ctx, kwargs):
return cf_reservations(cli_ctx, **kwargs).reservation_order


def base_mgmt_client_factory(cli_ctx, kwargs):
return cf_reservations(cli_ctx, **kwargs)
14 changes: 14 additions & 0 deletions src/reservation/azext_reservation/_exception_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from azure.cli.core.util import CLIError


def reservations_exception_handler(ex):
from .vendored_sdks.reservations.models import Error
if isinstance(ex, Error):
message = ex.error.error.message
raise CLIError(message)
raise ex
182 changes: 182 additions & 0 deletions src/reservation/azext_reservation/_help.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
# coding=utf-8
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from knack.help_files import helps # pylint: disable=unused-import
# pylint: disable=line-too-long, too-many-lines

helps['reservations'] = """
type: group
short-summary: Manage Azure Reservations.
"""

helps['reservations catalog'] = """
type: group
short-summary: See catalog of available reservations
"""

helps['reservations catalog show'] = """
type: command
short-summary: Get catalog of available reservation.
long-summary: |
Get the regions and skus that are available for RI purchase for the specified Azure subscription.
parameters:
- name: --subscription-id
type: string
short-summary: Id of the subscription to get the catalog for
- name: --reserved-resource-type
type: string
short-summary: Type of the resource for which the skus should be provided.
"""

helps['reservations reservation'] = """
type: group
short-summary: Manage reservation entities
"""

helps['reservations reservation list'] = """
type: command
short-summary: Get all reservations.
long-summary: |
List all reservations within a reservation order.
parameters:
- name: --reservation-order-id
type: string
short-summary: Id of container reservation order
"""

helps['reservations reservation list-history'] = """
type: command
short-summary: Get history of a reservation.
parameters:
- name: --reservation-order-id
type: string
short-summary: Order id of the reservation
- name: --reservation-id
type: string
short-summary: Reservation id of the reservation
"""

helps['reservations reservation merge'] = """
type: command
short-summary: Merge two reservations.
parameters:
- name: --reservation-order-id
type: string
short-summary: Reservation order id of the reservations to merge
- name: --reservation-id-1 -1
type: string
short-summary: Id of the first reservation to merge
- name: --reservation-id-2 -2
type: string
short-summary: Id of the second reservation to merge
"""

helps['reservations reservation show'] = """
type: command
short-summary: Get details of a reservation.
parameters:
- name: --reservation-order-id
type: string
short-summary: Order id of reservation to look up
- name: --reservation-id
type: string
short-summary: Reservation id of reservation to look up
"""

helps['reservations reservation split'] = """
type: command
short-summary: Split a reservation.
parameters:
- name: --reservation-order-id
type: string
short-summary: Reservation order id of the reservation to split
- name: --reservation-id
type: string
short-summary: Reservation id of the reservation to split
- name: --quantity-1 -1
type: int
short-summary: Quantity of the first reservation that will be created from split operation
- name: --quantity-2 -2
type: int
short-summary: Quantity of the second reservation that will be created from split operation
"""

helps['reservations reservation update'] = """
type: command
short-summary: Updates the applied scopes of the reservation.
parameters:
- name: --reservation-order-id
type: string
short-summary: Reservation order id of the reservation to update
- name: --reservation-id
type: string
short-summary: Id of the reservation to update
- name: --applied-scope-type -t
type: string
short-summary: Type of the Applied Scope to update the reservation with
- name: --applied-scopes -s
type: string
short-summary: Subscription that the benefit will be applied. Do not specify if AppliedScopeType is Shared.
- name: --instance-flexibility -i
type: string
short-summary: Type of the Instance Flexibility to update the reservation with
"""

helps['reservations reservation-order'] = """
type: group
short-summary: Manage reservation order, which is container for reservations
"""

helps['reservations reservation-order list'] = """
type: command
short-summary: Get all reservation orders
long-summary: |
List of all the reservation orders that the user has access to in the current tenant.
"""

helps['reservations reservation-order show'] = """
type: command
short-summary: Get a specific reservation order.
long-summary: Get the details of the reservation order.
parameters:
- name: --reservation-order-id
type: string
short-summary: Id of reservation order to look up
"""

helps['reservations reservation-order-id'] = """
type: group
short-summary: See reservation order ids that are applied to subscription
"""

helps['reservations reservation-order-id list'] = """
type: command
short-summary: Get list of applicable reservation order ids.
long-summary: |
Get applicable reservations that are applied to this subscription.
parameters:
- name: --subscription-id
type: string
short-summary: Id of the subscription to look up applied reservations
"""

helps['reservations reservation-order calculate'] = """
type: command
short-summary: Calculate price for a reservation order.
long-summary: Calculate price for placing a reservation order.
examples:
- name: Calculate price and get quote for specific resource type.
text: az reservations reservation-order calculate --sku standard_b1ls --location westus --reserved-resource-type VirtualMachines --billing-scope {SubId} --term P1Y --billing-plan Upfront --quantity 1 --applied-scope-type Single --applied-scope SubId --display-name test
"""

helps['reservations reservation-order purchase'] = """
type: command
short-summary: Purchase reservation order
long-summary: Purchase reservation order and create resource under the specified URI.
examples:
- name: Purchase reservation order
text: az reservations reservation-order purchase --reservation-order-id {reservationOrderId} --sku standard_b1ls --location westus --reserved-resource-type VirtualMachines --billing-scope {SubId} --term P1Y --billing-plan Upfront --quantity 1 --applied-scope-type Single --applied-scope {SubId} --display-name test
"""
Loading

0 comments on commit a1e0008

Please sign in to comment.