Skip to content

Commit

Permalink
Chef resource ref has reference to parent (#450)
Browse files Browse the repository at this point in the history
Merged PR #450.
  • Loading branch information
slevenick authored and modular-magician committed Sep 12, 2018
1 parent 3da6f81 commit 94e688f
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 20 deletions.
1 change: 1 addition & 0 deletions api/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ def full_delete_url
def regex_url
self_link_url.join.gsub('{{project}}', '.*')
.gsub('{{name}}', '[a-z1-9\-]*')
.gsub('{{zone}}', '[a-z1-9\-]*')
end

private
Expand Down
2 changes: 1 addition & 1 deletion build/chef/compute
Submodule compute updated 24 files
+9 −6 libraries/google/compute/property/address_address.rb
+9 −6 libraries/google/compute/property/backendservice_selflink.rb
+9 −6 libraries/google/compute/property/disk_name.rb
+9 −6 libraries/google/compute/property/disk_selflink.rb
+18 −6 libraries/google/compute/property/disktype_selflink.rb
+13 −10 libraries/google/compute/property/forwardingrule_selflink.rb
+9 −6 libraries/google/compute/property/httphealthcheck_selflink.rb
+13 −10 libraries/google/compute/property/instance_selflink.rb
+9 −6 libraries/google/compute/property/instancegroup_selflink.rb
+9 −6 libraries/google/compute/property/instancetemplate_selflink.rb
+22 −10 libraries/google/compute/property/license_selflink.rb
+9 −6 libraries/google/compute/property/machinetype_name.rb
+18 −6 libraries/google/compute/property/machinetype_selflink.rb
+9 −6 libraries/google/compute/property/network_selflink.rb
+9 −6 libraries/google/compute/property/region_name.rb
+18 −6 libraries/google/compute/property/region_selflink.rb
+9 −6 libraries/google/compute/property/router_selflink.rb
+9 −6 libraries/google/compute/property/snapshot_selflink.rb
+13 −10 libraries/google/compute/property/sslcertificate_selflink.rb
+9 −6 libraries/google/compute/property/subnetwork_selflink.rb
+13 −10 libraries/google/compute/property/targetpool_selflink.rb
+9 −6 libraries/google/compute/property/targetvpngateway_selflink.rb
+9 −6 libraries/google/compute/property/urlmap_selflink.rb
+9 −6 libraries/google/compute/property/zone_name.rb
2 changes: 1 addition & 1 deletion build/chef/container
2 changes: 1 addition & 1 deletion build/chef/dns
2 changes: 1 addition & 1 deletion build/chef/iam
2 changes: 1 addition & 1 deletion build/chef/sql
2 changes: 1 addition & 1 deletion build/chef/storage
2 changes: 1 addition & 1 deletion build/terraform
48 changes: 36 additions & 12 deletions templates/chef/property/resourceref.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.
<% end -%>
<% if emit_array -%>
require 'google/<%= prop_ns_dir -%>/property/array'
<% end -%>
<%# Requires: resource - name of resource being fetched -%>
<%# imports - name of property being fetched -%>
<%= compile('templates/license.erb') -%>

<%= lines(autogen_notice :ruby) -%>

<% if emit_array -%>
require 'google/<%= prop_ns_dir -%>/property/array'
<% end -%>

module Google
module <%= product_ns %>
module Data
Expand Down Expand Up @@ -61,8 +62,9 @@ module Google
:disabled), 6))
-%>
class <%= class_name %>Catalog < <%= class_name %>
def initialize(title)
def initialize(title, parent_resource)
@title = title
@parent_resource = parent_resource
end

# Chef requires the title for autorequiring
Expand All @@ -84,6 +86,25 @@ module Google
Chef.run_context.resource_collection.each do |entry|
return entry.exports[:<%= imports -%>] if entry.name == @title
end

<%# Readonly ResourceRefs should regex check + assemble from shortnames for selflinks
If the user inputs a shortname e.g. 'n1-standard-1' we can assemble that into a full URL
based off of the parent's project and zone. -%>
<% if property.resource_ref.readonly && property.imports == 'selfLink' -%>

unless /<%= property.resource_ref.regex_url.gsub('/', '\/') -%>/.match(@title)
# We'll assemble the self_link for the user if a full URL was not specified
# We need to retrieve attributes from the parent resource to qualify the URL
if @parent_resource.nil?
raise "Cannot find parent resource for resource #{@title}"
end
return "<%= property.resource_ref.self_link_url.join
.gsub('{{name}}', '#{@title}')
.gsub('{{project}}', '#{@parent_resource.project}')
.gsub('{{zone}}', '#{@parent_resource.zone}')
-%>"
end
<% end # if resource_ref.readonly && selfLink -%>
<% if property.resource_ref.readonly -%>
@title
<% else -%>
Expand Down Expand Up @@ -120,16 +141,19 @@ module Google
module Property
# A class to manage fetching <%= imports -%> from a <%= resource %>
class <%= class_name %>
<%= emit_coerce(product_ns, class_name, 8) -%>
def catalog_parse(value)
def self.coerce
->(parent_resource, value) { <%= "::Google::#{product_ns}::Property::#{class_name}"%>.catalog_parse(value, parent_resource) }
end
def catalog_parse(value, parent_resource = nil)
return if value.nil?
self.class.catalog_parse(value)
self.class.catalog_parse(value, parent_resource)
end
def self.catalog_parse(value)
def self.catalog_parse(value, parent_resource = nil)
return if value.nil?
return value if value.is_a? Data::<%= class_name %>
Data::<%= class_name %>Catalog.new(value)
Data::<%= class_name %>Catalog.new(value, parent_resource)
end
# Used for fetched JSON values
Expand All @@ -145,11 +169,11 @@ module Google
class <%= class_name %>Array < Google::<%= product_ns -%>::Property::Array
<%= emit_coerce(product_ns, "#{class_name}Array", 8) -%>
# Used for parsing Chef catalog
def self.catalog_parse(value)
def self.catalog_parse(value, parent_resource = nil)
return if value.nil?
return <%= class_name %>.catalog_parse(value) \
return <%= class_name %>.catalog_parse(value, parent_resource) \
unless value.is_a?(::Array)
value.map { |v| <%= class_name %>.catalog_parse(v) }
value.map { |v| <%= class_name %>.catalog_parse(v, parent_resource) }
end
# Used for parsing GCP API responses
Expand Down

0 comments on commit 94e688f

Please sign in to comment.