11.0.0
This commit is contained in:
@@ -10,6 +10,9 @@ description: See L(Module Documentation,https://repository.basistech.ru/BASIS/de
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.decort_utils import DecortController
|
||||
|
||||
from dynamix_sdk import exceptions as sdk_exceptions
|
||||
import dynamix_sdk.types as sdk_types
|
||||
|
||||
|
||||
class DecortSecurityGroup(DecortController):
|
||||
id: int = 0
|
||||
@@ -52,18 +55,18 @@ class DecortSecurityGroup(DecortController):
|
||||
options=dict(
|
||||
direction=dict(
|
||||
type='str',
|
||||
choices=[
|
||||
e.name for e in
|
||||
self.SecurityGroupRuleDirection
|
||||
],
|
||||
choices=(
|
||||
sdk_types.TrafficDirection.
|
||||
_member_names_
|
||||
),
|
||||
required=True,
|
||||
),
|
||||
ethertype=dict(
|
||||
type='str',
|
||||
choices=[
|
||||
e.name for e in
|
||||
self.SecurityGroupRuleEtherType
|
||||
],
|
||||
choices=(
|
||||
sdk_types.SGRuleEthertype.
|
||||
_member_names_
|
||||
),
|
||||
),
|
||||
id=dict(
|
||||
type='int',
|
||||
@@ -81,12 +84,11 @@ class DecortSecurityGroup(DecortController):
|
||||
),
|
||||
protocol=dict(
|
||||
type='str',
|
||||
choices=[
|
||||
e.name for e in
|
||||
self.SecurityGroupRuleProtocol
|
||||
],
|
||||
choices=(
|
||||
sdk_types.SGRuleProtocol._member_names_
|
||||
),
|
||||
),
|
||||
remote_ip_prefix=dict(
|
||||
remote_net_cidr=dict(
|
||||
type='str',
|
||||
),
|
||||
),
|
||||
@@ -101,16 +103,17 @@ class DecortSecurityGroup(DecortController):
|
||||
supports_check_mode=True,
|
||||
)
|
||||
|
||||
@DecortController.handle_sdk_exceptions
|
||||
def run(self):
|
||||
if self.aparams['id'] is not None:
|
||||
self.id = self.aparams['id']
|
||||
elif self.aparams['name'] is not None:
|
||||
security_group = self.security_group_find(
|
||||
security_groups = self.api.cloudapi.security_group.list(
|
||||
account_id=self.aparams['account_id'],
|
||||
name=self.aparams['name'],
|
||||
)
|
||||
if security_group:
|
||||
self.id = security_group['id']
|
||||
if security_groups.data:
|
||||
self.id = security_groups.data[0].id
|
||||
|
||||
if self.id:
|
||||
self.get_info()
|
||||
@@ -127,14 +130,25 @@ class DecortSecurityGroup(DecortController):
|
||||
self.exit()
|
||||
|
||||
def get_info(self):
|
||||
self.facts: dict = self.security_group_get(id=self.id)
|
||||
self.facts['created_timestamp'] = self.facts.pop('created_at')
|
||||
self.facts['updated_timestamp'] = self.facts.pop('updated_at')
|
||||
for rule in self.facts['rules']:
|
||||
rule['port_range'] = {
|
||||
'min': rule.pop('port_range_min'),
|
||||
'max': rule.pop('port_range_max'),
|
||||
}
|
||||
try:
|
||||
storage_policy_model = self.api.cloudapi.security_group.get(
|
||||
security_group_id=self.id
|
||||
)
|
||||
except sdk_exceptions.RequestException as e:
|
||||
if (
|
||||
e.orig_exception.response
|
||||
and e.orig_exception.response.status_code == 404
|
||||
):
|
||||
self.message(
|
||||
self.MESSAGES.obj_not_found(
|
||||
obj='security_group',
|
||||
id=self.id,
|
||||
)
|
||||
)
|
||||
self.exit(fail=True)
|
||||
raise e
|
||||
|
||||
self.facts = storage_policy_model.model_dump()
|
||||
|
||||
def check_amodule_args_for_create(self):
|
||||
check_errors = False
|
||||
@@ -242,16 +256,13 @@ class DecortSecurityGroup(DecortController):
|
||||
return not check_errors
|
||||
|
||||
def create(self):
|
||||
security_groups_by_account_id = self.user_security_groups(
|
||||
account_id=self.aparams['account_id']
|
||||
id = self.sdk_checkmode(self.api.cloudapi.security_group.create)(
|
||||
account_id=self.aparams['account_id'],
|
||||
name=self.aparams['name'],
|
||||
description=self.aparams['description'],
|
||||
)
|
||||
sg_names = [sg['name'] for sg in security_groups_by_account_id]
|
||||
if self.aparams['name'] not in sg_names:
|
||||
self.id = self.security_group_create(
|
||||
account_id=self.aparams['account_id'],
|
||||
name=self.aparams['name'],
|
||||
description=self.aparams['description'],
|
||||
)
|
||||
if id:
|
||||
self.id = id
|
||||
|
||||
def change(self):
|
||||
self.change_state()
|
||||
@@ -277,7 +288,7 @@ class DecortSecurityGroup(DecortController):
|
||||
):
|
||||
new_description = aparam_description
|
||||
if new_name or new_description:
|
||||
self.security_group_update(
|
||||
self.sdk_checkmode(self.api.cloudapi.security_group.update)(
|
||||
security_group_id=self.id,
|
||||
name=new_name,
|
||||
description=new_description,
|
||||
@@ -317,7 +328,9 @@ class DecortSecurityGroup(DecortController):
|
||||
self.create_rule(rule=rule)
|
||||
|
||||
def delete(self):
|
||||
self.security_group_detele(security_group_id=self.id)
|
||||
self.sdk_checkmode(self.api.cloudapi.security_group.delete)(
|
||||
security_group_id=self.id,
|
||||
)
|
||||
self.facts = {}
|
||||
self.exit()
|
||||
|
||||
@@ -326,20 +339,22 @@ class DecortSecurityGroup(DecortController):
|
||||
if rule.get('port_range'):
|
||||
port_range_min = rule['port_range'].get('min')
|
||||
port_range_max = rule['port_range'].get('max')
|
||||
self.security_group_create_rule(
|
||||
self.sdk_checkmode(self.api.cloudapi.security_group.create_rule)(
|
||||
security_group_id=self.id,
|
||||
direction=self.SecurityGroupRuleDirection[rule['direction']],
|
||||
traffic_direction=(
|
||||
sdk_types.TrafficDirection[rule['direction']]
|
||||
),
|
||||
ethertype=(
|
||||
self.SecurityGroupRuleEtherType[rule['ethertype']]
|
||||
if rule.get('ethertype') else None
|
||||
sdk_types.SGRuleEthertype[rule['ethertype']]
|
||||
if rule.get('ethertype') else sdk_types.SGRuleEthertype.IPV4
|
||||
),
|
||||
protocol=(
|
||||
self.SecurityGroupRuleProtocol[rule['protocol']]
|
||||
sdk_types.SGRuleProtocol[rule['protocol']]
|
||||
if rule.get('protocol') else None
|
||||
),
|
||||
port_range_min=port_range_min,
|
||||
port_range_max=port_range_max,
|
||||
remote_ip_prefix=rule.get('remote_ip_prefix'),
|
||||
remote_net_cidr=rule.get('remote_net_cidr'),
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user