Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(QENG-1905) Adding VM-tagging support via PUT /vm/:hostname endpoint #72

Merged
merged 2 commits into from
Mar 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ $ curl --url vmpooler.company.com/vm/pxpmtoonx7fiqg6

Modify a checked-out VM.

The following are valid PUT parameters and their required data structures:

parameter | description | required structure
--------- | ----------- | ------------------
*lifetime* | VM TTL (in hours) | integer
*tags* | free-form VM tagging | hash

Any modifications can be verified using the [GET /vm/<hostname>](#get-vmhostname) endpoint.

```
$ curl -X PUT -d '{"lifetime":"2"}' --url vmpooler.company.com/vm/fq6qlpjlsskycq6
```
Expand All @@ -177,6 +186,15 @@ $ curl -X PUT -d '{"lifetime":"2"}' --url vmpooler.company.com/vm/fq6qlpjlsskycq
}
```

```
$ curl -X PUT -d '{"tags":{"department":"engineering","user":"sschneid"}}' --url vmpooler.company.com/vm/fq6qlpjlsskycq6
```
```json
{
"ok": true
}
```

##### DELETE /vm/<hostname>

Schedule a checked-out VM for deletion.
Expand Down
49 changes: 41 additions & 8 deletions lib/vmpooler/api/v1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -445,12 +445,21 @@ def validate_date_str(date_str)
status 200
result['ok'] = true

rdata = $redis.hgetall('vmpooler__vm__' + params[:hostname])

result[params[:hostname]] = {}

result[params[:hostname]]['template'] = $redis.hget('vmpooler__vm__' + params[:hostname], 'template')
result[params[:hostname]]['lifetime'] = $redis.hget('vmpooler__vm__' + params[:hostname], 'lifetime') || $config[:config]['vm_lifetime']
result[params[:hostname]]['template'] = rdata['template']
result[params[:hostname]]['lifetime'] = rdata['lifetime'] || $config[:config]['vm_lifetime']
result[params[:hostname]]['running'] = ((Time.now - Time.parse($redis.hget('vmpooler__active__' + result[params[:hostname]]['template'], params[:hostname]))) / 60 / 60).round(2)

rdata.keys.each do |key|
if key.match('^tag\:(.+?)$')
result[params[:hostname]]['tags'] ||= {}
result[params[:hostname]]['tags'][$1] = rdata[key]
end
end

if $config[:config]['domain']
result[params[:hostname]]['domain'] = $config[:config]['domain']
end
Expand Down Expand Up @@ -485,6 +494,8 @@ def validate_date_str(date_str)
put "#{api_prefix}/vm/:hostname/?" do
content_type :json

failure = false

result = {}

status 404
Expand All @@ -495,18 +506,40 @@ def validate_date_str(date_str)
if $redis.exists('vmpooler__vm__' + params[:hostname])
jdata = JSON.parse(request.body.read)

# Validate data payload
jdata.each do |param, arg|
case param
when 'lifetime'
arg = arg.to_i
unless arg.to_i > 0
failure = true
end
when 'tags'
unless arg.is_a?(Hash)
failure = true
end
else
failure = true
end
end

if arg > 0
$redis.hset('vmpooler__vm__' + params[:hostname], param, arg)
if failure
status 400
else
jdata.each do |param, arg|
case param
when 'lifetime'
arg = arg.to_i

status 200
result['ok'] = true
end
$redis.hset('vmpooler__vm__' + params[:hostname], param, arg)
when 'tags'
arg.keys.each do |tag|
$redis.hset('vmpooler__vm__' + params[:hostname], 'tag:' + tag, arg[tag])
end
end
end

status 200
result['ok'] = true
end
end

Expand Down