Skip to content

Commit de62c23

Browse files
glittersharkadisbladis
authored andcommitted
Allow Route53 recordsets to point at EIPs
Allow the values in a route53 recordset to be ElasticIP resources, in the same way that they are allowed to be machine resources, and resolve the ip address of the ElasticIP when creating the recordset
1 parent 7983b03 commit de62c23

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

nixops_aws/nix/route53-recordset.nix

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ with (import ./lib.nix lib);
8181
};
8282

8383
recordValues = mkOption {
84-
type = types.listOf (types.either types.str (resource "machine"));
84+
type = types.listOf (types.oneOf [
85+
types.str
86+
(resource "machine")
87+
(resource "elastic-ip")
88+
]);
8589

8690
apply = l: map (x: if (builtins.isString x) || ( x == null) then x else "res-" + x._name) l;
8791

nixops_aws/resources/route53_recordset.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
import boto3
77
import nixops.util
88
import nixops.resources
9+
import nixops.deployment
910
import nixops_aws.ec2_utils
10-
from . import route53_hosted_zone, route53_health_check
11+
from . import route53_hosted_zone, route53_health_check, elastic_ip
1112
from .route53_hosted_zone import Route53HostedZoneState
1213
from .route53_health_check import Route53HealthCheckState
1314
from nixops_aws.backends.ec2 import EC2State
@@ -218,18 +219,32 @@ def create(self, defn, check, allow_reboot, allow_recreate): # noqa: C901
218219
)
219220
)
220221

221-
def resolve_machine_ip(v):
222+
def resolve_ip(v):
222223
if v.startswith("res-"):
223-
m = self.depl.get_machine(v[4:], EC2State)
224-
if not m.public_ipv4:
225-
raise Exception(
226-
"cannot create record set for a machine that has not yet been created"
227-
)
228-
return m.public_ipv4
224+
name = v[4:]
225+
res = self.depl.active_resources.get(name)
226+
if res is None:
227+
raise Exception(f"Resource ‘{name}’ does not exist")
228+
229+
if isinstance(res, EC2State):
230+
m: EC2State = res
231+
if not m.public_ipv4:
232+
raise Exception(
233+
"cannot create record set for a machine that has not yet been created"
234+
)
235+
return m.public_ipv4
236+
elif isinstance(res, elastic_ip.ElasticIPState):
237+
eip: elastic_ip.ElasticIPState = res
238+
if not eip.public_ipv4:
239+
raise Exception(
240+
"cannot create record set for an ElasticIP that has not yet been created"
241+
)
242+
return eip.public_ipv4
243+
# elif v.startswith
229244
else:
230245
return v
231246

232-
defn.record_values = [resolve_machine_ip(m) for m in defn.record_values]
247+
defn.record_values = [resolve_ip(m) for m in defn.record_values]
233248

234249
changed = (
235250
self.record_values != defn.record_values
@@ -350,4 +365,5 @@ def create_after(self, resources, defn):
350365
if isinstance(r, route53_hosted_zone.Route53HostedZoneState)
351366
or isinstance(r, route53_health_check.Route53HealthCheckState)
352367
or isinstance(r, nixops.backends.MachineState)
368+
or isinstance(r, elastic_ip.ElasticIPState)
353369
}

0 commit comments

Comments
 (0)