~smoser/ubuntu/trusty/maas/lp-1172566

« back to all changes in this revision

Viewing changes to src/maasserver/models/nodegroupinterface.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2014-04-03 13:45:02 UTC
  • mto: This revision was merged to the branch mainline in revision 58.
  • Revision ID: package-import@ubuntu.com-20140403134502-8a6wvuqwyuekufh0
Tags: upstream-1.5+bzr2227
ImportĀ upstreamĀ versionĀ 1.5+bzr2227

Show diffs side-by-side

added added

removed removed

Lines of Context:
86
86
 
87
87
    @property
88
88
    def network(self):
89
 
        """Return the network defined by the broadcast address and net mask.
90
 
 
91
 
        If either the broadcast address or the subnet mask is unset, returns
92
 
        None.
93
 
 
94
 
        :return: :class:`IPNetwork`
95
 
        :raise AddrFormatError: If the combination of broadcast address and
 
89
        """Return the network defined by the interface's address and netmask.
 
90
 
 
91
        :return: :class:`IPNetwork`, or `None` if the netmask is unset.
 
92
        :raise AddrFormatError: If the combination of interface address and
96
93
            subnet mask is malformed.
97
94
        """
98
 
        broadcast = self.broadcast_ip
 
95
        ip = self.ip
99
96
        netmask = self.subnet_mask
100
97
        # Nullness check for GenericIPAddress fields is deliberately kept
101
98
        # vague: GenericIPAddressField seems to represent nulls as empty
102
99
        # strings.
103
 
        if broadcast and netmask:
104
 
            return make_network(broadcast, netmask).cidr
 
100
        if netmask:
 
101
            return make_network(ip, netmask).cidr
105
102
        else:
106
103
            return None
107
104
 
116
113
    def clean_network_valid(self):
117
114
        """Validate the network.
118
115
 
119
 
        This validates that the network defined by broadcast_ip and
120
 
        subnet_mask is valid.
 
116
        This validates that the network defined by `ip` and `subnet_mask` is
 
117
        valid.
121
118
        """
122
119
        try:
123
120
            self.network
124
 
        except AddrFormatError, e:
125
 
            # Technically, this should be a global error but it's
126
 
            # more user-friendly to precisely point out where the error
127
 
            # comes from.
128
 
            raise ValidationError(
129
 
                {
130
 
                    'broadcast_ip': [e.message],
131
 
                    'subnet_mask': [e.message],
132
 
                })
 
121
        except AddrFormatError as e:
 
122
            # The interface's address is validated separately.  If the
 
123
            # combination with the netmask is invalid, either there's already
 
124
            # going to be a specific validation error for the IP address, or
 
125
            # the failure is due to an invalid netmask.
 
126
            raise ValidationError({'subnet_mask': [e.message]})
133
127
 
134
128
    def clean_network_not_too_big(self):
135
129
        # If management is not 'UNMANAGED', refuse huge networks.
141
135
                        "Cannot create an address space bigger than "
142
136
                        "a /%d network.  This network is a /%d network." % (
143
137
                            MINIMUM_NETMASK_BITS, network.prefixlen))
144
 
                    raise ValidationError({
145
 
                        'broadcast_ip': [message],
146
 
                        'subnet_mask': [message],
147
 
                    })
 
138
                    raise ValidationError({'subnet_mask': [message]})
148
139
 
149
140
    def clean_network_config_if_managed(self):
150
141
        # If management is not 'UNMANAGED', all the network information
152
143
        if self.management != NODEGROUPINTERFACE_MANAGEMENT.UNMANAGED:
153
144
            mandatory_fields = [
154
145
                'interface',
155
 
                'broadcast_ip',
156
146
                'subnet_mask',
157
147
                'router_ip',
158
148
                'ip_range_low',
170
160
    def clean_ips_in_network(self):
171
161
        """Ensure that the network settings are all congruent.
172
162
 
173
 
        Specifically, it ensures that the interface address, router address,
174
 
        and the address range, all fall within the network defined by the
175
 
        broadcast address and subnet mask.
 
163
        Specifically, it ensures that the router address, the DHCP address
 
164
        range, and the broadcast address if given, all fall within the network
 
165
        defined by the interface's IP address and the subnet mask.
 
166
 
 
167
        If no broadcast address is given, the network's default broadcast
 
168
        address will be used.
176
169
        """
177
170
        network = self.network
178
171
        if network is None:
179
172
            return
180
173
        network_settings = (
181
 
            ("ip", self.ip),
 
174
            ("broadcast_ip", self.broadcast_ip),
182
175
            ("router_ip", self.router_ip),
183
176
            ("ip_range_low", self.ip_range_low),
184
177
            ("ip_range_high", self.ip_range_high),
191
184
        if len(network_errors) != 0:
192
185
            raise ValidationError(network_errors)
193
186
 
 
187
        # Deliberately vague nullness check.  A null IP address seems to be
 
188
        # None in some situations, or an empty string in others.
 
189
        if not self.broadcast_ip:
 
190
            # No broadcast address given.  Set the default.  Set it in string
 
191
            # form; validation breaks if we pass an IPAddress.
 
192
            self.broadcast_ip = unicode(network.broadcast)
 
193
 
194
194
    def clean_fields(self, *args, **kwargs):
195
195
        super(NodeGroupInterface, self).clean_fields(*args, **kwargs)
196
196
        self.clean_network_valid()