Changed pattern on how objects lookup themselves by name and project.
[snaps.git] / snaps / config / network.py
index bc6ae1b..a2d008a 100644 (file)
@@ -13,6 +13,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 import enum
+from neutronclient.common.utils import str2bool
 
 from snaps.openstack.utils import keystone_utils, neutron_utils
 
@@ -52,19 +53,19 @@ class NetworkConfig(object):
 
         self.name = kwargs.get('name')
         if kwargs.get('admin_state_up') is not None:
-            self.admin_state_up = bool(kwargs['admin_state_up'])
+            self.admin_state_up = str2bool(str(kwargs['admin_state_up']))
         else:
             self.admin_state_up = True
 
         if kwargs.get('shared') is not None:
-            self.shared = bool(kwargs['shared'])
+            self.shared = str2bool(str(kwargs['shared']))
         else:
             self.shared = None
 
         self.project_name = kwargs.get('project_name')
 
         if kwargs.get('external') is not None:
-            self.external = bool(kwargs.get('external'))
+            self.external = str2bool(str(kwargs.get('external')))
         else:
             self.external = False
 
@@ -181,7 +182,7 @@ class SubnetConfig(object):
                                 Specify each name server as an IP address
                                 and separate multiple entries with a space.
                                 For example [8.8.8.7 8.8.8.8]
-                                (default '8.8.8.8')
+                                (default [])
         :param host_routes: A list of host route dictionaries for the subnet.
                             For example:
                                 "host_routes":[
@@ -220,10 +221,7 @@ class SubnetConfig(object):
         if 'dns_nameservers' in kwargs:
             self.dns_nameservers = kwargs.get('dns_nameservers')
         else:
-            if self.ip_version == 4:
-                self.dns_nameservers = ['8.8.8.8']
-            else:
-                self.dns_nameservers = list()
+            self.dns_nameservers = list()
 
         self.host_routes = kwargs.get('host_routes')
         self.destination = kwargs.get('destination')
@@ -351,6 +349,9 @@ class PortConfig(object):
                          self.fixed_ips. These values will be directly
                          translated into the fixed_ips dict (optional)
         :param security_groups: One or more security group IDs.
+        :param port_security_enabled: When True, security groups will be
+                                      applied to the port else not
+                                      (default - True)
         :param allowed_address_pairs: A dictionary containing a set of zero or
                                       more allowed address pairs. An address
                                       pair contains an IP address and MAC
@@ -361,6 +362,7 @@ class PortConfig(object):
                              For example, a DHCP agent (optional)
         :param device_id: The ID of the device that uses this port.
                           For example, a virtual server (optional)
+        :param extra_dhcp_opts: k/v of options to use with your DHCP (optional)
         :return:
         """
         if 'port' in kwargs:
@@ -370,7 +372,7 @@ class PortConfig(object):
         self.network_name = kwargs.get('network_name')
 
         if kwargs.get('admin_state_up') is not None:
-            self.admin_state_up = bool(kwargs['admin_state_up'])
+            self.admin_state_up = str2bool(str(kwargs['admin_state_up']))
         else:
             self.admin_state_up = True
 
@@ -378,11 +380,19 @@ class PortConfig(object):
         self.mac_address = kwargs.get('mac_address')
         self.ip_addrs = kwargs.get('ip_addrs')
         self.security_groups = kwargs.get('security_groups')
+
+        if kwargs.get('port_security_enabled') is not None:
+            self.port_security_enabled = str2bool(
+                str(kwargs['port_security_enabled']))
+        else:
+            self.port_security_enabled = None
+
         self.allowed_address_pairs = kwargs.get('allowed_address_pairs')
         self.opt_value = kwargs.get('opt_value')
         self.opt_name = kwargs.get('opt_name')
         self.device_owner = kwargs.get('device_owner')
         self.device_id = kwargs.get('device_id')
+        self.extra_dhcp_opts = kwargs.get('extra_dhcp_opts')
 
         if not self.network_name:
             raise PortConfigError(
@@ -425,16 +435,10 @@ class PortConfig(object):
 
         out = dict()
 
-        project_id = None
-        if self.project_name:
-            keystone = keystone_utils.keystone_client(os_creds)
-            project = keystone_utils.get_project(
-                keystone=keystone, project_name=self.project_name)
-            if project:
-                project_id = project.id
-
+        keystone = keystone_utils.keystone_client(os_creds)
         network = neutron_utils.get_network(
-            neutron, network_name=self.network_name, project_id=project_id)
+            neutron, keystone, network_name=self.network_name,
+            project_name=self.project_name)
         if not network:
             raise PortConfigError(
                 'Cannot locate network with name - ' + self.network_name)
@@ -446,6 +450,11 @@ class PortConfig(object):
         if self.name:
             out['name'] = self.name
         if self.project_name:
+            project = keystone_utils.get_project(
+                keystone=keystone, project_name=self.project_name)
+            project_id = None
+            if project:
+                project_id = project.id
             if project_id:
                 out['tenant_id'] = project_id
             else:
@@ -460,7 +469,16 @@ class PortConfig(object):
             out['fixed_ips'] = fixed_ips
 
         if self.security_groups:
-            out['security_groups'] = self.security_groups
+            sec_grp_ids = list()
+            for sec_grp_name in self.security_groups:
+                sec_grp = neutron_utils.get_security_group(
+                    neutron, sec_grp_name=sec_grp_name,
+                    project_name=self.project_name)
+                if sec_grp:
+                    sec_grp_ids.append(sec_grp.id)
+            out['security_groups'] = sec_grp_ids
+        if self.port_security_enabled is not None:
+            out['port_security_enabled'] = self.port_security_enabled
         if self.allowed_address_pairs and len(self.allowed_address_pairs) > 0:
             out['allowed_address_pairs'] = self.allowed_address_pairs
         if self.opt_value:
@@ -471,6 +489,8 @@ class PortConfig(object):
             out['device_owner'] = self.device_owner
         if self.device_id:
             out['device_id'] = self.device_id
+        if self.extra_dhcp_opts:
+            out['extra_dhcp_opts'] = self.extra_dhcp_opts
         return {'port': out}
 
     def __eq__(self, other):