github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/systemauth/converter.go (about) 1 package systemauth 2 3 import ( 4 "database/sql" 5 "encoding/json" 6 7 "github.com/kyma-incubator/compass/components/director/internal/model" 8 9 "github.com/kyma-incubator/compass/components/director/pkg/graphql" 10 pkgmodel "github.com/kyma-incubator/compass/components/director/pkg/model" 11 12 "github.com/kyma-incubator/compass/components/director/internal/repo" 13 14 "github.com/pkg/errors" 15 ) 16 17 // AuthConverter missing godoc 18 //go:generate mockery --name=AuthConverter --output=automock --outpkg=automock --case=underscore --disable-version-string 19 type AuthConverter interface { 20 ToGraphQL(in *model.Auth) (*graphql.Auth, error) 21 ModelFromGraphQLInput(in graphql.AuthInput) (*model.Auth, error) 22 } 23 24 type converter struct { 25 authConverter AuthConverter 26 } 27 28 // NewConverter missing godoc 29 func NewConverter(authConverter AuthConverter) *converter { 30 return &converter{ 31 authConverter: authConverter, 32 } 33 } 34 35 // ToGraphQL missing godoc 36 func (c *converter) ToGraphQL(in *pkgmodel.SystemAuth) (graphql.SystemAuth, error) { 37 if in == nil { 38 return nil, nil 39 } 40 41 auth, err := c.authConverter.ToGraphQL(in.Value) 42 if err != nil { 43 return nil, errors.Wrap(err, "while converting Auth") 44 } 45 46 objectType, err := in.GetReferenceObjectType() 47 if err != nil { 48 return nil, err 49 } 50 51 systemAuthTypeApplication := graphql.SystemAuthReferenceTypeApplication 52 systemAuthTypeRuntime := graphql.SystemAuthReferenceTypeRuntime 53 systemAuthTypeIntSystem := graphql.SystemAuthReferenceTypeIntegrationSystem 54 switch objectType { 55 case pkgmodel.ApplicationReference: 56 return &graphql.AppSystemAuth{ 57 ID: in.ID, 58 Auth: auth, 59 Type: &systemAuthTypeApplication, 60 TenantID: in.TenantID, 61 ReferenceObjectID: in.AppID, 62 }, nil 63 case pkgmodel.IntegrationSystemReference: 64 return &graphql.IntSysSystemAuth{ 65 ID: in.ID, 66 Auth: auth, 67 Type: &systemAuthTypeIntSystem, 68 TenantID: in.TenantID, 69 ReferenceObjectID: in.IntegrationSystemID, 70 }, nil 71 case pkgmodel.RuntimeReference: 72 return &graphql.RuntimeSystemAuth{ 73 ID: in.ID, 74 Auth: auth, 75 Type: &systemAuthTypeRuntime, 76 TenantID: in.TenantID, 77 ReferenceObjectID: in.RuntimeID, 78 }, nil 79 default: 80 return nil, errors.New("invalid object type") 81 } 82 } 83 84 // ToEntity missing godoc 85 func (c *converter) ToEntity(in pkgmodel.SystemAuth) (Entity, error) { 86 value := sql.NullString{} 87 if in.Value != nil { 88 valueMarshalled, err := json.Marshal(in.Value) 89 if err != nil { 90 return Entity{}, errors.Wrap(err, "while marshalling Value") 91 } 92 value.Valid = true 93 value.String = string(valueMarshalled) 94 } 95 96 return Entity{ 97 ID: in.ID, 98 TenantID: repo.NewNullableString(in.TenantID), 99 AppID: repo.NewNullableString(in.AppID), 100 RuntimeID: repo.NewNullableString(in.RuntimeID), 101 IntegrationSystemID: repo.NewNullableString(in.IntegrationSystemID), 102 Value: value, 103 }, nil 104 } 105 106 // FromEntity missing godoc 107 func (c *converter) FromEntity(in Entity) (pkgmodel.SystemAuth, error) { 108 var value *model.Auth 109 if in.Value.Valid { 110 var tmpAuth model.Auth 111 err := json.Unmarshal([]byte(in.Value.String), &tmpAuth) 112 if err != nil { 113 return pkgmodel.SystemAuth{}, err 114 } 115 value = &tmpAuth 116 } 117 118 return pkgmodel.SystemAuth{ 119 ID: in.ID, 120 TenantID: repo.StringPtrFromNullableString(in.TenantID), 121 AppID: repo.StringPtrFromNullableString(in.AppID), 122 RuntimeID: repo.StringPtrFromNullableString(in.RuntimeID), 123 IntegrationSystemID: repo.StringPtrFromNullableString(in.IntegrationSystemID), 124 Value: value, 125 }, nil 126 }