Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing name_pattern -> pattern #1976

Merged
merged 12 commits into from
Jul 11, 2019
6 changes: 0 additions & 6 deletions api/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,6 @@ module Properties
# often used to extract an object from a parent object or a collection.
attr_reader :nested_query

# [Optional] If a resource requires a partial URL when sending the name
# in the API request, this is the pattern that maps a name to a
# partial URL.
attr_reader :name_pattern

# ====================
# IAM Configuration
# ====================
Expand Down Expand Up @@ -159,7 +154,6 @@ def validate
check :read_verb, type: Symbol, default: :GET, allowed: %i[GET POST]
check :delete_verb, type: Symbol, default: :DELETE, allowed: %i[POST PUT PATCH DELETE]
check :update_verb, type: Symbol, default: :PUT, allowed: %i[POST PUT PATCH]
check :name_pattern, type: String

check :input, type: :boolean
check :min_version, type: String
Expand Down
4 changes: 4 additions & 0 deletions api/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ module Fields

# Can only be overriden - we should never set this ourselves.
attr_reader :new_type

# A pattern that maps expected user input to expected API input.
attr_reader :pattern
end

include Fields
Expand Down Expand Up @@ -88,6 +91,7 @@ def validate
default: @__resource&.update_verb

check :update_url, type: ::String
check :pattern, type: ::String

check_default_value_property
check_conflicts
Expand Down
12 changes: 0 additions & 12 deletions products/pubsub/ansible.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,6 @@ datasources: !ruby/object:Overrides::ResourceOverrides
does_not_exist: |
"\"{{resource_name}}\" not in \"{{ results['resources'] | map(attribute='name') | list }}\""
overrides: !ruby/object:Overrides::ResourceOverrides
Topic: !ruby/object:Overrides::Ansible::ResourceOverride
transport: !ruby/object:Overrides::Ansible::Transport
encoder: encode_request
decoder: decode_request
provider_helpers:
- 'products/pubsub/helpers/python/provider_topic.py'
Subscription: !ruby/object:Overrides::Ansible::ResourceOverride
transport: !ruby/object:Overrides::Ansible::Transport
encoder: encode_request
decoder: decode_request
provider_helpers:
- 'products/pubsub/helpers/python/provider_subscription.py'
files: !ruby/object:Provider::Config::Files
compile:
<%= lines(indent(compile('provider/ansible/product~compile.yaml'), 4)) -%>
3 changes: 3 additions & 0 deletions products/pubsub/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ objects:
required: true
description: 'Name of the topic.'
input: true
pattern: 'projects/{{project}}/topics/{{name}}'
- !ruby/object:Api::Type::String
name: 'kmsKeyName'
description: |
Expand Down Expand Up @@ -86,6 +87,7 @@ objects:
description: 'Name of the subscription.'
required: true
input: true
pattern: 'projects/{{project}}/subscriptions/{{name}}'
- !ruby/object:Api::Type::ResourceRef
name: 'topic'
resource: 'Topic'
Expand All @@ -94,6 +96,7 @@ objects:
A reference to a Topic resource.
required: true
input: true
pattern: 'projects/{{project}}/topics/{{topic}}'
- !ruby/object:Api::Type::KeyValuePairs
name: 'labels'
description: |
Expand Down
35 changes: 0 additions & 35 deletions products/pubsub/helpers/python/provider_subscription.py

This file was deleted.

23 changes: 0 additions & 23 deletions products/pubsub/helpers/python/provider_topic.py

This file was deleted.

2 changes: 1 addition & 1 deletion products/sourcerepo/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ objects:
'Official Documentation': 'https://cloud.google.com/source-repositories/'
api: 'https://cloud.google.com/source-repositories/docs/reference/rest/v1/projects.repos'
collection_url_key: 'repos'
name_pattern: 'projects/{{project}}/repos/{{name}}'
description: |
A repository (or repo) is a Git repository storing versioned source content.
properties:
Expand All @@ -48,6 +47,7 @@ objects:
description: |
Resource name of the repository, of the form projects/{{project}}/repos/{{repo}}.
The repo name may contain slashes. eg, projects/myproject/repos/name/with/slash
pattern: 'projects/{{project}}/repos/{{name}}'
- !ruby/object:Api::Type::String
name: 'url'
output: true
Expand Down
8 changes: 4 additions & 4 deletions provider/ansible/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ def response_output(prop, hash_name, module_name)
end
end

def request_output(prop, hash_name, module_name)
# If name + name_pattern, use the function.
return "name_partial_to_full(#{hash_name}.get('name'), module.params)" \
if prop.name == 'name' && prop.__resource.name_pattern
def request_output(prop, hash_name, module_name, allow_pattern = true)
# If type has a pattern, use the function.
return "#{prop.name.underscore}_pattern(#{request_output(prop, hash_name, module_name, false)}, module)" \
if prop.pattern && allow_pattern

return "response.get(#{quote_string(prop.name)})" \
if prop.is_a? Api::Type::FetchedExternal
Expand Down
10 changes: 0 additions & 10 deletions templates/ansible/name_pattern.py.erb

This file was deleted.

26 changes: 26 additions & 0 deletions templates/ansible/pattern.py.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<% object.all_user_properties.select(&:pattern).each do |p| -%>
<%
prop_names = p.pattern.scan(/{{([a-z]*)}}/).flatten
props = object.all_user_properties.select { |p| prop_names.include?(p.name) }
-%>
def <%= p.name.underscore -%>_pattern(name, module):
if name is None:
return

regex = r<%= lines(build_url(regex_url(p.pattern))) -%>

if not re.match(regex, name):
<% if props.any? { |p| p.is_a?(Api::Type::ResourceRef) } -%>
formatted_params = {
'project': module.params['project'],
<% props.each do |url_prop| -%>
'<%= url_prop.name -%>': replace_resource_dict(module.params['<%= url_prop.name -%>'], '<%= url_prop.imports -%>'),
<% end -%>
}
name = <%= build_url(p.pattern) -%>.format(**formatted_params)
<% else -%>
name = <%= build_url(p.pattern) -%>.format(**module.params)
<% end -%>

return name
<% end -%>
6 changes: 3 additions & 3 deletions templates/ansible/resource.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import json
<%
imports = object.imports || []
imports << 'time' if object.async
imports << 're' if !readonly_selflink_rrefs.empty? || object.name_pattern
imports << 're' if !readonly_selflink_rrefs.empty? || object.all_user_properties.any?(&:pattern)
-%>
<%= lines(imports.sort.uniq.map { |i| "import #{i}" }) -%>

Expand Down Expand Up @@ -419,8 +419,8 @@ def is_different(module, response):
# This is for doing comparisons with Ansible's current parameters.
def response_to_hash(module, response):
return <%= lines(python_literal(response_properties(object.properties), use_hash_brackets: true)) -%>
<% if object.name_pattern -%>
<%= lines(compile('templates/ansible/name_pattern.py.erb')) -%>
<% if object.all_user_properties.any?(&:pattern) -%>
<%= lines(compile('templates/ansible/pattern.py.erb')) -%>
<% end -%>
<% readonly_selflink_rrefs.each do |resource| -%>

Expand Down