Skip to content

Commit

Permalink
Reserved Instance cli public PR (#4838)
Browse files Browse the repository at this point in the history
  • Loading branch information
corquiri authored and troydai committed Nov 8, 2017
1 parent 55b7b73 commit f061602
Show file tree
Hide file tree
Showing 28 changed files with 1,494 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/sphinx/azhelpgen/doc_source_map.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"provider": "src/command_modules/azure-cli-resource/azure/cli/command_modules/resource/_help.py",
"rdbms": "src/command_modules/azure-cli-rdbms/azure/cli/command_modules/rdbms/_help.py",
"redis": "src/command_modules/azure-cli-redis/azure/cli/command_modules/redis/_help.py",
"reservations": "src/command_modules/azure-cli-reservations/azure/cli/command_modules/reservations/_help.py",
"resource": "src/command_modules/azure-cli-resource/azure/cli/command_modules/resource/_help.py",
"role": "src/command_modules/azure-cli-role/azure/cli/command_modules/role/_help.py",
"sql": "src/command_modules/azure-cli-sql/azure/cli/command_modules/sql/_help.py",
Expand Down
1 change: 1 addition & 0 deletions src/azure-cli/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
'azure-cli-profile',
'azure-cli-rdbms',
'azure-cli-redis',
'azure-cli-reservations',
'azure-cli-resource',
'azure-cli-role',
'azure-cli-sql',
Expand Down
9 changes: 9 additions & 0 deletions src/command_modules/azure-cli-reservations/HISTORY.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.. :changelog:
Release History
===============

0.1.0
+++++

* Initial release.
1 change: 1 addition & 0 deletions src/command_modules/azure-cli-reservations/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include *.rst
5 changes: 5 additions & 0 deletions src/command_modules/azure-cli-reservations/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Microsoft Azure CLI 'reservations' Command Module
=================================================

This package is for the 'reservations' module.
i.e. 'az reservations'
7 changes: 7 additions & 0 deletions src/command_modules/azure-cli-reservations/azure/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import pkg_resources
pkg_resources.declare_namespace(__name__)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import pkg_resources
pkg_resources.declare_namespace(__name__)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import pkg_resources
pkg_resources.declare_namespace(__name__)
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.
# --------------------------------------------------------------------------------------------

import azure.cli.command_modules.reservations._help # pylint: disable=unused-import


def load_params(_):
import azure.cli.command_modules.reservations._params # pylint: disable=redefined-outer-name, unused-variable


def load_commands():
import azure.cli.command_modules.reservations.commands # pylint: disable=redefined-outer-name, unused-variable
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# --------------------------------------------------------------------------------------------
# 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(**_):
from azure.cli.core.commands.client_factory import get_mgmt_service_client
from azure.mgmt.reservations.azure_reservation_api import AzureReservationAPI
return get_mgmt_service_client(AzureReservationAPI, subscription_bound=False)


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


def reservation_order_mgmt_client_factory(kwargs):
return cf_reservations(**kwargs).reservation_order
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# --------------------------------------------------------------------------------------------
# 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 azure.mgmt.reservations.models import Error
if isinstance(ex, Error):
message = ex.error.error.message
raise CLIError(message)
else:
import sys
from six import reraise
reraise(*sys.exc_info())
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
# --------------------------------------------------------------------------------------------
# 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.help_files import helps

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

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

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

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

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

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 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 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
"""

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 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 update'] = """
type: command
short-summary: Updates the applied scopes of the reservation.
parameters:
- name: --reservation-order-id
type: string
short-summary: Order id of reservation to update
- name: --reservation-id
type: string
short-summary: Reservation id of reservation to update
- name: --applied-scope-type -t
type: string
short-summary: 'Type is either Single or Shared'
- name: --applied-scopes -s
type: string
short-summary: 'If applied scope type is Single, this field must be provided'
"""

helps['reservations reservation split'] = """
type: command
short-summary: Split a reservation.
parameters:
- name: --reservation-order-id
type: string
short-summary: Order id of original reservation
- name: --reservation-id
type: string
short-summary: Reservation id of original reservation
- 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 merge'] = """
type: command
short-summary: Merge two reservations.
parameters:
- name: --reservation-order-id
type: string
short-summary: Order id of original reservation
- 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 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
"""
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.
# --------------------------------------------------------------------------------------------

# pylint: disable=line-too-long
from azure.cli.core.commands import register_cli_argument

register_cli_argument('reservations reservation update', 'applied_scope_type', options_list=('--applied-scope-type', '-t'), required=True)
register_cli_argument('reservations reservation update', 'applied_scopes', options_list=('--applied-scopes', '-s'))
register_cli_argument('reservations reservation split', 'quantity_1', options_list=('--quantity-1', '-1'), required=True)
register_cli_argument('reservations reservation split', 'quantity_2', options_list=('--quantity-2', '-2'), required=True)
register_cli_argument('reservations reservation merge', 'reservation_id_1', options_list=('--reservation-id-1', '-1'), required=True)
register_cli_argument('reservations reservation merge', 'reservation_id_2', options_list=('--reservation-id-2', '-2'), required=True)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

# pylint: disable=line-too-long

from azure.cli.core.commands import cli_command
from azure.cli.command_modules.reservations._client_factory import reservation_mgmt_client_factory, reservation_order_mgmt_client_factory
from ._exception_handler import reservations_exception_handler

reservation_path = 'azure.mgmt.reservations.operations.reservation_operations#'
reservation_order_path = 'azure.mgmt.reservations.operations.reservation_order_operations#'
custom_path = 'azure.cli.command_modules.reservations.custom#'
reservation_client_path = 'azure.mgmt.reservations.azure_reservation_api#'


def reservation_command(*args, **kwargs):
cli_command(*args, exception_handler=reservations_exception_handler, **kwargs)


reservation_command(__name__, 'reservations reservation-order list', reservation_order_path + 'ReservationOrderOperations.list', reservation_order_mgmt_client_factory)
reservation_command(__name__, 'reservations reservation-order show', reservation_order_path + 'ReservationOrderOperations.get', reservation_order_mgmt_client_factory)
reservation_command(__name__, 'reservations reservation-order-id list', reservation_client_path + 'AzureReservationAPI.get_applied_reservation_list', reservation_order_mgmt_client_factory)
reservation_command(__name__, 'reservations catalog show', reservation_client_path + 'AzureReservationAPI.get_catalog', reservation_order_mgmt_client_factory)
reservation_command(__name__, 'reservations reservation list', reservation_path + 'ReservationOperations.list', reservation_mgmt_client_factory)
reservation_command(__name__, 'reservations reservation show', reservation_path + 'ReservationOperations.get', reservation_mgmt_client_factory)
reservation_command(__name__, 'reservations reservation update', custom_path + 'cli_reservation_update_reservation', reservation_mgmt_client_factory)
reservation_command(__name__, 'reservations reservation split', custom_path + 'cli_reservation_split_reservation', reservation_mgmt_client_factory)
reservation_command(__name__, 'reservations reservation merge', custom_path + 'cli_reservation_merge_reservation', reservation_mgmt_client_factory)
reservation_command(__name__, 'reservations reservation list-history', reservation_path + 'ReservationOperations.list_revisions', reservation_mgmt_client_factory)
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from azure.mgmt.reservations.models.patch import Patch
from azure.mgmt.reservations.models.split_request import SplitRequest
from azure.mgmt.reservations.models.merge_request import MergeRequest


def cli_reservation_update_reservation(client, reservation_order_id, reservation_id,
applied_scope_type, applied_scopes=None):
if applied_scopes:
patch = Patch(applied_scope_type, [applied_scopes])
else:
patch = Patch(applied_scope_type)
return client.update(reservation_order_id, reservation_id, patch)


def create_resource_id(reservation_order_id, reservation_id):
template = '/providers/Microsoft.Capacity/reservationOrders/{0}/reservations/{1}'
return template.format(reservation_order_id, reservation_id)


def cli_reservation_split_reservation(client, reservation_order_id, reservation_id, quantity_1, quantity_2):
split = SplitRequest([quantity_1, quantity_2], create_resource_id(reservation_order_id, reservation_id))
return client.split(reservation_order_id, split)


def cli_reservation_merge_reservation(client, reservation_order_id, reservation_id_1, reservation_id_2):
merge = MergeRequest([create_resource_id(reservation_order_id, reservation_id_1),
create_resource_id(reservation_order_id, reservation_id_2)])
return client.merge(reservation_order_id, merge)
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
interactions:
- request:
body: null
headers:
Accept: [application/json]
Accept-Encoding: ['gzip, deflate']
CommandName: [reservations reservation-order-id list]
Connection: [keep-alive]
Content-Type: [application/json; charset=utf-8]
User-Agent: [python/3.5.4 (Windows-10-10.0.14393-SP0) requests/2.18.4 msrest/0.4.18
msrest_azure/0.4.16 azurereservationapi/0.1.0 Azure-SDK-For-Python AZURECLI/2.0.21]
accept-language: [en-US]
method: GET
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Capacity/appliedReservations?api-version=2017-11-01
response:
body: {string: '{"id":"/subscriptions/00000000-0000-0000-0000-000000000000/providers/microsoft.capacity/AppliedReservations/default","type":"Microsoft.Capacity/AppliedReservations","name":"default","properties":{"reservationOrderIds":{"value":["/providers/Microsoft.Capacity/reservationorders/f8dc1a06-c795-4513-b387-11416b058f51","/providers/Microsoft.Capacity/reservationorders/8c165114-9981-4e50-9cf2-da5145f1a9a5","/providers/Microsoft.Capacity/reservationorders/86d9870a-bf1e-4635-94c8-b0f08932bc3a"]}}}'}
headers:
cache-control: [no-cache]
content-length: ['492']
content-type: [application/json; charset=utf-8]
date: ['Mon, 06 Nov 2017 19:00:14 GMT']
expires: ['-1']
pragma: [no-cache]
server: [Microsoft-IIS/8.5]
strict-transport-security: [max-age=31536000; includeSubDomains]
transfer-encoding: [chunked]
vary: ['Accept-Encoding,Accept-Encoding']
x-content-type-options: [nosniff]
status: {code: 200, message: OK}
version: 1
Loading

0 comments on commit f061602

Please sign in to comment.