|
| 1 | +from dataclasses import dataclass, asdict |
| 2 | +from enum import Enum, auto |
| 3 | +from typing import TYPE_CHECKING, Optional, List |
| 4 | + |
| 5 | +from cloudfoundry_client.v3.entities import EntityManager, ToManyRelationship, Entity, ToOneRelationship |
| 6 | + |
| 7 | +if TYPE_CHECKING: |
| 8 | + from cloudfoundry_client.client import CloudFoundryClient |
| 9 | + |
| 10 | + |
| 11 | +class RuleProtocol(Enum): |
| 12 | + TCP = auto() |
| 13 | + UDP = auto() |
| 14 | + ICMP = auto() |
| 15 | + ALL = auto() |
| 16 | + |
| 17 | + def __repr__(self): |
| 18 | + return '%s' % self.name.lower() |
| 19 | + |
| 20 | + |
| 21 | +@dataclass |
| 22 | +class Rule: |
| 23 | + protocol: RuleProtocol |
| 24 | + destination: str |
| 25 | + ports: Optional[str] = None |
| 26 | + type: Optional[int] = None |
| 27 | + code: Optional[int] = None |
| 28 | + description: Optional[str] = None |
| 29 | + log: Optional[bool] = None |
| 30 | + |
| 31 | + |
| 32 | +@dataclass |
| 33 | +class GloballyEnabled: |
| 34 | + running: Optional[bool] = None |
| 35 | + staging: Optional[bool] = None |
| 36 | + |
| 37 | + |
| 38 | +class SecurityGroupManager(EntityManager): |
| 39 | + def __init__(self, target_endpoint: str, client: "CloudFoundryClient"): |
| 40 | + super(SecurityGroupManager, self).__init__(target_endpoint, client, "/v3/security_groups") |
| 41 | + |
| 42 | + def create(self, |
| 43 | + name: str, |
| 44 | + rules: Optional[List[Rule]] = None, |
| 45 | + globally_enabled: Optional[GloballyEnabled] = None, |
| 46 | + staging_spaces: Optional[ToManyRelationship] = None, |
| 47 | + running_spaces: Optional[ToManyRelationship] = None) -> Entity: |
| 48 | + payload = self._generate_payload(name, rules, globally_enabled, staging_spaces, running_spaces) |
| 49 | + return super()._create(payload) |
| 50 | + |
| 51 | + def update(self, |
| 52 | + security_group_id: str, |
| 53 | + name: Optional[str] = None, |
| 54 | + rules: Optional[List[Rule]] = None, |
| 55 | + globally_enabled: Optional[GloballyEnabled] = None, |
| 56 | + staging_spaces: Optional[ToManyRelationship] = None, |
| 57 | + running_spaces: Optional[ToManyRelationship] = None) -> Entity: |
| 58 | + payload = self._generate_payload(name, rules, globally_enabled, staging_spaces, running_spaces) |
| 59 | + return super()._update(security_group_id, payload) |
| 60 | + |
| 61 | + def remove(self, security_group_id: str): |
| 62 | + return super()._remove(security_group_id) |
| 63 | + |
| 64 | + def bind_running_security_group_to_spaces(self, security_group_id: str, space_guids: ToManyRelationship) \ |
| 65 | + -> ToManyRelationship: |
| 66 | + relationship = "running_spaces" |
| 67 | + return self._bind_spaces(security_group_id, space_guids, relationship) |
| 68 | + |
| 69 | + def bind_staging_security_group_to_spaces(self, security_group_id: str, space_guids: ToManyRelationship) \ |
| 70 | + -> ToManyRelationship: |
| 71 | + relationship = "staging_spaces" |
| 72 | + return self._bind_spaces(security_group_id, space_guids, relationship) |
| 73 | + |
| 74 | + def unbind_running_security_group_from_space(self, security_group_id: str, space_guid: ToOneRelationship): |
| 75 | + relationship = "running_spaces" |
| 76 | + return self._unbind_space(security_group_id, space_guid, relationship) |
| 77 | + |
| 78 | + def unbind_staging_security_group_from_space(self, security_group_id: str, space_guid: ToOneRelationship): |
| 79 | + relationship = "staging_spaces" |
| 80 | + return self._unbind_space(security_group_id, space_guid, relationship) |
| 81 | + |
| 82 | + def _bind_spaces(self, security_group_id: str, space_guids: ToManyRelationship, relationship: str) \ |
| 83 | + -> ToManyRelationship: |
| 84 | + url = "%s%s/%s/relationships/%s" % (self.target_endpoint, self.entity_uri, security_group_id, relationship) |
| 85 | + return ToManyRelationship.from_json_object(super()._post(url, space_guids)) |
| 86 | + |
| 87 | + def _unbind_space(self, security_group_id: str, space_guid: ToOneRelationship, relationship: str): |
| 88 | + url = "%s%s/%s/relationships/%s/%s" \ |
| 89 | + % (self.target_endpoint, self.entity_uri, security_group_id, relationship, space_guid.guid) |
| 90 | + super()._delete(url) |
| 91 | + |
| 92 | + @staticmethod |
| 93 | + def _generate_payload(name: Optional[str], |
| 94 | + rules: Optional[List[Rule]], |
| 95 | + globally_enabled: Optional[GloballyEnabled], |
| 96 | + staging_spaces: Optional[ToManyRelationship], |
| 97 | + running_spaces: Optional[ToManyRelationship]): |
| 98 | + payload = {} |
| 99 | + if name: |
| 100 | + payload["name"] = name |
| 101 | + if rules: |
| 102 | + payload["rules"] = [asdict(rule, dict_factory=lambda x: {k: repr(v) if k == "protocol" else v |
| 103 | + for (k, v) in x if v is not None}) |
| 104 | + for rule in rules] |
| 105 | + if globally_enabled: |
| 106 | + payload["globally_enabled"] = asdict(globally_enabled, |
| 107 | + dict_factory=lambda x: {k: v for (k, v) in x if v is not None}) |
| 108 | + relationships = dict() |
| 109 | + if staging_spaces: |
| 110 | + relationships["staging_spaces"] = staging_spaces |
| 111 | + if running_spaces: |
| 112 | + relationships["running_spaces"] = running_spaces |
| 113 | + if len(relationships) > 0: |
| 114 | + payload["relationships"] = relationships |
| 115 | + return payload |
0 commit comments