简体   繁体   中英

Django connect vcenter and list VM

Can't get list of VM with rest API, i managed to get the authentication session but I don't know how to get the ger for the vm list.

I'm looking fot days but i don't have solution, i stored my session to connect on vcenter but i don't know how to get to get the vm list. I try everything.

def vcenter_api(request):

    is_cached = ('value' in request.session)
    context = {}
    if not is_cached:
        ip_address = request.META.get('HTTP_X_FORWARDED_FOR', '')     
        response=requests.post
        ('https://192.168.74.130/rest/com/vmware/cis/session', 
        verify=False,auth=HTTPBasicAuth(api_user, api_pass))
        request.session['value'] = response.json()  
    x = request.session['value']
    vm = request.GET.get('https://192.168.74.130/rest/vcenter/vm/')
    context.update({ 'vm' : vm })
    return render(request, 'ok.html', context)

In the emplate it returns as a value 'none' if instead I take the value of x I get the authentication token. Please help me i don't understand. Thank you so much!!!

For now i solved in this way:

    s = request.session['value']
    if is_cached:
    listvm_request = requests.get('https://192.168.74.130/rest/vcenter/vm/', 
    verify=False, headers={'vmware-api-session-id':s})
    vm = listvm_request.json()

Now i get the session token: 8656eecc59f8759158ac909406c8afdc

and get vm list:

{'value': [{'memory_size_MiB': 1024, 'vm': 'vm-48', 'name': 'VmA1', 
'power_state': 
'POWERED_ON', 'cpu_count': 1}, {'memory_size_MiB': 1024, 'vm': 'vm-49', 'name': 
'VmA2', 'power_state': 'POWERED_OFF', 'cpu_count': 1}]}

But, what is the best way to manage json (maybe serializer)? Thank you so much!

To get data in template i wrote those tags (in template), work well :

<table>
{%  for key in vm.value %}

<tr>
    <td>{{key.name}}</td>
    <td>{{key.power_state}}</td>
</tr>

{% endfor %}
</table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM