etna 0.1.43 → 0.1.46

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/bin/etna +12 -1
  3. data/etna.completion +36788 -1
  4. data/lib/commands.rb +118 -0
  5. data/lib/etna/application.rb +13 -16
  6. data/lib/etna/auth.rb +37 -14
  7. data/lib/etna/clients/janus/client.rb +17 -18
  8. data/lib/etna/clients/janus/models.rb +52 -0
  9. data/lib/etna/clients/magma/client.rb +41 -0
  10. data/lib/etna/clients/magma/formatting/models_csv.rb +14 -4
  11. data/lib/etna/clients/magma/models.rb +19 -8
  12. data/lib/etna/clients/magma/workflows/create_project_workflow.rb +1 -1
  13. data/lib/etna/clients/magma/workflows/json_validators.rb +5 -5
  14. data/lib/etna/clients/magma/workflows/model_synchronization_workflow.rb +9 -9
  15. data/lib/etna/clients/metis/client.rb +7 -2
  16. data/lib/etna/clients/metis/models.rb +6 -3
  17. data/lib/etna/clients/metis/workflows/metis_download_workflow.rb +1 -1
  18. data/lib/etna/clients/metis/workflows/sync_metis_data_workflow.rb +23 -0
  19. data/lib/etna/clients/metis/workflows/walk_metis_diff_workflow.rb +95 -0
  20. data/lib/etna/clients/metis/workflows/walk_metis_workflow.rb +31 -0
  21. data/lib/etna/clients/metis/workflows.rb +2 -0
  22. data/lib/etna/controller.rb +48 -1
  23. data/lib/etna/cwl.rb +8 -0
  24. data/lib/etna/environment_variables.rb +50 -0
  25. data/lib/etna/filesystem.rb +2 -0
  26. data/lib/etna/injection.rb +57 -0
  27. data/lib/etna/janus_utils.rb +55 -0
  28. data/lib/etna/permissions.rb +104 -0
  29. data/lib/etna/redirect.rb +36 -0
  30. data/lib/etna/route.rb +54 -5
  31. data/lib/etna/server.rb +13 -1
  32. data/lib/etna/spec/auth.rb +11 -0
  33. data/lib/etna/spec/vcr.rb +28 -3
  34. data/lib/etna/test_auth.rb +25 -1
  35. data/lib/etna/user.rb +4 -26
  36. data/lib/etna.rb +4 -0
  37. metadata +13 -6
data/lib/commands.rb CHANGED
@@ -2,6 +2,7 @@ require 'date'
2
2
  require 'logger'
3
3
  require 'rollbar'
4
4
  require 'tempfile'
5
+ require 'securerandom'
5
6
  require_relative 'helpers'
6
7
  require 'yaml'
7
8
 
@@ -88,6 +89,123 @@ class EtnaApp
88
89
  end
89
90
  end
90
91
 
92
+ class Metis
93
+ include Etna::CommandExecutor
94
+ string_flags << "--project-name"
95
+ string_flags << "--bucket-name"
96
+ string_flags << "--path"
97
+ ROLLING_COUNT = 14
98
+
99
+ class PutArchive < Etna::Command
100
+ include WithEtnaClients
101
+
102
+ def execute(file, project_name:, bucket_name:, path:)
103
+ @project_name = project_name
104
+ @bucket_name = bucket_name
105
+ basename = ::File.basename(path)
106
+ dir = ::File.dirname(path)
107
+
108
+ puts "Creating archive folder"
109
+ metis_client.create_folder(Etna::Clients::Metis::CreateFolderRequest.new(
110
+ project_name: project_name,
111
+ bucket_name: bucket_name,
112
+ folder_path: dir
113
+ ))
114
+
115
+ puts "Listing archive folder"
116
+ files = metis_client.list_folder(Etna::Clients::Metis::ListFolderRequest.new(
117
+ project_name: project_name,
118
+ bucket_name: bucket_name,
119
+ folder_path: dir
120
+ )).files.all
121
+
122
+ if files.select { |f| f.file_name == basename }.length > 0
123
+ timestr = DateTime.now.strftime("%Y%m%d_%H%M")
124
+ puts "Backing up #{dir}/#{basename} to #{dir}/#{basename}.#{timestr}"
125
+ metis_client.rename_file(Etna::Clients::Metis::RenameFileRequest.new(
126
+ project_name: project_name,
127
+ bucket_name: bucket_name,
128
+ file_path: ::File.join(dir, basename),
129
+ new_bucket_name: bucket_name,
130
+ new_file_path: ::File.join(dir, "#{basename}.#{timestr}"),
131
+ ))
132
+ end
133
+
134
+ create_upload_workflow.do_upload(
135
+ ::File.open(file, "r"),
136
+ path
137
+ ) do |progress|
138
+ case progress[0]
139
+ when :error
140
+ puts("Error while uploading: #{progress[1].to_s}")
141
+ else
142
+ end
143
+ end
144
+ puts "Completed upload"
145
+
146
+ backups = files.select { |f| f.file_name != basename }.sort_by(&:file_name).reverse
147
+ if backups.length > ROLLING_COUNT
148
+ backups.slice(ROLLING_COUNT..-1).reverse.each do |f|
149
+ puts "Removing rolling back up #{f.file_name}"
150
+ metis_client.delete_file(Etna::Clients::Metis::DeleteFileRequest.new(
151
+ project_name: project_name,
152
+ bucket_name: bucket_name,
153
+ file_path: ::File.join(dir, f.file_name)
154
+ ))
155
+ end
156
+ end
157
+ end
158
+
159
+ def create_upload_workflow
160
+ Etna::Clients::Metis::MetisUploadWorkflow.new(
161
+ metis_client: @metis_client,
162
+ metis_uid: SecureRandom.hex,
163
+ project_name: @project_name,
164
+ bucket_name: @bucket_name,
165
+ max_attempts: 3
166
+ )
167
+ end
168
+ end
169
+
170
+ class PullArchive < Etna::Command
171
+ include WithEtnaClients
172
+
173
+ def execute(project_name:, bucket_name:, path:)
174
+ @project_name = project_name
175
+ @bucket_name = bucket_name
176
+ basename = ::File.basename(path)
177
+ dir = ::File.dirname(path)
178
+
179
+ files = metis_client.list_folder(Etna::Clients::Metis::ListFolderRequest.new(
180
+ project_name: project_name,
181
+ bucket_name: bucket_name,
182
+ folder_path: dir
183
+ )).files.all
184
+
185
+ files.sort_by(&:file_name).reverse
186
+
187
+ archived = files.select { |f| f.file_name == basename }.first
188
+ if archived.nil?
189
+ archived = files.first
190
+ end
191
+
192
+ tmp = Tempfile.new('download', Dir.pwd)
193
+ begin
194
+ puts "Downloading to #{basename} from #{archived.file_name}"
195
+ metis_client.download_file(archived) do |chunk|
196
+ tmp.write(chunk)
197
+ end
198
+
199
+ # Atomic operation -- ensure we only place the file in position when it successfully loads
200
+ ::File.rename(tmp.path, ::File.join(::Dir.pwd, basename))
201
+ rescue
202
+ tmp.close!
203
+ raise
204
+ end
205
+ end
206
+ end
207
+ end
208
+
91
209
  class Administrate
92
210
  include Etna::CommandExecutor
93
211
 
@@ -51,22 +51,7 @@ module Etna::Application
51
51
  end
52
52
 
53
53
  def configure(opts)
54
- @config = opts
55
-
56
- # Apply environmental variables of the form "APP__x__y__z"
57
- prefix = "#{self.class.name.upcase}__"
58
- ENV.keys.select { |k| k.start_with?(prefix) }.each do |key|
59
- path = key.split("__", -1)
60
- path.shift # drop the first, just app name
61
-
62
- target = @config
63
- while path.length > 1
64
- n = path.shift
65
- target = (target[n.downcase.to_sym] ||= {})
66
- end
67
-
68
- target[path.last.downcase.to_sym] ||= ENV[key]
69
- end
54
+ @config = Etna::EnvironmentVariables.load_from_env('ETNA', root: opts) { |path, value| load_config_from_env_path(path, value) }
70
55
 
71
56
  if (rollbar_config = config(:rollbar)) && rollbar_config[:access_token]
72
57
  Rollbar.configure do |config|
@@ -75,6 +60,14 @@ module Etna::Application
75
60
  end
76
61
  end
77
62
 
63
+ def load_config_from_env_path(path, value)
64
+ return nil if path.empty?
65
+ return nil unless path.last.end_with?('_file')
66
+ path.last.sub!(/_file$/, '')
67
+
68
+ [path.map(&:to_sym), YAML.load(File.read(value))]
69
+ end
70
+
78
71
  def setup_yabeda
79
72
  application = self.id
80
73
  Yabeda.configure do
@@ -168,6 +161,10 @@ module Etna::Application
168
161
  (ENV["#{self.class.name.upcase}_ENV"] || :development).to_sym
169
162
  end
170
163
 
164
+ def test?
165
+ environment == "test"
166
+ end
167
+
171
168
  def id
172
169
  ENV["APP_NAME"] || self.class.name.snake_case.split(/::/).last
173
170
  end
data/lib/etna/auth.rb CHANGED
@@ -18,12 +18,16 @@ module Etna
18
18
  if [ approve_noauth(request), approve_hmac(request), approve_user(request) ].all?{|approved| !approved}
19
19
  return fail_or_redirect(request)
20
20
  end
21
-
21
+
22
22
  @app.call(request.env)
23
23
  end
24
24
 
25
25
  private
26
26
 
27
+ def janus
28
+ @janus ||= Etna::JanusUtils.new
29
+ end
30
+
27
31
  def application
28
32
  @application ||= Etna::Application.instance
29
33
  end
@@ -61,7 +65,7 @@ module Etna
61
65
  application.config(:auth_redirect).chomp('/') + '/login'
62
66
  )
63
67
  uri.query = URI.encode_www_form(refer: request.url)
64
- return [ 302, { 'Location' => uri.to_s }, [] ]
68
+ Etna::Redirect(request).to(uri.to_s)
65
69
  end
66
70
 
67
71
  def approve_noauth(request)
@@ -76,21 +80,35 @@ module Etna
76
80
  # some routes don't need janus approval
77
81
  return true if route && route.ignore_janus?
78
82
 
79
- # only process task tokens right now
80
- return true unless payload['task']
83
+ # process task tokens
84
+ payload[:task] ? janus.valid_task_token?(token) : true
85
+ end
81
86
 
82
- return false unless application.config(:janus) && application.config(:janus)[:host]
87
+ def symbolize_payload_keys(payload)
88
+ payload.map{|k,v| [k.to_sym, v]}.to_h
89
+ end
83
90
 
84
- janus_client = Etna::Clients::Janus.new(
85
- token: token,
86
- host: application.config(:janus)[:host]
87
- )
91
+ def permissions(payload)
92
+ Etna::Permissions.from_encoded_permissions(payload[:perm])
93
+ end
88
94
 
89
- response = janus_client.validate_task_token
95
+ def update_payload(payload, token, request)
96
+ route = server.find_route(request)
90
97
 
91
- return false unless response.code == '200'
98
+ return payload unless route
92
99
 
93
- return true
100
+ begin
101
+ permissions = permissions(payload)
102
+
103
+ janus.resource_projects(token).each do |resource_project|
104
+ permissions.add_permission(
105
+ Etna::Permission.new('v', resource_project.project_name)
106
+ )
107
+ end
108
+ payload[:perm] = permissions.to_string
109
+ end if (!route.ignore_janus? && route.has_user_constraint?(:can_view?))
110
+
111
+ payload
94
112
  end
95
113
 
96
114
  def approve_user(request)
@@ -101,9 +119,14 @@ module Etna
101
119
  begin
102
120
  payload, header = application.sign.jwt_decode(token)
103
121
 
122
+ payload = symbolize_payload_keys(payload)
123
+
104
124
  return false unless janus_approved?(payload, token, request)
105
- return request.env['etna.user'] = Etna::User.new(payload.map{|k,v| [k.to_sym, v]}.to_h, token)
106
- rescue
125
+ return request.env['etna.user'] = Etna::User.new(
126
+ update_payload(payload, token, request),
127
+ token)
128
+ rescue => e
129
+ application.logger.log_error(e)
107
130
  # bail out if anything goes wrong
108
131
  return false
109
132
  end
@@ -8,25 +8,33 @@ module Etna
8
8
  module Clients
9
9
  class Janus < Etna::Clients::BaseClient
10
10
  def get_project(get_project_request = GetProjectRequest.new)
11
- html = nil
11
+ json = nil
12
12
  @etna_client.get(
13
- "/project/#{get_project_request.project_name}",
14
- get_project_request) do |res|
15
- html = res.body
13
+ "/api/admin/#{get_project_request.project_name}/info") do |res|
14
+ json = JSON.parse(res.body, symbolize_names: true)
16
15
  end
17
16
 
18
- HtmlResponse.new(html)
17
+ GetProjectResponse.new(json)
18
+ end
19
+
20
+ def get_projects()
21
+ json = nil
22
+ @etna_client.get('/api/user/projects') do |res|
23
+ json = JSON.parse(res.body, symbolize_names: true)
24
+ end
25
+
26
+ GetProjectsResponse.new(json)
19
27
  end
20
28
 
21
29
  def add_project(add_project_request = AddProjectRequest.new)
22
- @etna_client.post('/add_project', add_project_request) do |res|
30
+ @etna_client.post('/api/admin/add_project', add_project_request) do |res|
23
31
  # Redirect, no response data
24
32
  end
25
33
  end
26
34
 
27
35
  def add_user(add_user_request = AddUserRequest.new)
28
36
  @etna_client.post(
29
- "/add_user/#{add_user_request.project_name}",
37
+ "/api/admin/#{add_user_request.project_name}/add_user",
30
38
  add_user_request) do |res|
31
39
  # Redirect, no response data
32
40
  end
@@ -34,7 +42,7 @@ module Etna
34
42
 
35
43
  def update_permission(update_permission_request = UpdatePermissionRequest.new)
36
44
  @etna_client.post(
37
- "/update_permission/#{update_permission_request.project_name}",
45
+ "/api/admin/#{update_permission_request.project_name}/update_permission",
38
46
  update_permission_request) do |res|
39
47
  # Redirect, no response data
40
48
  end
@@ -42,16 +50,7 @@ module Etna
42
50
 
43
51
  def refresh_token(refresh_token_request = RefreshTokenRequest.new)
44
52
  token = nil
45
- @etna_client.get('/refresh_token', refresh_token_request) do |res|
46
- token = res.body
47
- end
48
-
49
- TokenResponse.new(token)
50
- end
51
-
52
- def viewer_token(viewer_token_request = ViewerTokenRequest.new)
53
- token = nil
54
- @etna_client.get('/viewer_token', viewer_token_request) do |res|
53
+ @etna_client.post('/api/tokens/generate') do |res|
55
54
  token = res.body
56
55
  end
57
56
 
@@ -69,6 +69,58 @@ module Etna
69
69
  end
70
70
  end
71
71
 
72
+ class GetProjectResponse
73
+ attr_reader :raw
74
+
75
+ def initialize(raw = '')
76
+ @raw = raw
77
+ end
78
+
79
+ def project
80
+ Project.new(@raw[:project])
81
+ end
82
+ end
83
+
84
+ class GetProjectsResponse
85
+ attr_reader :raw
86
+
87
+ def initialize(raw = '')
88
+ @raw = raw
89
+ end
90
+
91
+ def projects
92
+ @raw[:projects].map do |project|
93
+ Project.new(project)
94
+ end
95
+ end
96
+ end
97
+
98
+ class Project
99
+ def initialize(raw = '')
100
+ @raw = raw
101
+ end
102
+
103
+ def project_name
104
+ @raw[:project_name]
105
+ end
106
+
107
+ def project_name_full
108
+ @raw[:project_name_full]
109
+ end
110
+
111
+ def permissions
112
+ @raw[:permissions] || []
113
+ end
114
+
115
+ def resource
116
+ !!@raw[:resource]
117
+ end
118
+
119
+ def requires_agreement
120
+ !!@raw[:requires_agreement]
121
+ end
122
+ end
123
+
72
124
  class TokenResponse
73
125
  attr_reader :raw
74
126
 
@@ -6,6 +6,47 @@ require_relative './models'
6
6
 
7
7
  module Etna
8
8
  module Clients
9
+ class LocalMagmaClient # May only be used from within magma app.
10
+ def logger
11
+ ::Magma.instance.logger
12
+ end
13
+
14
+ def user
15
+ permissions = Etna::Permissions.new([])
16
+ permissions.add_permission(Etna::Permission.new('A', 'administration'))
17
+ @user ||= Etna::User.new({name: 'Admin', email: 'etnaagent@ucsf.edu', perm: permissions.to_string})
18
+ end
19
+
20
+ def fake_request(request)
21
+ Rack::Request.new({
22
+ 'rack.request.params' => request.as_json,
23
+ 'etna.logger' => self.logger,
24
+ 'etna.user' => user,
25
+ 'etna.request_id' => 'local',
26
+ })
27
+ end
28
+
29
+ def parse_json_response(response)
30
+ status, headers, body = response
31
+ body = body.join('')
32
+ if status < 200 || status >= 300
33
+ raise Etna::Error.new(body, status)
34
+ end
35
+
36
+ JSON.parse(body)
37
+ end
38
+
39
+ def retrieve(retrieval_request = RetrievalRequest.new)
40
+ controller = ::RetrieveController.new(fake_request(retrieval_request), nil)
41
+ Magma::RetrievalResponse.new(parse_json_response(controller.action))
42
+ end
43
+
44
+ def update_model(update_model_request = UpdateModelRequest.new)
45
+ controller = ::UpdateModelController.new(fake_request(update_model_request), nil)
46
+ Magma::UpdateModelResponse.new(parse_json_response(controller.action))
47
+ end
48
+ end
49
+
9
50
  class Magma < Etna::Clients::BaseClient
10
51
  # This endpoint returns models and records by name:
11
52
  # e.g. params:
@@ -22,6 +22,7 @@ module Etna
22
22
  :new_attribute_name,
23
23
  :attribute_type,
24
24
  :link_model_name,
25
+ :link_attribute_name,
25
26
  :description,
26
27
  :display_name,
27
28
  :format_hint,
@@ -114,7 +115,10 @@ module Etna
114
115
  if attribute.attribute_type == Etna::Clients::Magma::AttributeType::IDENTIFIER
115
116
  # Identifiers for models whose parent link type ends up being a table are non configurable, so we don't
116
117
  # want to include them in the CSV.
117
- if models.find_reciprocal(model: model, link_attribute_name: model.template.parent)&.attribute_type == Etna::Clients::Magma::AttributeType::TABLE
118
+ if models.find_reciprocal(
119
+ model: model,
120
+ link_attribute_name: model.name,
121
+ link_model: models.model(model.template.parent))&.attribute_type == Etna::Clients::Magma::AttributeType::TABLE
118
122
  return
119
123
  end
120
124
  else
@@ -137,6 +141,7 @@ module Etna
137
141
  attribute_name: attribute.name,
138
142
  attribute_type: attribute.attribute_type,
139
143
  link_model_name: attribute.link_model_name,
144
+ link_attribute_name: attribute.link_attribute_name,
140
145
  reciprocal_link_type: models.find_reciprocal(model: model, attribute: attribute)&.attribute_type,
141
146
  description: attribute.description,
142
147
  display_name: attribute.display_name,
@@ -175,7 +180,7 @@ module Etna
175
180
  # This should line up with the attribute names _on the model itself_.
176
181
  ATTRIBUTE_ROW_ENTRIES = [
177
182
  :attribute_type,
178
- :link_model_name, :description,
183
+ :link_model_name, :link_attribute_name, :description,
179
184
  :display_name, :format_hint,
180
185
  :restricted, :read_only,
181
186
  :validation, :attribute_group,
@@ -223,6 +228,7 @@ module Etna
223
228
  end
224
229
  rescue ImportError => e
225
230
  validation_err_block.call(e.message)
231
+ raise e
226
232
  end
227
233
 
228
234
 
@@ -255,7 +261,11 @@ module Etna
255
261
  end
256
262
 
257
263
  row_processor.process(:parent_link_type, :model_name, :parent_model_name) do |parent_link_type, template|
258
- reciprocal = models.find_reciprocal(model_name: template.name, link_attribute_name: template.parent)
264
+ reciprocal = models.find_reciprocal(
265
+ model_name: template.name,
266
+ link_attribute_name: template.name,
267
+ link_model: models.model(template.parent)
268
+ )
259
269
  if reciprocal && reciprocal.attribute_type.to_s != parent_link_type
260
270
  raise ImportError.new("Model #{template.name} was provided multiple parent_link_types: #{reciprocal.attribute_type} and #{parent_link_type}")
261
271
  end
@@ -333,7 +343,7 @@ module Etna
333
343
  att.send(:"#{prop_name}=", value)
334
344
 
335
345
  if att.attribute_type && att.link_model_name
336
- if att.attribute_type == Etna::Clients::Magma::AttributeType::LINK && models.find_reciprocal(model_name: template.name, attribute: att).nil?
346
+ if att.attribute_type == Etna::Clients::Magma::AttributeType::LINK && models.find_reciprocal(model_name: template.name, attribute: att, link_attribute_name: att.link_attribute_name).nil?
337
347
  models.build_model(att.link_model_name).build_template.build_attributes.build_attribute(template.name).tap do |rec_att|
338
348
  rec_att.attribute_name = rec_att.name = template.name
339
349
  rec_att.display_name = prettify(template.name)
@@ -22,12 +22,12 @@ module Etna
22
22
  include JsonSerializableStruct
23
23
  end
24
24
 
25
- class UpdateRequest < Struct.new(:revisions, :project_name, keyword_init: true)
25
+ class UpdateRequest < Struct.new(:revisions, :project_name, :dry_run, keyword_init: true)
26
26
  include JsonSerializableStruct
27
27
  include MultipartSerializableNestedHash
28
28
 
29
29
  def initialize(**params)
30
- super({revisions: {}}.update(params))
30
+ super({revisions: {}, dry_run: false}.update(params))
31
31
  end
32
32
 
33
33
  def update_revision(model_name, record_name, attrs)
@@ -110,7 +110,7 @@ module Etna
110
110
  include JsonSerializableStruct
111
111
  end
112
112
 
113
- class AddProjectAction < Struct.new(:action_name, keyword_init: true)
113
+ class AddProjectAction < Struct.new(:action_name, :no_metis_bucket, keyword_init: true)
114
114
  include JsonSerializableStruct
115
115
 
116
116
  def initialize(**args)
@@ -215,8 +215,8 @@ module Etna
215
215
  end
216
216
 
217
217
  def model(model_key)
218
- return nil unless raw.include?(model_key)
219
- Model.new(raw[model_key])
218
+ return nil unless raw.include?(model_key.to_s)
219
+ Model.new(raw[model_key.to_s])
220
220
  end
221
221
 
222
222
  def all
@@ -239,6 +239,9 @@ module Etna
239
239
  link_model: self.model(attribute&.link_model_name)
240
240
  )
241
241
  return nil if model.nil? || model.name.nil?
242
+
243
+ return link_model&.template&.attributes&.all&.find { |a| a.name == link_attribute_name } if link_attribute_name
244
+
242
245
  link_model&.template&.attributes&.all&.find { |a| a.link_model_name == model.name }
243
246
  end
244
247
 
@@ -434,8 +437,8 @@ module Etna
434
437
  end
435
438
 
436
439
  def attribute(attribute_key)
437
- return nil unless raw.include?(attribute_key)
438
- Attribute.new(raw[attribute_key])
440
+ return nil unless raw.include?(attribute_key.to_s)
441
+ Attribute.new(raw[attribute_key.to_s])
439
442
  end
440
443
 
441
444
  def build_attribute(key)
@@ -457,7 +460,7 @@ module Etna
457
460
  def is_edited?(other)
458
461
  # Don't just override == in case need to do a full comparison.
459
462
  editable_attribute_names = Attribute::EDITABLE_ATTRIBUTE_ATTRIBUTES.map(&:to_s)
460
-
463
+
461
464
  self_editable = raw.slice(*editable_attribute_names)
462
465
  other_editable = other.raw.slice(*editable_attribute_names)
463
466
 
@@ -505,6 +508,14 @@ module Etna
505
508
  @raw['attribute_type'] = val
506
509
  end
507
510
 
511
+ def link_attribute_name
512
+ @raw['link_attribute_name']
513
+ end
514
+
515
+ def link_attribute_name=(val)
516
+ @raw['link_attribute_name'] = val
517
+ end
518
+
508
519
  def link_model_name
509
520
  @raw['link_model_name']
510
521
  end
@@ -59,7 +59,7 @@ module Etna
59
59
  end
60
60
 
61
61
  def create_magma_project_record!
62
- magma_client.update(Etna::Clients::Magma::UpdateRequest.new(
62
+ magma_client.update_json(Etna::Clients::Magma::UpdateRequest.new(
63
63
  project_name: project_name,
64
64
  revisions: {
65
65
  'project' => { project_name => { name: project_name } },
@@ -113,7 +113,11 @@ module Etna
113
113
  end
114
114
 
115
115
  def parent_reciprocal_attribute
116
- @models.find_reciprocal(model: @model, link_attribute_name: @model.template.parent)
116
+ @models.find_reciprocal(
117
+ model: @model,
118
+ link_attribute_name: @model.name,
119
+ link_model: @models.model(@model.template.parent)
120
+ )
117
121
  end
118
122
 
119
123
  def name
@@ -172,10 +176,6 @@ module Etna
172
176
  link_attributes.each do |attribute|
173
177
  check_key("attribute #{attribute.attribute_name}", attribute.raw, 'link_model_name')
174
178
 
175
- if attribute.attribute_name != attribute.link_model_name
176
- @errors << "Linked model, \"#{attribute.link_model_name}\", does not match attribute #{attribute.attribute_name}, link attribute names must match the model name."
177
- end
178
-
179
179
  unless @models.model_keys.include?(attribute.link_model_name)
180
180
  @errors << "Linked model, \"#{attribute.link_model_name}\", on attribute #{attribute.attribute_name} does not exist!"
181
181
  end
@@ -29,7 +29,7 @@ module Etna
29
29
  end
30
30
 
31
31
  def execute_planned!
32
- @planned_actions.each do |action|
32
+ planned_actions.each do |action|
33
33
  execute_update(action)
34
34
  end
35
35
  end
@@ -131,7 +131,7 @@ module Etna
131
131
  unless attribute.attribute_type == AttributeType::PARENT
132
132
  if attribute.link_model_name
133
133
  ensure_model(attribute.link_model_name)
134
- ensure_model_link(model_name, attribute.link_model_name, attribute.attribute_name)
134
+ ensure_model_link(model_name, attribute.link_model_name, attribute.attribute_name, attribute.link_attribute_name)
135
135
  else
136
136
  ensure_model_attribute(model_name, attribute.attribute_name)
137
137
  end
@@ -144,26 +144,26 @@ module Etna
144
144
  end
145
145
  end
146
146
 
147
- def ensure_model_link(model_name, link_model_name, attribute_name)
147
+ def ensure_model_link(model_name, link_model_name, attribute_name, link_attribute_name)
148
148
  return unless (model = source_models.model(model_name))
149
149
  return unless (source_attribute = model.template.attributes.attribute(attribute_name))
150
150
 
151
151
  return unless (link_model = source_models.model(link_model_name))
152
- link_model_attributes = link_model.template.attributes
153
- reciprocal = link_model_attributes.all.find do |attr|
154
- attr.link_model_name == model_name
155
- end
152
+ return unless (reciprocal = link_model.template.attributes.attribute(link_attribute_name))
156
153
 
157
154
  target_model_name = target_of_source(model_name)
158
155
  target_link_model_name = target_of_source(link_model_name)
159
156
 
160
157
  target_attributes = target_models.model(target_model_name).template.attributes
161
158
  return if target_attributes.attribute_keys.include?(target_link_model_name)
159
+ return if target_attributes.attribute_keys.include?(reciprocal.attribute_name)
160
+
161
+ # skip non-links for circular references so they don't get added twice
162
+ return if link_model_name == model_name && reciprocal.attribute_type != 'link'
162
163
 
163
164
  add_link = AddLinkAction.new
164
- add_link.links << AddLinkDefinition.new(model_name: target_model_name, attribute_name: target_link_model_name, type: source_attribute.attribute_type)
165
+ add_link.links << AddLinkDefinition.new(model_name: target_model_name, attribute_name: attribute_name, type: source_attribute.attribute_type)
165
166
  add_link.links << AddLinkDefinition.new(model_name: target_link_model_name, attribute_name: reciprocal.attribute_name, type: reciprocal.attribute_type)
166
-
167
167
  queue_update(add_link)
168
168
  end
169
169