Search for project and target before upserting

This commit is contained in:
sophia 2021-11-17 11:07:20 -06:00 committed by Paul Hinze
parent a5f0064a86
commit eeafbe4954
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
2 changed files with 18 additions and 9 deletions

View File

@ -148,9 +148,7 @@ func (s *State) projectPut(
s.log.Trace("storing project", "project", value, "basis", value.Basis)
// Grab the stored project if it's available
existProject, err := s.projectGet(dbTxn, memTxn, &vagrant_plugin_sdk.Ref_Project{
ResourceId: value.ResourceId,
})
existProject, err := s.projectFind(dbTxn, memTxn, value)
if err != nil {
// ensure value is nil to identify non-existence
existProject = nil

View File

@ -170,12 +170,23 @@ func (s *State) targetPut(
}
if value.ResourceId == "" {
s.log.Trace("target has no resource id, assuming new target",
"target", value)
if value.ResourceId, err = s.newResourceId(); err != nil {
s.log.Error("failed to create resource id for target", "target", value,
"error", err)
return
// If no resource id is provided, try to find the target based on the name and project
foundTarget, erro := s.targetFind(dbTxn, memTxn, value)
// If an invalid return code is returned from find then an error occured
if _, ok := status.FromError(erro); !ok {
return erro
}
if foundTarget != nil {
value.ResourceId = foundTarget.ResourceId
value.Uuid = foundTarget.Uuid
} else {
s.log.Trace("target has no resource id and could not find matching target, assuming new target",
"target", value)
if value.ResourceId, err = s.newResourceId(); err != nil {
s.log.Error("failed to create resource id for target", "target", value,
"error", err)
return
}
}
if value.Uuid == "" {
s.log.Trace("target has no uuid assigned, assigning...", "target", value)