def create_security_group(**_):
    utils.set_runtime_properties_from_file()
    security_group_name = utils.set_resource_name(_get_security_group_name, 'SECUIRTY_GROUP',
                                             constants.SECURITY_GROUP_KEY, constants.EXISTING_SECURITY_GROUP_KEY,
                                             constants.SECURITY_GROUP_PREFIX)
    if security_group_name is None:
        # Using an existing public ip, so don't create anything
        return

    headers, location, subscription_id = auth.get_credentials()
    resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]
    if constants.SECURITY_GROUP_KEY in ctx.instance.runtime_properties:
        security_group_name = ctx.instance.runtime_properties[constants.SECURITY_GROUP_KEY]
    else:
        random_suffix_value = utils.random_suffix_generator()
        security_group_name = constants.SECURITY_GROUP_PREFIX+random_suffix_value

    security_group_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/Microsoft.Network/networkSecurityGroups/'+security_group_name+'?api-version='+constants.api_version_network

    ctx.logger.info("Creating a new security group: {0}".format(security_group_name))
    security_group_json = _get_security_group_json(location)
    security_group_params = _get_security_group_params(security_group_json)
    response_nsg = requests.put(url=security_group_url, data=security_group_params, headers=headers)
    ctx.instance.runtime_properties[constants.SECURITY_GROUP_KEY] = security_group_name
    return response_nsg.status_code
Example #2
0
def create_vnet(**_):
    utils.set_runtime_properties_from_file()
    vnet_name = utils.set_resource_name(_get_vnet_name, 'VNET',
                                        constants.VNET_KEY,
                                        constants.EXISTING_VNET_KEY,
                                        constants.VNET_PREFIX)
    if vnet_name is None:
        # Using an existing VNET, so don't create anything
        return

    headers, location, subscription_id = auth.get_credentials()
    resource_group_name = ctx.instance.runtime_properties[
        constants.RESOURCE_GROUP_KEY]
    if constants.VNET_KEY not in ctx.instance.runtime_properties:
        ctx.instance.runtime_properties[constants.VNET_KEY] = vnet_name

    create_vnet_url = constants.azure_url + '/subscriptions/' + subscription_id + '/resourceGroups/' + resource_group_name + '/providers/microsoft.network/virtualNetworks/' + vnet_name + '?api-version=' + constants.api_version_network
    vnet_json = _get_vnet_json(vnet_name, location, subscription_id,
                               resource_group_name)
    vnet_params = json.dumps(vnet_json)

    status_code = utils.create_resource(headers, vnet_name, vnet_params,
                                        create_vnet_url, 'VNET')
    ctx.logger.info("{0} is {1}".format(constants.VNET_KEY, vnet_name))
    return status_code
Example #3
0
def create_storage_account(**_):
    utils.set_runtime_properties_from_file()
    storage_account_name = utils.set_resource_name(
        _get_storage_account_name, 'Storage account',
        constants.STORAGE_ACCOUNT_KEY, constants.EXISTING_STORAGE_ACCOUNT_KEY,
        constants.STORAGE_ACCOUNT_PREFIX)
    if storage_account_name is None:
        # Using an existing storage account, so don't create anything
        return constants.ACCEPTED_STATUS_CODE

    headers, location, subscription_id = auth.get_credentials()
    resource_group_name = ctx.instance.runtime_properties[
        constants.RESOURCE_GROUP_KEY]
    if constants.STORAGE_ACCOUNT_KEY not in ctx.instance.runtime_properties:
        ctx.instance.runtime_properties[
            constants.STORAGE_ACCOUNT_KEY] = storage_account_name

    ctx.logger.info(
        "Creating a new storage account: {0}".format(storage_account_name))
    storage_account_url = constants.azure_url + '/subscriptions/' + subscription_id + '/resourceGroups/' + resource_group_name + '/providers/Microsoft.Storage/storageAccounts/' + storage_account_name + '?api-version=' + constants.api_version
    storage_account_params = json.dumps({
        "properties": {
            "accountType": constants.storage_account_type,
        },
        "location": location
    })
    status_code = utils.create_resource(headers, storage_account_name,
                                        storage_account_params,
                                        storage_account_url,
                                        constants.STORAGE_ACCOUNT)
    ctx.logger.info("{0} is {1}".format(constants.STORAGE_ACCOUNT_KEY,
                                        storage_account_name))
    return status_code
def create_resource_group(**_):
    resource_group_name = utils.set_resource_name(_get_resource_group_name, 'Resource group',
                                                  constants.RESOURCE_GROUP_KEY, constants.EXISTING_RESOURCE_GROUP_KEY,
                                                  constants.RESOURCE_GROUP_PREFIX)
    if resource_group_name is None:
        # Using an existing resource group, so don't create anything
        return

    headers, location, subscription_id = auth.get_credentials()

    resource_group_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'?api-version='+constants.api_version_resource_group

    try:
        ctx.logger.info("Creating a new Resource group: {}".format(resource_group_name))
        resource_group_params = json.dumps({"name": resource_group_name, "location": location})
        response_rg = requests.put(url=resource_group_url, data=resource_group_params, headers=headers)
        if response_rg.text:
            ctx.logger.info("create_resource_group {0} response_rg.text is {1}".format(resource_group_name, response_rg.text))
            if utils.request_failed("{0}:{1}".format('create_resource_group', resource_group_name), response_rg):
                raise NonRecoverableError("Resource group {0} could not be created".format(resource_group_name))

        ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY] = resource_group_name
    except:
        ctx.logger.info("Resource Group {0} could not be created".format(resource_group_name))
        raise NonRecoverableError("Resource Group {0} could not be created".format(resource_group_name))
Example #5
0
def create_public_ip(**_):
    public_ip_name = utils.set_resource_name(_get_public_ip_name, 'Public IP',
                                             constants.PUBLIC_IP_KEY, constants.EXISTING_PUBLIC_IP_NAME,
                                             constants.PUBLIC_IP_PREFIX)
    if public_ip_name is None:
        # Using an existing public ip, so don't create anything
        return

    headers, location, subscription_id = auth.get_credentials()
    resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]
    ctx.instance.runtime_properties[constants.PUBLIC_IP_KEY] = public_ip_name

    check_public_ip_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/microsoft.network/publicIPAddresses/'+public_ip_name+'?api-version='+constants.api_version
    create_public_ip_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/microsoft.network/publicIPAddresses/'+public_ip_name+'?api-version='+constants.api_version
    public_ip_params = _get_public_ip_params(location, public_ip_name)
    utils.check_or_create_resource(headers, public_ip_name, public_ip_params, check_public_ip_url, create_public_ip_url, 'public_ip')

    ctx.logger.info("{0} is {1}".format(constants.PUBLIC_IP_KEY, public_ip_name))
def create_resource_group(**_):
    utils.set_runtime_properties_from_file()
    resource_group_name = utils.set_resource_name(_get_resource_group_name, 'Resource group',
                                                  constants.RESOURCE_GROUP_KEY, constants.EXISTING_RESOURCE_GROUP_KEY,
                                                  constants.RESOURCE_GROUP_PREFIX)
    if resource_group_name is None:
        # Using an existing resource group, so don't create anything
        return constants.ACCEPTED_STATUS_CODE

    headers, location, subscription_id = auth.get_credentials()

    resource_group_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'?api-version='+constants.api_version_resource_group

    ctx.logger.info("Creating a new Resource group: {}".format(resource_group_name))
    resource_group_params = json.dumps({"name": resource_group_name, "location": location})
    response_rg = requests.put(url=resource_group_url, data=resource_group_params, headers=headers)
    ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY] = resource_group_name
    return response_rg.status_code
Example #7
0
def create_public_ip(**_):
    utils.set_runtime_properties_from_file()
    public_ip_name = utils.set_resource_name(_get_public_ip_name, 'Public IP',
                                             constants.PUBLIC_IP_KEY, constants.EXISTING_PUBLIC_IP_NAME,
                                             constants.PUBLIC_IP_PREFIX)
    if public_ip_name is None:
        # Using an existing public ip, so don't create anything
        return

    headers, location, subscription_id = auth.get_credentials()
    resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]
    if constants.PUBLIC_IP_KEY not in ctx.instance.runtime_properties:
        ctx.instance.runtime_properties[constants.PUBLIC_IP_KEY] = public_ip_name

    create_public_ip_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/microsoft.network/publicIPAddresses/'+public_ip_name+'?api-version='+constants.api_version_network
    public_ip_params = _get_public_ip_params(location, public_ip_name)
    status_code = utils.create_resource(headers, public_ip_name, public_ip_params, create_public_ip_url, 'public_ip')
    ctx.logger.info("{0} is {1}".format(constants.PUBLIC_IP_KEY, public_ip_name))
    return status_code
Example #8
0
def create_vnet(**_):
    utils.set_runtime_properties_from_file()
    vnet_name = utils.set_resource_name(_get_vnet_name, 'VNET', constants.VNET_KEY, constants.EXISTING_VNET_KEY,
                                        constants.VNET_PREFIX)
    if vnet_name is None:
        # Using an existing VNET, so don't create anything
        return

    headers, location, subscription_id = auth.get_credentials()
    resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]
    if constants.VNET_KEY not in ctx.instance.runtime_properties:
        ctx.instance.runtime_properties[constants.VNET_KEY] = vnet_name 

    create_vnet_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/microsoft.network/virtualNetworks/'+vnet_name+'?api-version='+constants.api_version_network
    vnet_json = _get_vnet_json(vnet_name, location, subscription_id, resource_group_name)
    vnet_params = json.dumps(vnet_json)
    
    status_code = utils.create_resource(headers, vnet_name, vnet_params, create_vnet_url, 'VNET')
    ctx.logger.info("{0} is {1}".format(constants.VNET_KEY, vnet_name))
    return status_code
def create_storage_account(**_):
    utils.set_runtime_properties_from_file()
    storage_account_name = utils.set_resource_name(_get_storage_account_name, 'Storage account',
                      constants.STORAGE_ACCOUNT_KEY, constants.EXISTING_STORAGE_ACCOUNT_KEY,
                      constants.STORAGE_ACCOUNT_PREFIX)
    if storage_account_name is None:
        # Using an existing storage account, so don't create anything
        return constants.ACCEPTED_STATUS_CODE

    headers, location, subscription_id = auth.get_credentials()
    resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]
    if constants.STORAGE_ACCOUNT_KEY not in ctx.instance.runtime_properties:
        ctx.instance.runtime_properties[constants.STORAGE_ACCOUNT_KEY] = storage_account_name

    ctx.logger.info("Creating a new storage account: {0}".format(storage_account_name))
    storage_account_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/Microsoft.Storage/storageAccounts/'+storage_account_name+'?api-version='+constants.api_version
    storage_account_params = json.dumps({"properties": {"accountType": constants.storage_account_type, }, "location":location})
    status_code = utils.create_resource(headers, storage_account_name, storage_account_params, storage_account_url, constants.STORAGE_ACCOUNT)
    ctx.logger.info("{0} is {1}".format(constants.STORAGE_ACCOUNT_KEY, storage_account_name))
    return status_code
Example #10
0
def create_vnet(**_):

    vnet_name = utils.set_resource_name(_get_vnet_name, 'VNET', constants.VNET_KEY, constants.EXISTING_VNET_KEY,
                                        constants.VNET_PREFIX)
    if vnet_name is None:
        # Using an existing VNET, so don't create anything
        return

    headers, location, subscription_id = auth.get_credentials()
    resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]

    if constants.VNET_KEY in ctx.instance.runtime_properties:
        current_subnet_name = ctx.instance.runtime_properties[constants.SUBNET_KEY]
    else:
        ctx.instance.runtime_properties[constants.VNET_KEY] = vnet_name
        current_subnet_name = constants.SUBNET_PREFIX+utils.random_suffix_generator()
        ctx.instance.runtime_properties[constants.SUBNET_KEY] = current_subnet_name

    check_vnet_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/microsoft.network/virtualNetworks/'+vnet_name+'?api-version='+constants.api_version
    create_vnet_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/microsoft.network/virtualNetworks/'+vnet_name+'?api-version='+constants.api_version
    vnet_params = json.dumps({"name": vnet_name, "location": location, "properties": {"addressSpace": {"addressPrefixes": constants.vnet_address_prefixes},"subnets": [{"name": current_subnet_name, "properties": {"addressPrefix": constants.address_prefix}}]}})
    utils.check_or_create_resource(headers, vnet_name, vnet_params, check_vnet_url, create_vnet_url, 'VNET')

    ctx.logger.info("{0} is {1}".format(constants.VNET_KEY, vnet_name))
def create_availability_set(**_):
    utils.set_runtime_properties_from_file()
    availability_set_name = utils.set_resource_name(_get_availability_set_name, 'Availabilty set',
                      constants.AVAILABILITY_SET_KEY, constants.EXISTING_AVAILABILITY_SET_KEY,
                      constants.AVAILABILITY_SET_PREFIX)
    if availability_set_name is None:
        # Using an existing storage account, so don't create anything
        return constants.ACCEPTED_STATUS_CODE
        
    headers, location, subscription_id = auth.get_credentials()
    resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]
    if constants.AVAILABILITY_SET_KEY not in ctx.instance.runtime_properties:
        ctx.instance.runtime_properties[constants.AVAILABILITY_SET_KEY] = availability_set_name

    ctx.logger.info("Creating a new availability set: {0}".format(availability_set_name))
    availability_set_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/Microsoft.Compute/availabilitySets/'+availability_set_name+'?api-version='+constants.api_version
    availability_set_params = json.dumps({ 
           "name": availability_set_name, 
           "type": "Microsoft.Compute/availabilitySets", 
           "location": location
        })
    status_code = utils.create_resource(headers, availability_set_name, availability_set_params, availability_set_url, 'Availability set')
    ctx.logger.info("{0} is {1}".format(constants.AVAILABILITY_SET_KEY, availability_set_name))
    return status_code