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
Issue: LIBCLOUD-477 Add list/delete/create network (VPC) calls within EC... #203
Changes from 1 commit
6d6c2c7
e0872c1
7777034
b5a105a
e35c989
78e45c4
07c9f14
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1236,6 +1236,131 @@ def ex_describe_keypair(self, name): | |
'keyFingerprint': fingerprint | ||
} | ||
|
||
def ex_list_networks(self): | ||
""" | ||
Return all private virtual private cloud (VPC) networks | ||
|
||
:return: list of network dicts | ||
:rtype: ``list`` | ||
""" | ||
params = {'Action': 'DescribeVpcs'} | ||
|
||
result = self.connection.request(self.path, | ||
params=params.copy()).object | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Certainly isn't, just came over with a copy paste from a different call :). |
||
|
||
# The list which we return | ||
networks = [] | ||
for element in findall(element=result, | ||
xpath='vpcSet/item', | ||
namespace=NAMESPACE): | ||
|
||
# Get the network id | ||
vpc_id = findtext(element=element, | ||
xpath='vpcId', | ||
namespace=NAMESPACE) | ||
|
||
# Get tags | ||
tags = dict((findtext(element=item, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like a regular for loop would make this couple of lines a bit more readable. |
||
xpath='key', | ||
namespace=NAMESPACE), | ||
findtext(element=item, | ||
xpath='value', | ||
namespace=NAMESPACE)) | ||
for item in findall(element=element, | ||
xpath='tagSet/item', | ||
namespace=NAMESPACE)) | ||
|
||
# Set our name is the Name key/value if available | ||
# If we don't get anything back then use the vpc_id | ||
name = tags.get('Name', vpc_id) | ||
|
||
networks.append({'vpc_id': vpc_id, | ||
'name': name, | ||
'state': findtext(element=element, | ||
xpath='state', | ||
namespace=NAMESPACE), | ||
'cidr_block': findtext(element=element, | ||
xpath='cidrBlock', | ||
namespace=NAMESPACE), | ||
'dhcp_options_id': findtext(element=element, | ||
xpath='dhcpOptionsId', | ||
namespace=NAMESPACE), | ||
'tags': tags, | ||
'instance_tenancy': findtext(element=element, | ||
xpath= | ||
'instance_tenancy', | ||
namespace=NAMESPACE), | ||
'is_default': findtext(element=element, | ||
xpath='isDefault', | ||
namespace=NAMESPACE)}) | ||
|
||
return networks | ||
|
||
def ex_create_network(self, cidr_block, name=None, | ||
instance_tenancy='default'): | ||
""" | ||
Create a network/VPC | ||
|
||
:param cidr_block: The CIDR block assigned to the network | ||
:type cidr_block: ``str`` | ||
|
||
:param name: An optional name for the network | ||
:type name: ``str`` | ||
|
||
:param instance_tenancy: The allowed tenancy of instances launched | ||
into the VPC. | ||
Valid values: default/dedicated | ||
:type instance_tenancy: ``str`` | ||
|
||
:return: Dictionary of network properties | ||
:rtype: ``dict`` | ||
""" | ||
params = {'Action': 'CreateVpc', | ||
'CidrBlock': cidr_block, | ||
'InstanceTenancy': instance_tenancy} | ||
|
||
result = self.connection.request(self.path, params=params).object | ||
|
||
# Get our properties | ||
response = {'vpc_id': findtext(element=result, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method should now also return Something like You can then also pass this instance to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also appreciate if you could synchronize your branch with trunk after you make those changes. Otherwise I will have a bad time merging this patch since there will be a bunch of merge conflicts. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup my fault on that. I'll make sure to synchronize my branch before submitting future PRs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No problem and thanks for all those PRs :) |
||
xpath='vpc/vpcId', | ||
namespace=NAMESPACE), | ||
'state': findtext(element=result, | ||
xpath='vpc/state', | ||
namespace=NAMESPACE), | ||
'cidr_block': findtext(element=result, | ||
xpath='vpc/cidrBlock', | ||
namespace=NAMESPACE)} | ||
|
||
# Attempt to tag our network if the name was provided | ||
if name is not None: | ||
# Build a resource object | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you define a new class which represents a Network, you could get rid of this code. In cases like this, defining a class is also generally better than using a dict. Dict enforces no structure which means it's fragile and it also makes it harder for the end user to know what to expect. Then you can also modify There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No objection here. I'll go ahead and make that change now. |
||
class Resource: | ||
pass | ||
|
||
resource = Resource() | ||
resource.id = response['vpc_id'] | ||
self.ex_create_tags(resource, {'Name': name}) | ||
|
||
return response | ||
|
||
def ex_destroy_network(self, vpc_id): | ||
""" | ||
Deletes a network/VPC. | ||
|
||
:param vpc_id: The ID of the VPC | ||
:type vpc_id: ``str`` | ||
|
||
:rtype: ``bool`` | ||
""" | ||
params = {'Action': 'DeleteVpc', 'VpcId': vpc_id} | ||
|
||
result = self.connection.request(self.path, params=params).object | ||
element = findtext(element=result, xpath='return', | ||
namespace=NAMESPACE) | ||
|
||
return element == 'true' | ||
|
||
def ex_list_security_groups(self): | ||
""" | ||
List existing Security Groups. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<CreateVpcResponse xmlns="http://ec2.amazonaws.com/doc/2013-10-15/"> | ||
<requestId>7a662fe5-1f34-4e17-9ee9-69a28a8ac0be</requestId> | ||
<vpc> | ||
<vpcId>vpc-ad3527cf</vpcId> | ||
<state>pending</state> | ||
<cidrBlock>192.168.55.0/24</cidrBlock> | ||
<dhcpOptionsId>dopt-7eded312</dhcpOptionsId> | ||
<instanceTenancy>default</instanceTenancy> | ||
</vpc> | ||
</CreateVpcResponse> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<DeleteVpcResponse xmlns="http://ec2.amazonaws.com/doc/2013-10-15/"> | ||
<requestId>85793fa6-2ece-480c-855f-0f82c3257e50</requestId> | ||
<return>true</return> | ||
</DeleteVpcResponse> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<DescribeVpcsResponse xmlns="http://ec2.amazonaws.com/doc/2013-10-15/"> | ||
<requestId>be8cfa34-0710-4895-941f-961c5738f8f8</requestId> | ||
<vpcSet> | ||
<item> | ||
<vpcId>vpc-532335e1</vpcId> | ||
<state>available</state> | ||
<cidrBlock>192.168.51.0/24</cidrBlock> | ||
<dhcpOptionsId>dopt-7eded312</dhcpOptionsId> | ||
<tagSet></tagSet> | ||
<instanceTenancy>default</instanceTenancy> | ||
<isDefault>false</isDefault> | ||
</item> | ||
<item> | ||
<vpcId>vpc-62ded30e</vpcId> | ||
<state>available</state> | ||
<cidrBlock>192.168.51.1/24</cidrBlock> | ||
<dhcpOptionsId>dopt-7eded312</dhcpOptionsId> | ||
<tagSet> | ||
<item> | ||
<key>Name</key> | ||
<value>Test VPC</value> | ||
</item> | ||
</tagSet> | ||
<instanceTenancy>default</instanceTenancy> | ||
<isDefault>false</isDefault> | ||
</item> | ||
</vpcSet> | ||
</DescribeVpcsResponse> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
list
ofdict