def _check_security_group(self, name): """Checks if the security group exists. :param str name: name of the security group :return: str - security group id of the security group :raises: `SecurityGroupError` if group does not exist """ connection = self._connect() filters = {} if self._vpc: filters = {'vpc-id': self._vpc_id} security_groups = connection.get_all_security_groups(filters=filters) matching_groups = [ group for group in security_groups if name in [group.name, group.id] ] if len(matching_groups) == 0: raise SecurityGroupError( "the specified security group %s does not exist" % name) elif len(matching_groups) == 1: return matching_groups[0].id elif self._vpc and len(matching_groups) > 1: raise SecurityGroupError( "the specified security group name %s matches " "more than one security group" % name)
def _check_security_groups(self, names): """ Raise an exception if any of the named security groups does not exist. :param List[str] groups: List of security group names :raises: `SecurityGroupError` if group does not exist """ self._init_os_api() log.debug("Checking existence of security group(s) %s ...", names) try: # python-novaclient < 8.0.0 security_groups = self.nova_client.security_groups.list() existing = set(sg.name for sg in security_groups) except AttributeError: security_groups = self.neutron_client.list_security_groups( )['security_groups'] existing = set(sg[u'name'] for sg in security_groups) # TODO: We should be able to create the security group if it # doesn't exist and at least add a rule to accept ssh access. # Also, we should be able to add new rules to a security group # if needed. nonexisting = set(names) - existing if nonexisting: raise SecurityGroupError( "Security group(s) `{0}` do not exist".format( ', '.join(nonexisting))) # if we get to this point, all sec groups exist return True
def _check_security_group(self, name): """Checks if the security group exists. :param str name: name of the security group :raises: `SecurityGroupError` if group does not exist """ connection = self._connect() security_groups = connection.get_all_security_groups() if not security_groups: raise SecurityGroupError( "the specified security group %s does not exist" % name) security_groups = dict((s.name, s) for s in security_groups) if name not in security_groups: raise SecurityGroupError( "the specified security group %s does not exist" % name)
def _check_security_group(self, name): """ Checks if the security group exists. TODO: if the configuration file specify some rules, ensure that the security group has those rules, and if the security group does not exists, create it. """ connection = self._connect() security_groups = connection.get_all_security_groups() if not security_groups: raise SecurityGroupError( "the specified security group %s does not exist" % name) security_groups = dict((s.name, s) for s in security_groups) if name not in security_groups: raise SecurityGroupError( "the specified security group %s does not exist" % name)
def _check_security_group(self, name): """Checks if the security group exists. :param str name: name of the security group :raises: `SecurityGroupError` if group does not exist """ security_groups = self.client.security_groups.list() # TODO: We should be able to create the security group if it # doesn't exist and at least add a rule to accept ssh access. # Also, we should be able to add new rules to a security group # if needed. if name not in [sg.name for sg in security_groups]: raise SecurityGroupError( "the specified security group %s does not exist" % name)