Giter Club home page Giter Club logo

container-instances-python-manage's Introduction

page_type languages products description urlFragment
sample
python
azure
This sample shows how to use the Python SDK to create, retreive, and delete Azure Container Instances.
container-instances-python-manage

Manage Azure Container Instances

This sample shows how to use the Python SDK to create, retreive, and delete Azure Container Instances.

Overview

Getting Started

Install prerequisites

  • Python installed. See the the SDK for supported Python versions

  • A virtual environment initialized (optional)

    TIP: using virtual environments is recommended, but not required. To initialize a virtual environment, open a terminal and run the following (replace <myvirtualenv> in the example below with the name of your virtual environment):

    pip install virtualenv
    virtualenv <myvirtualenv>
    cd <myvirtualenv>
    source bin/activate
    
  • Git installed. Download the latest client for your OS platform.

Have an Azure security principal

Get or create a security principal to authenticate with the Azure subscription that the sample will execute against. This can be done a few ways, including:

Setup The Sample

  1. First, clone the repository. To do this, open a terminal, then type the following command:

    git clone https://github.com/Azure-Samples/container-instances-python-manage.git
    
  2. Next, install the Python packages the sample depends on. In the same terminal window, enter the following command:

    cd container-instances-python-manage/src
    pip install -r requirements.txt
    

Run The Sample

Update placeholders

To run the sample, the information for the Azure security principal obtained from the Azure security principal section must be used to update the placeholder values in the sample.

  1. First, open example.py and locate the following codeblock at the top of the file:
azure_context = AzureContext(
      subscription_id = '<SUBSCRIPTION ID>',
      client_id = '<CLIENT ID>',
      client_secret = '<CLIENT SECRET>',
      tenant = '<TENANT ID (AZURE ACTIVE DIRECTORY)>'
   )
  1. Replace the values for <SUBSCRIPTION ID>, <CLIENT ID>, <CLIENT SECRET>, and <TENANT ID (AZURE ACTIVE DIRECTORY)>.

  2. Save the changes made to example.py.

Execute example.py

Using the terminal window from the previous step, or opening a new window and changing the directory to container-instances-python-manage/src, and type the following command:

python example.py

TIP: if you decided to use virtual environnements as recommended, make sure the virtual environment has been activated as shown in step 3 of Getting Started above.

Understand The Code

Define execution context

First, an Azure execution context is created with a subscription id and security principal credentials at the start of example.py.

azure_context = AzureContext(
      subscription_id = '<SUBSCRIPTION ID>',
      client_id = '<CLIENT ID>',
      client_secret = '<CLIENT SECRET>',
      tenant = '<TENANT ID (AZURE ACTIVE DIRECTORY)>'
   )

src/utilities/azure_context.py

This helper class is used to demonstrate the concept of an Azure execution context.

TIP: There is no formal "execution context" object, but is conceptual. It is important to understand particularly when having to execute programs against multiple Azure subscriptions.

class AzureContext(object):
   def __init__(self, subscription_id, client_id, client_secret, tenant):
      self.credentials = ServicePrincipalCredentials(
         client_id = client_id,
         secret = client_secret,
         tenant = tenant
      )
      self.subscription_id = subscription_id

Construct the clients

Next, the clients are constructed. This is done outside main method so the variables can be used in each function without having to define a client for every action.

resource_client = ResourceManagementClient(azure_context.credentials, azure_context.subscription_id)
client = ContainerInstanceManagementClient(azure_context.credentials, azure_context.subscription_id)

Execute activities

main()

The execution steps of the code in example.py performs the following actions:

  • List all the container instances in the subscription
  • Define attributes of the container instance
  • Ensure the resource group is created
  • Create the container
  • Retrieve and show the newly created container instance
  • Delete and clean up

container-instances-python-manage's People

Contributors

microsoftopensource avatar contrast-alexb avatar mmacy avatar msftgits avatar sptramer avatar v-rajagt-zz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.