Beispiel #1
0
 def get_token(username=options.username,
               password=options.password,
               tenant=options.tenant):
     backend = cache.Backend()
     if tenant:
         token = backend.get("keystone_tenant_endpoint")
         if not token:
             data = {
                 "auth": {
                     "passwordCredentials": {
                         "username": username,
                         "password": password
                     },
                     'tenantName': tenant
                 },
             }
             r = post_http(method='post',
                           url='%s/tokens' %
                           options.keystone_admin_endpoint,
                           data=json.dumps(data))
             if r.status_code == 200 and r.json().get('access', ''):
                 token = r.json().get('access').get("token").get("id")
                 backend.set("keystone_tenant_endpoint", token)
                 return r.json()['access']['token']['id']
             else:
                 return False
         return token
     else:
         token = backend.get("keystone_admin_endpoint")
         if not token:
             data = {
                 "auth": {
                     "passwordCredentials": {
                         "username": options.username,
                         "password": options.password
                     },
                     "tenantName": options.tenant
                 }
             }
             try:
                 # first get token from redis. if not get from keystone
                 r = post_http(method='post',
                               url='%s/tokens' %
                               options.keystone_admin_endpoint,
                               data=json.dumps(data))
                 data = r.json()
                 if r.status_code == 200 and data.get("access"):
                     token = data.get('access').get("token").get("id")
                     if not token:
                         em = "can not get admin token"
                         LOG.exception(em)
                         return False
                     backend.set("keystone_admin_endpoint", token)
                     return token
                 else:
                     return False
             except Exception as e:
                 LOG.exception(e)
                 return False
         return token
Beispiel #2
0
 def sw_add_port(network_id, sw_ip, sw_password, sw_username, token,
                 sw_port):
     try:
         # set l2 switch port to access 4094
         data = {
             "network_id": network_id,
             "sw_ip": sw_ip,
             "sw_pwd": sw_password,
             "sw_user": sw_username,
             "sw_port": sw_port
         }
         headers = {
             'X-Auth-Token': token.strip(),
             'Content-type': 'application/json'
         }
         data = json.dumps(data)
         ret = post_http(url=options.l2_sw_ep, data=data, headers=headers)
         if ret.status_code != 200:
             em = "config l2 switch error. switch ip: <{0}> switch port: <{1}>".format(
                 sw_ip, sw_port)
             LOG.exception(em)
             return False, 500
         return True, 200
     except Exception as e:
         em = "l2 switch config error. switch ip: <{0}>. msg: <{1}>".format(
             sw_ip, e)
         LOG.exception(em)
         return False, 500
Beispiel #3
0
 def sw_add_vtep(sw_ips, network_id, sw_password, sw_username, token):
     try:
         for sw_sdn_ip in sw_ips:
             data = {
                 "network_id": network_id,
                 "sw_ip": sw_sdn_ip,
                 "sw_pwd": sw_password,
                 "sw_user": sw_username
             }
             data = json.dumps(data)
             headers = {
                 'X-Auth-Token': token.strip(),
                 'Content-type': 'application/json'
             }
             ret = post_http(url=options.sdn_sw_ep,
                             headers=headers,
                             data=data)
             if ret.status_code != 200:
                 em = "config sdn switch error. switch ip: <{0}>".format(
                     sw_sdn_ip)
                 LOG.exception(em)
                 return False, 500
         return True, 200
     except Exception as e:
         em = "config sdn switch error. switch ip: <{0}>. msg: <{1}>".format(
             sw_sdn_ip, e)
         LOG.exception(em)
         return False, 500
Beispiel #4
0
 def add_flow2os(ip, mac, network_id):
     data = {"ip": ip,
             "mac": mac,
             "network_id": network_id,
             }
     data = json.dumps(data)
     ret = post_http(url=options.flow_control_ep, data=data)
     if ret.status_code != 200:
         return False, 500
     return True, 200
 def host_charging(user_id, cobbler_sysname, token, user_name, resource_type="1_pm", used=1, order_type=2):
     try:
         ret = PhysicalTable.select_by_userid_sysname(user_id, cobbler_sysname)
         if not ret:
             em = "can not found cobbler system name: <{0}> with user id: <{1}>".format(cobbler_sysname, user_id)
             LOG.exception(em)
             return False, 400
         resource_id = ret.id
         timestamp = time.strftime("%Y-%m-%d %H:%M:%s", time.localtime())
         # charging
         data = {"timestamp": timestamp,
                 "resources": {resource_type: used,
                               },
                 "resource_id": resource_id,
                 "tenant_id": user_id,
                 "_context_project_name": user_name,
                 "_context_user_name": user_name,
                 "resource": "physical machine",
                 "user_id": user_id,
                 "order_type": order_type,
                 }
         data = json.dumps(data)
         headers = {'X-Auth-Token': token.strip(),
                    'Content-type': 'application/json'}
         ret = post_http(url=options.charging_ep, data=data, headers=headers)
         if ret.status_code != 200:
             em = "charging error. system name: <{0}>  user id: <{1}>".format(cobbler_sysname, user_id)
             LOG.exception(em)
             return False, ret.status_code
         LOG.debug("host charging with successful. system name: <{0}>".format(cobbler_sysname))
         return True, 200
     except Exception as e:
         em = "host charging error. cobbler system name: <{0}>. user id: <{1}>. msg: <{2}>".format(cobbler_sysname,
                                                                                                   user_id,
                                                                                                   e)
         LOG.exception(em)
         return False, 500
Beispiel #6
0
    def apply_port(tenant_id, network_id, subnet_id, mac_address):
        """
        method to get network's ip address from openstack 
        :param token: 
        :param network_id: 
        :param subnet_id: 
        :return: 
        """
        try:
            url = '{0}/ports'.format(options.neutron_endpoint)

            if not network_id or not subnet_id or not mac_address:
                em = "invalid parameter network_id or subnet_id  or mac address"
                LOG.exception(em)
                return False, 400
            # get admin token
            token = OpenStackHandle.get_token()
            if not token:
                em = "get admin token error....."
                LOG.exception(em)
                return False, 400
            bind_host = options.overlay_ip.replace(".", "-")
            #
            # data = {"port": {"network_id": network_id,
            #                  "fixed_ips": [{"subnet_id": subnet_id}],
            #                  "mac_address": mac_address,
            #                  "device_owner": "neutron:compute:nova"
            #                  }
            #         }

            data = {
                "port": {
                    "network_id": network_id,
                    "tenant_id": tenant_id,
                    "fixed_ips": [{
                        "subnet_id": subnet_id,
                    }],
                    "mac_address": mac_address,
                    "binding:host_id": bind_host,
                    "device_owner": "compute:nova"
                }
            }
            data = json.dumps(data)
            if not token:
                em = "invalid parameter token"
                LOG.exception(em)
                return False, 500
            header = {
                'Content-type': 'application/json',
                'X-Auth-Token': token.strip()
            }
            ret = post_http(url=url, data=data, headers=header)
            # check is it error
            if ret.status_code != 201:
                em = "openstack error.assign ip address from openstack error. network id: <{0}>  subnet_id: <{1}> msg: <{2}>".format(
                    network_id, subnet_id, ret.json())
                LOG.exception(em)
                return False, ret.status_code
            return True, ret.json()
        except Exception as e:
            em = "apply port from OpenStack error. network id: <{0}> subnet_id: <1>. msg: <{2}>".format(
                network_id, subnet_id, e)
            LOG.exception(em)
            return False, 500