access_view.py
from .ecs_sdk import AliyunEcsCreate
from django.http import JsonResponse
from rest_framework.generics import GenericAPIView
# 启动实例机器
class StartInstanceAPIView(GenericAPIView):
def get(self,request):
acc_id = "XXXXXX"
acc_key = "XXXXXX"
region = "cn-hangzhou"
ecs_obj = AliyunEcsCreate(acc_id, acc_key, Region=region)
region_name = request.GET.get("region_name")
instance_id = request.GET.get("instance_id")
# instance_id = 'i-bp11itscfjcatu93k6fb'
ecs_obj.StartInstance(region_name,instance_id)
data = {
"massage":"启动成功",
'instanceID':instance_id
}
print(ecs_obj)
return JsonResponse(data=data,safe=False)
# 停止实例机器
class StopInstanceAPIView(GenericAPIView):
def get(self,request):
acc_id = "XXXXX"
acc_key = "XXXXX"
region = "cn-hangzhou"
ecs_obj = AliyunEcsCreate(acc_id, acc_key, Region=region)
region_name = request.GET.get("region_name")
instance_id = request.GET.get("instance_id")
# instance_id = 'i-bp11itscfjcatu93k6fb'
ecs_obj.StopInstance(region_name,instance_id)
data = {
"massage":"关闭成功",
'instanceID':instance_id
}
print(ecs_obj)
return JsonResponse(data=data,safe=False)
# 查询单个实例详情
class InstanceDetailAPIView(GenericAPIView):
def get(self,request):
"""
:param request:
:return:
"""
acc_id = "XXXX"
acc_key = "XXXXX"
region = "cn-hangzhou"
ecs_obj = AliyunEcsCreate(acc_id, acc_key, Region=region)
# instance_id = 'i-bp11itscfjcatu93k6fb'
instances = ecs_obj.DescribeInstanceList()
print(instances)
return JsonResponse({"massage":"返回详情成功"},safe=False)```