From 6970064107e3889953f9137bd63073cf406b3cdb Mon Sep 17 00:00:00 2001 From: Alex Stephen Date: Wed, 2 May 2018 16:51:19 +0000 Subject: [PATCH] Default values on properties Change-Id: I480dba6f5e173b781091a5179919b8022769fb7e --- api/type.rb | 23 +++++++++++++++++++++++ products/compute/api.yaml | 4 ++++ provider/ansible/documentation.rb | 9 +++++++-- provider/ansible/module.rb | 7 ++++--- 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/api/type.rb b/api/type.rb index 85f78c540391..e8b67892b1e8 100644 --- a/api/type.rb +++ b/api/type.rb @@ -16,12 +16,14 @@ module Api # Represents a property type + # rubocop:disable Metrics/ClassLength class Type < Api::Object::Named # The list of properties (attr_reader) that can be overridden in # .yaml. module Fields include Api::Object::Named::Properties + attr_reader :default_value attr_reader :description attr_reader :exclude @@ -57,6 +59,26 @@ def validate check_optional_property_oneof_default \ :update_verb, %i[POST PUT PATCH NONE], @__resource&.update_verb, Symbol check_optional_property :update_url, ::String + + check_default_value_property + end + + def check_default_value_property + return if @default_value.nil? + + case self + when Api::Type::String + clazz = ::String + when Api::Type::Integer + clazz = ::Integer + when Api::Type::Enum + clazz = ::Symbol + else + raise "Update 'check_default_value_property' method to support " \ + "default value for type #{self.class}" + end + + check_optional_property :default_value, clazz end def type @@ -422,4 +444,5 @@ def property_ns_prefix ] end end + # rubocop:enable Metrics/ClassLength end diff --git a/products/compute/api.yaml b/products/compute/api.yaml index a63bf5f7c0eb..ec821bb47fef 100644 --- a/products/compute/api.yaml +++ b/products/compute/api.yaml @@ -1025,6 +1025,7 @@ objects: description: | How often (in seconds) to send a health check. The default value is 5 seconds. + default_value: 5 - !ruby/object:Api::Type::Time name: 'creationTimestamp' description: 'Creation timestamp in RFC3339 text format.' @@ -1179,6 +1180,7 @@ objects: description: | How often (in seconds) to send a health check. The default value is 5 seconds. + default_value: 5 - !ruby/object:Api::Type::Time name: 'creationTimestamp' description: 'Creation timestamp in RFC3339 text format.' @@ -1215,11 +1217,13 @@ objects: How long (in seconds) to wait before claiming failure. The default value is 5 seconds. It is invalid for timeoutSec to have greater value than checkIntervalSec. + default_value: 5 - !ruby/object:Api::Type::Integer name: 'unhealthyThreshold' description: | A so-far healthy instance will be marked unhealthy after this many consecutive failures. The default value is 2. + default_value: 2 - !ruby/object:Api::Type::Enum name: 'type' description: | diff --git a/provider/ansible/documentation.rb b/provider/ansible/documentation.rb index 6b606c85bfd4..9ae394b97b41 100644 --- a/provider/ansible/documentation.rb +++ b/provider/ansible/documentation.rb @@ -141,11 +141,15 @@ def nested_doc(properties, object, spaces) end # Builds out the minimal YAML block for DOCUMENTATION + # rubocop:disable Metrics/CyclomaticComplexity + # rubocop:disable Metrics/MethodLength def minimal_doc_block(prop, _object, spaces) + required = prop.required && !prop.default_value ? 'true' : 'false' [ minimal_yaml(prop, spaces), indent([ - "required: #{prop.required ? 'true' : 'false'}", + "required: #{required}", + ("default: #{prop.default_value}" if prop.default_value), ('type: bool' if prop.is_a? Api::Type::Boolean), ("aliases: [#{prop.aliases.join(', ')}]" if prop.aliases), (if prop.is_a? Api::Type::Enum @@ -157,7 +161,8 @@ def minimal_doc_block(prop, _object, spaces) ].compact, 4) ] end - # rubocop:enable Metrics/AbcSize + # rubocop:enable Metrics/CyclomaticComplexity + # rubocop:enable Metrics/MethodLength # Builds out the minimal YAML block for RETURNS def minimal_return_block(prop, spaces) diff --git a/provider/ansible/module.rb b/provider/ansible/module.rb index e540b478dcb8..d07bc9074728 100644 --- a/provider/ansible/module.rb +++ b/provider/ansible/module.rb @@ -47,9 +47,11 @@ def nested_obj_dict(prop, object, properties, spaces) end # Returns an array of all base options for a given property. + # rubocop:disable Metrics/CyclomaticComplexity def prop_options(prop, _object, spaces) [ - ('required=True' if prop.required), + ('required=True' if prop.required && !prop.default_value), + ("default=#{prop.default_value}" if prop.default_value), "type=#{quote_string(python_type(prop))}", (choices_enum(prop, spaces) if prop.is_a? Api::Type::Enum), ("elements=#{quote_string(python_type(prop.item_type))}" \ @@ -58,8 +60,7 @@ def prop_options(prop, _object, spaces) if prop.aliases) ].compact end - # rubocop:enable Metrics/AbcSize - # rubocop:enable Metrics/MethodLength + # rubocop:enable Metrics/CyclomaticComplexity # Returns a formatted string represented the choices of an enum # rubocop:disable Metrics/AbcSize