Beispiel #1
0
    def _check_zone(self, name, create=False):
        '''Checks whether a zone specified in a source exist in Azure server.

            Note that Azure zones omit end '.' eg: contoso.com vs contoso.com.
            Returns the name if it exists.

            :param name: Name of a zone to checks
            :type  name: str
            :param create: If True, creates the zone of that name.
            :type  create: bool

            :type return: str or None
        '''
        self.log.debug('_check_zone: name=%s', name)
        try:
            if name in self._azure_zones:
                return name
            self._dns_client.zones.get(self._resource_group, name)
            self._azure_zones.add(name)
            return name
        except CloudError as err:
            msg = 'The Resource \'Microsoft.Network/dnszones/{}\''.format(name)
            msg += ' under resource group \'{}\''.format(self._resource_group)
            msg += ' was not found.'
            if msg == err.message:
                # Then the only error is that the zone doesn't currently exist
                if create:
                    self.log.debug('_check_zone:no matching zone; creating %s',
                                   name)
                    create_zone = self._dns_client.zones.create_or_update
                    create_zone(self._resource_group, name, Zone('global'))
                    return name
                else:
                    return
            raise
Beispiel #2
0
    def _check_zone(self, name, create=False):
        '''Checks whether a zone specified in a source exist in Azure server.

            Note that Azure zones omit end '.' eg: contoso.com vs contoso.com.
            Returns the name if it exists.

            :param name: Name of a zone to checks
            :type  name: str
            :param create: If True, creates the zone of that name.
            :type  create: bool

            :type return: str or None
        '''
        self.log.debug('_check_zone: name=%s create=%s', name, create)
        # Check if the zone already exists in our set
        if name in self._azure_zones:
            return name
        # If not, and its time to create, lets do it.
        if create:
            self.log.debug('_check_zone:no matching zone; creating %s', name)
            create_zone = self._dns_client.zones.create_or_update
            create_zone(self._resource_group, name, Zone(location='global'))
            self._azure_zones.add(name)
            return name
        else:
            # Else return nothing (aka false)
            return
Beispiel #3
0
    def test_populate_zone(self):
        provider = self._get_provider()

        zone_list = provider._dns_client.zones.list_by_resource_group
        zone_1 = AzureZone(location='global')
        # This is far from ideal but the
        # zone constructor doesn't let me set it on creation
        zone_1.name = "zone-1"
        zone_2 = AzureZone(location='global')
        # This is far from ideal but the
        # zone constructor doesn't let me set it on creation
        zone_2.name = "zone-2"
        zone_list.return_value = [zone_1, zone_2, zone_1]

        provider._populate_zones()

        # This should be returning two zones since two zones are the same
        self.assertEquals(len(provider._azure_zones), 2)
Beispiel #4
0
def create_dns_zone(client,
                    resource_group_name,
                    zone_name,
                    location='global',
                    tags=None,
                    if_none_match=False):
    from azure.mgmt.dns.models import Zone

    kwargs = {
        'resource_group_name': resource_group_name,
        'zone_name': zone_name,
        'parameters': Zone(location, tags=tags)
    }

    if if_none_match:
        kwargs['if_none_match'] = '*'

    return client.create_or_update(**kwargs)
    def exec_module(self, **kwargs):

        # create a new zone variable in case the 'try' doesn't find a zone
        zone = None
        for key in list(self.module_arg_spec.keys()) + ['tags']:
            setattr(self, key, kwargs[key])

        self.results['check_mode'] = self.check_mode

        # retrieve resource group to make sure it exists
        resource_group = self.get_resource_group(self.resource_group)

        changed = False
        results = dict()

        try:
            self.log('Fetching DNS zone {0}'.format(self.name))
            zone = self.dns_client.zones.get(self.resource_group, self.name)

            # serialize object into a dictionary
            results = zone_to_dict(zone)

            # don't change anything if creating an existing zone, but change if deleting it
            if self.state == 'present':
                changed = False

                update_tags, results['tags'] = self.update_tags(
                    results['tags'])
                if update_tags:
                    changed = True

            elif self.state == 'absent':
                changed = True

        except CloudError:
            # the zone does not exist so create it
            if self.state == 'present':
                changed = True
            else:
                # you can't delete what is not there
                changed = False

        self.results['changed'] = changed
        self.results['state'] = results

        # return the results if your only gathering information
        if self.check_mode:
            return self.results

        if changed:
            if self.state == 'present':
                if not zone:
                    # create new zone
                    self.log('Creating zone {0}'.format(self.name))
                    zone = Zone(location='global', tags=self.tags)
                else:
                    # update zone
                    zone = Zone(location='global', tags=results['tags'])
                self.results['state'] = self.create_or_update_zone(zone)
            elif self.state == 'absent':
                # delete zone
                self.delete_zone()
                # the delete does not actually return anything. if no exception, then we'll assume
                # it worked.
                self.results['state']['status'] = 'Deleted'

        return self.results