Home » General » First Post!
        

First Post!

Hello,

 

This blog is intended to be a journal of my adventures in hobbytime, from python to Android, Usenet to beef stew. It is my goal to do one post a week, every Monday, for one year. My inspiration for doing this is http://draxa.cx and the delicious food to be found there.

 

Since this is my first post, I want to talk about, uh, what i want to talk about. (How’s that for meta?) I will be writing about:

  • Asterisk, FreePBX, and the Xbox360
  • Burgers!
  • Eggdrops! (The bot, not the soup!)
  • BOFH
  • python and vCloud

 

So, lets start with a teaser:

Heres a nice chunk of my python, for talking to the vCloud API. If you dont know what that means, you can still oogle my python. (Yes, I said that.)

I needed to find a way to list what was running and/or deployed but not running (and still costing us money), so, using “Requests” and “BeautifulSoup4”, two fantastic python modules, and the ingenuity of some co-workers, I came out with:

 

/weetabytes/blob/master/vcloud_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/python2.7
 
# Ryan Weetabix Parkyn 2013 
 
# Free to use with attribution.
 
import requests
import re
from bs4 import BeautifulSoup
 
lstfile = open('C:/dir/to/where/you/want/files/', 'w') # Location of where to write data
 
def main():
    headers = {'Accept': 'application/*+xml;version=1.5', 'Host': 'my fqdn'}
    params = {'pageSize': '150', 'type': 'vApp', 'format': 'idrecords'}
    auth = ('username@vdc', 'password')
    proxies = {"https": "https://proxyURL:80/"} #remove proxies=proxies if no proxy
    s = requests.Session()
    r = s.post('https://vcd1.dell.com/api/sessions', proxies=proxies, headers=headers, \
               verify=True, auth=auth)
    s.headers.update({'x-vcloud-authorization': r.headers['x-vcloud-authorization']})
    t = s.get('https://vcd1.dell.com/api/query?', proxies=proxies, headers=headers, \
              verify=True, params=params)
 
    for line in t.iter_lines():
        if line:
            parse(line)
 
def parse(text=""):
    soup = BeautifulSoup(text, "xml")
    tags = soup.find_all('VAppRecord')
    for tag in tags:
        toprint = []
        for attr in ['name', 'networkName', 'id', 'taskStatusName', 'memoryAllocationMB']:
            if attr in tag.attrs:
                toprint.append((attr, tag[attr]))
 
        nsname = re.sub(" ", "_", toprint[0][1]) #remove spaces from names
        lstfile.write(toprint)
 
main()
lstfile.close()

Click on the link at the top of the code block (“/weetabix/weetabytes/blob/master/vcloud_example.py”) to download it as a text file.

It logs into the API, saves your token, and lists your running machines. Changing line 23 “parse(line)” to “print line” will show you the raw XML, and allow you to see all the categories of information being filtered out by the parser. Some of this may be more useful to you than me.

EDIT: I moved the code to GitHub, and have added a link at the top of the code blocks.

My repo is : https://github.com/weetabix/weetabytes

See you next Monday.

-weetabix

2 Comments

  1. Comment by Jay:

    Great new blog, looking forward to many informative posts!

  2. Comment by weetabix:

    Thanks Jay. Keep reading 🙂

Leave a Reply