示例#1
0
    def test_createtenant(self, moDir, tenantname):
        """
        create a tenant and commit it
        """
        dcid = str(time.time()).replace('.', '')

        polUni = cobra.model.pol.Uni('')
        tenant = cobra.model.fv.Tenant(polUni, tenantname[0])

        configRequest = cobra.mit.request.ConfigRequest()
        configRequest.addMo(tenant)
        configRequest.subtree = 'full'
        configRequest.id = dcid

        r = moDir.commit(configRequest)
        assert r.status_code == requests.codes.ok

        if moDir._accessImpl._session.formatType == cobra.mit.session.AbstractSession.XML_FORMAT:
            mos = fromXMLStr(r.content)
        else:
            mos = fromJSONStr(r.content)

        mo = mos[0]
        assert len(mos) > 0
        assert str(mo.dn) == str(tenant.dn)
        assert len(list(mo.children)) >= 1
示例#2
0
    def test_createtenant(self, moDir, apic, tenantname, getResponseMock):
        """
        create a tenant and commit it
        """
        tenantname = tenantname[0]
        if apic[0] == 'http://mock':
            getResponseMock.start()

        dcid = str(time.time()).replace('.', '')

        polUni = cobra.model.pol.Uni('')
        tenant = cobra.model.fv.Tenant(polUni, tenantname)
        bd = cobra.model.fv.BD(tenant, 'b')
        ap = cobra.model.fv.Ap(tenant, 'a')

        configRequest = cobra.mit.request.ConfigRequest()
        configRequest.addMo(tenant)
        configRequest.subtree = 'full'
        configRequest.id = dcid

        r = moDir.commit(configRequest)
        logger.debug('commit response {0}'.format(r.content))
        assert r.status_code == 200

        mos = fromJSONStr(str(r.text))
        mo = mos[0]
        logger.debug('r.content: {0}'.format(r.content))
        assert len(mos) > 0
        assert str(mo.dn) == str(tenant.dn)
        assert len(list(mo.children)) >= 2  # expect at least fvBD and fvAp
        assert str(mo.BD['b'].dn) == 'uni/tn-{0}/BD-b'.format(tenantname)
        assert str(mo.ap['a'].dn) == 'uni/tn-{0}/ap-a'.format(tenantname)
        if apic[0] == 'http://mock':
            getResponseMock.stop()
示例#3
0
文件: codec.py 项目: bischatt78/cobra
    def fromStr(self, moStr):
        """Convert an json formatted string into mo.

        Args:
          moStr (str): mo in json format.

        Returns:
          list: A list of managed objects converted from the json.
        """
        return fromJSONStr(moStr)
示例#4
0
    def fromStr(self, moStr):
        """Convert an json formatted string into mo.

        Args:
          moStr (str): mo in json format.

        Returns:
          list: A list of managed objects converted from the json.
        """
        return fromJSONStr(moStr)
 def post_sync_wait(self, request, timeout=180):
     """Mimics the behavior of this class's post method but additionally
     waits and polls request state if in progress for a configurable amount
     of time
     Args:
         request (ConfigRequest): ConfigRequest object
         timeout : time to poll for the request in progress to complete
     Return:
         requests.response
     """
     url = request.getUrl(self._session)
     rsp = self._requests.post(url, **request.requestargs(self._session))
     if rsp.status_code >= requests.codes.bad:
         return self.__parseError(rsp, CommitError, rsp.status_code)
     elif rsp.status_code < requests.codes.ok:
         # Here we look and poll for the status of a request in progress
         re_pat = r'Request in progress, please check state using URL: /api/checkRequestState.xml\?id=(?P<id>[0-9]+)'
         m = re.search(re_pat, rsp.text)
         if not m:
             return self.__parseError(rsp, CommitError, rsp.status_code)
         request_id = m.groupdict()['id']
         crs_query = CheckRequestStateQuery()
         crs_query.requestId = request_id
         crs_rsp = self._get(crs_query)
         request_complete = False
         request_success = False
         start = int(time())
         # Begin polling
         while (int(time()) - start) < timeout:
             if self._session.refreshTimeoutSeconds < 60:
                 # Refresh our session if we are close to timing out
                 self.refreshSession()
             crs_rsp = self._get(crs_query)
             if self._session.formatType == AbstractSession.XML_FORMAT:
                 xRsp = fromXMLStr(crs_rsp.text, tree_only=True)
                 status_code = xRsp.getchildren()[0].attrib['code']
             else:
                 jRsp = fromJSONStr(crs_rsp.text, tree_only=True)
                 status_code = jRsp['imdata'][0]['status']['attributes'][
                     'code']
             if status_code < requests.codes.ok:
                 sleep(1)
             else:
                 request_complete = True
                 request_success = True if status_code == requests.codes.ok else False
                 break
         if not request_complete or not request_success:
             crs_rsp = self._get(crs_query)
             return self.__parseError(crs_rsp, CommitError,
                                      crs_rsp.status_code)
         else:
             rsp = crs_rsp
     return rsp
 def __parseResponse(self, rsp):
     if self._session.formatType == AbstractSession.XML_FORMAT:
         return fromXMLStr(rsp.text)
     return fromJSONStr(rsp.text)
示例#7
0
 def __parseResponse(self, rsp):
     if self._session.formatType == AbstractSession.XML_FORMAT:
         return fromXMLStr(rsp.text)
     return fromJSONStr(rsp.text)