Skip to content
Permalink
Browse files
Merge branch 'tag_support'
Conflicts:
	setup.py
  • Loading branch information
brogand93 committed Jun 1, 2014
2 parents 11d9344 + f54926c commit 10a2f42295e999b6438472fe9fc87e91de2d398c
Showing 14 changed files with 538 additions and 17 deletions.
@@ -96,28 +96,67 @@ def _create_config_file(config_folder):
)

if configure_instance_type_mapings.lower() in ['yes', 'y']:
instance_type_map = {}
while True:
key = raw_input(
'Insert the AWS EC2 instance type you wish to map: '
)
config_file = _read_user_instance_mappings(config_file)

value = raw_input(
'Insert the name of the instance type you wish to map this to: '
)
configure_resource_type_mapings = raw_input(
'Do you wish to input resource type to resource id mappings'
+ ' for tag support? (Yes/No): '
)

if configure_resource_type_mapings.lower() in ['yes', 'y']:
config_file = _read_user_resource_type_mappings(config_file)

instance_type_map[key] = value
config_file.close()

add_more = raw_input(
'Do you wish to add more mappings? (Yes/No): ')
if add_more.lower() in ['no', 'n']:
break

config_file.write(
'INSTANCE_TYPE_MAP = %s\n' % instance_type_map
def _read_user_instance_mappings(config_file):
instance_type_map = {}
while True:
key = raw_input(
'Insert the AWS EC2 instance type you wish to map: '
)

config_file.close()
value = raw_input(
'Insert the name of the instance type you wish to map this to: '
)

instance_type_map[key] = value

add_more = raw_input(
'Do you wish to add more mappings? (Yes/No): ')
if add_more.lower() in ['no', 'n']:
break

config_file.write(
'INSTANCE_TYPE_MAP = %s\n' % instance_type_map
)

return config_file


def _read_user_resource_type_mappings(config_file):
resource_type_map = {}
while True:
key = raw_input(
'Insert the cloudstack resource id you wish to map: '
)

value = raw_input(
'Insert the cloudstack resource type you wish to map this to: '
)

resource_type_map[key] = value

add_more = raw_input(
'Do you wish to add more mappings? (Yes/No): ')
if add_more.lower() in ['no', 'n']:
break

config_file.write(
'RESOURCE_TYPE_MAP = %s\n' % resource_type_map
)

return config_file


def _create_database():
@@ -8,7 +8,7 @@
from ec2stack.core import Ec2stackError
from ec2stack.services import USERS
from ec2stack.providers.cloudstack import images, instances, keypairs, \
passwords, security_groups, zones, volumes
passwords, security_groups, zones, volumes, tags


DEFAULT = Blueprint('default', __name__)
@@ -45,9 +45,11 @@ def _get_action(action):
security_groups.authenticate_security_group_ingress,
'CreateKeyPair': keypairs.create_keypair,
'CreateSecurityGroup': security_groups.create_security_group,
'CreateTags': tags.create_tags,
'CreateVolume': volumes.create_volume,
'DeleteKeyPair': keypairs.delete_keypair,
'DeleteSecurityGroup': security_groups.delete_security_group,
'DeleteTags': tags.delete_tags,
'DeleteVolume': volumes.delete_volume,
'DescribeAvailabilityZones': zones.describe_zones,
'DescribeImageAttribute': images.describe_image_attribute,
@@ -56,6 +58,7 @@ def _get_action(action):
'DescribeInstances': instances.describe_instances,
'DescribeKeyPairs': keypairs.describe_keypairs,
'DescribeSecurityGroups': security_groups.describe_security_groups,
'DescribeTags': tags.describe_tags,
'DescribeVolumes': volumes.describe_volumes,
'DetachVolume': volumes.detach_volume,
'GetPasswordData': passwords.get_password_data,
@@ -150,6 +150,19 @@ def duplicate_keypair_name():
)


def invalid_resource_id():
"""
Resource with this ID does not exist.
@raise Ec2stackError: Defining a bad request and message.
"""
raise Ec2stackError(
'400',
'InvalidID',
'The specified ID for the resource you are trying to tag is not valid.'
)


def duplicate_security_group():
"""
Duplicate Security Group.
@@ -101,6 +101,9 @@ def _generate_request_url(args, secretkey):
)
)

request_url = request_url.replace('%5B', '[')
request_url = request_url.replace('%5D', ']')

signature = _generate_signature(request_url, secretkey)

request_url += '&signature=%s' % signature
@@ -0,0 +1,152 @@
#!/usr/bin/env python
# encoding: utf-8

"""This module contains functions for handling requests in relation to tags.
"""

from flask import current_app

from ec2stack import helpers, errors
from ec2stack.providers.cloudstack import requester


@helpers.authentication_required
def create_tags():
"""
Create a tag.
@return: Response.
"""
response = _create_tag_request()
return _create_tag_response(response)


def _create_tag_request():
"""
Request to create a tag.
@return: Response.
"""

key = helpers.get('Tag.1.Key')
value = helpers.get('Tag.1.Value')
resource_id = helpers.get('ResourceId.1')

if resource_id in current_app.config['RESOURCE_TYPE_MAP']:
resource_type = current_app.config['RESOURCE_TYPE_MAP'][resource_id]
else:
errors.invalid_request(
str(resource_id) + " not found in configuration")

args = {
'command': 'createTags',
'resourceids': resource_id,
'resourcetype': resource_type,
'tags[0].key': key,
'tags[0].value': value
}

response = requester.make_request_async(args)

return response


def _create_tag_response(response):
"""
Generates a response for a create tag request.
@return: Response.
"""
if 'errortext' in response:
if 'Unable to find resource by id' in response['errortext']:
errors.invalid_resource_id()

return {
'template_name_or_list': 'status.xml',
'response_type': 'CreateTagsResponse',
'return': 'true'
}


@helpers.authentication_required
def delete_tags():
"""
delete a tag.
@return: Response.
"""
response = _delete_tag_request()
return _delete_tag_response(response)


def _delete_tag_request():
"""
Request to delete a tag.
@return: Response.
"""
key = helpers.get('Tag.1.Key')
resource_id = helpers.get('ResourceId.1')

if resource_id in current_app.config['RESOURCE_TYPE_MAP']:
resource_type = current_app.config['RESOURCE_TYPE_MAP'][resource_id]
else:
errors.invalid_request(
str(resource_id) + " not found in configuration")

args = {
'command': 'deleteTags',
'resourceids': resource_id,
'resourcetype': resource_type,
'tags[0].key': key
}

response = requester.make_request_async(args)

return response


def _delete_tag_response(response):
"""
Generates a response for a delete tag request.
@return: Response.
"""
if 'errortext' in response:
if 'Unable to find resource by id' in response['errortext']:
errors.invalid_resource_id()

return {
'template_name_or_list': 'status.xml',
'response_type': 'DeleteTagsResponse',
'return': 'true'
}


@helpers.authentication_required
def describe_tags():
"""
Describe all tags.
@return: Response.
"""
args = {'command': 'listTags'}
response = requester.make_request(args)

return _describe_tags_response(
response
)


def _describe_tags_response(response):
"""
Generates a response for a describe tags request.
@param response: Response from Cloudstack.
@return: Response.
"""
return {
'template_name_or_list': 'tags.xml',
'response_type': 'DescribeTagsResponse',
'response': response
}
@@ -0,0 +1,11 @@
{% extends "response.xml" %}
{% block response_content %}
{% for tag in response.tag %}
<item>
<resourceId>{{tag.resourceid}}</resourceId>
<resourceType>{{tag.resourcetype}}</resourceType>
<key>{{tag.key}}</key>
<value>{{tag.value}}</value>
</item>
{% endfor %}
{% endblock %}
@@ -2,6 +2,10 @@
# encoding: utf-8

import os
<<<<<<< HEAD
=======

>>>>>>> tag_support
from setuptools import setup


@@ -0,0 +1,16 @@
{
"queryasyncjobresultresponse": {
"jobprocstatus": 0,
"created": "2014-06-01T12:00:56+0200",
"cmd": "org.apache.cloudstack.api.command.user.tag.CreateTagsCmd",
"userid": "26a772da-dc25-4f2b-b0f1-e095e3717a30",
"jobstatus": 1,
"jobid": "036bf202-6fe5-4b73-8823-d9e22cd3c63f",
"jobresultcode": 0,
"jobresulttype": "object",
"jobresult": {
"success": true
},
"accountid": "ddbdf378-e8d9-47e0-964b-661d0d8414b8"
}
}
@@ -0,0 +1,16 @@
{
"queryasyncjobresultresponse": {
"jobprocstatus": 0,
"created": "2014-06-01T12:22:48+0200",
"cmd": "org.apache.cloudstack.api.command.user.tag.DeleteTagsCmd",
"userid": "26a772da-dc25-4f2b-b0f1-e095e3717a30",
"jobstatus": 1,
"jobid": "5af5c011-c708-4f36-8a99-caca51590458",
"jobresultcode": 0,
"jobresulttype": "object",
"jobresult": {
"success": true
},
"accountid": "ddbdf378-e8d9-47e0-964b-661d0d8414b8"
}
}
@@ -0,0 +1,17 @@
{
"queryasyncjobresultresponse": {
"jobprocstatus": 0,
"created": "2014-06-01T13:43:16+0200",
"cmd": "org.apache.cloudstack.api.command.user.tag.CreateTagsCmd",
"userid": "26a772da-dc25-4f2b-b0f1-e095e3717a30",
"jobstatus": 2,
"jobid": "3cca108f-53df-4a57-8975-3caf29e9d313",
"jobresultcode": 530,
"jobresulttype": "object",
"jobresult": {
"errorcode": 530,
"errortext": "Unable to find resource by id a0ae3a53-1f4d-42d3-bbb8-7227bb0e0d and type UserVm"
},
"accountid": "ddbdf378-e8d9-47e0-964b-661d0d8414b8"
}
}
@@ -0,0 +1,17 @@
{
"queryasyncjobresultresponse": {
"jobprocstatus": 0,
"created": "2014-06-01T13:43:16+0200",
"cmd": "org.apache.cloudstack.api.command.user.tag.DeleteTagsCmd",
"userid": "26a772da-dc25-4f2b-b0f1-e095e3717a30",
"jobstatus": 2,
"jobid": "3cca108f-53df-4a57-8975-3caf29e9d313",
"jobresultcode": 530,
"jobresulttype": "object",
"jobresult": {
"errorcode": 530,
"errortext": "Unable to find resource by id a0ae3a53-1f4d-42d3-bbb8-7227bb0e0d and type UserVm"
},
"accountid": "ddbdf378-e8d9-47e0-964b-661d0d8414b8"
}
}

0 comments on commit 10a2f42

Please sign in to comment.