|
| 1 | +# Copyright The Cloud Custodian Authors. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import logging |
| 5 | + |
| 6 | +from huaweicloudsdkcore.exceptions import exceptions |
| 7 | +from huaweicloudsdkeip.v2 import DeletePublicipRequest |
| 8 | +from huaweicloudsdkeip.v3 import DisassociatePublicipsRequest |
| 9 | + |
| 10 | +from c7n.utils import type_schema, local_session |
| 11 | +from c7n.filters import Filter |
| 12 | +from c7n_huaweicloud.actions.base import HuaweiCloudBaseAction |
| 13 | +from c7n_huaweicloud.provider import resources |
| 14 | +from c7n_huaweicloud.query import QueryResourceManager, TypeInfo |
| 15 | + |
| 16 | +log = logging.getLogger("custodian.huaweicloud.resources.eip") |
| 17 | + |
| 18 | + |
| 19 | +@resources.register("eip") |
| 20 | +class EIP(QueryResourceManager): |
| 21 | + """HuaweiCloud Elastic IP Resource |
| 22 | +
|
| 23 | + :example: |
| 24 | +
|
| 25 | + .. code-block:: yaml |
| 26 | +
|
| 27 | + policies: |
| 28 | + - name: eip-unattached |
| 29 | + resource: huaweicloud.eip |
| 30 | + filters: |
| 31 | + - type: value |
| 32 | + key: status |
| 33 | + value: DOWN |
| 34 | + """ |
| 35 | + |
| 36 | + class resource_type(TypeInfo): |
| 37 | + service = "eip" |
| 38 | + enum_spec = ("list_publicips", "publicips", "marker") |
| 39 | + id = "id" |
| 40 | + tag_resource_type = "eip" |
| 41 | + |
| 42 | + |
| 43 | +@EIP.filter_registry.register("associate-instance-type") |
| 44 | +class AssociateInstanceTypeFilter(Filter): |
| 45 | + """EIP Associated Instance Type Filter |
| 46 | +
|
| 47 | + Filter EIPs based on associated instance type (e.g., PORT, NATGW, ELB, ELBV1, VPN, etc.) |
| 48 | +
|
| 49 | + :example: |
| 50 | +
|
| 51 | + .. code-block:: yaml |
| 52 | +
|
| 53 | + policies: |
| 54 | + - name: eip-associated-with-elb |
| 55 | + resource: huaweicloud.eip |
| 56 | + filters: |
| 57 | + - type: associate-instance-type |
| 58 | + instance_type: ELB |
| 59 | + """ |
| 60 | + schema = type_schema( |
| 61 | + "associate-instance-type", |
| 62 | + instance_type={"type": "string", "enum": ["PORT", "NATGW", "ELB", "ELBV1", "VPN", "NONE"]}, |
| 63 | + required=["instance_type"] |
| 64 | + ) |
| 65 | + |
| 66 | + def process(self, resources, event=None): |
| 67 | + instance_type = self.data.get("instance_type") |
| 68 | + results = [] |
| 69 | + |
| 70 | + for resource in resources: |
| 71 | + # Check if associate_instance_type is empty (not associated with any instance) |
| 72 | + resource_instance_type = resource.get("associate_instance_type", "") |
| 73 | + |
| 74 | + if not resource_instance_type: |
| 75 | + # Not associated with any instance |
| 76 | + if instance_type == "NONE": |
| 77 | + results.append(resource) |
| 78 | + continue |
| 79 | + |
| 80 | + # Match based on associate_instance_type returned by API |
| 81 | + if resource_instance_type == instance_type: |
| 82 | + results.append(resource) |
| 83 | + |
| 84 | + return results |
| 85 | + |
| 86 | + |
| 87 | +@EIP.action_registry.register("delete") |
| 88 | +class EIPDelete(HuaweiCloudBaseAction): |
| 89 | + """Delete Elastic IP |
| 90 | +
|
| 91 | + Automatically disassociates the EIP before deletion if it's bound to an instance |
| 92 | +
|
| 93 | + Note: If the EIP is associated with a NATGW instance, please use nat-snat-rule or nat-dnat-rule |
| 94 | + delete actions instead |
| 95 | +
|
| 96 | + :example: |
| 97 | +
|
| 98 | + .. code-block:: yaml |
| 99 | +
|
| 100 | + policies: |
| 101 | + - name: delete-unassociated-eips |
| 102 | + resource: huaweicloud.eip |
| 103 | + filters: |
| 104 | + - type: value |
| 105 | + key: status |
| 106 | + value: DOWN |
| 107 | + actions: |
| 108 | + - delete |
| 109 | + """ |
| 110 | + schema = type_schema("delete") |
| 111 | + |
| 112 | + def process(self, resources): |
| 113 | + session = local_session(self.manager.session_factory) |
| 114 | + # Use eip_v3 client for disassociation |
| 115 | + client_v3 = self.manager.get_client() |
| 116 | + # Use eip_v2 client for deletion |
| 117 | + client_v2 = session.client('eip_v2') |
| 118 | + processed_resources = [] |
| 119 | + |
| 120 | + for resource in resources: |
| 121 | + try: |
| 122 | + # Check if EIP is associated with NATGW instance |
| 123 | + if resource.get("associate_instance_type") == "NATGW": |
| 124 | + self.log.error( |
| 125 | + f"Cannot delete EIP {resource['id']} associated with NATGW, " |
| 126 | + f"please use nat-snat-rule or nat-dnat-rule delete action instead." |
| 127 | + ) |
| 128 | + self.failed_resources.append(resource) |
| 129 | + continue |
| 130 | + |
| 131 | + # If EIP status is ACTIVE (bound), disassociate it first |
| 132 | + if resource.get("status") == "ACTIVE": |
| 133 | + try: |
| 134 | + request = DisassociatePublicipsRequest() |
| 135 | + request.publicip_id = resource["id"] |
| 136 | + client_v3.disassociate_publicips(request) |
| 137 | + self.log.info(f"Successfully disassociated EIP {resource['id']}") |
| 138 | + except exceptions.ClientRequestException as e: |
| 139 | + self.log.error( |
| 140 | + f"Failed to disassociate EIP {resource['id']}, " |
| 141 | + f"Request ID: {e.request_id}," |
| 142 | + f" Error Code: {e.error_code}, Error Message: {e.error_msg}" |
| 143 | + ) |
| 144 | + self.failed_resources.append(resource) |
| 145 | + continue |
| 146 | + |
| 147 | + # Perform deletion |
| 148 | + request = DeletePublicipRequest(publicip_id=resource["id"]) |
| 149 | + client_v2.delete_publicip(request) |
| 150 | + self.log.info(f"Successfully deleted EIP {resource['id']}") |
| 151 | + processed_resources.append(resource) |
| 152 | + except exceptions.ClientRequestException as e: |
| 153 | + self.log.error( |
| 154 | + f"Failed to delete EIP {resource['id']}, " |
| 155 | + f"Request ID: {e.request_id}, Error Code: {e.error_code}" |
| 156 | + f", Error Message: {e.error_msg}" |
| 157 | + ) |
| 158 | + self.failed_resources.append(resource) |
| 159 | + |
| 160 | + # Add successfully processed resources to the result |
| 161 | + self.result.get("succeeded_resources").extend(processed_resources) |
| 162 | + return self.result |
| 163 | + |
| 164 | + def perform_action(self, resource): |
| 165 | + # No additional operation needed as we have |
| 166 | + # already processed each resource in the process method |
| 167 | + pass |
| 168 | + |
| 169 | + |
| 170 | +@EIP.action_registry.register("disassociate") |
| 171 | +class EIPDisassociate(HuaweiCloudBaseAction): |
| 172 | + """Disassociate Elastic IP |
| 173 | +
|
| 174 | + Disassociates an EIP from the instance it is bound to |
| 175 | +
|
| 176 | + Note: If the EIP is associated with a NATGW instance, |
| 177 | + please use nat-snat-rule or nat-dnat-rule |
| 178 | + delete actions instead |
| 179 | +
|
| 180 | + :example: |
| 181 | +
|
| 182 | + .. code-block:: yaml |
| 183 | +
|
| 184 | + policies: |
| 185 | + - name: disassociate-eips-from-instances |
| 186 | + resource: huaweicloud.eip |
| 187 | + filters: |
| 188 | + - type: value |
| 189 | + key: status |
| 190 | + value: ACTIVE |
| 191 | + actions: |
| 192 | + - disassociate |
| 193 | + """ |
| 194 | + schema = type_schema("disassociate") |
| 195 | + |
| 196 | + def process(self, resources): |
| 197 | + client = self.manager.get_client() |
| 198 | + # Filter EIPs with ACTIVE status (bound to instances) |
| 199 | + active_resources = [r for r in resources if r.get("status") == "ACTIVE"] |
| 200 | + processed_resources = [] |
| 201 | + |
| 202 | + for resource in active_resources: |
| 203 | + try: |
| 204 | + # Check if EIP is associated with NATGW instance |
| 205 | + if resource.get("associate_instance_type") == "NATGW": |
| 206 | + self.log.error( |
| 207 | + f"Cannot disassociate EIP {resource['id']} associated with NATGW, " |
| 208 | + f"please use nat-snat-rule or nat-dnat-rule delete action instead." |
| 209 | + ) |
| 210 | + self.failed_resources.append(resource) |
| 211 | + continue |
| 212 | + |
| 213 | + request = DisassociatePublicipsRequest() |
| 214 | + request.publicip_id = resource["id"] |
| 215 | + client.disassociate_publicips(request) |
| 216 | + self.log.info(f"Successfully disassociated EIP {resource['id']}") |
| 217 | + processed_resources.append(resource) |
| 218 | + except exceptions.ClientRequestException as e: |
| 219 | + self.log.error( |
| 220 | + f"Failed to disassociate EIP {resource['id']}, " |
| 221 | + f"Request ID: {e.request_id}, Error Code: {e.error_code}," |
| 222 | + f" Error Message: {e.error_msg}" |
| 223 | + ) |
| 224 | + self.failed_resources.append(resource) |
| 225 | + |
| 226 | + # Add successfully processed resources to the result |
| 227 | + self.result.get("succeeded_resources").extend(processed_resources) |
| 228 | + return self.result |
| 229 | + |
| 230 | + def perform_action(self, resource): |
| 231 | + # No additional operation needed as we have already |
| 232 | + # processed each resource in the process method |
| 233 | + pass |
0 commit comments