38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/fastlyctl/clone_utils.rb', line 38
def self.copy(obj,type,sid,version)
meta = FastlyCTL::CloneUtils::OBJECT_TYPES[type]
meta ||= {}
abort "No service ID on object" unless obj.key?("service_id")
source_sid = obj["service_id"]
abort "No version on object" unless obj.key?("version")
source_version = obj["version"]
main = false
backends = obj["backends"].dup if type == "director"
main = true if type == "vcl" && obj["main"] === true
if type == "snippet" && obj["dynamic"] == "1"
obj.merge!(FastlyCTL::Fetcher.api_request(:get, "/service/#{source_sid}/snippet/#{obj["id"]}"))
end
if type.start_with?("logging/") && obj["response_condition"] == ""
obj.delete("response_condition")
end
obj_id = obj["id"]
obj = FastlyCTL::CloneUtils.filter(type,obj)
obj = FastlyCTL::Fetcher.api_request(meta.key?(:method) ? meta[:method] : :post, "/service/#{sid}/version/#{version}/#{type}", body: obj )
if main === true
FastlyCTL::Fetcher.api_request(:put, "/service/#{sid}/version/#{version}/vcl/#{FastlyCTL::Utils.percent_encode(obj["name"])}/main")
end
if type == "director"
backends.each do |b|
FastlyCTL::Fetcher.api_request(:post, "/service/#{sid}/version/#{version}/director/#{FastlyCTL::Utils.percent_encode(obj["name"])}/backend/#{b}", body: obj )
end
end
if type == "dictionary" && obj["write_only"] === true
puts "Unable to clone the contents of a write only dictionary. Creating empty dictionary instead..."
return obj
end
return obj unless meta.key?(:child)
new_obj_id = obj["id"]
child = meta[:child]
path = FastlyCTL::CloneUtils.construct_path(source_sid,source_version,meta[:include_version])
items = []
entries = []
FastlyCTL::Fetcher.api_request(:get, "#{path}/#{type}/#{obj_id}/#{FastlyCTL::CloneUtils.pluralize(child)}").each do |child_obj|
case child
when "item"
items.push({
"op" => "create","item_key" => child_obj["item_key"],"item_value" => child_obj["item_value"]
})
next
when "entry"
entries.push({
"op" => "create","ip" => child_obj["ip"],"subnet" => child_obj["subnet"], "negate" => child_obj["negate"]
})
next
end
child_obj = FastlyCTL::CloneUtils.filter(type,child_obj)
path = FastlyCTL::CloneUtils.construct_path(sid,version,meta[:include_version])
FastlyCTL::Fetcher.api_request(:post, "#{path}/#{type}/#{new_obj_id}/#{child}", body: child_obj )
end
FastlyCTL::Fetcher.api_request(:patch, "/service/#{sid}/dictionary/#{new_obj_id}/items", body: {"items" => items}.to_json, headers: {"Content-Type" => "application/json"} ) if items.length > 0
FastlyCTL::Fetcher.api_request(:patch, "/service/#{sid}/acl/#{new_obj_id}/entries", body: {"entries" => entries}.to_json, headers: {"Content-Type" => "application/json"} ) if entries.length > 0
return obj
end
|