Open In App

Microsoft Azure – Building Your Own Azure CLI Extensions

Last Updated : 30 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Pre-requisite: AZURE, Python wheel, PIP

Extensions for the Azure CLI are really useful. This Azure Tip has information on them. You may choose extensions from this list, which is also accessible when you run the Azure CLI command 

az extension list-available —output table.

Steps To Build Azure CLI Extensions

Step 1: The first thing we’ll do is make a new folder in which to store all the files we’ll need for the extension. Give it the name Tipsextension.

Step 2: We’ll construct a few files that make up the extension and place them in the Tips extension folder. Which are.

  • (folder) azext_tipsextension
  • _init__.py
  • setup.cfg
  • setup.py

Step 3: We will now fill up the files’ content. Starting with setup.py, we’ll proceed. The Azure CLI will learn what is in the extension from this file. We’ll enter this code here.

from codecs import open
from setuptools import setup, find_packages
VERSION = "0.0.1"
CLASSIFIERS = [
   'Development Status :: 4 - Beta',
   'Intended Audience :: Developers',
   'Intended Audience :: System Administrators',
   'Programming Language :: Python',
   'Programming Language :: Python :: 2',
   'Programming Language :: Python :: 2.7',
   'Programming Language :: Python :: 3',
   'Programming Language :: Python :: 3.4',
   'Programming Language :: Python :: 3.5',
   'Programming Language :: Python :: 3.6',
   'License :: OSI Approved :: MIT License',
]
DEPENDENCIES = []
setup(
   name='tipsextension',
   version=VERSION,
   description='My CLI extension',
   long_description='An example Azure CLI Extension.',
   license='MIT',
   author='MY CORP.',
   author_email='example@contoso.com',
   url='https://github.com/ORG/REPO',
   classifiers=CLASSIFIERS,
   packages=find_packages(),
   install_requires=DEPENDENCIES
)

Step 4: The setup.cfg file will be the next file we fill up. The package that the CLI may use will be created by Python wheel using this file. There is only one line of code in this little file.

[bdist_wheel]
universal=1

Step 5: The __init .py file located in the azext tips extension folder will be the final file we populate. The real extension functionality is included in this file. Python is used to write it. This code will be added to the file.

from knack.help_files import helps
from azure.cli.core import AzCommandsLoader
helps['gimme tips'] = """
   type: command
   short-summary: Points you to a world of Azure Tips and Tricks.
"""
def showtipsurl():
   print('Azure Tips and Tricks - The Complete List: https://aka.ms/azuretipsandtricks')
class TipsAndTricksCommandsLoader(AzCommandsLoader):
   def __init__(self, cli_ctx=None):
       from azure.cli.core.commands import CliCommandType
       custom_type = CliCommandType(operations_tmpl='azext_tipsextension#{}')
       super(TipsAndTricksCommandsLoader, self).__init__(cli_ctx=cli_ctx,
                                                      custom_command_type=custom_type)
   def load_command_table(self, args):
       with self.command_group('gimme') as g:
           g.custom_command('tips', 'showtipsurl')
       return self.command_table
   def load_arguments(self, _):
       pass
COMMAND_LOADER_CLS = TipsAndTricksCommandsLoader

Output:

Python code

 

Step 6: The application must then be built and assembled into a wheel kit. With the command below, we can accomplish that. The directory must correspond to the directory where all of the extension files are located.

cd /Source/extension/Tipsextension
python setup.py bdist_wheel

Step 7: We may now use the extension. Installing it with the following command will enable us to achieve that.

az extension add --source C:\Source\extension
\tipsextension\dist\tipsextension-
0.0.1-py2.py3-none-any.whl

Output:

Installing the extension

 

Step 8: After installing the extension, you can use the commands below commands to view the help or az gimme tips to view the results.

 az gimme tips -h

Output:

 extensions

 

The process of creating an Azure CLI Extension goes like this. When building an extension, you often need to have greater control and debug it.

Conclusion:

A highly effective method to make the CLI work for you is through Azure CLI extensions. The procedures for creating an Azure CLI extension are not too difficult. The fact that the extensions can only presently be developed in Python is a drawback. Other languages may be offered in the future. In any case, it is fantastic that the CLI may be extended. Go ahead and create your ideal extension and let the community know about it!


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads