Beispiel #1
0
 def do(self, body, uuid):
     # body 是 request.json, 需要删除我添加的 servers 字段
     body.pop('servers')
     url = self.endpoint + '/servers' + '/' + uuid + '/action'
     res = http_request(url, headers=self.headers, body=json.dumps(body), method='POST')
     if res.status == 500:
         raise MyError('Error with server action, reason: %s' % res.read())
     return res
Beispiel #2
0
 def set_user_data(self, data_base64):
     url = self.endpoint + '/servers' + '/' + self.id
     body = {"server": {"user_data": data_base64}}
     res = http_request(url,
                        body=json.dumps(body),
                        headers=self.headers,
                        method='PUT')
     if res.status != 200:
         raise MyError('set user data failed, %s' % res.read())
Beispiel #3
0
 def nova_show(self, uuid):
     url = self.endpoint + '/servers/' + uuid
     res = http_request(url, headers=self.headers, method='GET')
     if res.status == 500:
         raise MyError('Error with show server, reason: %s' % res.read())
     dd = json.loads(res.read())
     logger.info('scloudm response: %s' % dd)
     instance = Instance(dd['server'], token=self.token, endpoint=self.endpoint)
     return instance
Beispiel #4
0
 def set_user_data(self, data_base64):
     url = self.endpoint + '/servers' + '/' + self.id
     body = {
         "server": {
             "user_data": data_base64
         }
     }
     res = http_request(url, body=json.dumps(body), headers=self.headers, method='PUT')
     if res.status != 200:
         raise MyError('set user data failed, %s' % res.read())
Beispiel #5
0
 def nova_list(self, f, t):
     url = self.endpoint + '/servers/detail'
     res = http_request(url, headers=self.headers, method='GET')
     if res.status == 500:
         raise MyError('Error with list server, reason: %s' % res.read())
     dd = json.loads(res.read())
     logger.info('scloudm response: %s' % dd)
     servers_list = dd["servers"]
     instances = [Instance(i, token=self.token, endpoint=self.endpoint) for i in servers_list[f:t]]
     return instances
Beispiel #6
0
 def do(self, body, uuid):
     # body 是 request.json, 需要删除我添加的 servers 字段
     body.pop('servers')
     url = self.endpoint + '/servers' + '/' + uuid + '/action'
     res = http_request(url,
                        headers=self.headers,
                        body=json.dumps(body),
                        method='POST')
     if res.status == 500:
         raise MyError('Error with server action, reason: %s' % res.read())
     return res
Beispiel #7
0
 def nova_show(self, uuid):
     url = self.endpoint + '/servers/' + uuid
     res = http_request(url, headers=self.headers, method='GET')
     if res.status == 500:
         raise MyError('Error with show server, reason: %s' % res.read())
     dd = json.loads(res.read())
     logger.info('scloudm response: %s' % dd)
     instance = Instance(dd['server'],
                         token=self.token,
                         endpoint=self.endpoint)
     return instance
Beispiel #8
0
 def get_flavor(self, flavor_id):
     if not flavor_id:
         return None, None, None
     url = self.endpoint + '/flavors' + '/' + flavor_id
     res = http_request(url, headers=self.headers, method='GET')
     if res.status == 500:
         raise MyError('Error with get flavor, reason: %s' % res.read())
     dd = json.loads(res.read())
     disk = dd["flavor"]["disk"]
     mem = int(dd["flavor"]["ram"] / 1024)
     cpu = dd["flavor"]["vcpus"]
     return disk, mem, cpu
Beispiel #9
0
 def get_flavor(self):
     if not self.flavor_id:
         return None, None, None
     url = self.endpoint + '/flavors' + '/' + self.flavor_id
     res = http_request(url, headers=self.headers, method='GET')
     if res.status == 500:
         raise MyError('Error with get flavor, flavor_id: %s, reason: %s' % (self.flavor_id, res.read()))
     dd = json.loads(res.read())
     disk = dd["flavor"]["disk"]
     mem = int(dd["flavor"]["ram"]/1024)
     cpu = dd["flavor"]["vcpus"]
     return disk, mem, cpu
Beispiel #10
0
 def nova_list(self, f, t):
     url = self.endpoint + '/servers/detail'
     res = http_request(url, headers=self.headers, method='GET')
     if res.status == 500:
         raise MyError('Error with list server, reason: %s' % res.read())
     dd = json.loads(res.read())
     logger.info('scloudm response: %s' % dd)
     servers_list = dd["servers"]
     instances = [
         Instance(i, token=self.token, endpoint=self.endpoint)
         for i in servers_list[f:t]
     ]
     return instances
Beispiel #11
0
def get_tokens():
    body = json.dumps(request.json)
    headers = {"Content-type": "application/json"}
    method = request.method
    try:
        url = urlparse.urljoin('http://' + g.uri + '/', '/v2.0/tokens')
        res = http_request(url, body=body, headers=headers, method=method)
        if res.status == 200 or res.status == 203:
            dd = json.loads(res.read())
            return jsonify(dd), 200
        else:
            return jsonify(openstack_error(res)), res.status
    except Exception, e:
        logger.exception('Error with get token')
        return jsonify({"code": 400, "msg": "%s" % e}), 400
Beispiel #12
0
def get_tokens():
    body = json.dumps(request.json)
    headers = {"Content-type": "application/json"}
    method = request.method
    try:
        url = urlparse.urljoin('http://' + g.uri + '/', '/v2.0/tokens')
        res = http_request(url, body=body, headers=headers, method=method)
        if res.status == 200 or res.status == 203:
            dd = json.loads(res.read())
            return jsonify(dd), 200
        else:
            return jsonify(openstack_error(res)), res.status
    except Exception, e:
        logger.exception('Error with get token')
        return jsonify({"code": 400, "msg": "%s" % e}), 400
Beispiel #13
0
def statistics(endpoint, metric, duration, res_id):
    headers = {"X-Auth-Token": g.token, "Content-type": "application/json"}
    now = int(time.time())
    duration_sec = DUR[duration]
    period_start = msecs2utc(now - 8 * 3600 - duration_sec)
    data1 = {
        "Meteric": metric,
        "RES_ID": res_id,
        "period": PERIOD[duration],
        "period_start": urllib.quote(period_start, ''),
    }
    url = endpoint + "/v2/meters/%(Meteric)s/statistics?q.field=resource_id&q.field=timestamp&q.op=eq&" \
        "q.op=gt&q.type=&q.type=&q.value=%(RES_ID)s&q.value=%(period_start)s&period=%(period)s" % data1
    res = http_request(url, headers=headers, method='GET')
    if res.status == 500:
        raise MyError('Error with get vm statistics, reason: %s' % res.read())
    dd = json.loads(res.read())
    return dd
Beispiel #14
0
def statistics(endpoint, metric, duration, res_id):
    headers = {"X-Auth-Token": g.token, "Content-type":"application/json" }
    now=int(time.time())
    duration_sec = DUR[duration]
    period_start = msecs2utc(now-8*3600-duration_sec)
    data1={
        "Meteric": metric,
        "RES_ID": res_id,
        "period": PERIOD[duration],
        "period_start":urllib.quote(period_start,''),
    }
    url = endpoint + "/v2/meters/%(Meteric)s/statistics?q.field=resource_id&q.field=timestamp&q.op=eq&" \
        "q.op=gt&q.type=&q.type=&q.value=%(RES_ID)s&q.value=%(period_start)s&period=%(period)s" % data1
    res = http_request(url, headers=headers, method='GET')
    if res.status == 500:
        raise MyError('Error with get vm statistics, reason: %s' % res.read())
    dd = json.loads(res.read())
    return dd