How to use update_firewall_rule method in localstack

Best Python code snippet using localstack_python

profitbricks_firewall_rule.py

Source:profitbricks_firewall_rule.py Github

copy

Full Screen

...304 'firewall_rule': firewall_rule_response305 }306 except Exception as e:307 module.fail_json(msg="failed to create the firewall rule: %s" % to_native(e))308def update_firewall_rule(module, client):309 """310 Updates a firewall rule.311 module : AnsibleModule object312 client: authenticated ionosenterprise object.313 Returns:314 The firewall rule instance being updated315 """316 datacenter = module.params.get('datacenter')317 server = module.params.get('server')318 nic = module.params.get('nic')319 name = module.params.get('name')320 source_mac = module.params.get('source_mac')321 source_ip = module.params.get('source_ip')322 target_ip = module.params.get('target_ip')323 port_range_start = module.params.get('port_range_start')324 port_range_end = module.params.get('port_range_end')325 icmp_type = module.params.get('icmp_type')326 icmp_code = module.params.get('icmp_code')327 wait = module.params.get('wait')328 wait_timeout = module.params.get('wait_timeout')329 # Locate UUID for virtual datacenter330 datacenter_list = client.list_datacenters()331 datacenter_id = _get_resource_id(datacenter_list, datacenter, module, "Data center")332 # Locate UUID for server333 server_list = client.list_servers(datacenter_id)334 server_id = _get_resource_id(server_list, server, module, "Server")335 # Locate UUID for NIC336 nic_list = client.list_nics(datacenter_id, server_id)337 nic_id = _get_resource_id(nic_list, nic, module, "NIC")338 # Locate UUID for firewall rule339 fw_list = client.get_firewall_rules(datacenter_id, server_id, nic_id)340 fw_id = _get_resource_id(fw_list, name, module, "Firewall rule")341 if module.check_mode:342 module.exit_json(changed=True)343 try:344 firewall_rule_response = client.update_firewall_rule(345 datacenter_id,346 server_id,347 nic_id,348 fw_id,349 source_mac=source_mac,350 source_ip=source_ip,351 target_ip=target_ip,352 port_range_start=port_range_start,353 port_range_end=port_range_end,354 icmp_type=icmp_type,355 icmp_code=icmp_code356 )357 if wait:358 _wait_for_completion(client, firewall_rule_response,359 wait_timeout, "update_firewall_rule")360 return {361 'changed': True,362 'firewall_rule': firewall_rule_response363 }364 except Exception as e:365 module.fail_json(msg="failed to update the firewall rule: %s" % to_native(e))366def delete_firewall_rule(module, client):367 """368 Removes a firewall rule369 module : AnsibleModule object370 client: authenticated ionosenterprise object.371 Returns:372 True if the firewall rule was removed, false otherwise373 """374 datacenter = module.params.get('datacenter')375 server = module.params.get('server')376 nic = module.params.get('nic')377 name = module.params.get('name')378 # Locate UUID for virtual datacenter379 datacenter_list = client.list_datacenters()380 datacenter_id = _get_resource_id(datacenter_list, datacenter, module, "Datacenter")381 # Locate UUID for server382 server_list = client.list_servers(datacenter_id)383 server_id = _get_resource_id(server_list, server, module, "Server")384 # Locate UUID for NIC385 nic_list = client.list_nics(datacenter_id, server_id)386 nic_id = _get_resource_id(nic_list, nic, module, "NIC")387 # Locate UUID for firewall rule388 firewall_rule_list = client.get_firewall_rules(datacenter_id, server_id, nic_id)389 firewall_rule_id = _get_resource_id(firewall_rule_list, name, module, "Firewall rule")390 if module.check_mode:391 module.exit_json(changed=True)392 try:393 firewall_rule_response = client.delete_firewall_rule(394 datacenter_id, server_id, nic_id, firewall_rule_id395 )396 return firewall_rule_response397 except Exception as e:398 module.fail_json(msg="failed to remove the firewall rule: %s" % to_native(e))399def _get_resource_id(resource_list, identity, module, resource_type):400 """401 Fetch and return the UUID of a resource regardless of whether the name or402 UUID is passed. Throw an error otherwise.403 """404 for resource in resource_list['items']:405 if identity in (resource['properties']['name'], resource['id']):406 return resource['id']407 module.fail_json(msg='%s \'%s\' could not be found.' % (resource_type, identity))408def main():409 module = AnsibleModule(410 argument_spec=dict(411 datacenter=dict(type='str', required=True),412 server=dict(type='str', required=True),413 nic=dict(type='str', required=True),414 name=dict(type='str', required=True),415 protocol=dict(type='str', choices=PROTOCOLS, required=False),416 source_mac=dict(type='str', default=None),417 source_ip=dict(type='str', default=None),418 target_ip=dict(type='str', default=None),419 port_range_start=dict(type='int', default=None),420 port_range_end=dict(type='int', default=None),421 icmp_type=dict(type='int', default=None),422 icmp_code=dict(type='int', default=None),423 api_url=dict(type='str', default=None),424 username=dict(425 type='str',426 required=True,427 aliases=['subscription_user'],428 fallback=(env_fallback, ['PROFITBRICKS_USERNAME'])429 ),430 password=dict(431 type='str',432 required=True,433 aliases=['subscription_password'],434 fallback=(env_fallback, ['PROFITBRICKS_PASSWORD']),435 no_log=True436 ),437 wait=dict(type='bool', default=True),438 wait_timeout=dict(type='int', default=600),439 state=dict(type='str', default='present'),440 ),441 supports_check_mode=True442 )443 if not HAS_SDK:444 module.fail_json(msg='ionosenterprise is required for this module, run `pip install ionosenterprise`')445 username = module.params.get('username')446 password = module.params.get('password')447 api_url = module.params.get('api_url')448 if not api_url:449 ionosenterprise = IonosEnterpriseService(username=username, password=password)450 else:451 ionosenterprise = IonosEnterpriseService(452 username=username,453 password=password,454 host_base=api_url455 )456 user_agent = 'profitbricks-sdk-python/%s Ansible/%s' % (sdk_version, __version__)457 ionosenterprise.headers = {'User-Agent': user_agent}458 state = module.params.get('state')459 if state == 'absent':460 try:461 (changed) = delete_firewall_rule(module, ionosenterprise)462 module.exit_json(changed=changed)463 except Exception as e:464 module.fail_json(msg='failed to set firewall rule state: %s' % to_native(e))465 elif state == 'present':466 try:467 (firewall_rule_dict) = create_firewall_rule(module, ionosenterprise)468 module.exit_json(**firewall_rule_dict)469 except Exception as e:470 module.fail_json(msg='failed to set firewall rules state: %s' % to_native(e))471 elif state == 'update':472 try:473 (firewall_rule_dict) = update_firewall_rule(module, ionosenterprise)474 module.exit_json(**firewall_rule_dict)475 except Exception as e:476 module.fail_json(msg='failed to update firewall rule: %s' % to_native(e))477if __name__ == '__main__':...

Full Screen

Full Screen

firewall_rules_crud.py

Source:firewall_rules_crud.py Github

copy

Full Screen

...136 sddc=self.sddc_id,137 edge_id=self.edge_id,138 rule_id=self.rule_id)139 self.print_output(rule)140 def update_firewall_rule(self):141 print('\n# Example: Update the firewall rule')142 self.nfwr.description = 'Updated description'143 self.nfwr.name = 'Updated ' + self.rule_name144 self.nfwr.action = 'deny'145 self.nfwr.source.ip_address = ['127.0.0.1']146 self.vmc_client.orgs.sddcs.networks.edges.firewall.config.Rules.update(147 org=self.org_id,148 sddc=self.sddc_id,149 edge_id=self.edge_id,150 rule_id=self.rule_id,151 nsxfirewallrule=self.nfwr)152 rule = self.vmc_client.orgs.sddcs.networks.edges.firewall.config.Rules.get(153 org=self.org_id,154 sddc=self.sddc_id,155 edge_id=self.edge_id,156 rule_id=self.rule_id)157 print('# List the updated firewall rule specs')158 self.print_output(rule)159 def delete_firewall_rule(self):160 if self.cleanup:161 self.vmc_client.orgs.sddcs.networks.edges.firewall.config.Rules.delete(162 org=self.org_id,163 sddc=self.sddc_id,164 edge_id=self.edge_id,165 rule_id=self.rule_id)166 print('\n# Example: Firewall rule {} is deleted'.format(167 self.rule_name))168 def print_output(self, rule):169 print(170 'Name: {}, Action: {}, Source IPs: {}, Destination IPs: {},Service Protocol: {}, Service Port: {}'171 .format(rule.name, rule.action, rule.source.ip_address,172 rule.destination.ip_address,173 rule.application.service[0].protocol,174 rule.application.service[0].port))175def main():176 firewall_rules = FirewallRulesCrud()177 firewall_rules.setup()178 firewall_rules.create_firewall_rule()179 firewall_rules.get_firewall_rule()180 firewall_rules.update_firewall_rule()181 firewall_rules.delete_firewall_rule()182if __name__ == '__main__':...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful