2021-07-30 08:48:03 -04:00
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
2023-02-02 17:15:17 -08:00
2021-06-22 22:26:26 -04:00
package target
import (
"context"
"fmt"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/errors"
"github.com/hashicorp/boundary/internal/kms"
"github.com/hashicorp/boundary/internal/oplog"
)
2021-07-30 08:48:03 -04:00
// AddTargetHostSources provides the ability to add host sources (hostSourceIds)
// to a target (targetId). The target's current db version must match the
// targetVersion or an error will be returned. The target and a list of current
// host source ids will be returned on success. Zero is not a valid value for the
// WithVersion option and will return an error.
2023-03-30 15:19:30 -04:00
func ( r * Repository ) AddTargetHostSources ( ctx context . Context , targetId string , targetVersion uint32 , hostSourceIds [] string , _ ... Option ) ( Target , error ) {
2021-07-30 08:48:03 -04:00
const op = "target.(Repository).AddTargetHostSources"
2021-06-22 22:26:26 -04:00
if targetId == "" {
2023-03-30 15:19:30 -04:00
return nil , errors . New ( ctx , errors . InvalidParameter , op , "missing target id" )
2021-06-22 22:26:26 -04:00
}
if targetVersion == 0 {
2023-03-30 15:19:30 -04:00
return nil , errors . New ( ctx , errors . InvalidParameter , op , "missing version" )
2021-06-22 22:26:26 -04:00
}
2021-07-30 08:48:03 -04:00
if len ( hostSourceIds ) == 0 {
2023-03-30 15:19:30 -04:00
return nil , errors . New ( ctx , errors . InvalidParameter , op , "missing host source ids" )
2021-06-22 22:26:26 -04:00
}
2022-10-25 18:29:38 -05:00
newHostSources := make ([] any , 0 , len ( hostSourceIds ))
2021-07-30 08:48:03 -04:00
for _ , id := range hostSourceIds {
2023-06-06 17:09:08 -04:00
ths , err := NewTargetHostSet ( ctx , targetId , id )
2021-06-22 22:26:26 -04:00
if err != nil {
2023-03-30 15:19:30 -04:00
return nil , errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to create in memory target host set" ))
2021-06-22 22:26:26 -04:00
}
2021-07-30 08:48:03 -04:00
newHostSources = append ( newHostSources , ths )
2021-06-22 22:26:26 -04:00
}
t := allocTargetView ()
t . PublicId = targetId
if err := r . reader . LookupByPublicId ( ctx , & t ); err != nil {
2023-03-30 15:19:30 -04:00
return nil , errors . Wrap ( ctx , err , op , errors . WithMsg ( fmt . Sprintf ( "failed for %s" , targetId )))
2021-06-22 22:26:26 -04:00
}
var metadata oplog . Metadata
2021-09-29 13:13:49 -04:00
alloc , ok := subtypeRegistry . allocFunc ( t . Subtype ())
if ! ok {
2023-03-30 15:19:30 -04:00
return nil , errors . New ( ctx , errors . InvalidParameter , op , fmt . Sprintf ( "%s is an unsupported target type %s" , t . PublicId , t . Type ))
2021-06-22 22:26:26 -04:00
}
2021-09-29 13:13:49 -04:00
target := alloc ()
target . SetPublicId ( ctx , t . PublicId )
target . SetVersion ( targetVersion + 1 )
metadata = target . Oplog ( oplog . OpType_OP_TYPE_UPDATE )
metadata [ "op-type" ] = append ( metadata [ "op-type" ], oplog . OpType_OP_TYPE_CREATE . String ())
2022-07-20 16:24:46 -07:00
oplogWrapper , err := r . kms . GetWrapper ( ctx , t . GetProjectId (), kms . KeyPurposeOplog )
2021-06-22 22:26:26 -04:00
if err != nil {
2023-03-30 15:19:30 -04:00
return nil , errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to get oplog wrapper" ))
2021-06-22 22:26:26 -04:00
}
2021-07-30 08:48:03 -04:00
var currentHostSources [] HostSource
2021-07-27 12:48:36 -04:00
var currentCredSources [] CredentialSource
2022-12-06 12:28:55 -08:00
var updatedTarget Target
2021-06-22 22:26:26 -04:00
_ , err = r . writer . DoTx (
ctx ,
db . StdRetryCnt ,
db . ExpBackoff {},
func ( reader db . Reader , w db . Writer ) error {
2022-12-06 12:28:55 -08:00
address , err := fetchAddress ( ctx , reader , targetId )
if err != nil && ! errors . IsNotFoundError ( err ) {
return errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to retrieve current target address" ))
}
if address != nil && address . GetAddress () != "" {
return errors . New ( ctx , errors . Conflict , op , "unable to add host sources because a network address is directly assigned to the given target" )
}
2021-06-22 22:26:26 -04:00
msgs := make ([] * oplog . Message , 0 , 2 )
2022-01-05 14:38:38 -05:00
targetTicket , err := w . GetTicket ( ctx , target )
2021-06-22 22:26:26 -04:00
if err != nil {
2021-07-29 18:52:56 -06:00
return errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to get ticket" ))
2021-06-22 22:26:26 -04:00
}
2022-12-06 12:28:55 -08:00
updatedTarget = target . Clone ()
2021-06-22 22:26:26 -04:00
var targetOplogMsg oplog . Message
rowsUpdated , err := w . Update ( ctx , updatedTarget , [] string { "Version" }, nil , db . NewOplogMsg ( & targetOplogMsg ), db . WithVersion ( & targetVersion ))
if err != nil {
2021-07-29 18:52:56 -06:00
return errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to update target version" ))
2021-06-22 22:26:26 -04:00
}
if rowsUpdated != 1 {
2021-07-29 18:52:56 -06:00
return errors . New ( ctx , errors . MultipleRecords , op , fmt . Sprintf ( "updated target and %d rows updated" , rowsUpdated ))
2021-06-22 22:26:26 -04:00
}
msgs = append ( msgs , & targetOplogMsg )
2021-07-30 08:48:03 -04:00
hostSourcesOplogMsgs := make ([] * oplog . Message , 0 , len ( newHostSources ))
if err := w . CreateItems ( ctx , newHostSources , db . NewOplogMsgs ( & hostSourcesOplogMsgs )); err != nil {
return errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to add target host sources" ))
2021-06-22 22:26:26 -04:00
}
2021-07-30 08:48:03 -04:00
msgs = append ( msgs , hostSourcesOplogMsgs ... )
2021-06-22 22:26:26 -04:00
if err := w . WriteOplogEntryWith ( ctx , oplogWrapper , targetTicket , metadata , msgs ); err != nil {
2021-07-29 18:52:56 -06:00
return errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to write oplog" ))
2021-06-22 22:26:26 -04:00
}
2021-07-30 08:48:03 -04:00
currentHostSources , err = fetchHostSources ( ctx , reader , targetId )
2021-06-22 22:26:26 -04:00
if err != nil {
2021-07-30 08:48:03 -04:00
return errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to retrieve current host sources after adds" ))
2021-06-22 22:26:26 -04:00
}
2021-07-27 12:48:36 -04:00
currentCredSources , err = fetchCredentialSources ( ctx , reader , targetId )
2021-06-22 22:26:26 -04:00
if err != nil {
2021-07-29 18:52:56 -06:00
return errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to retrieve current credential sources after adds" ))
2021-06-22 22:26:26 -04:00
}
return nil
},
)
if err != nil {
2023-03-30 15:19:30 -04:00
return nil , errors . Wrap ( ctx , err , op , errors . WithMsg ( "error creating sets" ))
2021-06-22 22:26:26 -04:00
}
2022-12-06 12:28:55 -08:00
2023-03-30 15:19:30 -04:00
updatedTarget . SetHostSources ( currentHostSources )
updatedTarget . SetCredentialSources ( currentCredSources )
return updatedTarget , nil
2021-06-22 22:26:26 -04:00
}
2021-07-30 08:48:03 -04:00
// DeleteTargeHostSources deletes host sources from a target (targetId). The
// target's current db version must match the targetVersion or an error will be
// returned. Zero is not a valid value for the WithVersion option and will
// return an error.
func ( r * Repository ) DeleteTargetHostSources ( ctx context . Context , targetId string , targetVersion uint32 , hostSourceIds [] string , _ ... Option ) ( int , error ) {
const op = "target.(Repository).DeleteTargetHostSources"
2021-06-22 22:26:26 -04:00
if targetId == "" {
2021-07-29 18:52:56 -06:00
return db . NoRowsAffected , errors . New ( ctx , errors . InvalidParameter , op , "missing target id" )
2021-06-22 22:26:26 -04:00
}
if targetVersion == 0 {
2021-07-29 18:52:56 -06:00
return db . NoRowsAffected , errors . New ( ctx , errors . InvalidParameter , op , "missing version" )
2021-06-22 22:26:26 -04:00
}
2021-07-30 08:48:03 -04:00
if len ( hostSourceIds ) == 0 {
return db . NoRowsAffected , errors . New ( ctx , errors . InvalidParameter , op , "missing host source ids" )
2021-06-22 22:26:26 -04:00
}
2022-10-25 18:29:38 -05:00
deleteTargetHostSources := make ([] any , 0 , len ( hostSourceIds ))
2021-07-30 08:48:03 -04:00
for _ , id := range hostSourceIds {
2023-06-06 17:09:08 -04:00
ths , err := NewTargetHostSet ( ctx , targetId , id )
2021-06-22 22:26:26 -04:00
if err != nil {
2021-07-29 18:52:56 -06:00
return db . NoRowsAffected , errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to create in memory target host set" ))
2021-06-22 22:26:26 -04:00
}
2021-07-30 08:48:03 -04:00
deleteTargetHostSources = append ( deleteTargetHostSources , ths )
2021-06-22 22:26:26 -04:00
}
t := allocTargetView ()
t . PublicId = targetId
if err := r . reader . LookupByPublicId ( ctx , & t ); err != nil {
2021-07-29 18:52:56 -06:00
return db . NoRowsAffected , errors . Wrap ( ctx , err , op , errors . WithMsg ( fmt . Sprintf ( "failed for %s" , targetId )))
2021-06-22 22:26:26 -04:00
}
var metadata oplog . Metadata
2021-09-29 13:13:49 -04:00
alloc , ok := subtypeRegistry . allocFunc ( t . Subtype ())
if ! ok {
2021-07-29 18:52:56 -06:00
return db . NoRowsAffected , errors . New ( ctx , errors . InvalidParameter , op , fmt . Sprintf ( "%s is an unsupported target type %s" , t . PublicId , t . Type ))
2021-06-22 22:26:26 -04:00
}
2021-09-29 13:13:49 -04:00
target := alloc ()
target . SetPublicId ( ctx , t . PublicId )
target . SetVersion ( targetVersion + 1 )
metadata = target . Oplog ( oplog . OpType_OP_TYPE_UPDATE )
metadata [ "op-type" ] = append ( metadata [ "op-type" ], oplog . OpType_OP_TYPE_DELETE . String ())
2022-07-20 16:24:46 -07:00
oplogWrapper , err := r . kms . GetWrapper ( ctx , t . GetProjectId (), kms . KeyPurposeOplog )
2021-06-22 22:26:26 -04:00
if err != nil {
2021-07-29 18:52:56 -06:00
return db . NoRowsAffected , errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to get oplog wrapper" ))
2021-06-22 22:26:26 -04:00
}
var totalRowsDeleted int
_ , err = r . writer . DoTx (
ctx ,
db . StdRetryCnt ,
db . ExpBackoff {},
func ( reader db . Reader , w db . Writer ) error {
msgs := make ([] * oplog . Message , 0 , 2 )
2022-01-05 14:38:38 -05:00
targetTicket , err := w . GetTicket ( ctx , target )
2021-06-22 22:26:26 -04:00
if err != nil {
2021-07-29 18:52:56 -06:00
return errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to get ticket" ))
2021-06-22 22:26:26 -04:00
}
updatedTarget := target .( Cloneable ). Clone ()
var targetOplogMsg oplog . Message
rowsUpdated , err := w . Update ( ctx , updatedTarget , [] string { "Version" }, nil , db . NewOplogMsg ( & targetOplogMsg ), db . WithVersion ( & targetVersion ))
if err != nil {
2021-07-29 18:52:56 -06:00
return errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to update target version" ))
2021-06-22 22:26:26 -04:00
}
if rowsUpdated != 1 {
2021-07-29 18:52:56 -06:00
return errors . New ( ctx , errors . MultipleRecords , op , fmt . Sprintf ( "updated target and %d rows updated" , rowsUpdated ))
2021-06-22 22:26:26 -04:00
}
msgs = append ( msgs , & targetOplogMsg )
2021-07-30 08:48:03 -04:00
hostSourcesOplogMsgs := make ([] * oplog . Message , 0 , len ( deleteTargetHostSources ))
rowsDeleted , err := w . DeleteItems ( ctx , deleteTargetHostSources , db . NewOplogMsgs ( & hostSourcesOplogMsgs ))
2021-06-22 22:26:26 -04:00
if err != nil {
2021-07-30 08:48:03 -04:00
return errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to delete target host sources" ))
2021-06-22 22:26:26 -04:00
}
2021-07-30 08:48:03 -04:00
if rowsDeleted != len ( deleteTargetHostSources ) {
return errors . New ( ctx , errors . MultipleRecords , op , fmt . Sprintf ( "target host sources deleted %d did not match request for %d" , rowsDeleted , len ( deleteTargetHostSources )))
2021-06-22 22:26:26 -04:00
}
totalRowsDeleted += rowsDeleted
2021-07-30 08:48:03 -04:00
msgs = append ( msgs , hostSourcesOplogMsgs ... )
2021-06-22 22:26:26 -04:00
if err := w . WriteOplogEntryWith ( ctx , oplogWrapper , targetTicket , metadata , msgs ); err != nil {
2021-07-29 18:52:56 -06:00
return errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to write oplog" ))
2021-06-22 22:26:26 -04:00
}
return nil
},
)
if err != nil {
2021-07-29 18:52:56 -06:00
return db . NoRowsAffected , errors . Wrap ( ctx , err , op )
2021-06-22 22:26:26 -04:00
}
return totalRowsDeleted , nil
}
2021-07-30 08:48:03 -04:00
// SetTargetHostSources will set the target's host sources. Set add and/or delete
// target host sources as need to reconcile the existing sets with the sets
// requested. If hostSourceIds is empty, the target host sources will be cleared. Zero
2021-06-22 22:26:26 -04:00
// is not a valid value for the WithVersion option and will return an error.
2021-07-30 08:48:03 -04:00
func ( r * Repository ) SetTargetHostSources ( ctx context . Context , targetId string , targetVersion uint32 , hostSourceIds [] string , _ ... Option ) ([] HostSource , [] CredentialSource , int , error ) {
const op = "target.(Repository).SetTargetHostSources"
2021-06-22 22:26:26 -04:00
if targetId == "" {
2021-07-29 18:52:56 -06:00
return nil , nil , db . NoRowsAffected , errors . New ( ctx , errors . InvalidParameter , op , "missing target id" )
2021-06-22 22:26:26 -04:00
}
if targetVersion == 0 {
2021-07-29 18:52:56 -06:00
return nil , nil , db . NoRowsAffected , errors . New ( ctx , errors . InvalidParameter , op , "missing version" )
2021-06-22 22:26:26 -04:00
}
t := allocTargetView ()
t . PublicId = targetId
if err := r . reader . LookupByPublicId ( ctx , & t ); err != nil {
2021-07-29 18:52:56 -06:00
return nil , nil , db . NoRowsAffected , errors . Wrap ( ctx , err , op , errors . WithMsg ( fmt . Sprintf ( "failed for %s" , targetId )))
2021-06-22 22:26:26 -04:00
}
// NOTE: calculating that to set can safely happen outside of the write
// transaction since we're using targetVersion to ensure that the only
// operate on the same set of data from these queries that calculate the
// set.
// TODO(mgaffney) 08/2020: Use SQL to calculate changes.
2021-07-30 08:48:03 -04:00
foundThs , err := fetchHostSources ( ctx , r . reader , targetId )
2021-06-22 22:26:26 -04:00
if err != nil {
2021-07-30 08:48:03 -04:00
return nil , nil , db . NoRowsAffected , errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to search for existing target host sources" ))
2021-06-22 22:26:26 -04:00
}
2021-07-30 08:48:03 -04:00
found := map [ string ] HostSource {}
2021-06-22 22:26:26 -04:00
for _ , s := range foundThs {
2021-07-30 08:48:03 -04:00
found [ s . Id ()] = s
2021-06-22 22:26:26 -04:00
}
2022-10-25 18:29:38 -05:00
addHostSources := make ([] any , 0 , len ( hostSourceIds ))
2021-07-30 08:48:03 -04:00
for _ , id := range hostSourceIds {
2021-06-22 22:26:26 -04:00
if _ , ok := found [ id ]; ok {
// found a match, so do nothing (we want to keep it), but remove it
// from found
delete ( found , id )
continue
}
2023-06-06 17:09:08 -04:00
hs , err := NewTargetHostSet ( ctx , targetId , id )
2021-06-22 22:26:26 -04:00
if err != nil {
2021-07-29 18:52:56 -06:00
return nil , nil , db . NoRowsAffected , errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to create in memory target host set" ))
2021-06-22 22:26:26 -04:00
}
2021-07-30 08:48:03 -04:00
addHostSources = append ( addHostSources , hs )
2021-06-22 22:26:26 -04:00
}
2022-10-25 18:29:38 -05:00
deleteHostSources := make ([] any , 0 , len ( hostSourceIds ))
2021-06-22 22:26:26 -04:00
if len ( found ) > 0 {
for _ , s := range found {
2023-06-06 17:09:08 -04:00
hs , err := NewTargetHostSet ( ctx , targetId , s . Id ())
2021-06-22 22:26:26 -04:00
if err != nil {
2021-07-29 18:52:56 -06:00
return nil , nil , db . NoRowsAffected , errors . Wrap ( ctx , err , op , errors . WithMsg ( " unable to create in memory target host set" ))
2021-06-22 22:26:26 -04:00
}
2021-07-30 08:48:03 -04:00
deleteHostSources = append ( deleteHostSources , hs )
2021-06-22 22:26:26 -04:00
}
}
2021-07-30 08:48:03 -04:00
if len ( addHostSources ) == 0 && len ( deleteHostSources ) == 0 {
2021-06-22 22:26:26 -04:00
return foundThs , nil , db . NoRowsAffected , nil
}
var metadata oplog . Metadata
2021-09-29 13:13:49 -04:00
alloc , ok := subtypeRegistry . allocFunc ( t . Subtype ())
if ! ok {
2021-07-29 18:52:56 -06:00
return nil , nil , db . NoRowsAffected , errors . New ( ctx , errors . InvalidParameter , op , fmt . Sprintf ( "%s is an unsupported target type %s" , t . PublicId , t . Type ))
2021-06-22 22:26:26 -04:00
}
2021-09-29 13:13:49 -04:00
target := alloc ()
target . SetPublicId ( ctx , t . PublicId )
target . SetVersion ( targetVersion + 1 )
metadata = target . Oplog ( oplog . OpType_OP_TYPE_UPDATE )
2022-07-20 16:24:46 -07:00
oplogWrapper , err := r . kms . GetWrapper ( ctx , t . GetProjectId (), kms . KeyPurposeOplog )
2021-06-22 22:26:26 -04:00
if err != nil {
2021-07-29 18:52:56 -06:00
return nil , nil , db . NoRowsAffected , errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to get oplog wrapper" ))
2021-06-22 22:26:26 -04:00
}
var totalRowsAffected int
_ , err = r . writer . DoTx (
ctx ,
db . StdRetryCnt ,
db . ExpBackoff {},
func ( reader db . Reader , w db . Writer ) error {
2022-12-06 12:28:55 -08:00
address , err := fetchAddress ( ctx , reader , targetId )
if err != nil && ! errors . IsNotFoundError ( err ) {
return errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to retrieve current target address" ))
}
if address != nil && address . GetAddress () != "" {
return errors . New ( ctx , errors . Conflict , op , "unable to set host sources because a network address is directly assigned to the given target" )
}
2021-06-22 22:26:26 -04:00
msgs := make ([] * oplog . Message , 0 , 2 )
2022-01-05 14:38:38 -05:00
targetTicket , err := w . GetTicket ( ctx , target )
2021-06-22 22:26:26 -04:00
if err != nil {
2021-07-29 18:52:56 -06:00
return errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to get ticket" ))
2021-06-22 22:26:26 -04:00
}
updatedTarget := target .( Cloneable ). Clone ()
var targetOplogMsg oplog . Message
rowsUpdated , err := w . Update ( ctx , updatedTarget , [] string { "Version" }, nil , db . NewOplogMsg ( & targetOplogMsg ), db . WithVersion ( & targetVersion ))
if err != nil {
2021-07-29 18:52:56 -06:00
return errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to update target version" ))
2021-06-22 22:26:26 -04:00
}
if rowsUpdated != 1 {
2021-07-30 08:48:03 -04:00
return errors . New ( ctx , errors . MultipleRecords , op , fmt . Sprintf ( "set target host sources: updated target and %d rows updated" , rowsUpdated ))
2021-06-22 22:26:26 -04:00
}
msgs = append ( msgs , & targetOplogMsg )
// Write the new ones in
2021-07-30 08:48:03 -04:00
if len ( addHostSources ) > 0 {
hostSourceOplogMsgs := make ([] * oplog . Message , 0 , len ( addHostSources ))
if err := w . CreateItems ( ctx , addHostSources , db . NewOplogMsgs ( & hostSourceOplogMsgs )); err != nil {
return errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to add target host sources" ))
2021-06-22 22:26:26 -04:00
}
2021-07-30 08:48:03 -04:00
totalRowsAffected += len ( addHostSources )
msgs = append ( msgs , hostSourceOplogMsgs ... )
2021-06-22 22:26:26 -04:00
metadata [ "op-type" ] = append ( metadata [ "op-type" ], oplog . OpType_OP_TYPE_CREATE . String ())
}
// Anything we didn't take out of found needs to be removed
2021-07-30 08:48:03 -04:00
if len ( deleteHostSources ) > 0 {
hostSourceOplogMsgs := make ([] * oplog . Message , 0 , len ( deleteHostSources ))
rowsDeleted , err := w . DeleteItems ( ctx , deleteHostSources , db . NewOplogMsgs ( & hostSourceOplogMsgs ))
2021-06-22 22:26:26 -04:00
if err != nil {
2021-07-30 08:48:03 -04:00
return errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to delete target host source" ))
2021-06-22 22:26:26 -04:00
}
2021-07-30 08:48:03 -04:00
if rowsDeleted != len ( deleteHostSources ) {
return errors . New ( ctx , errors . MultipleRecords , op , fmt . Sprintf ( "target host sources deleted %d did not match request for %d" , rowsDeleted , len ( deleteHostSources )))
2021-06-22 22:26:26 -04:00
}
totalRowsAffected += rowsDeleted
2021-07-30 08:48:03 -04:00
msgs = append ( msgs , hostSourceOplogMsgs ... )
2021-06-22 22:26:26 -04:00
metadata [ "op-type" ] = append ( metadata [ "op-type" ], oplog . OpType_OP_TYPE_DELETE . String ())
}
if err := w . WriteOplogEntryWith ( ctx , oplogWrapper , targetTicket , metadata , msgs ); err != nil {
2021-07-29 18:52:56 -06:00
return errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to write oplog" ))
2021-06-22 22:26:26 -04:00
}
2023-03-30 15:19:30 -04:00
currentHostSources , err := fetchHostSources ( ctx , reader , targetId )
2021-06-22 22:26:26 -04:00
if err != nil {
2021-07-30 08:48:03 -04:00
return errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to retrieve current target host sources after set" ))
2021-06-22 22:26:26 -04:00
}
2023-03-30 15:19:30 -04:00
t . SetHostSources ( currentHostSources )
currentCredSources , err := fetchCredentialSources ( ctx , reader , targetId )
2021-06-22 22:26:26 -04:00
if err != nil {
2021-07-29 18:52:56 -06:00
return errors . Wrap ( ctx , err , op , errors . WithMsg ( "unable to retrieve current target credential sources after set" ))
2021-06-22 22:26:26 -04:00
}
2023-03-30 15:19:30 -04:00
t . SetCredentialSources ( currentCredSources )
2021-06-22 22:26:26 -04:00
return nil
},
)
if err != nil {
2021-07-29 18:52:56 -06:00
return nil , nil , db . NoRowsAffected , errors . Wrap ( ctx , err , op )
2021-06-22 22:26:26 -04:00
}
2023-03-30 15:19:30 -04:00
return t . HostSource , t . CredentialSources , totalRowsAffected , nil
2021-06-22 22:26:26 -04:00
}
2021-07-30 08:48:03 -04:00
func fetchHostSources ( ctx context . Context , r db . Reader , targetId string ) ([] HostSource , error ) {
const op = "target.fetchHostSources"
2021-06-22 22:26:26 -04:00
var hostSets [] * TargetSet
2022-10-25 18:29:38 -05:00
if err := r . SearchWhere ( ctx , & hostSets , "target_id = ?" , [] any { targetId }); err != nil {
2021-07-29 18:52:56 -06:00
return nil , errors . Wrap ( ctx , err , op )
2021-06-22 22:26:26 -04:00
}
2021-07-30 08:48:03 -04:00
// FIXME: When we have direct host additions, there will need to be an
// updated view that unions between sets and hosts, at which point the type
// above will change. For now we just take the libraries and wrap them.
2021-06-22 22:26:26 -04:00
if len ( hostSets ) == 0 {
return nil , nil
}
2021-07-30 08:48:03 -04:00
ret := make ([] HostSource , len ( hostSets ))
for i , lib := range hostSets {
ret [ i ] = lib
}
return ret , nil
2021-06-22 22:26:26 -04:00
}