From e937ffda8eb5b2cca6677789dc09983ee827911c Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 12 Nov 2013 17:29:29 +0100 Subject: [PATCH] Provide a method for looking up resources by name+type --- nixops/backends/ec2.py | 13 ++----------- nixops/deployment.py | 9 +++++++++ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/nixops/backends/ec2.py b/nixops/backends/ec2.py index 39567c293..a718546eb 100644 --- a/nixops/backends/ec2.py +++ b/nixops/backends/ec2.py @@ -815,12 +815,7 @@ def create(self, defn, check, allow_reboot, allow_recreate): # Assign the elastic IP. If necessary, dereference the resource. elastic_ipv4 = defn.elastic_ipv4 if elastic_ipv4.startswith("res-"): - res_name = elastic_ipv4[4:] - res = self.depl.active_resources.get(res_name, None) - if not res: - raise Exception("resource ‘{0}’ does not exist".format(res_name)) - if not isinstance(res, ElasticIPState): - raise Exception("resource ‘{0}’ is not an elastic IP address".format(res_name)) + res = self.depl.get_typed_resource(elastic_ipv4[4:], "elastic-ip") elastic_ipv4 = res.public_ipv4 self.assign_elastic_ip(elastic_ipv4, instance, check) @@ -887,11 +882,7 @@ def create(self, defn, check, allow_reboot, allow_recreate): elif v['disk'].startswith("res-"): res_name = v['disk'][4:] - res = self.depl.active_resources.get(res_name, None) - if not res: - raise Exception("resource ‘{0}’ does not exist".format(res_name)) - if not isinstance(res, EBSVolumeState): - raise Exception("resource ‘{0}’ is not an EBS volume".format(res_name)) + res = self.depl.get_typed_resource(res_name, "ebs-volume") if res.state != self.UP: raise Exception("EBS volume ‘{0}’ has not been created yet".format(res_name)) assert res.volume_id diff --git a/nixops/deployment.py b/nixops/deployment.py index 6e5e1748d..d0a1f5043 100644 --- a/nixops/deployment.py +++ b/nixops/deployment.py @@ -100,6 +100,15 @@ def active_resources(self): return {n: r for n, r in self.resources.items() if not r.obsolete} + def get_typed_resource(self, name, type): + res = self.active_resources.get(name, None) + if not res: + raise Exception("resource ‘{0}’ does not exist".format(name)) + if res.get_type() != type: + raise Exception("resource ‘{0}’ is not of type ‘{1}’".format(name, type)) + return res + + def _set_attrs(self, attrs): """Update deployment attributes in the state file.""" with self._db: