Exemple #1
0
 def handle(self, request, data):
     try:
         template_id = request.path_info.split('/')[-1]
         try:
             template_db = neutron.networktemplate_get(request, template_id)
         except Exception:
             raise Exception(_("Could not find a template with that ID."))
         # validate the body
         extract_fields_from_body(request, template_db.body)
         templateassignment_dict = {
             'tenant_id': request.user.tenant_id,
             'template_id': template_db.id,
             'stack_id': 'PENDING'
         }
         neutron.networktemplateassignment_create(request,
                                                  **templateassignment_dict)
         args = {
             'stack_name': "auto-%s" % template_db.name,
             'parameters': data,
             'template': template_db.body
         }
         hc = heat.heatclient(request)
         try:
             req = hc.stacks.create(**args)
         except Exception as e:
             raise e
         neutron.networktemplateassignment_update(
             request, request.user.tenant_id,
             **{'stack_id': req['stack']['id']})
     except Exception as e:
         msg = _("Error loading template: %s") % e
         exceptions.handle(self.request, msg, redirect=self.failure_url)
     return True
Exemple #2
0
    def get_data(self, request, stack_id):
        assignment = None
        try:
            assignments = neutron.networktemplateassignment_list(
                request, **{'stack_id': stack_id})
            if not assignments:
                raise Exception('Network template associationg not found.')
            assignment = assignments[0]

            template = neutron.networktemplate_get(request,
                                                   assignment.template_id)

            stack = heat.stack_get(request, stack_id)
            resources = heat.resources_list(request, stack.stack_name)
            if stack.stack_status == 'DELETE_COMPLETE':
                # returning 404 to the ajax call removes the
                # row from the table on the ui
                raise Http404
            rowdata = {
                'template_id':
                assignment.template_id,
                'template_name':
                template.name,
                'stack_id':
                stack_id,
                'heat_stack_name':
                stack.stack_name,
                'description':
                stack.description,
                'status':
                stack.status,
                'stack_status':
                stack.stack_status,
                'stack_status_reason':
                stack.stack_status_reason,
                'resources':
                mark_safe('<br>'.join([
                    ('%s (%s)' % (r.resource_name, r.resource_type)).replace(
                        ' ', '&nbsp;') for r in resources
                ]))
            }
            return rowdata
        except Http404:
            try:
                # remove corresponding network template
                if assignment:
                    neutron.networktemplateassignment_delete(
                        request, assignment.id)
                msg = _('Removed template association for stack_id %s') % \
                    stack_id
                LOG.debug(msg)
                messages.success(request, msg)
            except Exception as e:
                msg = _('Network template association removal failed '
                        'due to %s') % e
                LOG.error(msg)
                messages.error(request, msg)
            raise
        except Exception as e:
            raise
 def __init__(self, request, *args, **kwargs):
     id = request.path_info.split('/')[-1]
     try:
         template = neutron.networktemplate_get(request, id)
     except Exception:
         pass
     super(DetailNetworkTemplate, self).__init__(request, *args, **kwargs)
     if template:
         self.fields['id'].initial = template.id
         self.fields['name'].initial = template.name
         self.fields['body'].initial = template.body
Exemple #4
0
    def get_data(self, request, stack_id):
        assignment = None
        try:
            assignments = neutron.networktemplateassignment_list(
                request, **{'stack_id': stack_id})
            if not assignments:
                raise Exception('Network template associationg not found.')
            assignment = assignments[0]

            template = neutron.networktemplate_get(request,
                                                   assignment.template_id)

            stack = heat.stack_get(request, stack_id)
            resources = heat.resources_list(request, stack.stack_name)
            if stack.stack_status == 'DELETE_COMPLETE':
                # returning 404 to the ajax call removes the
                # row from the table on the ui
                raise Http404
            rowdata = {
                'template_id': assignment.template_id,
                'template_name': template.name,
                'stack_id': stack_id,
                'heat_stack_name': stack.stack_name,
                'description': stack.description,
                'status': stack.status,
                'stack_status': stack.stack_status,
                'stack_status_reason': stack.stack_status_reason,
                'resources': mark_safe('<br>'.join([
                    ('%s (%s)' % (r.resource_name,
                                  r.resource_type)).replace(' ', '&nbsp;')
                    for r in resources]))
            }
            return rowdata
        except Http404:
            try:
                # remove corresponding network template
                if assignment:
                    neutron.networktemplateassignment_delete(
                        request, assignment.id)
                msg = _('Removed template association for stack_id %s') % \
                    stack_id
                LOG.debug(msg)
                messages.success(request, msg)
            except Exception as e:
                msg = _('Network template association removal failed '
                        'due to %s') % e
                LOG.error(msg)
                messages.error(request, msg)
            raise
        except Exception as e:
            raise
Exemple #5
0
def get_stack_topology(request):
    try:
        assign = neutron.networktemplateassignment_get(request,
                                                       request.user.tenant_id)
        networktemplate = neutron.networktemplate_get(request,
                                                      assign.template_id)
    except Exception:
        return {"network_entities": "{}", "network_connections": "{}"}

    hc = heat.heatclient(request)
    # we only want the one that matches the network template
    stacks = [
        s for s in hc.stacks.list(tenant_id=request.user.tenant_id)
        if s.id == assign.stack_id
    ]
    if not stacks:
        # leftover association, delete the assignment
        neutron.networktemplateassignment_delete(request,
                                                 request.user.tenant_id)
        return {"network_entities": "", "network_connections": ""}
    resources = hc.resources.list(assign.stack_id)
    entities = {}
    connections = []
    for res in resources:
        if res.resource_type in ('OS::Neutron::Router', 'OS::Neutron::Subnet'):
            entities[res.physical_resource_id] = {
                'properties': {
                    'name': res.physical_resource_id
                }
            }
        if res.resource_type == 'OS::Neutron::RouterInterface':
            connections.append({
                'source':
                res.physical_resource_id.split(':subnet_id=')[0],
                'destination':
                res.physical_resource_id.split('subnet_id=')[-1],
                'expected_connection':
                'forward'
            })
    resp = {
        'template': networktemplate,
        'assign': assign,
        'network_entities': json.dumps(entities),
        'network_connections': json.dumps(connections),
        'stack_resources': resources,
        'stack': stacks[0]
    }
    return resp
Exemple #6
0
    def clean(self):
        cleaned_data = super(SelectTemplateForm, self).clean()

        def update_cleaned_data(key, value):
            cleaned_data[key] = value
            self.errors.pop(key, None)

        network_template_id = cleaned_data.get('network_templates')

        if network_template_id == 'default':
            msg = _('A template must be selected.')
            raise ValidationError(msg)
        record = neutron.networktemplate_get(self.request, network_template_id)
        if not record:
            msg = _('A template must be selected.')
            raise ValidationError(msg)
        return cleaned_data
Exemple #7
0
def get_stack_topology(request):
    try:
        assign = neutron.networktemplateassignment_get(
            request, request.user.tenant_id)
        networktemplate = neutron.networktemplate_get(
            request, assign.template_id)
    except Exception:
        return {"network_entities": "{}",
                "network_connections": "{}"}

    hc = heat.heatclient(request)
    # we only want the one that matches the network template
    stacks = [s for s in hc.stacks.list(tenant_id=request.user.tenant_id)
              if s.id == assign.stack_id]
    if not stacks:
        # leftover association, delete the assignment
        neutron.networktemplateassignment_delete(request,
                                                 request.user.tenant_id)
        return {"network_entities": "",
                "network_connections": ""}
    resources = hc.resources.list(assign.stack_id)
    entities = {}
    connections = []
    for res in resources:
        if res.resource_type in ('OS::Neutron::Router', 'OS::Neutron::Subnet'):
            entities[res.physical_resource_id] = {
                'properties': {'name': res.physical_resource_id}
            }
        if res.resource_type == 'OS::Neutron::RouterInterface':
            connections.append({
                'source': res.physical_resource_id.split(':subnet_id=')[0],
                'destination':
                    res.physical_resource_id.split('subnet_id=')[-1],
                'expected_connection': 'forward'
            })
    resp = {'template': networktemplate,
            'assign': assign,
            'network_entities': json.dumps(entities),
            'network_connections': json.dumps(connections),
            'stack_resources': resources,
            'stack': stacks[0]}
    return resp
Exemple #8
0
 def __init__(self, *args, **kwargs):
     super(ApplyTemplateForm, self).__init__(*args, **kwargs)
     try:
         template_id = self.request.path_info.split('/')[-1]
         try:
             template_db = neutron.networktemplate_get(
                 self.request, template_id)
         except Exception:
             raise Exception(_("Could not find a template with that ID."))
         try:
             assign = neutron.networktemplateassignment_get(
                 self.request, self.request.user.tenant_id)
             if assign:
                 raise Exception(
                     _("This tenant has already deployed "
                       "a network template."))
         except Exception:
             pass
         template = extract_fields_from_body(self.request, template_db.body)
         # Sorts the parameters in the template.
         parameters = list(template['Parameters'].keys())
         parameters.sort()
         parameters.reverse()
         # Populates the form dynamically with information from the template
         for parameter in parameters:
             self.fields[parameter] = forms.CharField(
                 max_length="255",
                 label=template['Parameters'][parameter]['Label'],
                 initial=findDefault(template['Parameters'][parameter],
                                     'Default'),
                 help_text=template['Parameters'][parameter]['Description'],
                 required=True)
     except Exception as e:
         msg = _("Failed preparing template. You may not have "
                 "permissions to use Heat templates. %s") % e
         exceptions.handle(self.request,
                           msg,
                           redirect=reverse(self.failure_url))
 def get(self, request, template_id):
     result = bsnneutron.networktemplate_get(request, template_id)
     return result
Exemple #10
0
 def get(self, request, template_id):
     result = bsnneutron.networktemplate_get(request, template_id)
     return result