|
6 | 6 | <language>en-US</language> |
7 | 7 | <author>Enthusiastic Hugo User</author> |
8 | 8 | <rights>Copyright (c) 2014, Enthusiastic Hugo User; all rights reserved.</rights> |
9 | | - <updated>Mon, 02 Oct 2017 00:00:00 UTC</updated> |
| 9 | + <updated>Wed, 18 Oct 2017 00:00:00 UTC</updated> |
| 10 | + |
| 11 | + <item> |
| 12 | + <title>Add, list, and remove security group rules</title> |
| 13 | + <link>https://softlayer.github.io/python/securitygroup_rule_ops/</link> |
| 14 | + <pubDate>Wed, 18 Oct 2017 00:00:00 UTC</pubDate> |
| 15 | + <author>Enthusiastic Hugo User</author> |
| 16 | + <guid>https://softlayer.github.io/python/securitygroup_rule_ops/</guid> |
| 17 | + <description> |
| 18 | + |
| 19 | +<h2 id="adding-a-rule-to-a-security-group">Adding a rule to a security group</h2> |
| 20 | + |
| 21 | +<pre><code class="language-python">import SoftLayer |
| 22 | +# For nice debug output |
| 23 | +from pprint import pprint as pp |
| 24 | + |
| 25 | +# Create a client for use with the NetworkManager |
| 26 | +client = SoftLayer.Client() |
| 27 | +net_mgr = SoftLayer.NetworkManager(client) |
| 28 | + |
| 29 | +sg_id = 123045 |
| 30 | +direction = 'ingress' |
| 31 | +ethertype = 'IPv4' |
| 32 | +remote_ip = '169.148.34.0/24' |
| 33 | +protocol = 'tcp' |
| 34 | +port_min = 22 |
| 35 | +port_max = 22 |
| 36 | +try: |
| 37 | + result = net_mgr.add_securitygroup_rule(sg_id, |
| 38 | + direction=direction, |
| 39 | + ethertype=ethertype, |
| 40 | + remote_ip=remote_ip, |
| 41 | + protocol=protocol, |
| 42 | + port_min=port_min, |
| 43 | + port_max=port_max) |
| 44 | + pp(result) |
| 45 | +except SoftLayer.SoftLayerAPIError as e: |
| 46 | + pp('Failed... Unable to add a rule to the security group: faultCode=%s, faultString=%s' |
| 47 | + % (e.faultCode, e.faultString)) |
| 48 | +</code></pre> |
| 49 | + |
| 50 | +<h2 id="listing-rules-in-a-security-group">Listing rules in a security group</h2> |
| 51 | + |
| 52 | +<pre><code class="language-python">import SoftLayer |
| 53 | +# For nice debug output |
| 54 | +from pprint import pprint as pp |
| 55 | + |
| 56 | +# Create a client for use with the NetworkManager |
| 57 | +client = SoftLayer.Client() |
| 58 | +net_mgr = SoftLayer.NetworkManager(client) |
| 59 | + |
| 60 | +sg_id = 123045 |
| 61 | +try: |
| 62 | + result = net_mgr.list_securitygroup_rules(sg_id) |
| 63 | + pp(result) |
| 64 | +except SoftLayer.SoftLayerAPIError as e: |
| 65 | + pp('Failed... Unable to list rules in the security group: faultCode=%s, faultString=%s' |
| 66 | + % (e.faultCode, e.faultString)) |
| 67 | +</code></pre> |
| 68 | + |
| 69 | +<h2 id="removing-a-rule-from-a-security-group">Removing a rule from a security group</h2> |
| 70 | + |
| 71 | +<pre><code class="language-python">import SoftLayer |
| 72 | +# For nice debug output |
| 73 | +from pprint import pprint as pp |
| 74 | + |
| 75 | +# Create a client for use with the NetworkManager |
| 76 | +client = SoftLayer.Client() |
| 77 | +net_mgr = SoftLayer.NetworkManager(client) |
| 78 | + |
| 79 | +sg_id = 123045 |
| 80 | +rule_id = 475879 |
| 81 | +try: |
| 82 | + result = net_mgr.remove_securitygroup_rule(sg_id, rule_id) |
| 83 | + pp(result) |
| 84 | +except SoftLayer.SoftLayerAPIError as e: |
| 85 | + pp('Failed... Unable to remove rule from the security group: faultCode=%s, faultString=%s' |
| 86 | + % (e.faultCode, e.faultString)) |
| 87 | +</code></pre> |
| 88 | + |
| 89 | +<h2 id="remove-all-rules-from-a-security-group">Remove all rules from a security group</h2> |
| 90 | + |
| 91 | +<pre><code class="language-python">import SoftLayer |
| 92 | +# For nice debug output |
| 93 | +from pprint import pprint as pp |
| 94 | + |
| 95 | +# Create a client for use with the NetworkManager |
| 96 | +client = SoftLayer.Client() |
| 97 | +net_mgr = SoftLayer.NetworkManager(client) |
| 98 | + |
| 99 | +sg_id = 123045 |
| 100 | +try: |
| 101 | + rules = net_mgr.list_securitygroup_rules(sg_id) |
| 102 | + rules = [rule['id'] for rule in rules] |
| 103 | + result = net_mgr.remove_securitygroup_rules(sg_id, rules) |
| 104 | + pp(result) |
| 105 | +except SoftLayer.SoftLayerAPIError as e: |
| 106 | + pp('Failed... Unable to remove rules from the security group: faultCode=%s, faultString=%s' |
| 107 | + % (e.faultCode, e.faultString)) |
| 108 | +</code></pre> |
| 109 | +</description> |
| 110 | + </item> |
| 111 | + |
| 112 | + <item> |
| 113 | + <title>Create, list, get, and delete security groups</title> |
| 114 | + <link>https://softlayer.github.io/python/securitygroup_ops/</link> |
| 115 | + <pubDate>Wed, 18 Oct 2017 00:00:00 UTC</pubDate> |
| 116 | + <author>Enthusiastic Hugo User</author> |
| 117 | + <guid>https://softlayer.github.io/python/securitygroup_ops/</guid> |
| 118 | + <description> |
| 119 | + |
| 120 | +<h2 id="creating-a-security-group">Creating a security group</h2> |
| 121 | + |
| 122 | +<pre><code class="language-python">import SoftLayer |
| 123 | +# For nice debug output |
| 124 | +from pprint import pprint as pp |
| 125 | + |
| 126 | +# Create a client for use with the NetworkManager |
| 127 | +client = SoftLayer.Client() |
| 128 | +net_mgr = SoftLayer.NetworkManager(client) |
| 129 | + |
| 130 | +name = 'pythonCreatedGroupExample' |
| 131 | +description = 'Security Group created via python' |
| 132 | +try: |
| 133 | + result = net_mgr.create_securitygroup(name=name, description=description) |
| 134 | + pp(result) |
| 135 | +except SoftLayer.SoftLayerAPIError as e: |
| 136 | + pp('Failed... Unable to create a new security group: faultCode=%s, faultString=%s' |
| 137 | + % (e.faultCode, e.faultString)) |
| 138 | +</code></pre> |
| 139 | + |
| 140 | +<h2 id="deleting-a-security-group">Deleting a security group</h2> |
| 141 | + |
| 142 | +<pre><code class="language-python">import SoftLayer |
| 143 | +# For nice debug output |
| 144 | +from pprint import pprint as pp |
| 145 | + |
| 146 | +# Create a client for use with the NetworkManager |
| 147 | +client = SoftLayer.Client() |
| 148 | +net_mgr = SoftLayer.NetworkManager(client) |
| 149 | + |
| 150 | +sg_id = 12045 |
| 151 | +try: |
| 152 | + result = net_mgr.delete_securitygroup(sg_id) |
| 153 | + pp(result) |
| 154 | +except SoftLayer.SoftLayerAPIError as e: |
| 155 | + pp('Failed... Unable to delete security group: faultCode=%s, faultString=%s' |
| 156 | + % (e.faultCode, e.faultString)) |
| 157 | +</code></pre> |
| 158 | + |
| 159 | +<h2 id="getting-a-security-group">Getting a security group</h2> |
| 160 | + |
| 161 | +<pre><code class="language-python">import SoftLayer |
| 162 | +# For nice debug output |
| 163 | +from pprint import pprint as pp |
| 164 | + |
| 165 | +# Create a client for use with the NetworkManager |
| 166 | +client = SoftLayer.Client() |
| 167 | +net_mgr = SoftLayer.NetworkManager(client) |
| 168 | + |
| 169 | +sg_id = 12045 |
| 170 | +try: |
| 171 | + result = net_mgr.get_securitygroup(sg_id) |
| 172 | + pp(result) |
| 173 | +except SoftLayer.SoftLayerAPIError as e: |
| 174 | + pp('Failed... Unable to get security group: faultCode=%s, faultString=%s' |
| 175 | + % (e.faultCode, e.faultString)) |
| 176 | +</code></pre> |
| 177 | + |
| 178 | +<h2 id="list-all-security-groups-in-account">List all security groups in account</h2> |
| 179 | + |
| 180 | +<pre><code class="language-python">import SoftLayer |
| 181 | +# For nice debug output |
| 182 | +from pprint import pprint as pp |
| 183 | + |
| 184 | +# Create a client for use with the NetworkManager |
| 185 | +client = SoftLayer.Client() |
| 186 | +net_mgr = SoftLayer.NetworkManager(client) |
| 187 | + |
| 188 | +result = net_mgr.list_securitygroups() |
| 189 | +pp(result) |
| 190 | +</code></pre> |
| 191 | +</description> |
| 192 | + </item> |
| 193 | + |
| 194 | + <item> |
| 195 | + <title>VSIs and security groups</title> |
| 196 | + <link>https://softlayer.github.io/python/securitygroup_vsis/</link> |
| 197 | + <pubDate>Wed, 18 Oct 2017 00:00:00 UTC</pubDate> |
| 198 | + <author>Enthusiastic Hugo User</author> |
| 199 | + <guid>https://softlayer.github.io/python/securitygroup_vsis/</guid> |
| 200 | + <description> |
| 201 | + |
| 202 | +<h2 id="creating-a-vsi-with-security-groups">Creating a VSI with security groups</h2> |
| 203 | + |
| 204 | +<pre><code class="language-python">import SoftLayer |
| 205 | +# For nice debug output |
| 206 | +from pprint import pprint as pp |
| 207 | + |
| 208 | +# Create a client for use with the VSManager |
| 209 | +client = SoftLayer.Client() |
| 210 | +vs_mgr = SoftLayer.VSManager(client) |
| 211 | + |
| 212 | +http_sg_id = 384757 |
| 213 | +ssh_sg_id = 576973 |
| 214 | + |
| 215 | +# Allow only HTTP on the public interface of the VSI |
| 216 | +public_groups = [http_sg_id] |
| 217 | + |
| 218 | +# Allow HTTP and SSH on the private interface of the VSI |
| 219 | +private_groups = [http_sg_id, ssh_sg_id] |
| 220 | + |
| 221 | +# If we didn't want to set any security groups on an interface |
| 222 | +# (which allows all traffic), we don't set the associated |
| 223 | +# creation kwarg |
| 224 | + |
| 225 | +create_kwargs = { |
| 226 | + 'hostname': 'sg-vsi', |
| 227 | + 'domain': 'mycompany.com', |
| 228 | + 'os_code': 'UBUNTU_LATEST_64', |
| 229 | + 'datacenter': 'dal13', |
| 230 | + 'cpus': 1, |
| 231 | + 'memory': 1024, |
| 232 | + 'hourly': True, |
| 233 | + 'disks': ('100',), |
| 234 | + 'public_security_groups': public_groups, |
| 235 | + 'private_security_groups': private_groups, |
| 236 | +} |
| 237 | + |
| 238 | +try: |
| 239 | + vsi = vs_mgr.create_instance(**create_kwargs) |
| 240 | + pp(vsi) |
| 241 | +except SoftLayer.SoftLayerAPIError as e: |
| 242 | + pp('Failed... Unable to create VSI with security group: faultCode=%s, faultString=%s' |
| 243 | + % (e.faultCode, e.faultString)) |
| 244 | +</code></pre> |
| 245 | + |
| 246 | +<h2 id="attach-an-existing-vsi-to-security-groups">Attach an existing VSI to security groups</h2> |
| 247 | + |
| 248 | +<pre><code class="language-python">import SoftLayer |
| 249 | +# For nice debug output |
| 250 | +from pprint import pprint as pp |
| 251 | + |
| 252 | +# Create a client for use with the NetworkManager and VSManager |
| 253 | +client = SoftLayer.Client() |
| 254 | +net_mgr = SoftLayer.NetworkManager(client) |
| 255 | +vs_mgr = SoftLayer.VSManager(client) |
| 256 | + |
| 257 | +http_sg_id = 384757 |
| 258 | +vsi_id = 4018735 |
| 259 | +private_interface = False |
| 260 | +port_number = 0 if private_interface else 1 |
| 261 | +network_component_mask = 'networkComponents[id, port]' |
| 262 | + |
| 263 | +try: |
| 264 | + vsi_components = vs_mgr.get_instance(vsi_id, mask=network_component_mask) |
| 265 | + component_to_attach = [comp for comp in vsi_components['networkComponents'] |
| 266 | + if comp['port'] == port_number][0] |
| 267 | + result = net_mgr.attach_securitygroup_component(http_sg_id, |
| 268 | + component_to_attach['id']) |
| 269 | + pp(result) |
| 270 | + |
| 271 | + # If this is the first time the server is being associated with security groups, |
| 272 | + # a reboot is required for the security group to take effect on the VSI |
| 273 | + client['Virtual_Guest'].rebootSoft(id=vsi_id) |
| 274 | +except SoftLayer.SoftLayerAPIError as e: |
| 275 | + pp('Failed... Unable to associate VSI with security group: faultCode=%s, faultString=%s' |
| 276 | + % (e.faultCode, e.faultString)) |
| 277 | +</code></pre> |
| 278 | + |
| 279 | +<h2 id="detach-a-vsi-from-a-security-group">Detach a VSI from a security group</h2> |
| 280 | + |
| 281 | +<pre><code class="language-python">import SoftLayer |
| 282 | +# For nice debug output |
| 283 | +from pprint import pprint as pp |
| 284 | + |
| 285 | +# Create a client for use with the NetworkManager and VSManager |
| 286 | +client = SoftLayer.Client() |
| 287 | +net_mgr = SoftLayer.NetworkManager(client) |
| 288 | +vs_mgr = SoftLayer.VSManager(client) |
| 289 | + |
| 290 | +http_sg_id = 384757 |
| 291 | +vsi_id = 4018735 |
| 292 | +private_interface = False |
| 293 | +port_number = 0 if private_interface else 1 |
| 294 | +network_component_mask = 'networkComponents[id, port]' |
| 295 | + |
| 296 | +try: |
| 297 | + vsi_components = vs_mgr.get_instance(vsi_id, mask=network_component_mask) |
| 298 | + component_to_detach = [comp for comp in vsi_components['networkComponents'] |
| 299 | + if comp['port'] == port_number][0] |
| 300 | + net_mgr.detach_securitygroup_component(http_sg_id, |
| 301 | + component_to_detach['id']) |
| 302 | + |
| 303 | +except SoftLayer.SoftLayerAPIError as e: |
| 304 | + pp('Failed... Unable to disassociate VSI with security group: faultCode=%s, faultString=%s' |
| 305 | + % (e.faultCode, e.faultString)) |
| 306 | +</code></pre> |
| 307 | +</description> |
| 308 | + </item> |
10 | 309 |
|
11 | 310 | <item> |
12 | 311 | <title>Add, get, edit and remove Security Group rules</title> |
@@ -352,37 +651,6 @@ secGroupId = 70501 |
352 | 651 |
|
353 | 652 | getAll = client['SoftLayer_Network_SecurityGroup'].object_with_id(secGroupId).getAllObjects |
354 | 653 | pp getAll |
355 | | -</code></pre> |
356 | | -</description> |
357 | | - </item> |
358 | | - |
359 | | - <item> |
360 | | - <title>Create a Security group</title> |
361 | | - <link>https://softlayer.github.io/python/createsecuritygroup/</link> |
362 | | - <pubDate>Tue, 20 Jun 2017 00:00:00 UTC</pubDate> |
363 | | - <author>Enthusiastic Hugo User</author> |
364 | | - <guid>https://softlayer.github.io/python/createsecuritygroup/</guid> |
365 | | - <description> |
366 | | - |
367 | | -<h2 id="creating-a-security-group">Creating a Security Group</h2> |
368 | | - |
369 | | -<pre><code class="language-python">import SoftLayer |
370 | | -# For nice debug output: |
371 | | -from pprint import pprint as pp |
372 | | -# Create an object template to create the item. |
373 | | -objectTemplate = { |
374 | | - 'accountId': YOUR_ACCOUNT_ID, |
375 | | - 'name': 'pythonCreatedGroupExample', |
376 | | - 'description': 'Sec Group created via python' |
377 | | -} |
378 | | -client = SoftLayer.Client() |
379 | | -try: |
380 | | - result = client['SoftLayer_Network_SecurityGroup'].createObjects([objectTemplate]) |
381 | | - pp(result) |
382 | | -except SoftLayer.SoftLayerAPIError as e: |
383 | | - pp('Failed ... Unable to create a new SecGroup faultCode=%s, faultString=%s' |
384 | | - % (e.faultCode, e.faultString)) |
385 | | - |
386 | 654 | </code></pre> |
387 | 655 | </description> |
388 | 656 | </item> |
|
0 commit comments