Skip to content
Closed
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
12 changes: 9 additions & 3 deletions example_aliyun_ecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import sys

from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver
from libcloud.compute.base import NodeAuthPassword
Expand Down Expand Up @@ -51,8 +53,12 @@

sgs = ecs.ex_list_security_groups()
print('Found %d security groups' % len(sgs))
sg = sgs[0]
print('Use security group %s' % sg)
if len(sgs) == 0:
sg = ecs.ex_create_security_group(description='test')
print('Create security group %s' % sg)
else:
sg = sgs[0].id
print('Use security group %s' % sg)

nodes = ecs.list_nodes()
print('Found %d nodes' % len(nodes))
Expand All @@ -67,7 +73,7 @@
auth = NodeAuthPassword('P@$$w0rd')

node = ecs.create_node(image=image, size=small, name='test',
ex_security_group_id=sg.id,
ex_security_group_id=sg,
ex_internet_charge_type=ecs.internet_charge_types.BY_TRAFFIC,
ex_internet_max_bandwidth_out=1,
ex_data_disk=data_disk,
Expand Down
35 changes: 35 additions & 0 deletions libcloud/compute/drivers/ecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,41 @@ def ex_stop_node(self, node, ex_force_stop=False):
return resp.success() and \
self._wait_until_state([node], NodeState.STOPPED)

def ex_create_security_group(self, description=None, client_token=None):
"""
Create a new security group.

:keyword description: security group description
:type description: ``unicode``

:keyword client_token: a token generated by client to identify
each request.
:type client_token: ``str``
"""
params = {'Action': 'CreateSecurityGroup',
'RegionId': self.region}

if description:
params['Description'] = description
if client_token:
params['ClientToken'] = client_token
resp = self.connection.request(self.path, params)
return findtext(resp.object, 'SecurityGroupId',
namespace=self.namespace)

def ex_delete_security_group_by_id(self, group_id=None):
"""
Delete a new security group.

:keyword group_id: security group id
:type group_id: ``str``
"""
params = {'Action': 'DeleteSecurityGroup',
'RegionId': self.region,
'SecurityGroupId': group_id}
resp = self.connection.request(self.path, params)
return resp.success()

def ex_list_security_groups(self, ex_filters=None):
"""
List security groups in the current region.
Expand Down
4 changes: 4 additions & 0 deletions libcloud/test/compute/fixtures/ecs/create_security_group.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version='1.0' encoding='UTF-8'?>
<RequestId>CEF72CEB-54B6-4AE8-B225-F876FF7BA984</RequestId>
<SecurityGroupId>sg-F876FF7BA</SecurityGroupId>
</CreateSecurityGroupResponse>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version='1.0' encoding='UTF-8'?>
<DeleteSecurityGroupResponse>
<RequestId>CEF72CEB-54B6-4AE8-B225-F876FF7BA984</RquestID>
</DeleteSecurityGroupResponse>
15 changes: 15 additions & 0 deletions libcloud/test/compute/test_ecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,21 @@ def _list_sgs_filters_DescribeSecurityGroups(self, method, url, body,
self.assertUrlContainsQueryParams(url, params)
return self._DescribeSecurityGroups(method, url, body, headers)

def _create_sg_CreateSecurityGroup(self, method, url, body, headers):
params = {'RegionId': self.test.region,
'Description': 'description',
'ClientToken': 'clientToken'}
self.assertUrlContainsQueryParams(url, params)
resp_body = self.fixtures.load('create_security_group.xml')
return (httplib.OK, resp_body, {}, httplib.responses[httplib.OK])

def _delete_sg_by_id_DeleteSecurityGroup(self, method, url, body, headers):
params = {'RegionId': self.test.region,
'SecurityGroupId': 'sg-fakeSecurityGroupId'}
self.assertUrlContainsQueryParams(url, params)
resp_body = self.fixtures.load('delete_security_group_by_id.xml')
return (httplib.OK, resp_body, {}, httplib.responses[httplib.OK])

def _DescribeZones(self, method, url, body, headers):
resp_body = self.fixtures.load('describe_zones.xml')
return (httplib.OK, resp_body, {}, httplib.responses[httplib.OK])
Expand Down