Skip to content

Commit 64eeae9

Browse files
author
Tianhao He
committed
add security group crud samples
Signed-off-by: Tianhao He <[email protected]>
1 parent 3d1a6d8 commit 64eeae9

File tree

4 files changed

+340
-0
lines changed

4 files changed

+340
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env python
2+
"""
3+
* *******************************************************
4+
* Copyright (c) VMware, Inc. 2019. All Rights Reserved.
5+
* SPDX-License-Identifier: MIT
6+
* *******************************************************
7+
*
8+
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
9+
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
10+
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
11+
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
12+
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
13+
"""
14+
15+
__author__ = 'VMware, Inc'
16+
__vcenter_version__ = 'VMware Cloud on AWS'
17+
18+
import argparse
19+
import random
20+
21+
import requests
22+
from com.vmware.nsx_policy.infra_client import Domains
23+
from com.vmware.nsx_policy.model_client import (Expression, Group,
24+
IPAddressExpression)
25+
from com.vmware.nsx_policy_client_for_vmc import \
26+
create_nsx_policy_client_for_vmc
27+
from vmware.vapi.bindings.struct import PrettyPrinter
28+
from vmware.vapi.lib import connect
29+
from vmware.vapi.security.user_password import \
30+
create_user_password_security_context
31+
from vmware.vapi.stdlib.client.factories import StubConfigurationFactory
32+
"""
33+
Create a new NSX-T Group on MGW or CGW
34+
35+
Sample Prerequisites:
36+
- SDDC deployed in VMware Cloud on AWS
37+
"""
38+
parser = argparse.ArgumentParser(
39+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
40+
41+
parser.add_argument('--refresh_token',
42+
required=True,
43+
help='Refresh token obtained from CSP')
44+
45+
parser.add_argument('--org_id',
46+
required=True,
47+
help='Orgization ID')
48+
49+
parser.add_argument('--sddc_id',
50+
required=True,
51+
help='Sddc ID')
52+
53+
parser.add_argument('--gateway_type',
54+
default='mgw',
55+
help='Gateway type. Either mgw or cgw')
56+
57+
parser.add_argument('--name',
58+
required=True,
59+
help='Name of the security group to be created')
60+
61+
parser.add_argument('--ip_address',
62+
default='172.31.0.0/24',
63+
help='IP address for the expression')
64+
65+
parser.add_argument('--group_id',
66+
help='ID of the group. A random ID will be used by default')
67+
68+
args = parser.parse_args()
69+
70+
gateway_type = args.gateway_type.lower()
71+
72+
id = args.group_id or 'AppGroup-{}'.format(random.randint(1, 10))
73+
74+
nsx_client = create_nsx_policy_client_for_vmc(
75+
refresh_token=args.refresh_token, org_id=args.org_id, sddc_id=args.sddc_id)
76+
77+
print('Create a new NSX-T security group for "{}" with id "{}" and name "{}" \n'
78+
.format(gateway_type, id, args.name))
79+
80+
ipa = IPAddressExpression(ip_addresses=[args.ip_address])
81+
group = Group(display_name=args.name, expression=[ipa])
82+
83+
nsx_client.infra.domains.Groups.update(gateway_type, id, group)
84+
85+
print('Successfully created the security group\n')
86+
87+
print('Retrieve security group properties\n')
88+
security_group = nsx_client.infra.domains.Groups.get(gateway_type, id)
89+
print(security_group)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
* *******************************************************
5+
* Copyright (c) VMware, Inc. 2019. All Rights Reserved.
6+
* SPDX-License-Identifier: MIT
7+
* *******************************************************
8+
*
9+
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
10+
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
11+
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
12+
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
13+
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
14+
"""
15+
16+
__author__ = 'VMware, Inc'
17+
__vcenter_version__ = 'VMware Cloud on AWS'
18+
19+
import argparse
20+
import random
21+
22+
import requests
23+
from com.vmware.nsx_policy.infra_client import Domains
24+
from com.vmware.nsx_policy.model_client import (Expression, Group,
25+
IPAddressExpression)
26+
from com.vmware.nsx_policy_client_for_vmc import \
27+
create_nsx_policy_client_for_vmc
28+
from com.vmware.vapi.std.errors_client import NotFound
29+
from vmware.vapi.bindings.struct import PrettyPrinter
30+
from vmware.vapi.lib import connect
31+
from vmware.vapi.security.user_password import \
32+
create_user_password_security_context
33+
from vmware.vapi.stdlib.client.factories import StubConfigurationFactory
34+
35+
"""
36+
Delete a NSX-T Group on MGW or CGW
37+
38+
Sample Prerequisites:
39+
- SDDC deployed in VMware Cloud on AWS
40+
- A NSX-T security group
41+
"""
42+
parser = argparse.ArgumentParser(
43+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
44+
45+
parser.add_argument('--refresh_token',
46+
required=True,
47+
help='Refresh token obtained from CSP')
48+
49+
parser.add_argument('--org_id',
50+
required=True,
51+
help='Orgization ID')
52+
53+
parser.add_argument('--sddc_id',
54+
required=True,
55+
help='Sddc ID')
56+
57+
parser.add_argument('--gateway_type',
58+
default='mgw',
59+
help='Gateway type. Either mgw or cgw')
60+
61+
parser.add_argument('--group_id',
62+
help='ID of the group to be deleted')
63+
64+
args = parser.parse_args()
65+
66+
gateway_type = args.gateway_type.lower()
67+
68+
nsx_client = create_nsx_policy_client_for_vmc(
69+
refresh_token=args.refresh_token,
70+
org_id=args.org_id,
71+
sddc_id=args.sddc_id)
72+
73+
try:
74+
security_group = nsx_client.infra.domains.Groups.get(gateway_type, args.group_id)
75+
except NotFound:
76+
raise ValueError('Security group "{}" not found'.format(args.group_id))
77+
78+
print('Deleting the NSX-T security group "{}"\n'.format(args.group_id))
79+
80+
nsx_client.infra.domains.Groups.delete(gateway_type, args.group_id)
81+
82+
groups = nsx_client.infra.domains.Groups.list(gateway_type).results
83+
84+
if any(g.id == args.group_id for g in groups):
85+
raise Exception('Failed to delete the security group')
86+
87+
print('Successfully deleted the security group\n')
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
* *******************************************************
5+
* Copyright (c) VMware, Inc. 2019. All Rights Reserved.
6+
* SPDX-License-Identifier: MIT
7+
* *******************************************************
8+
*
9+
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
10+
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
11+
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
12+
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
13+
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
14+
"""
15+
16+
__author__ = 'VMware, Inc'
17+
__vcenter_version__ = 'VMware Cloud on AWS'
18+
19+
import requests
20+
import argparse
21+
22+
from com.vmware.nsx_policy.infra_client import Domains
23+
from com.vmware.nsx_policy_client_for_vmc import create_nsx_policy_client_for_vmc
24+
from vmware.vapi.bindings.struct import PrettyPrinter
25+
from vmware.vapi.lib import connect
26+
from vmware.vapi.security.user_password import \
27+
create_user_password_security_context
28+
from vmware.vapi.stdlib.client.factories import StubConfigurationFactory
29+
30+
31+
"""
32+
List all Network Security Groups
33+
34+
Sample Prerequisites:
35+
- SDDC deployed in VMware Cloud on AWS
36+
"""
37+
parser = argparse.ArgumentParser(
38+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
39+
40+
parser.add_argument('--refresh_token',
41+
required=True,
42+
help='Refresh token obtained from CSP')
43+
44+
parser.add_argument('--org_id',
45+
required=True,
46+
help='Orgization ID')
47+
48+
parser.add_argument('--sddc_id',
49+
required=True,
50+
help='Sddc ID')
51+
52+
parser.add_argument('--gateway_type',
53+
default='mgw',
54+
help='Gateway type. Either mgw or cgw')
55+
56+
args = parser.parse_args()
57+
58+
gateway_type = args.gateway_type.lower()
59+
60+
nsx_client = create_nsx_policy_client_for_vmc(
61+
refresh_token=args.refresh_token,
62+
org_id=args.org_id,
63+
sddc_id=args.sddc_id)
64+
65+
print('Listing all security groups for "{}"\n'.format(gateway_type))
66+
67+
security_groups = nsx_client.infra.domains.Groups.list(gateway_type).results
68+
69+
for group in security_groups:
70+
print('* Group "{}":'.format(group.id))
71+
print('{}\n'.format(group))
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
* *******************************************************
5+
* Copyright (c) VMware, Inc. 2019. All Rights Reserved.
6+
* SPDX-License-Identifier: MIT
7+
* *******************************************************
8+
*
9+
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
10+
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
11+
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
12+
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
13+
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
14+
"""
15+
16+
__author__ = 'VMware, Inc'
17+
__vcenter_version__ = 'VMware Cloud on AWS'
18+
19+
import argparse
20+
import random
21+
22+
import requests
23+
from com.vmware.nsx_policy.infra_client import Domains
24+
from com.vmware.nsx_policy.model_client import (Expression, Group,
25+
IPAddressExpression)
26+
from com.vmware.nsx_policy_client_for_vmc import \
27+
create_nsx_policy_client_for_vmc
28+
from com.vmware.vapi.std.errors_client import NotFound
29+
from vmware.vapi.bindings.struct import PrettyPrinter
30+
from vmware.vapi.lib import connect
31+
from vmware.vapi.security.user_password import \
32+
create_user_password_security_context
33+
from vmware.vapi.stdlib.client.factories import StubConfigurationFactory
34+
35+
36+
"""
37+
Update a NSX-T Group on MGW or CGW
38+
39+
Sample Prerequisites:
40+
- SDDC deployed in VMware Cloud on AWS
41+
- A NSX-T security group
42+
"""
43+
parser = argparse.ArgumentParser(
44+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
45+
46+
parser.add_argument('--refresh_token',
47+
required=True,
48+
help='Refresh token obtained from CSP')
49+
50+
parser.add_argument('--org_id',
51+
required=True,
52+
help='Orgization ID')
53+
54+
parser.add_argument('--sddc_id',
55+
required=True,
56+
help='Sddc ID')
57+
58+
parser.add_argument('--gateway_type',
59+
default='mgw',
60+
help='Gateway type. Either mgw or cgw')
61+
62+
parser.add_argument('--group_id',
63+
help='ID of the group to be updated')
64+
65+
parser.add_argument('--name',
66+
required=True,
67+
help='New name of the security group to be updated')
68+
69+
args = parser.parse_args()
70+
71+
gateway_type = args.gateway_type.lower()
72+
73+
nsx_client = create_nsx_policy_client_for_vmc(
74+
refresh_token=args.refresh_token,
75+
org_id=args.org_id,
76+
sddc_id=args.sddc_id)
77+
78+
try:
79+
security_group = nsx_client.infra.domains.Groups.get(gateway_type, args.group_id)
80+
except NotFound:
81+
raise ValueError('Security group "{}" not found'.format(args.group_id))
82+
83+
print('Updating NSX-T security group\'s name from "{}" to "{}"\n'.format(
84+
security_group.display_name, args.name))
85+
86+
new_description = 'new description'
87+
security_group.description = new_description
88+
security_group.display_name = args.name
89+
90+
group_updated = nsx_client.infra.domains.Groups.update(gateway_type, args.group_id, security_group)
91+
assert group_updated.description == new_description
92+
assert group_updated.display_name == args.name
93+
print('Successfully updated the security group\n')

0 commit comments

Comments
 (0)