aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/moto/py3/moto/ec2/responses/elastic_ip_addresses.py
blob: eea5e2f6e062143e18b1530e23af1511e1703396 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from moto.ec2.utils import add_tag_specification
from ._base_response import EC2BaseResponse


class ElasticIPAddresses(EC2BaseResponse):
    def allocate_address(self):
        domain = self._get_param("Domain", if_none="standard")
        reallocate_address = self._get_param("Address", if_none=None)
        tags = self._get_multi_param("TagSpecification")
        tags = add_tag_specification(tags)

        if self.is_not_dryrun("AllocateAddress"):
            if reallocate_address:
                address = self.ec2_backend.allocate_address(
                    domain, address=reallocate_address, tags=tags
                )
            else:
                address = self.ec2_backend.allocate_address(domain, tags=tags)
            template = self.response_template(ALLOCATE_ADDRESS_RESPONSE)
            return template.render(address=address)

    def associate_address(self):
        instance = eni = None

        if "InstanceId" in self.querystring:
            instance = self.ec2_backend.get_instance(self._get_param("InstanceId"))
        elif "NetworkInterfaceId" in self.querystring:
            eni = self.ec2_backend.get_network_interface(
                self._get_param("NetworkInterfaceId")
            )
        else:
            self.ec2_backend.raise_error(
                "MissingParameter",
                "Invalid request, expect InstanceId/NetworkId parameter.",
            )

        reassociate = False
        if "AllowReassociation" in self.querystring:
            reassociate = self._get_param("AllowReassociation") == "true"

        if self.is_not_dryrun("AssociateAddress"):
            if instance or eni:
                if "PublicIp" in self.querystring:
                    eip = self.ec2_backend.associate_address(
                        instance=instance,
                        eni=eni,
                        address=self._get_param("PublicIp"),
                        reassociate=reassociate,
                    )
                elif "AllocationId" in self.querystring:
                    eip = self.ec2_backend.associate_address(
                        instance=instance,
                        eni=eni,
                        allocation_id=self._get_param("AllocationId"),
                        reassociate=reassociate,
                    )
                else:
                    self.ec2_backend.raise_error(
                        "MissingParameter",
                        "Invalid request, expect PublicIp/AllocationId parameter.",
                    )
            else:
                self.ec2_backend.raise_error(
                    "MissingParameter",
                    "Invalid request, expect either instance or ENI.",
                )

            template = self.response_template(ASSOCIATE_ADDRESS_RESPONSE)
            return template.render(address=eip)

    def describe_addresses(self):
        self.error_on_dryrun()
        allocation_ids = self._get_multi_param("AllocationId")
        public_ips = self._get_multi_param("PublicIp")
        filters = self._filters_from_querystring()
        addresses = self.ec2_backend.describe_addresses(
            allocation_ids, public_ips, filters
        )
        template = self.response_template(DESCRIBE_ADDRESS_RESPONSE)
        return template.render(addresses=addresses)

    def disassociate_address(self):
        if self.is_not_dryrun("DisAssociateAddress"):
            if "PublicIp" in self.querystring:
                self.ec2_backend.disassociate_address(
                    address=self._get_param("PublicIp")
                )
            elif "AssociationId" in self.querystring:
                self.ec2_backend.disassociate_address(
                    association_id=self._get_param("AssociationId")
                )
            else:
                self.ec2_backend.raise_error(
                    "MissingParameter",
                    "Invalid request, expect PublicIp/AssociationId parameter.",
                )

            return self.response_template(DISASSOCIATE_ADDRESS_RESPONSE).render()

    def release_address(self):
        if self.is_not_dryrun("ReleaseAddress"):
            if "PublicIp" in self.querystring:
                self.ec2_backend.release_address(address=self._get_param("PublicIp"))
            elif "AllocationId" in self.querystring:
                self.ec2_backend.release_address(
                    allocation_id=self._get_param("AllocationId")
                )
            else:
                self.ec2_backend.raise_error(
                    "MissingParameter",
                    "Invalid request, expect PublicIp/AllocationId parameter.",
                )

            return self.response_template(RELEASE_ADDRESS_RESPONSE).render()


ALLOCATE_ADDRESS_RESPONSE = """<AllocateAddressResponse xmlns="http://ec2.amazonaws.com/doc/2013-10-15/">
  <requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
  <publicIp>{{ address.public_ip }}</publicIp>
  <domain>{{ address.domain }}</domain>
  {% if address.allocation_id %}
    <allocationId>{{ address.allocation_id }}</allocationId>
  {% endif %}
</AllocateAddressResponse>"""

ASSOCIATE_ADDRESS_RESPONSE = """<AssociateAddressResponse xmlns="http://ec2.amazonaws.com/doc/2013-10-15/">
  <requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
  <return>true</return>
  {% if address.association_id %}
    <associationId>{{ address.association_id }}</associationId>
  {% endif %}
</AssociateAddressResponse>"""

DESCRIBE_ADDRESS_RESPONSE = """<DescribeAddressesResponse xmlns="http://ec2.amazonaws.com/doc/2013-10-15/">
  <requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
  <addressesSet>
    {% for address in addresses %}
        <item>
          <publicIp>{{ address.public_ip }}</publicIp>
          <domain>{{ address.domain }}</domain>
          {% if address.instance %}
            <instanceId>{{ address.instance.id }}</instanceId>
          {% else %}
            <instanceId/>
          {% endif %}
          {% if address.eni %}
            <networkInterfaceId>{{ address.eni.id }}</networkInterfaceId>
            <privateIpAddress>{{ address.eni.private_ip_address }}</privateIpAddress>
            <networkInterfaceOwnerId>{{ address.eni.owner_id }}</networkInterfaceOwnerId>
          {% else %}
            <networkInterfaceId/>
          {% endif %}
          {% if address.allocation_id %}
            <allocationId>{{ address.allocation_id }}</allocationId>
          {% endif %}
          {% if address.association_id %}
            <associationId>{{ address.association_id }}</associationId>
          {% endif %}
          <tagSet>
          {% for tag in address.get_tags() %}
              <item>
                  <key>{{ tag.key }}</key>
                  <value>{{ tag.value }}</value>
              </item>
          {% endfor %}
          </tagSet>
        </item>
    {% endfor %}
  </addressesSet>
</DescribeAddressesResponse>"""

DISASSOCIATE_ADDRESS_RESPONSE = """<DisassociateAddressResponse xmlns="http://ec2.amazonaws.com/doc/2013-10-15/">
  <requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
  <return>true</return>
</DisassociateAddressResponse>"""

RELEASE_ADDRESS_RESPONSE = """<ReleaseAddressResponse xmlns="http://ec2.amazonaws.com/doc/2013-10-15/">
  <requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
  <return>true</return>
</ReleaseAddressResponse>"""