From ce78ba14625155eb885b52b639aee7974e1334b2 Mon Sep 17 00:00:00 2001 From: huangtianhua Date: Mon, 27 Jun 2016 17:58:37 +0800 Subject: [PATCH] Correct the usage of properties.get() with default value Class Properties overwrites the __getitem__ method, 1. to raise KeyError if the key is an invalid property, then the default value is return in supper.get() method 2. if the key is a valid property, the default value won't be returned by using the format: self.properties.get(key, default_value) This patch changes to the format as following for case2: self.properties.get(key) or default_value Also, if the property is required or has default value in schema, let's use self.properties[key] instead. Change-Id: I2f546c69aa128c9aa6240ebd065df18a799b754d --- contrib/rackspace/rackspace/resources/cloud_loadbalancer.py | 2 +- contrib/rackspace/rackspace/resources/cloud_server.py | 2 +- heat/engine/resources/openstack/heat/instance_group.py | 2 +- heat/engine/resources/openstack/heat/software_component.py | 2 +- heat/engine/resources/openstack/heat/software_deployment.py | 4 ++-- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/contrib/rackspace/rackspace/resources/cloud_loadbalancer.py b/contrib/rackspace/rackspace/resources/cloud_loadbalancer.py index a73474eafd..0e4fc400d0 100644 --- a/contrib/rackspace/rackspace/resources/cloud_loadbalancer.py +++ b/contrib/rackspace/rackspace/resources/cloud_loadbalancer.py @@ -1150,7 +1150,7 @@ class CloudLoadBalancer(resource.Resource): self._validate_https_redirect() # if a vip specifies and id, it can't specify version or type; # otherwise version and type are required - for vip in self.properties.get(self.VIRTUAL_IPS, []): + for vip in self.properties[self.VIRTUAL_IPS]: has_id = vip.get(self.VIRTUAL_IP_ID) is not None has_version = vip.get(self.VIRTUAL_IP_IP_VERSION) is not None has_type = vip.get(self.VIRTUAL_IP_TYPE) is not None diff --git a/contrib/rackspace/rackspace/resources/cloud_server.py b/contrib/rackspace/rackspace/resources/cloud_server.py index 50d134323d..4583008d0e 100644 --- a/contrib/rackspace/rackspace/resources/cloud_server.py +++ b/contrib/rackspace/rackspace/resources/cloud_server.py @@ -119,7 +119,7 @@ class CloudServer(server.Server): self._rack_connect_started_event_sent = False def _config_drive(self): - user_data_format = self.properties.get(self.USER_DATA_FORMAT, "") + user_data_format = self.properties[self.USER_DATA_FORMAT] is_sw_config = user_data_format == self.SOFTWARE_CONFIG user_data = self.properties.get(self.USER_DATA) config_drive = self.properties.get(self.CONFIG_DRIVE) diff --git a/heat/engine/resources/openstack/heat/instance_group.py b/heat/engine/resources/openstack/heat/instance_group.py index e99eaf5993..cddbb89726 100644 --- a/heat/engine/resources/openstack/heat/instance_group.py +++ b/heat/engine/resources/openstack/heat/instance_group.py @@ -362,7 +362,7 @@ class InstanceGroup(stack_resource.StackResource): self._lb_reload() def _lb_reload(self, exclude=None): - lb_names = self.properties.get(self.LOAD_BALANCER_NAMES, None) + lb_names = self.properties.get(self.LOAD_BALANCER_NAMES) or [] if lb_names: lb_dict = dict((name, self.stack[name]) for name in lb_names) lbutils.reload_loadbalancers(self, lb_dict, exclude) diff --git a/heat/engine/resources/openstack/heat/software_component.py b/heat/engine/resources/openstack/heat/software_component.py index f45a3c26ca..2ddfff9a4a 100644 --- a/heat/engine/resources/openstack/heat/software_component.py +++ b/heat/engine/resources/openstack/heat/software_component.py @@ -142,7 +142,7 @@ class SoftwareComponent(sc.SoftwareConfig): # One lifecycle action (e.g. CREATE) can only be associated with one # config; otherwise a way to define ordering would be required. - configs = self.properties.get(self.CONFIGS, []) + configs = self.properties[self.CONFIGS] config_actions = set() for config in configs: actions = config.get(self.CONFIG_ACTIONS) diff --git a/heat/engine/resources/openstack/heat/software_deployment.py b/heat/engine/resources/openstack/heat/software_deployment.py index 0df0005eb3..3f9ae1fa06 100644 --- a/heat/engine/resources/openstack/heat/software_deployment.py +++ b/heat/engine/resources/openstack/heat/software_deployment.py @@ -614,10 +614,10 @@ class SoftwareDeploymentGroup(resource_group.ResourceGroup): update_policy_schema = {} def get_size(self): - return len(self.properties.get(self.SERVERS, {})) + return len(self.properties[self.SERVERS]) def _resource_names(self): - return iter(self.properties.get(self.SERVERS, {})) + return iter(self.properties[self.SERVERS]) def get_resource_def(self, include_all=False): return dict(self.properties)