77 lines
2.8 KiB
Python
77 lines
2.8 KiB
Python
"""
|
|
Copyright (c) 2017 Platform9 Systems Inc.
|
|
Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
not use this file except in compliance with the License. You may obtain
|
|
a copy of the License at
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
WARRANTIES OR CONDITIONS OF ANY KIND, either expressed or implied. See the
|
|
License for the specific language governing permissions and limitations
|
|
under the License.
|
|
"""
|
|
|
|
from azure.common.credentials import ServicePrincipalCredentials
|
|
from azure.mgmt.compute import ComputeManagementClient
|
|
from azure.mgmt.resource import ResourceManagementClient
|
|
from functools import partial
|
|
from oslo_log import log as logging
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
def get_credentials(tenant_id, client_id, client_secret):
|
|
credentials = ServicePrincipalCredentials(
|
|
client_id=client_id, secret=client_secret, tenant=tenant_id)
|
|
return credentials
|
|
|
|
|
|
def _get_client(tenant_id, client_id, client_secret, subscription_id,
|
|
cls=None):
|
|
"""Returns Azure compute resource object for interacting with Azure API
|
|
|
|
:param tenant_id: string, tenant_id from azure account
|
|
:param client_id: string, client_id (application id)
|
|
:param client_secret: string, secret key of application
|
|
:param subscription_id: string, unique identification id of account
|
|
:return: :class:`Resource <Resource>` object
|
|
"""
|
|
credentials = get_credentials(tenant_id, client_id, client_secret)
|
|
client = cls(credentials, subscription_id)
|
|
return client
|
|
|
|
|
|
get_compute_client = partial(_get_client, cls=ComputeManagementClient)
|
|
get_resource_client = partial(_get_client, cls=ResourceManagementClient)
|
|
|
|
|
|
def get_image(compute, resource_group, name):
|
|
"""Return image info from Azure
|
|
"""
|
|
return compute.images.get(resource_group, name)
|
|
|
|
|
|
def check_resource_existence(client, resource_group):
|
|
"""Create if resource group exists in Azure or not
|
|
|
|
:param client: Azure object using ResourceManagementClient
|
|
:param resource_group: string, name of Azure resource group
|
|
:return: True if exists, otherwise False
|
|
:rtype: boolean
|
|
"""
|
|
response = client.resource_groups.check_existence(resource_group)
|
|
return response
|
|
|
|
|
|
def create_resource_group(client, resource_group, region):
|
|
"""Create resource group in Azure
|
|
|
|
:param client: Azure object using ResourceManagementClient
|
|
:param resource_group: string, name of Azure resource group
|
|
:param region: string, name of Azure region
|
|
"""
|
|
response = client.resource_groups.create_or_update(
|
|
resource_group, {'location': region})
|
|
LOG.debug("resource_group response: {0}".format(response))
|
|
LOG.debug("Created Resource Group '{0}' in Azure".format(resource_group))
|