def create_security_group(self, req, name, description, rules): """Create security group :param req: the incoming request :param name: security group name :param description: security group description :param rules: security group rules """ try: tenant_id = self.tenant_from_req(req) path = "os-security-groups" path = "/%s/%s" % (tenant_id, path) param_group = { "description": description, "name": name, } body = utils.make_body('security_group', param_group) os_req = self._get_req(req, path=path, content_type="application/json", body=json.dumps(body), method="POST") response_group = os_req.get_response(self.app) secgroup = self.get_from_response( response_group, "security_group", {}) sec_id = secgroup["id"] secgroup["rules"] = [] for rule in rules: port_min, port_max = os_helpers.security_group_rule_port( rule["port"] ) param_rules = { "parent_group_id": sec_id, "ip_protocol": rule["protocol"], "from_port": port_min, "to_port": port_max, "cidr": rule.get("range", "0.0.0.0/0") } body_rules = utils.make_body('security_group_rule', param_rules) path = "/%s/os-security-group-rules" % (tenant_id) os_req_rules = self._get_req(req, path=path, content_type="application/json", body=json.dumps(body_rules), method="POST") response_rules = os_req_rules.get_response(self.app) secrules = self.get_from_response( response_rules, "security_group_rule", {}) secgroup["rules"].append(secrules) ooi_sec = os_helpers.build_security_group_from_nova( [secgroup] ) return ooi_sec[0] except Exception as ex: raise ex
def create_security_group(self, req, name, description, rules): """Create security group :param req: the incoming request :param name: security group name :param description: security group description :param rules: security group rules """ try: tenant_id = self.tenant_from_req(req) path = "os-security-groups" path = "/%s/%s" % (tenant_id, path) param_group = { "description": description, "name": name, } body = utils.make_body('security_group', param_group) os_req = self._get_req(req, path=path, content_type="application/json", body=json.dumps(body), method="POST") response_group = os_req.get_response(self.app) secgroup = self.get_from_response(response_group, "security_group", {}) sec_id = secgroup["id"] secgroup["rules"] = [] for rule in rules: port_min, port_max = os_helpers.security_group_rule_port( rule["port"]) param_rules = { "parent_group_id": sec_id, "ip_protocol": rule["protocol"], "from_port": port_min, "to_port": port_max, "cidr": rule.get("range", "0.0.0.0/0") } body_rules = utils.make_body('security_group_rule', param_rules) path = "/%s/os-security-group-rules" % (tenant_id) os_req_rules = self._get_req(req, path=path, content_type="application/json", body=json.dumps(body_rules), method="POST") response_rules = os_req_rules.get_response(self.app) secrules = self.get_from_response(response_rules, "security_group_rule", {}) secgroup["rules"].append(secrules) ooi_sec = os_helpers.build_security_group_from_nova([secgroup]) return ooi_sec[0] except Exception as ex: raise ex
def create_port(self, req, network_id, device_id): """Add a port to the subnet Returns the port information :param req: the incoming network :param network_id: network id :param device_id: device id """ param_port = { 'net_id': network_id } tenant_id = self.tenant_from_req(req) path = "/%s/servers/%s/os-interface" % (tenant_id, device_id) body = utils.make_body("interfaceAttachment", param_port) os_req = self._get_req(req, path=path, content_type="application/json", body=json.dumps(body), method="POST") response = os_req.get_response(self.app) port = self.get_from_response(response, "interfaceAttachment", {}) for ip in port["fixed_ips"]: return self._build_link(port["net_id"], device_id, ip['ip_address'], ip_id=port["port_id"], mac=port['mac_addr'], state=port["port_state"])
def create_network(self, req, name, cidr, gateway=None, ip_version=None): """Create a network in nova-network. :param req: the incoming request :param name: network resource to manage :param cidr: parameters with values :param gateway: gateway ip :param ip_version: ip version """ net_param = {'label': name, 'cidr': cidr, 'gateway': gateway } path = "os-networks" tenant_id = self.tenant_from_req(req) path = "/%s/%s" % (tenant_id, path) body = utils.make_body('network', net_param) os_req = self._get_req(req, path=path, content_type="application/json", body=json.dumps(body), method="POST") response = os_req.get_response(self.app) net = self.get_from_response( response, "network", {}) ooi_net = self._build_networks([net]) return ooi_net[0]
def test_create_net(self, m_t, m_rq): tenant_id = uuid.uuid4().hex m_t.return_value = tenant_id name = "name_net" net_id = uuid.uuid4().hex cidr = "0.0.0.0" gateway = "0.0.0.1" parameters = {"label": name, "cidr": cidr, "gateway": gateway } resp = fakes_network.create_fake_json_resp( {"network": {"id": net_id, "label": name, "cidr": cidr, "gateway": gateway}}, 200 ) req_mock = mock.MagicMock() req_mock.get_response.return_value = resp m_rq.return_value = req_mock ret = self.helper.create_network(None, name=name, cidr=cidr, gateway=gateway, ) body = utils.make_body('network', parameters) m_rq.assert_called_with( None, method="POST", content_type='application/json', path="/%s/os-networks" % (tenant_id), body=json.dumps(body) ) self.assertEqual(cidr, ret['address']) self.assertEqual(name, ret['name']) self.assertEqual(gateway, ret['gateway']) self.assertEqual(net_id, ret['id'])
def test_create_resource_net_subnet_req(self, m): name = "name_net" net_id = uuid.uuid4().hex state = "ACTIVE" project = "project_id" ip_version = uuid.uuid4().hex cidr = "0.0.0.0" gate_way = "0.0.0.1" subnet_id = uuid.uuid4().hex parameters = {"occi.core.title": name, "occi.core.id": net_id, "occi.network.state": state, "X_PROJECT_ID": project, "org.openstack.network.ip_version": ip_version, "occi.network.address": cidr, "occi.network.gateway": gate_way } resp = fakes.create_fake_json_resp( {"network": {"id": net_id}, "subnet": {"id": subnet_id}}, 201 ) req_mock = mock.MagicMock() req_mock.get_response.return_value = resp m.return_value = req_mock ret = self.helper.create_resource(None, 'networks', parameters) self.assertEqual(net_id, ret["id"]) ret2 = self.helper.create_resource(None, 'subnets', parameters) self.assertEqual(subnet_id, ret2["id"]) m.assert_called_with(None, path="/subnets", content_type="application/json", body=json.dumps(utils.make_body( "subnet", parameters)), method="POST")
def create_port(self, req, network_id, device_id): """Add a port to the subnet Returns the port information :param req: the incoming network :param network_id: network id :param device_id: device id """ param_port = {'net_id': network_id} tenant_id = self.tenant_from_req(req) path = "/%s/servers/%s/os-interface" % (tenant_id, device_id) body = utils.make_body("interfaceAttachment", param_port) os_req = self._get_req(req, path=path, content_type="application/json", body=json.dumps(body), method="POST") response = os_req.get_response(self.app) port = self.get_from_response(response, "interfaceAttachment", {}) for ip in port["fixed_ips"]: return self._build_link(port["net_id"], device_id, ip['ip_address'], ip_id=port["port_id"], mac=port['mac_addr'], state=port["port_state"])
def _make_put_request(self, req, path, parameters): """Create DELETE request This method creates a DELETE Request instance :param req: the incoming request :param path: element location """ body = utils.make_body(None, parameters) return self._get_req(req, path=path, content_type="application/json", body=json.dumps(body), method="PUT")
def _make_create_request(self, req, resource, parameters): """Create CREATE request This method creates a CREATE Request instance :param req: the incoming request :param parameters: parameters with values """ path = "/%s" % resource single_resource = resource[:-1] body = utils.make_body(single_resource, parameters) return self._get_req(req, path=path, content_type="application/json", body=json.dumps(body), method="POST")
def _make_create_request(self, req, resource, parameters, resource_object_name=None): """Create CREATE request This method creates a CREATE Request instance :param req: the incoming request :param parameters: parameters with values :param resource_object_name: in case resource name is different to the response one. """ path = "/%s" % resource if not resource_object_name: resource_object_name = resource[:-1] body = utils.make_body(resource_object_name, parameters) return self._get_req(req, path=path, content_type="application/json", body=json.dumps(body), method="POST")
def test_create_resource_net_subnet_req(self, m): name = "name_net" net_id = uuid.uuid4().hex state = "ACTIVE" project = "project_id" ip_version = uuid.uuid4().hex cidr = "0.0.0.0" gate_way = "0.0.0.1" subnet_id = uuid.uuid4().hex parameters = { "occi.core.title": name, "occi.core.id": net_id, "occi.network.state": state, "X_PROJECT_ID": project, "org.openstack.network.ip_version": ip_version, "occi.network.address": cidr, "occi.network.gateway": gate_way } resp = fakes.create_fake_json_resp( { "network": { "id": net_id }, "subnet": { "id": subnet_id } }, 201) req_mock = mock.MagicMock() req_mock.get_response.return_value = resp m.return_value = req_mock ret = self.helper.create_resource(None, 'networks', parameters) self.assertEqual(net_id, ret["id"]) ret2 = self.helper.create_resource(None, 'subnets', parameters) self.assertEqual(subnet_id, ret2["id"]) m.assert_called_with(None, path="/subnets", content_type="application/json", body=json.dumps( utils.make_body("subnet", parameters)), method="POST")
def create_network(self, req, name, cidr, gateway=None, ip_version=None): """Create a network in nova-network. :param req: the incoming request :param name: network resource to manage :param cidr: parameters with values :param gateway: gateway ip :param ip_version: ip version """ net_param = {'label': name, 'cidr': cidr, 'gateway': gateway} path = "os-networks" tenant_id = self.tenant_from_req(req) path = "/%s/%s" % (tenant_id, path) body = utils.make_body('network', net_param) os_req = self._get_req(req, path=path, content_type="application/json", body=json.dumps(body), method="POST") response = os_req.get_response(self.app) net = self.get_from_response(response, "network", {}) ooi_net = self._build_networks([net]) return ooi_net[0]
def create_server_security_link(self, req, server_id, securitygroup_id): """Create security group link in a server :param req: incoming request :param server_id: server id :param securitygroup_id: segurity group id :return: empty """ tenant_id = self.tenant_from_req(req) path = "/%s/servers/%s/action" % (tenant_id, server_id) sg = self._get_security_group(req, securitygroup_id) if "name" not in sg: raise exception.NotFound("Security group %s not found." % securitygroup_id) param = {"name": sg["name"]} body = utils.make_body('addSecurityGroup', param) os_req = self._get_req(req, path=path, content_type="application/json", body=json.dumps(body), method="POST") os_req.get_response(self.app) return []
def test_create_net(self, m_t, m_rq): tenant_id = uuid.uuid4().hex m_t.return_value = tenant_id name = "name_net" net_id = uuid.uuid4().hex cidr = "0.0.0.0" gateway = "0.0.0.1" parameters = {"label": name, "cidr": cidr, "gateway": gateway} resp = fakes.create_fake_json_resp( { "network": { "id": net_id, "label": name, "cidr": cidr, "gateway": gateway } }, 200) req_mock = mock.MagicMock() req_mock.get_response.return_value = resp m_rq.return_value = req_mock ret = self.helper.create_network( None, name=name, cidr=cidr, gateway=gateway, ) body = utils.make_body('network', parameters) m_rq.assert_called_with(None, method="POST", content_type='application/json', path="/%s/os-networks" % (tenant_id), body=json.dumps(body)) self.assertEqual(cidr, ret['address']) self.assertEqual(name, ret['name']) self.assertEqual(gateway, ret['gateway']) self.assertEqual(net_id, ret['id'])