def delete(request, fabric_id): try: fabrics = maas.get_fabrics(fabric_id) if not fabrics: sweetify.info(request, _('Data Fabric not Found...'), timer=5000) return redirect('maas:fabrics:index') m = MAAS() fabric_id = fabrics[0]['id'] resp = m.delete(f'/fabrics/{fabric_id}/') if resp.status_code in m.ok: sweetify.sweetalert(request, 'Success', text=_('Data Successfully Deleted'), button='OK', icon='success', timer=200) return redirect('maas:fabrics:index') sweetify.sweetalert(request, 'Warning', icon='warning', text=_(str(resp.text)), timer=5000) except (MAASError, ConnectionError, TimeoutError) as e: sweetify.sweetalert(request, 'Warning', text=str(e), icon='error', button='Ok', timer=5000) return redirect('maas:fabrics:index')
def machine_commission(request, system_id=None): form = CommissionForm(request.POST or None, initial={'system_id': system_id}) if form.is_valid(): data = form.clean() try: m = MAAS() resp = m.post(f'machines/{system_id}/?op=commission', data=data) if resp.status_code in m.ok: return JsonResponse({ 'status': 'success', 'message': _('Commission Succesfully') }) except (MAASError, ConnectionError, TimeoutError) as e: return JsonResponse({'status': 'error', 'message': str(e)}) context = { 'form': form, 'url': reverse('maas:machines:machine_commission', args=[system_id]) } html = render_to_string('partials/form_core.html', request=request, context=context) return JsonResponse({'html': html}, safe=False)
def edit(request, id): try: maas = MAAS() domain = maas.get(f"domains/{id}/").json() form = EditDomainForm(request.POST or None, initial=domain) if form.is_valid(): resp = form.save(domain['id']) if resp.status_code in maas.ok: sweetify.sweetalert(request, 'Success', text=_('Successfully edited domain'), button='Ok', timer=2000) return redirect("maas:dns:index") sweetify.warning(request, _(resp.text), button='Ok', timer=2000) context = { 'title': 'Edit Domain', 'menu_active': 'domains', 'form': form, 'title_submit': 'Save Domain', 'col_size': '4' } except (MAASError, ConnectionError, TimeoutError) as e: sweetify.sweetalert(request, 'Warning', text=str(e), button='Ok', timer=5000) context = None return render(request, 'maas/form.html', context)
def delete(request, id): try: maas = MAAS() resp = maas.delete(f"domains/{id}/") if resp.status_code in maas.ok: sweetify.sweetalert(request, 'Success', icon='success', text=_('Successfully deleted domain'), button='Ok', timer=2000) else: sweetify.sweetalert(request, 'Warning', icon='warning', text=_(resp.text), button='Ok', timer=2000) except (MAASError, ConnectionError, TimeoutError) as e: sweetify.sweetalert(request, 'Warning', text=str(e), button='Ok', timer=5000, icon='error') return redirect("maas:dns:index")
def index(request): try: if settings.WITH_EX_RESPONSE: domains = load_document('domains.json') else: maas = MAAS() domains = maas.get("domains/").json() machine_file = open("hujan_ui/maas/ex_response/domains.json", "w") json.dump(domains, machine_file) machine_file.close() context = { 'title': 'DNS', 'domains': domains, 'menu_active': 'domains', } except (MAASError, ConnectionError, TimeoutError) as e: sweetify.sweetalert(request, 'Warning', text=str(e), button='OK', timer=5000) context = None return render(request, 'maas/dns/index.html', context)
def edit(request, fabric_id): try: fabrics = maas.get_fabrics(fabric_id) form = FabricForm(request.POST or None, initial=fabrics) if form.is_valid(): m = MAAS() data = form.clean() data.update({'id': fabric_id}) resp = m.put(f'/fabrics/{fabric_id}/', data=data) if resp.status_code in m.ok: sweetify.sweetalert(request, 'Success', icon='success', text=_('Successful'), button='OK', timer=2000) return redirect('maas:fabrics:index') sweetify.sweetalert(request, 'Warning', icon='warning', text=_(resp.text), button='Ok', timer=5000) context = {'title': 'Ubah Fabric', 'form': form} except (MAASError, ConnectionError, TimeoutError) as e: sweetify.sweetalert(request, 'Warning', text=str(e), icon='error', button='Ok', timer=5000) context = None return render(request, 'maas/fabrics/add.html', context)
def edit(request, subnet_id): try: subnet = maas.get_subnets(subnet_id) if not subnet: return redirect('maas:subnets:index') form = SubnetForm(request.POST or None, initial=subnet) if form.is_valid(): m = MAAS() data = form.clean() if data['vlan']: vl = maas.get_vlans(int(data['vlan'])) data['vid'] = vl['vid'] data['fabric'] = vl['fabric_id'] resp = m.put(f'subnets/{subnet_id}/', data=data) if resp.status_code in m.ok: sweetify.sweetalert(request, 'Success', text=_('Subnet Update Successfully'), icon='success', timer=2000) return redirect('maas:subnets:index') sweetify.sweetalert(request, 'Warning', text=_(resp.text), icon='warning', timer=5000) except (MAASError, ConnectionError, TimeoutError) as e: sweetify.sweetalert(request, 'Warning', text=str(e), icon='error', timer=5000) form = None context = { 'title': _('Form Edit Subnet'), 'form': form } return render(request, 'maas/subnets/add.html', context)
def onoff_machine(request, system_id): form = ConfirmForm(request.POST or None, initial={'system_id': system_id}) if form.is_valid(): try: cur_machine = maas.get_machines(system_id) m = MAAS() data = form.clean() if cur_machine['power_state'] == 'on': resp = m.post(f'machines/{system_id}/?op=power_off', data) else: resp = m.post(f'machines/{system_id}/?op=power_on', data) if resp.status_code in m.ok: return JsonResponse({ 'status': 'success', 'message': _('Machine Change Power Successfully') }) except (MAASError, ConnectionError, TimeoutError) as e: return JsonResponse({'status': 'error', 'message': str(e)}) context = { 'form': form, 'url': reverse('maas:machines:onoff_machine', args=[system_id]) } html = render_to_string('partials/form_core.html', context, request) return JsonResponse({'html': html}, safe=False)
def delete_machine(request, system_id): try: m = MAAS() resp = m.post(f'machines/{system_id}/', {'system_id': system_id}) if resp.status_code in m.ok: return JsonResponse({ 'status': 'success', 'message': _('Machine Delete Successfully') }) except (MAASError, ConnectionError, TimeoutError) as e: return JsonResponse({'status': 'error', 'message': str(e)})
def delete(request, vlan_id): try: vlan = maas.get_vlans(vlan_id) fid = vlan['fabric_id'] vid = vlan['vid'] m = MAAS() resp = m.delete(f'fabrics/{fid}/vlans/{vid}/') if resp.status_code in m.ok: sweetify.success(request, _('Vlan Deleted Successful'), timer=5000) return redirect('maas:subnets:index') return redirect('maas:subnets:index') except (MAASError, ConnectionError, TimeoutError) as e: sweetify.sweetalert(request, 'Warning', icon='error', text=str(e), button='Ok', timer=5000)
def delete(request, subnet_id): try: subnets = maas.get_subnets() subnet = [s for s in subnets if s['id'] == subnet_id] if subnet[0] is not None: m = MAAS() resp = m.delete(f'subnets/{subnet_id}/') if resp.status_code in m.ok: sweetify.success(request, _('Subnet Deleted Successfully'), timer=2000) return redirect('maas:subnets:index') sweetify.warning(request, _(resp.text), timer=5000) except (MAASError, ConnectionError, TimeoutError) as e: sweetify.sweetalert(request, 'Warning', text=str(e), icon='error', timer=5000) return redirect('maas:subnets:index')
def add(request): try: form = AddMachineForm(request.POST or None) form_ipmi = PowerTypeIPMIForm(request.POST or None) if form.is_valid() and form_ipmi.is_valid(): data = form.clean() ipmi_data = form_ipmi.clean() data.update({'commission': False, 'power_parameters': ipmi_data}) maas = MAAS() resp = maas.post("machines/", data=data) if resp.status_code in maas.ok: sweetify.success(request, _('Successfully added domain'), button='Ok', timer=2000) return redirect("maas:machines:index") sweetify.warning(request, _(resp.text), button='Ok', timer=10000) except (MAASError, ConnectionError, TimeoutError) as e: sweetify.sweetalert(request, 'Warning', icon='error', text=str(e), button='OK', timer=10000) form = None form_ipmi = None context = {'title': 'Add Machine', 'form': form, 'form_ipmi': form_ipmi} return render(request, "maas/machines/add-form.html", context)
def add(request): form = FabricForm(request.POST or None) if form.is_valid(): try: data = form.clean() maas = MAAS() resp = maas.post('fabrics/', data=data) if resp.status_code in maas.ok: sweetify.sweetalert(request, 'Success', text=_('Successfully added fabric'), icon='success', button='Ok', timer=2000) return redirect("maas:fabrics:index") sweetify.sweetalert(request, 'Warning', icon='warning', text=_(resp.text), button='Ok', timer=5000) except (MAASError, ConnectionError, TimeoutError) as e: sweetify.sweetalert(request, 'Warning', text=str(e), icon='error', button='Ok', timer=5000) context = { 'title': 'Add Fabric', 'form': form, } return render(request, 'maas/fabrics/add.html', context)
def delete(request, space_id): try: spaces = maas.get_spaces(space_id) if not spaces: sweetify.warning(request, _('Data Space not Found..'), timer=5000) return redirect('maas.spaces.index') space_id = spaces[0]['id'] m = MAAS() resp = m.delete(f'spaces/{space_id}/') if resp.status_code in m.ok: sweetify.success(request, _('Spaces Deteled Successfully'), timer=2000) sweetify.warning(request, _(resp.text), timer=5000) return redirect('maas:spaces:index') except (MAASError, ConnectionError, TimeoutError) as e: sweetify.sweetalert(request, 'Warning', icon='error', text=str(e), button='Ok', timer=5000)
def add(request): form = SubnetAddForm(request.POST or None) if request.method == 'POST' and form.is_valid(): try: m = MAAS() data = form.clean() resp = m.post('subnets/', data=data) if resp.status_code in m.ok: sweetify.sweetalert(request, 'Success', text=_('Subnet Added Successfully'), icon='success', timer=2000) return redirect('maas:subnets:index') sweetify.sweetalert(request, 'Warning', text=_(resp.text), icon='warning', timer=5000) except (MAASError, ConnectionError, TimeoutError) as e: sweetify.sweetalert(request, 'Error', text=str(e), button='OK', icon='error', timer=5000) context = { 'title': _('Form Add Subnet'), 'form': form } return render(request, 'maas/subnets/add.html', context)
def add(request): form = forms.SpacesForm(request.POST or None) if form.is_valid(): try: data = form.clean() m = MAAS() resp = m.post('spaces/', data=data) if resp.status_code in m.ok: sweetify.success(request, _( 'Spaces Successfully Added'), timer=2000) sweetify.warning(request, _(resp.text), timer=5000) return redirect('index') except (MAASError, ConnectionError, TimeoutError) as e: context = None sweetify.sweetalert(request, 'Warning', icon='error', text=str(e), button='Ok', timer=5000) context = { 'title': _('Form Space'), 'form': form, } return render(request, 'maas/spaces/add.html', context)
def mark_disconnect(request, system_id, id): try: m = MAAS() resp = m.post(f'nodes/{system_id}/interfaces/{id}/?op=disconnect', data={ 'system_id': system_id, 'id': id }) if resp.status_code in m.ok: return JsonResponse({ 'status': 'success', 'message': _('Interfaces Disconnected Successfully'), 'urlhref': reverse('maas:machines:index') }) return JsonResponse({'status': 'error', 'message': _(resp.text)}) except (MAASError, ConnectionError, TimeoutError) as e: return JsonResponse({'status': 'error', 'message': _(str(e))})
def add(request): form = VlanForm(request.POST or None) if form.is_valid(): try: m = MAAS() data = form.clean() fabId = data['fabric_id'] resp = m.post(f'fabrics/{fabId}/vlans/', data=data) if resp.status_code in m.ok: sweetify.success(request, _('Vlan Added Successful'), timer=3000) return redirect('maas:subnets:index') sweetify.warning(request, _(resp.text), timer=5000) except (MAASError, ConnectionError, TimeoutError) as e: sweetify.sweetalert(request, 'Warning', icon='error', text=str(e), button='Ok', timer=5000) context = { 'title': _('Add Vlan'), 'form': form } return render(request, 'maas/vlans/add.html', context)
def edit(request, vlan_id): try: vlan = maas.get_vlans(vlan_id) form = VlanEditForm(request.POST or None, initial=vlan) if form.is_valid(): m = MAAS() data = form.clean() fabId = data['fabric_id'] vid = data['vid'] resp = m.put(f'fabrics/{fabId}/vlans/{vid}/',data=data) if resp.status_code in m.ok: sweetify.success(request, _('Vlan Updated Successful'), timer=3000) return redirect('maas:subnets:index') sweetify.warning(request, _(resp.text), timer=5000) except (MAASError, ConnectionError, TimeoutError) as e: sweetify.sweetalert(request, 'Warning', icon='error', text=str(e), button='Ok', timer=5000) context = { 'title': 'Edit Vlan', 'form': form } return render(request, 'maas/vlans/add.html', context)
def delete_machine(request, system_id): form = ConfirmForm(request.POST or None, initial={'system_id': system_id}) if form.is_valid(): data = form.clean() try: m = MAAS() resp = m.delete(f'machines/{system_id}/', data) if resp.status_code in m.ok: return JsonResponse({ 'status': 'success', 'message': _('Delete Machine Successfully') }) except (MAASError, ConnectionError, TimeoutError) as e: return JsonResponse({'status': 'error', 'message': str(e)}) context = { 'title': 'Delete Machine', 'url': reverse('maas:machines:delete_machine', args=[system_id]), 'form': form } html = render_to_string('partials/form_core.html', context, request) return JsonResponse({'html': html}, safe=False)
def edit(request, space_id): try: spaces = maas.get_spaces(space_id) form = forms.SpacesForm(request.POST or None, initial=spaces) if form.is_valid(): data = form.clean() m = MAAS() resp = m.put(f'/spaces/{space_id}/', data=data) if resp.status_code in m.ok: sweetify.success(request, _( 'Space Successfully Updated'), timer=2000) sweetify.warning(request, _(resp.text), timer=5000) return redirect('maas:spaces:index') except (MAASError, ConnectionError, TimeoutError) as e: form = None sweetify.sweetalert(request, 'Warning', icon='error', button='Ok', timer=5000, text=str(e)) context = { 'title': _('Edit Space'), 'form': form } return render(request, 'maas/spaces/add.html', context)
def edit_physical(request, system_id, id=None): try: physical = maas.get_physicals(system_id, id) form = PhysicalForm(data=request.POST or None, initial=physical) if form.is_valid(): data = form.clean() data.update({'system_id': system_id, 'id': id}) m = MAAS() resp = m.put(f'nodes/{system_id}/interfaces/{id}/', data=data) if resp.status_code in m.ok: return JsonResponse({ 'status': 'success', 'message': _('Edit Interfaces Successfully'), 'urlhref': reverse('maas:machines:index') }) else: return JsonResponse({ 'status': 'error', 'message': _('Failed Edit Interface') }) except (MAASError, ConnectionError, TimeoutError) as e: form = None return JsonResponse({'status': 'error', 'message': _(str(e))}) context = { 'form': form, 'url': reverse('maas:machines:edit_physical', args=[system_id, id]) } html = render_to_string('partials/form_core.html', request=request, context=context) return JsonResponse({'html': html}, safe=False)
class AddMachineForm(forms.Form): hostname = forms.CharField(label='Machine name') domain = forms.ChoiceField() ARCHITECTURE = [ ('amd64/generic', 'amd64/generic'), ] architecture = forms.ChoiceField(choices=ARCHITECTURE) # minimum_kernel = forms.CharField() zone = forms.ChoiceField() resouce_pool = forms.ChoiceField() mac_addresses = forms.CharField(label='MAC Address') POWER_TYPE = [ # ('amt', 'Intel AMT'), # ('apc', 'American Power Conversion (APC) PDU'), # ('dli', 'Digital Loggers, Inc. PDU'), # ('fence_cdu', 'Sentry Switch CDU'), # ('hmc', 'IBM Hardware Management Console (HMC)'), ('ipmi', 'IPMI'), # ('moonshot', 'HP Moonshot - iLO4 (IPMI)'), # ('mscm', 'HP Moonshot - iLO Chassis Manager'), # ('msftocs', 'Microsoft OCS - Chassis Manager'), # ('nova', 'OpenStack Nova'), # ('nova', 'OpenStack Nova'), # ('openbmc', 'OpenBMC Power Driver'), # ('recs_box', 'Christmann RECS|Box Power Driver'), # ('redfish', 'Redfish'), # ('smk15k', 'SeaMicro 15000'), # ('ucsm', 'Cisco UCS Manager'), # ('virsh', 'Virsh (virtual systems)'), # ('vmware', 'VMware'), # ("wedge", "Facebook's Wedge"), # ("rsd", "Rack Scale Design"), # ('vmware', 'VMware'), # TODO : a temporary comment because the machine only supports IPMI types ] power_type = forms.ChoiceField(choices=POWER_TYPE, initial=POWER_TYPE[0][0]) def __init__(self, *args, **kwargs): self.maas = MAAS() super().__init__(*args, **kwargs) self.fields['domain'].choices = self.get_choices_domains() self.fields['zone'].choices = self.get_choices_zones() self.fields['resouce_pool'].choices = self.get_choices_resourcepools() def get_choices_zones(self): resp = self.maas.get(f"/zones/").json() return [ (zone['name'], zone['name']) for zone in resp ] def get_choices_domains(self): resp = self.maas.get(f"/domains/").json() return [ (domain['name'], domain['name']) for domain in resp ] def get_choices_resourcepools(self): resp = self.maas.get(f"/resourcepools/").json() return [ (resource['name'], resource['name']) for resource in resp ]
def __init__(self, *args, **kwargs): self.maas = MAAS() super().__init__(*args, **kwargs) self.fields['domain'].choices = self.get_choices_domains() self.fields['zone'].choices = self.get_choices_zones() self.fields['resouce_pool'].choices = self.get_choices_resourcepools()
def save(self): cleaned_data = super().clean() maas = MAAS() return maas.post("domains/", data=cleaned_data)
def save(self, id): cleaned_data = super().clean() cleaned_data.update({'id': id}) maas = MAAS() return maas.put(f"domains/{id}/", data=cleaned_data)
def __init__(self, *args, **kwargs): self.maas = MAAS() super().__init__(*args, **kwargs)