Exemple #1
0
    def delete(self, request, obj_id):
        record = obj_id[:obj_id.find('.')]
        domain = obj_id[obj_id.find('.') + 1:]
        if not domain.endswith('.'):
            domain += '.'

        # First let's make sure that this proxy is really ours to delete.
        existing_domains = [proxy.domain for proxy in get_proxy_list(request)]
        if obj_id not in existing_domains:
            raise Exception(
                "Proxy \'%s\' is to be deleted but is not owned by this view."
                % obj_id)

        if domain == 'wmflabs.org.':
            auth = identity_generic.Password(
                auth_url=base.url_for(request, 'identity'),
                username=getattr(settings, "WMFLABSDOTORG_ADMIN_USERNAME", ''),
                password=getattr(settings, "WMFLABSDOTORG_ADMIN_PASSWORD", ''),
                tenant_name='wmflabsdotorg',
                user_domain_id='default',
                project_domain_id='default')
            c = designateclientv2.Client(session=keystone_session.Session(
                auth=auth))

            # Delete the record from the wmflabsdotorg project. This is needed
            # since wmflabs.org lives in that project and designate (quite
            # reasonably) prevents subdomain deletion elsewhere.
            zoneid = None
            for zone in c.zones.list():
                if zone['name'] == 'wmflabs.org.':
                    zoneid = zone['id']
                    break
            else:
                raise Exception("No zone ID")
            recordsetid = None
            for recordset in c.recordsets.list(zoneid):
                if recordset['type'] == 'A' and recordset[
                        'name'] == record + '.' + domain:
                    recordsetid = recordset['id']
                    break
            else:
                raise Exception("No recordset ID")
            c.recordsets.delete(zoneid, recordsetid)
        else:
            c = designateapi.designateclient(request)
            domainid = None
            for d in c.domains.list():
                if d.name == domain:
                    domainid = d.id
                    break
            else:
                LOG.warn('Woops! Failed domain ID for domain ' + domain)
                raise Exception("No domain ID")
            recordid = None
            for r in c.records.list(domainid):
                if r.name == obj_id and r.type == 'A':
                    recordid = r.id
                    break
            else:
                LOG.warn('Woops! Failed record ID for record ' + record)
                raise Exception("No record ID")

            c.records.delete(domainid, recordid)

        resp = requests.delete(
            base.url_for(request, 'proxy') + '/mapping/' + obj_id)
        if not resp:
            raise Exception("Got status " + resp.status_code)
Exemple #2
0
    def handle(self, request, data):
        proxyip = socket.gethostbyname(
            urlparse.urlparse(base.url_for(request, 'proxy')).hostname)
        if data.get('domain') == 'wmflabs.org.':
            auth = identity_generic.Password(
                auth_url=base.url_for(request, 'identity'),
                username=getattr(settings, "WMFLABSDOTORG_ADMIN_USERNAME", ''),
                password=getattr(settings, "WMFLABSDOTORG_ADMIN_PASSWORD", ''),
                tenant_name='wmflabsdotorg',
                user_domain_id='default',
                project_domain_id='default')
            c = designateclientv2.Client(session=keystone_session.Session(
                auth=auth))

            LOG.warn('Got create client')
            # Create the record in the wmflabsdotorg project. This is needed
            # since wmflabs.org lives in that project and designate prevents
            # subdomain creation elsewhere.
            zoneid = None
            for zone in c.zones.list():
                if zone['name'] == 'wmflabs.org.':
                    zoneid = zone['id']
                    break
            else:
                raise Exception("No zone ID")
            LOG.warn('Got zone ID')
            c.recordsets.create(zoneid,
                                data.get('record') + '.wmflabs.org.', 'A',
                                [proxyip])
        else:
            # TODO: Move this to designate v2 API, reuse some code
            c = designateapi.designateclient(request)
            domainid = None
            for domain in c.domains.list():
                if domain.name == data.get('domain'):
                    domainid = domain.id
                    break
            else:
                raise Exception("No domain ID")
            record = Record(name=data.get('record') + '.' + data.get('domain'),
                            type='A',
                            data=proxyip)
            c.records.create(domainid, record)

        d = {
            "backends": [
                'http://%s:%s' %
                (data.get('backendInstance'), data.get('backendPort'))
            ],
            "domain":
            data.get('record') + '.' + data.get('domain').rstrip('.')
        }

        try:
            resp = requests.put(base.url_for(request, 'proxy') + '/mapping',
                                data=json.dumps(d))
            if resp:
                return True
            else:
                raise Exception("Got status: " + resp.status_code)
        except Exception:
            exceptions.handle(self.request,
                              _("Unable to create proxy: " + resp.text))
            return False