import volcenginesdknatgateway
from volcenginesdkcore.rest import ApiException


class NatClient:
    """NAT网关服务客户端"""
    
    def __init__(self):
        self._api_instance = volcenginesdknatgateway.NATGATEWAYApi()
    
    def list_nat_gateways(self, max_results=100):
        """获取NAT网关列表"""
        try:
            req = volcenginesdknatgateway.DescribeNatGatewaysRequest()
            if max_results:
                req.max_results = max_results
            
            response = self._api_instance.describe_nat_gateways(req)
            
            if response and response.nat_gateways:
                nat_gateways = []
                for nat in response.nat_gateways:
                    nat_gateways.append({
                        'nat_gateway_id': getattr(nat, 'nat_gateway_id', ''),
                        'nat_gateway_name': getattr(nat, 'nat_gateway_name', ''),
                        'status': getattr(nat, 'status', ''),
                        'spec': getattr(nat, 'spec', ''),
                    })
                
                return {
                    'success': True,
                    'nat_gateway_count': len(nat_gateways),
                    'nat_gateways': nat_gateways
                }
            else:
                return {
                    'success': False,
                    'error': '未找到NAT网关'
                }
                
        except ApiException as e:
            return {
                'success': False,
                'error': f'API调用异常: {str(e)}'
            }
        except Exception as e:
            return {
                'success': False,
                'error': f'其他异常: {str(e)}'
            }
    
    def get_nat_eips(self):
        """获取NAT网关及其关联的EIP地址信息"""
        try:
            req = volcenginesdknatgateway.DescribeNatGatewaysRequest()
            response = self._api_instance.describe_nat_gateways(req)
            
            if response and response.nat_gateways:
                result = []
                for nat in response.nat_gateways:
                    nat_id = getattr(nat, 'nat_gateway_id', '')
                    nat_name = getattr(nat, 'nat_gateway_name', '')
                    eip_addresses = [
                        eip.eip_address for eip in getattr(nat, "eip_addresses", [])
                    ]
                    result.append({
                        "nat_id": nat_id,
                        "nat_name": nat_name,
                        "eips": eip_addresses
                    })
                
                return {
                    'success': True,
                    'nat_count': len(result),
                    'nat_eips': result
                }
            else:
                return {
                    'success': False,
                    'error': '未找到NAT网关'
                }
                
        except ApiException as e:
            return {
                'success': False,
                'error': f'API调用异常: {str(e)}'
            }
        except Exception as e:
            return {
                'success': False,
                'error': f'其他异常: {str(e)}'
            }
