Skip to content

Commit

Permalink
Merge 49115f2 into 99ca603
Browse files Browse the repository at this point in the history
  • Loading branch information
rambleraptor authored Aug 10, 2018
2 parents 99ca603 + 49115f2 commit ee93eca
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 28 deletions.
20 changes: 10 additions & 10 deletions api/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,18 @@ def initialize(name)
NAME = Api::Type::String.new('name')
end

# Properties that are fetched externally
class FetchedExternal < Type
attr_writer :resource

def api_name
name
end
end

# Represents a fingerprint. A fingerprint is an output-only
# field used for optimistic locking during updates.
class Fingerprint < String
class Fingerprint < FetchedExternal
def validate
super
@output = true if @output.nil?
Expand Down Expand Up @@ -304,15 +313,6 @@ def validate
end
end

# Properties that are fetched externally
class FetchedExternal < Type
attr_writer :resource

def api_name
name
end
end

# Represents a 'selfLink' property, which returns the URI of the resource.
class SelfLink < FetchedExternal
EXPORT_KEY = 'selfLink'.freeze
Expand Down
3 changes: 3 additions & 0 deletions products/compute/chef.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ overrides: !ruby/object:Provider::ResourceOverrides
message = 'Instance cannot be edited'
Chef::Log.fatal message
raise message
provider_helpers:
- 'products/compute/helpers/ruby/provider_instance.rb'
- 'products/compute/helpers/ruby/instance_metadata.rb'
Network: !ruby/object:Provider::Chef::ResourceOverride
handlers: !ruby/object:Provider::Chef::Handlers
# TODO(alexstephen): Better editing from auto to custom
Expand Down
16 changes: 16 additions & 0 deletions provider/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,22 @@ def relative_path(target, base)
Pathname.new(target).relative_path_from(Pathname.new(base))
end

# Filter the properties to keep only the ones requiring custom update
# method and group them by update url & verb.
def properties_by_custom_update(properties)
update_props = properties.reject do |p|
p.update_url.nil? || p.update_verb.nil?
end
update_props.group_by do |p|
{ update_url: p.update_url, update_verb: p.update_verb }
end
end

def update_url(resource, url_part)
return build_url(resource.self_link_url) if url_part.nil?
[resource.__product.base_url, url_part].flatten.join
end

# TODO(nelsonjr): Review all object interfaces and move to private methods
# that should not be exposed outside the object hierarchy.
private
Expand Down
16 changes: 0 additions & 16 deletions provider/terraform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ def build_url(url_parts, _extra = false)
url_parts.flatten.join
end

def update_url(resource, url_part)
return build_url(resource.self_link_url) if url_part.nil?
[resource.__product.base_url, url_part].flatten.join
end

# Transforms a format string with field markers to a regex string with
# capture groups.
#
Expand Down Expand Up @@ -108,17 +103,6 @@ def nested_properties(property)
end
end

# Filter the properties to keep only the ones requiring custom update
# method and group them by update url & verb.
def properties_by_custom_update(properties)
update_props = properties.reject do |p|
p.update_url.nil? || p.update_verb.nil?
end
update_props.group_by do |p|
{ update_url: p.update_url, update_verb: p.update_verb }
end
end

private

# This function uses the resource.erb template to create one file
Expand Down
1 change: 1 addition & 0 deletions spec/provider_chef_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ def allow_open_real_templates
allow_open 'templates/resourceref_mocks.erb'
allow_open 'templates/return_if_object.erb'
allow_open 'templates/transport.erb'
allow_open 'templates/puppetchef/update_props.erb'

allow_open_real_tests

Expand Down
1 change: 1 addition & 0 deletions spec/provider_puppet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@ def allow_open_real_templates
allow_open 'templates/network_spec.yaml.erb'
allow_open 'templates/return_if_object.erb'
allow_open 'templates/transport.erb'
allow_open 'templates/puppetchef/update_props.erb'

allow_open_real_tests
allow_open_real_resourceref
Expand Down
27 changes: 26 additions & 1 deletion templates/ansible/resource.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ __metaclass__ = type
metadata_version = quote_string(@config.manifest.get('metadata_version',
object))
supported_by = quote_string(@config.manifest.get('supported_by', object))
supported_by = quote_string(@config.manifest.get('supported_by', config))
update_props = properties_by_custom_update(object.all_user_properties)
-%>
ANSIBLE_METADATA = {'metadata_version': <%= metadata_version -%>,
'status': <%= @config.manifest.get('status', object) -%>,
Expand Down Expand Up @@ -153,6 +155,10 @@ def main():
('fetch' if object.access_api_results)
])
-%>
<% unless update_props.empty? -%>
update_fields(module, resource_to_request(module),
response_to_hash(module, fetch))
<% end -%>
<%= lines(indent("fetch = #{method}", 16)) -%>
changed = True
else:
Expand Down Expand Up @@ -240,7 +246,7 @@ def main():
end
-%>
<% if object.update.nil? -%>
<% if !false?(object.editable) -%>
<% if object.editable || !update_props.empty? -%>
auth = GcpSession(module, <%= quote_string(prod_name) -%>)
<%
update_verb = object.update_verb.to_s.downcase
Expand All @@ -263,6 +269,25 @@ def main():
<% end # object.update.nil? -%>


<% unless update_props.empty? -%>
def update_fields(module, request, response):
difference = GcpRequest(request).difference(GcpRequest(response))
auth = GcpSession(module, <%= quote_string(prod_name) -%>)
<% update_props.each do |key, props| -%>
if <%= props.map { |prop| "difference.get(#{quote_string(prop.name.underscore)})" }.join(' || ') -%>:
auth.<%= key[:update_verb].downcase -%>(
''.join([
"<%= object.__product.base_url -%>",
"<%= key[:update_url].gsub('{{', '{').gsub('}}', '}') -%>"
]).format(**module.params),
{
<%= lines(request_properties(props, 16)) -%>
}
)
<% end # update_props.each -%>


<% end # unless update_props.empty? -%>
<%=
lines(method_decl('delete', ['module', 'link', ('kind' if object.kind?),
('fetch' if object.access_api_results)]))
Expand Down
10 changes: 9 additions & 1 deletion templates/chef/resource.erb
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,14 @@ module Google
puts # making a newline until we find a better way TODO: find!
compute_changes.each { |log| puts " - #{log.strip}\n" }
<% if object&.handlers&.update.nil? -%>
<%
<% update_props = properties_by_custom_update(object.all_user_properties) -%>
<% update_props.each do |key, props| -%>
<% prop_statement = props.map { |prop| "(@current_resource.#{prop.name.underscore} != @new_resource.#{prop.name.underscore})" }.join(' || ') -%>
if <%= prop_statement %>
<%= props.first.name.downcase -%>_update(@current_resource)
end
<% end # update_props.each -%>
<%
put_new = ["::Google::#{product_ns}::Network",
"#{object.update_verb.to_s.capitalize}.new"
].join('::')
Expand Down Expand Up @@ -505,6 +512,7 @@ module Google
<%= lines(indent(emit_method('self.resource_to_hash', %w[resource], r2h_code,
file_relative), 8)) -%>

<%= lines(indent(compile('templates/puppetchef/update_props.erb'), 2)) -%>
# Copied from Chef > Provider > #converge_if_changed
def compute_changes
properties = @new_resource.class.state_properties.map(&:name)
Expand Down
6 changes: 6 additions & 0 deletions templates/puppet/resource.erb
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ Puppet::Type.type(:<%= object.out_name -%>).provide(:google) do
<% # TODO(nelsonjr): Remove @dirty or SQL does not do idempotent updates. -%>
# return on !@dirty is for aiding testing (puppet already guarantees that)
return if @created || @deleted || !@dirty
<% update_props = properties_by_custom_update(object.all_user_properties) -%>
<% update_props.each do |key, props| -%>
<% prop_statement = props.map { |prop| "@dirty[:#{prop.name.underscore}]" }.join(' || ') -%>
<%= props.first.name.downcase -%>_update(@resource) if <%= prop_statement %>
<% end # update_props.each -%>
<% if object&.handlers&.flush.nil? -%>
<%
put_new = ["Google::#{product_ns}::Network",
Expand Down Expand Up @@ -352,6 +357,7 @@ Puppet::Type.type(:<%= object.out_name -%>).provide(:google) do
}
end

<%= lines(indent(compile('templates/puppetchef/update_props.erb'), 2)) -%>
<% unless object.exports.nil? -%>
<%
exp_list = [
Expand Down
23 changes: 23 additions & 0 deletions templates/puppetchef/update_props.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<% update_props = properties_by_custom_update(object.all_user_properties) -%>
<% update_props.each do |key, props| -%>
def <%= props[0].name.downcase -%>_update(data)
Google::<%= product_ns -%>::Network::<%= key[:update_verb].capitalize -%>.new(
<%= indent(build_url([object.__product.base_url, key[:update_url]]), 4) -%>,
fetch_auth(@resource),
'application/json',
{
<%
update_prop_statements = props.map do |prop|
if prop.is_a? Api::Type::FetchedExternal
"#{prop.api_name}: @fetched['#{prop.api_name}']"
else
"#{prop.api_name}: @resource[:#{prop.out_name}]"
end
end
-%>
<%= lines(indent_list(update_prop_statements, 6)) -%>
}.to_json
).send
end

<% end # update_props.each -%>

0 comments on commit ee93eca

Please sign in to comment.