github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/integrationsystem/resolver.go (about) 1 package integrationsystem 2 3 import ( 4 "context" 5 6 pkgmodel "github.com/kyma-incubator/compass/components/director/pkg/model" 7 8 "github.com/kyma-incubator/compass/components/director/pkg/apperrors" 9 10 "github.com/kyma-incubator/compass/components/director/internal/model" 11 "github.com/kyma-incubator/compass/components/director/pkg/graphql" 12 "github.com/kyma-incubator/compass/components/director/pkg/persistence" 13 ) 14 15 // IntegrationSystemService missing godoc 16 //go:generate mockery --name=IntegrationSystemService --output=automock --outpkg=automock --case=underscore --disable-version-string 17 type IntegrationSystemService interface { 18 Create(ctx context.Context, in model.IntegrationSystemInput) (string, error) 19 Get(ctx context.Context, id string) (*model.IntegrationSystem, error) 20 List(ctx context.Context, pageSize int, cursor string) (model.IntegrationSystemPage, error) 21 Update(ctx context.Context, id string, in model.IntegrationSystemInput) error 22 Delete(ctx context.Context, id string) error 23 } 24 25 // IntegrationSystemConverter missing godoc 26 //go:generate mockery --name=IntegrationSystemConverter --output=automock --outpkg=automock --case=underscore --disable-version-string 27 type IntegrationSystemConverter interface { 28 ToGraphQL(in *model.IntegrationSystem) *graphql.IntegrationSystem 29 MultipleToGraphQL(in []*model.IntegrationSystem) []*graphql.IntegrationSystem 30 InputFromGraphQL(in graphql.IntegrationSystemInput) model.IntegrationSystemInput 31 } 32 33 // SystemAuthService missing godoc 34 //go:generate mockery --name=SystemAuthService --output=automock --outpkg=automock --case=underscore --disable-version-string 35 type SystemAuthService interface { 36 ListForObject(ctx context.Context, objectType pkgmodel.SystemAuthReferenceObjectType, objectID string) ([]pkgmodel.SystemAuth, error) 37 } 38 39 // SystemAuthConverter missing godoc 40 //go:generate mockery --name=SystemAuthConverter --output=automock --outpkg=automock --case=underscore --disable-version-string 41 type SystemAuthConverter interface { 42 ToGraphQL(in *pkgmodel.SystemAuth) (graphql.SystemAuth, error) 43 } 44 45 // OAuth20Service missing godoc 46 //go:generate mockery --name=OAuth20Service --output=automock --outpkg=automock --case=underscore --disable-version-string 47 type OAuth20Service interface { 48 DeleteMultipleClientCredentials(ctx context.Context, auths []pkgmodel.SystemAuth) error 49 } 50 51 // Resolver missing godoc 52 type Resolver struct { 53 transact persistence.Transactioner 54 55 intSysSvc IntegrationSystemService 56 sysAuthSvc SystemAuthService 57 oAuth20Svc OAuth20Service 58 intSysConverter IntegrationSystemConverter 59 sysAuthConverter SystemAuthConverter 60 } 61 62 // NewResolver missing godoc 63 func NewResolver(transact persistence.Transactioner, intSysSvc IntegrationSystemService, sysAuthSvc SystemAuthService, oAuth20Svc OAuth20Service, intSysConverter IntegrationSystemConverter, sysAuthConverter SystemAuthConverter) *Resolver { 64 return &Resolver{ 65 transact: transact, 66 intSysSvc: intSysSvc, 67 sysAuthSvc: sysAuthSvc, 68 oAuth20Svc: oAuth20Svc, 69 intSysConverter: intSysConverter, 70 sysAuthConverter: sysAuthConverter, 71 } 72 } 73 74 // IntegrationSystem missing godoc 75 func (r *Resolver) IntegrationSystem(ctx context.Context, id string) (*graphql.IntegrationSystem, error) { 76 tx, err := r.transact.Begin() 77 if err != nil { 78 return nil, err 79 } 80 defer r.transact.RollbackUnlessCommitted(ctx, tx) 81 82 ctx = persistence.SaveToContext(ctx, tx) 83 84 is, err := r.intSysSvc.Get(ctx, id) 85 if err != nil { 86 if apperrors.IsNotFoundError(err) { 87 return nil, tx.Commit() 88 } 89 return nil, err 90 } 91 92 err = tx.Commit() 93 if err != nil { 94 return nil, err 95 } 96 97 return r.intSysConverter.ToGraphQL(is), nil 98 } 99 100 // IntegrationSystems missing godoc 101 func (r *Resolver) IntegrationSystems(ctx context.Context, first *int, after *graphql.PageCursor) (*graphql.IntegrationSystemPage, error) { 102 var cursor string 103 if after != nil { 104 cursor = string(*after) 105 } 106 if first == nil { 107 return nil, apperrors.NewInvalidDataError("missing required parameter 'first'") 108 } 109 110 tx, err := r.transact.Begin() 111 if err != nil { 112 return nil, err 113 } 114 defer r.transact.RollbackUnlessCommitted(ctx, tx) 115 116 ctx = persistence.SaveToContext(ctx, tx) 117 118 intSysPage, err := r.intSysSvc.List(ctx, *first, cursor) 119 if err != nil { 120 return nil, err 121 } 122 123 err = tx.Commit() 124 if err != nil { 125 return nil, err 126 } 127 128 gqlIntSys := r.intSysConverter.MultipleToGraphQL(intSysPage.Data) 129 130 return &graphql.IntegrationSystemPage{ 131 Data: gqlIntSys, 132 TotalCount: intSysPage.TotalCount, 133 PageInfo: &graphql.PageInfo{ 134 StartCursor: graphql.PageCursor(intSysPage.PageInfo.StartCursor), 135 EndCursor: graphql.PageCursor(intSysPage.PageInfo.EndCursor), 136 HasNextPage: intSysPage.PageInfo.HasNextPage, 137 }, 138 }, nil 139 } 140 141 // RegisterIntegrationSystem missing godoc 142 func (r *Resolver) RegisterIntegrationSystem(ctx context.Context, in graphql.IntegrationSystemInput) (*graphql.IntegrationSystem, error) { 143 convertedIn := r.intSysConverter.InputFromGraphQL(in) 144 145 tx, err := r.transact.Begin() 146 if err != nil { 147 return nil, err 148 } 149 defer r.transact.RollbackUnlessCommitted(ctx, tx) 150 151 ctx = persistence.SaveToContext(ctx, tx) 152 153 id, err := r.intSysSvc.Create(ctx, convertedIn) 154 if err != nil { 155 return nil, err 156 } 157 158 intSys, err := r.intSysSvc.Get(ctx, id) 159 if err != nil { 160 return nil, err 161 } 162 163 err = tx.Commit() 164 if err != nil { 165 return nil, err 166 } 167 168 gqlIntSys := r.intSysConverter.ToGraphQL(intSys) 169 170 return gqlIntSys, nil 171 } 172 173 // UpdateIntegrationSystem missing godoc 174 func (r *Resolver) UpdateIntegrationSystem(ctx context.Context, id string, in graphql.IntegrationSystemInput) (*graphql.IntegrationSystem, error) { 175 tx, err := r.transact.Begin() 176 if err != nil { 177 return nil, err 178 } 179 defer r.transact.RollbackUnlessCommitted(ctx, tx) 180 181 ctx = persistence.SaveToContext(ctx, tx) 182 183 convertedIn := r.intSysConverter.InputFromGraphQL(in) 184 err = r.intSysSvc.Update(ctx, id, convertedIn) 185 if err != nil { 186 return nil, err 187 } 188 189 intSys, err := r.intSysSvc.Get(ctx, id) 190 if err != nil { 191 return nil, err 192 } 193 194 err = tx.Commit() 195 if err != nil { 196 return nil, err 197 } 198 199 gqlIntSys := r.intSysConverter.ToGraphQL(intSys) 200 201 return gqlIntSys, nil 202 } 203 204 // UnregisterIntegrationSystem missing godoc 205 func (r *Resolver) UnregisterIntegrationSystem(ctx context.Context, id string) (*graphql.IntegrationSystem, error) { 206 tx, err := r.transact.Begin() 207 if err != nil { 208 return nil, err 209 } 210 defer r.transact.RollbackUnlessCommitted(ctx, tx) 211 212 ctx = persistence.SaveToContext(ctx, tx) 213 214 intSys, err := r.intSysSvc.Get(ctx, id) 215 if err != nil { 216 return nil, err 217 } 218 219 auths, err := r.sysAuthSvc.ListForObject(ctx, pkgmodel.IntegrationSystemReference, intSys.ID) 220 if err != nil { 221 return nil, err 222 } 223 224 err = r.intSysSvc.Delete(ctx, id) 225 if err != nil { 226 return nil, err 227 } 228 229 err = tx.Commit() 230 if err != nil { 231 return nil, err 232 } 233 234 err = r.oAuth20Svc.DeleteMultipleClientCredentials(ctx, auths) 235 if err != nil { 236 return nil, err 237 } 238 239 deletedIntSys := r.intSysConverter.ToGraphQL(intSys) 240 241 return deletedIntSys, nil 242 } 243 244 // Auths missing godoc 245 func (r *Resolver) Auths(ctx context.Context, obj *graphql.IntegrationSystem) ([]*graphql.IntSysSystemAuth, error) { 246 if obj == nil { 247 return nil, apperrors.NewInternalError("Integration System cannot be empty") 248 } 249 250 tx, err := r.transact.Begin() 251 if err != nil { 252 return nil, err 253 } 254 defer r.transact.RollbackUnlessCommitted(ctx, tx) 255 256 ctx = persistence.SaveToContext(ctx, tx) 257 258 sysAuths, err := r.sysAuthSvc.ListForObject(ctx, pkgmodel.IntegrationSystemReference, obj.ID) 259 if err != nil { 260 return nil, err 261 } 262 263 err = tx.Commit() 264 if err != nil { 265 return nil, err 266 } 267 268 out := make([]*graphql.IntSysSystemAuth, 0, len(sysAuths)) 269 for _, sa := range sysAuths { 270 c, err := r.sysAuthConverter.ToGraphQL(&sa) 271 if err != nil { 272 return nil, err 273 } 274 out = append(out, c.(*graphql.IntSysSystemAuth)) 275 } 276 277 return out, nil 278 }