@@ -246,6 +246,14 @@ def _req(self, method, resource, data=None, fmt=None, id=None, params=None,
246246 query_string = params , context = context ,
247247 headers = headers )
248248
249+ def _check_http_response (self , res ):
250+ # Things can go wrong - raise HTTP exc with res code only
251+ # so it can be caught by unit tests
252+ if res .status_int >= webob .exc .HTTPClientError .code :
253+ res .charset = 'utf8'
254+ raise webob .exc .HTTPClientError (explanation = str (res ),
255+ code = res .status_int )
256+
249257 def new_create_request (self , resource , data , fmt = None , id = None ,
250258 subresource = None , context = None ):
251259 return self ._req ('POST' , resource , data , fmt , id = id ,
@@ -523,10 +531,7 @@ def _make_network(self, fmt, name, admin_state_up, **kwargs):
523531 res = self ._create_network (fmt , name , admin_state_up , ** kwargs )
524532 # TODO(salvatore-orlando): do exception handling in this test module
525533 # in a uniform way (we do it differently for ports, subnets, and nets
526- # Things can go wrong - raise HTTP exc with res code only
527- # so it can be caught by unit tests
528- if res .status_int >= webob .exc .HTTPClientError .code :
529- raise webob .exc .HTTPClientError (code = res .status_int )
534+ self ._check_http_response (res )
530535 return self .deserialize (fmt , res )
531536
532537 def _make_subnet (self , fmt , network , gateway , cidr , subnetpool_id = None ,
@@ -551,10 +556,7 @@ def _make_subnet(self, fmt, network, gateway, cidr, subnetpool_id=None,
551556 ipv6_ra_mode = ipv6_ra_mode ,
552557 ipv6_address_mode = ipv6_address_mode ,
553558 set_context = set_context )
554- # Things can go wrong - raise HTTP exc with res code only
555- # so it can be caught by unit tests
556- if res .status_int >= webob .exc .HTTPClientError .code :
557- raise webob .exc .HTTPClientError (code = res .status_int )
559+ self ._check_http_response (res )
558560 return self .deserialize (fmt , res )
559561
560562 def _make_v6_subnet (self , network , ra_addr_mode , ipv6_pd = False ):
@@ -580,18 +582,30 @@ def _make_subnetpool(self, fmt, prefixes, admin=False, **kwargs):
580582 ** kwargs )
581583 # Things can go wrong - raise HTTP exc with res code only
582584 # so it can be caught by unit tests
583- if res .status_int >= webob .exc .HTTPClientError .code :
584- raise webob .exc .HTTPClientError (code = res .status_int )
585+ self ._check_http_response (res )
585586 return self .deserialize (fmt , res )
586587
587588 def _make_port (self , fmt , net_id , expected_res_status = None , ** kwargs ):
588589 res = self ._create_port (fmt , net_id , expected_res_status , ** kwargs )
589590 # Things can go wrong - raise HTTP exc with res code only
590591 # so it can be caught by unit tests
591- if res .status_int >= webob .exc .HTTPClientError .code :
592- raise webob .exc .HTTPClientError (code = res .status_int )
592+ self ._check_http_response (res )
593593 return self .deserialize (fmt , res )
594594
595+ def _make_security_group (self , fmt , name = None , expected_res_status = None ,
596+ project_id = None , is_admin = False ):
597+ name = name or 'sg-{}' .format (uuidutils .generate_uuid ())
598+ project_id = project_id or self ._tenant_id
599+ data = {'security_group' : {'name' : name ,
600+ 'description' : name ,
601+ 'project_id' : project_id }}
602+ sg_req = self .new_create_request ('security-groups' , data , fmt )
603+ sg_res = sg_req .get_response (self .api )
604+ if expected_res_status :
605+ self .assertEqual (expected_res_status , sg_res .status_int )
606+ self ._check_http_response (sg_res )
607+ return self .deserialize (fmt , sg_res )
608+
595609 def _create_qos_rule (self , fmt , qos_policy_id , rule_type , max_kbps = None ,
596610 max_burst_kbps = None , dscp_mark = None , min_kbps = None ,
597611 direction = constants .EGRESS_DIRECTION ,
0 commit comments