Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions api/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
# <provider>.yaml.
module Fields
include Api::Object::Named::Properties

attr_reader :default_value

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: it is common to have a unit associated with the value. like for example a timeout you could do: timeout_ms=long, or instead timeout=TimeUnit (where the type holds both the number and unit)

attr_reader :description
attr_reader :exclude

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -422,4 +444,5 @@ def property_ns_prefix
]
end
end
# rubocop:enable Metrics/ClassLength
end
4 changes: 4 additions & 0 deletions products/compute/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand Down Expand Up @@ -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.'
Expand Down Expand Up @@ -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: |
Expand Down
9 changes: 7 additions & 2 deletions provider/ansible/documentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions provider/ansible/module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))}" \
Expand All @@ -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
Expand Down