Example #1
0
def test_create_zone(cloud):
    """
    Unit test for creating a new DNS zone.
    """
    global __zone_id__
    global __num_zones__

    kwargs = {}
    kwargs['domain'] = 'domain.com'
    kwargs['type'] = 'master'
    kwargs['ttl'] = 172800
    kwargs['extra'] = {}

    __num_zones__ = len(Zone.objects(cloud=cloud))
    print "Zones initially %d" % __num_zones__
    print "**** Create DNS zone with domain %s" % kwargs['domain']
    Zone.add(owner=cloud.owner, cloud=cloud, id='', **kwargs)

    zones = Zone.objects()
    for zone in zones:
        if zone.domain == 'domain.com.':
            __zone_id__ = zone.id
            break

    if __zone_id__:
        if __num_zones__ == len(Zone.objects(cloud=cloud)) - 1:
            print "**DNS zone created succesfully on the provider and the DB"
            __num_zones__ += 1
            print "__num_zones__: %d" % __num_zones__
        else:
            raise Exception
Example #2
0
def create_dns_zone(request):
    """
    Create a new DNS zone under a specific cloud.
    ---
    """
    auth_context = auth_context_from_request(request)

    cloud_id = request.matchdict['cloud']
    auth_context.check_perm("cloud", "read", cloud_id)
    auth_context.check_perm("cloud", "create_resources", cloud_id)
    tags = auth_context.check_perm("zone", "add", None)
    # Try to get the specific cloud for which we will create the zone.
    try:
        cloud = Cloud.objects.get(owner=auth_context.owner, id=cloud_id)
    except me.DoesNotExist:
        raise CloudNotFoundError

    params = params_from_request(request)
    new_zone = Zone.add(owner=cloud.owner, cloud=cloud, **params).as_dict()

    if tags:
        resolve_id_and_set_tags(auth_context.owner,
                                'zone',
                                new_zone['id'],
                                tags,
                                cloud_id=cloud_id)

    # Schedule a UI update
    trigger_session_update(auth_context.owner, ['zones'])
    return new_zone
Example #3
0
def create_dns_zone(request):
    """
    Tags: dns
    ---
    Creates a new DNS zone under the given cloud.
    CREATE_RESOURCES permission required on cloud.
    ADD permission required on zone.
    ---
    cloud:
      in: path
      required: true
      type: string
    """
    auth_context = auth_context_from_request(request)

    cloud_id = request.matchdict['cloud']
    auth_context.check_perm("cloud", "read", cloud_id)
    auth_context.check_perm("cloud", "create_resources", cloud_id)
    tags, _ = auth_context.check_perm("zone", "add", None)

    try:
        cloud = Cloud.objects.get(owner=auth_context.owner, id=cloud_id)
    except me.DoesNotExist:
        raise CloudNotFoundError

    params = params_from_request(request)
    new_zone = Zone.add(owner=cloud.owner, cloud=cloud, **params)
    new_zone.assign_to(auth_context.user)

    if tags:
        resolve_id_and_set_tags(auth_context.owner, 'zone', new_zone.id,
                                tags, cloud_id=cloud_id)

    return new_zone.as_dict()
Example #4
0
def create_zone(create_zone_request=None):  # noqa: E501
    """Create zone

    Creates one or more zones on the specified cloud. If async is true, a jobId will be returned. READ permission required on cloud. CREATE_RESOURCES permission required on cloud. CREATE permission required on zone. # noqa: E501

    :param create_zone_request:
    :type create_zone_request: dict | bytes

    :rtype: CreateZoneResponse
    """
    from mist.api.methods import list_resources
    if connexion.request.is_json:
        create_zone_request = CreateZoneRequest.from_dict(connexion.request.get_json())  # noqa: E501
    try:
        auth_context = connexion.context['token_info']['auth_context']
    except KeyError:
        return 'Authentication failed', 401
    params = delete_none(create_zone_request.to_dict())
    try:
        [cloud], total = list_resources(
            auth_context, 'cloud', search=params.pop('cloud'), limit=1)
    except ValueError:
        return 'Cloud does not exist', 404
    try:
        auth_context.check_perm("cloud", "read", cloud.id)
        auth_context.check_perm("cloud", "create_resources", cloud.id)
        tags, _ = auth_context.check_perm("zone", "add", None)
    except PolicyUnauthorizedError:
        return 'You are not authorized to perform this action', 403
    params['domain'] = params.pop('name')
    try:
        new_zone = Zone.add(owner=cloud.owner, cloud=cloud, **params)
    except BadRequestError as e:
        return str(e), 400
    except CloudUnauthorizedError:
        return 'You are not authorized to perform this action', 403
    except ZoneCreationError as e:
        return str(e), 503
    except ZoneListingError as e:
        return str(e), 503
    except CloudUnavailableError as e:
        return str(e), 503
    new_zone.assign_to(auth_context.user)
    if tags:
        resolve_id_and_set_tags(auth_context.owner, 'zone', new_zone.id,
                                tags, cloud_id=cloud.id)
    return new_zone.as_dict()