我在开放堆栈版本 2.1 上使用 Django Rest API 无法更新服务器标签,它给出了响应文本:'{"itemNotFound": {"code": 404, "message": "The resource could not be found."}} ' 代码解释是有一个对象数组包含多个服务器对象,循环中带有更新的标签,它将请求并将响应附加到前端数组中
@classmethod
def update_tags(self, params):
check(self, params)
responses = []
try:
headers = {
"X-Auth-Token": params.headers['x-auth-token'],
"Content-type": 'application/json'
}
for server in params.data['payload']:
replace_tags = {"tags": server["tags"]}
response=requests.put(os.environ.get('PROTOCOL')+'://'+os.environ.get('OPENSTACK_HOST')+':'+os.environ.get('COMPUTE_PORT')+'/v2.1/servers/' +server['id']+'/tags', data=json.dumps(replace_tags), headers=headers, verify=False)
responses.append(response)
return responses
except Exception as e:
raise e
从下面写的检查方法传递的参数
def check(self, params):
self.connection = get_openstack_conn(params)
get_openstack_conn 方法
def get_openstack_conn(params):
token = params.META.get("HTTP_X_AUTH_TOKEN")
scope = params.META.get("HTTP_X_SCOPE")
organization = params.META.get("HTTP_X_ORG")
print("environment variables")
print("PROTOCOL = ",os.environ.get('PROTOCOL'))
print("OPENSTACK_HOST = ",os.environ.get('OPENSTACK_HOST'))
print("KEY_STONE_PORT = ",os.environ.get('KEY_STONE_PORT'))
if(scope == 'projectScope'):
connector = openstack.connect(auth_url=os.environ.get('PROTOCOL')+'://'+os.environ.get('OPENSTACK_HOST') +
':'+os.environ.get('KEY_STONE_PORT')+'/v3', verify=False, auth_type="token", token=token, project_id=organization)
elif(scope == 'domainScope'):
connector = openstack.connect(auth_url=os.environ.get('PROTOCOL')+'://'+os.environ.get('OPENSTACK_HOST') +
':'+os.environ.get('KEY_STONE_PORT')+'/v3', verify=False, auth_type="token", token=token, domain_id=organization)
return connector