예제 #1
0
    def create(self, request):
        """@description-title Create a new device
        @description Create a new device.

        @param (string) "hostname" [required=false] A hostname. If not given,
        one will be generated.

        @param (string) "domain" [required=false] The domain of the device. If
        not given the default domain is used.

        @param (string) "mac_addresses" [required=true] One or more MAC
        addresses for the device.

        @param (string) "parent" [required=false] The system id of the parent.

        @success (http-status-code) "server-success" 200
        @success (json) "success-json" A JSON object containing the new device.
        @success-example "success-json" [exkey=devices-create] placeholder text

        @error (http-status-code) "400" 400
        @error (content) "bad-param" There was a problem with the given
        parameters.
        """
        form = DeviceWithMACsForm(data=request.data, request=request)
        if form.is_valid():
            device = form.save()
            parent = device.parent
            maaslog.info(
                "%s: Added new device%s", device.hostname,
                "" if not parent else " (parent: %s)" % parent.hostname)
            return device
        else:
            raise MAASAPIValidationError(form.errors)
예제 #2
0
    def create(self, request):
        """Create a new device.

        :param hostname: A hostname. If not given, one will be generated.
        :type hostname: unicode

        :param domain: The domain of the device. If not given the default
            domain is used.
        :type domain: unicode

        :param mac_addresses: One or more MAC addresses for the device.
        :type mac_addresses: unicode

        :param parent: The system id of the parent.  Optional.
        :type parent: unicode
        """
        form = DeviceWithMACsForm(data=request.data, request=request)
        if form.is_valid():
            device = form.save()
            parent = device.parent
            maaslog.info(
                "%s: Added new device%s", device.hostname,
                "" if not parent else " (parent: %s)" % parent.hostname)
            return device
        else:
            raise MAASAPIValidationError(form.errors)
예제 #3
0
 def test_creates_device_with_domain_and_parent(self):
     parent = factory.make_Node()
     hostname = factory.make_name("device")
     mac1 = factory.make_mac_address()
     mac2 = factory.make_mac_address()
     domain = factory.make_Domain()
     form = DeviceWithMACsForm(data=get_QueryDict({
         "hostname":
         hostname,
         "mac_addresses": [mac1, mac2],
         "parent":
         parent.system_id,
         "domain":
         domain.name,
     }),
                               request=self.make_request())
     self.assertTrue(form.is_valid(), dict(form.errors))
     form.save()
     device = get_one(Device.objects.filter(hostname=hostname))
     self.assertThat(device.hostname, Equals(hostname))
     self.assertThat(device.domain, Equals(domain))
     iface = get_one(Interface.objects.filter(mac_address=mac1))
     self.assertThat(iface.node, Equals(device))
     iface = get_one(Interface.objects.filter(mac_address=mac2))
     self.assertThat(iface.node, Equals(device))
예제 #4
0
 def test_creates_device_with_mac(self):
     hostname = factory.make_name("device")
     mac = factory.make_mac_address()
     form = DeviceWithMACsForm(data=get_QueryDict({
         "hostname": hostname,
         "mac_addresses": mac
     }), request=self.make_request())
     self.assertTrue(form.is_valid(), dict(form.errors))
     form.save()
     device = get_one(Device.objects.filter(hostname=hostname))
     self.assertThat(device.hostname, Equals(hostname))
     iface = get_one(Interface.objects.filter(mac_address=mac))
     self.assertThat(iface.node, Equals(device))
예제 #5
0
 def test_contains_mac_addresses_field_and_converts_non_querydict(self):
     form = DeviceWithMACsForm(data={})
     self.assertThat(form.fields, Contains("mac_addresses"))
     self.assertIsInstance(form.data, QueryDict)