github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/bundle/converter.go (about)

     1  package bundle
     2  
     3  import (
     4  	"database/sql"
     5  	"encoding/json"
     6  	"time"
     7  
     8  	"github.com/kyma-incubator/compass/components/director/pkg/apperrors"
     9  
    10  	"github.com/kyma-incubator/compass/components/director/pkg/str"
    11  
    12  	"github.com/kyma-incubator/compass/components/director/internal/model"
    13  	"github.com/kyma-incubator/compass/components/director/internal/repo"
    14  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
    15  	"github.com/pkg/errors"
    16  )
    17  
    18  // AuthConverter missing godoc
    19  //
    20  //go:generate mockery --name=AuthConverter --output=automock --outpkg=automock --case=underscore --disable-version-string
    21  type AuthConverter interface {
    22  	ToGraphQL(in *model.Auth) (*graphql.Auth, error)
    23  	InputFromGraphQL(in *graphql.AuthInput) (*model.AuthInput, error)
    24  }
    25  
    26  type converter struct {
    27  	auth     AuthConverter
    28  	api      APIConverter
    29  	event    EventConverter
    30  	document DocumentConverter
    31  }
    32  
    33  // NewConverter missing godoc
    34  func NewConverter(auth AuthConverter, api APIConverter, event EventConverter, document DocumentConverter) *converter {
    35  	return &converter{
    36  		auth:     auth,
    37  		api:      api,
    38  		event:    event,
    39  		document: document,
    40  	}
    41  }
    42  
    43  // ToEntity missing godoc
    44  func (c *converter) ToEntity(in *model.Bundle) (*Entity, error) {
    45  	if in == nil {
    46  		return nil, nil
    47  	}
    48  
    49  	defaultInstanceAuth, err := c.marshalDefaultInstanceAuth(in.DefaultInstanceAuth)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  
    54  	output := &Entity{
    55  		ApplicationID:                 repo.NewNullableString(in.ApplicationID),
    56  		ApplicationTemplateVersionID:  repo.NewNullableString(in.ApplicationTemplateVersionID),
    57  		Name:                          in.Name,
    58  		Description:                   repo.NewNullableString(in.Description),
    59  		InstanceAuthRequestJSONSchema: repo.NewNullableString(in.InstanceAuthRequestInputSchema),
    60  		Version:                       repo.NewNullableString(in.Version),
    61  		ResourceHash:                  repo.NewNullableString(in.ResourceHash),
    62  		DefaultInstanceAuth:           repo.NewNullableString(defaultInstanceAuth),
    63  		OrdID:                         repo.NewNullableString(in.OrdID),
    64  		LocalTenantID:                 repo.NewNullableString(in.LocalTenantID),
    65  		ShortDescription:              repo.NewNullableString(in.ShortDescription),
    66  		Links:                         repo.NewNullableStringFromJSONRawMessage(in.Links),
    67  		Labels:                        repo.NewNullableStringFromJSONRawMessage(in.Labels),
    68  		CredentialExchangeStrategies:  repo.NewNullableStringFromJSONRawMessage(in.CredentialExchangeStrategies),
    69  		CorrelationIDs:                repo.NewNullableStringFromJSONRawMessage(in.CorrelationIDs),
    70  		Tags:                          repo.NewNullableStringFromJSONRawMessage(in.Tags),
    71  		DocumentationLabels:           repo.NewNullableStringFromJSONRawMessage(in.DocumentationLabels),
    72  		BaseEntity: &repo.BaseEntity{
    73  			ID:        in.ID,
    74  			Ready:     in.Ready,
    75  			CreatedAt: in.CreatedAt,
    76  			UpdatedAt: in.UpdatedAt,
    77  			DeletedAt: in.DeletedAt,
    78  			Error:     repo.NewNullableString(in.Error),
    79  		},
    80  	}
    81  
    82  	return output, nil
    83  }
    84  
    85  // FromEntity missing godoc
    86  func (c *converter) FromEntity(entity *Entity) (*model.Bundle, error) {
    87  	if entity == nil {
    88  		return nil, apperrors.NewInternalError("the Bundle entity is nil")
    89  	}
    90  
    91  	defaultInstanceAuth, err := c.unmarshalDefaultInstanceAuth(entity.DefaultInstanceAuth)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  
    96  	output := &model.Bundle{
    97  		ApplicationID:                  repo.StringPtrFromNullableString(entity.ApplicationID),
    98  		ApplicationTemplateVersionID:   repo.StringPtrFromNullableString(entity.ApplicationTemplateVersionID),
    99  		Name:                           entity.Name,
   100  		Description:                    repo.StringPtrFromNullableString(entity.Description),
   101  		Version:                        repo.StringPtrFromNullableString(entity.Version),
   102  		ResourceHash:                   repo.StringPtrFromNullableString(entity.ResourceHash),
   103  		InstanceAuthRequestInputSchema: repo.StringPtrFromNullableString(entity.InstanceAuthRequestJSONSchema),
   104  		DefaultInstanceAuth:            defaultInstanceAuth,
   105  		OrdID:                          repo.StringPtrFromNullableString(entity.OrdID),
   106  		LocalTenantID:                  repo.StringPtrFromNullableString(entity.LocalTenantID),
   107  		ShortDescription:               repo.StringPtrFromNullableString(entity.ShortDescription),
   108  		Links:                          repo.JSONRawMessageFromNullableString(entity.Links),
   109  		Labels:                         repo.JSONRawMessageFromNullableString(entity.Labels),
   110  		CredentialExchangeStrategies:   repo.JSONRawMessageFromNullableString(entity.CredentialExchangeStrategies),
   111  		CorrelationIDs:                 repo.JSONRawMessageFromNullableString(entity.CorrelationIDs),
   112  		Tags:                           repo.JSONRawMessageFromNullableString(entity.Tags),
   113  		DocumentationLabels:            repo.JSONRawMessageFromNullableString(entity.DocumentationLabels),
   114  		BaseEntity: &model.BaseEntity{
   115  			ID:        entity.ID,
   116  			Ready:     entity.Ready,
   117  			CreatedAt: entity.CreatedAt,
   118  			UpdatedAt: entity.UpdatedAt,
   119  			DeletedAt: entity.DeletedAt,
   120  			Error:     repo.StringPtrFromNullableString(entity.Error),
   121  		},
   122  	}
   123  
   124  	return output, nil
   125  }
   126  
   127  // ToGraphQL missing godoc
   128  func (c *converter) ToGraphQL(in *model.Bundle) (*graphql.Bundle, error) {
   129  	if in == nil {
   130  		return nil, apperrors.NewInternalError("the model Bundle is nil")
   131  	}
   132  
   133  	auth, err := c.auth.ToGraphQL(in.DefaultInstanceAuth)
   134  	if err != nil {
   135  		return nil, errors.Wrap(err, "while converting DefaultInstanceAuth to GraphQL")
   136  	}
   137  
   138  	correlationIDs, err := c.correlationIDsToStringSlice(in.CorrelationIDs)
   139  	if err != nil {
   140  		return nil, errors.Wrap(err, "while converting CorrelationIDs to GraphQL")
   141  	}
   142  	return &graphql.Bundle{
   143  		Name:                           in.Name,
   144  		Description:                    in.Description,
   145  		InstanceAuthRequestInputSchema: c.strPtrToJSONSchemaPtr(in.InstanceAuthRequestInputSchema),
   146  		DefaultInstanceAuth:            auth,
   147  		CorrelationIDs:                 correlationIDs,
   148  		BaseEntity: &graphql.BaseEntity{
   149  			ID:        in.ID,
   150  			Ready:     in.Ready,
   151  			CreatedAt: timePtrToTimestampPtr(in.CreatedAt),
   152  			UpdatedAt: timePtrToTimestampPtr(in.UpdatedAt),
   153  			DeletedAt: timePtrToTimestampPtr(in.DeletedAt),
   154  			Error:     in.Error,
   155  		},
   156  	}, nil
   157  }
   158  
   159  // MultipleToGraphQL missing godoc
   160  func (c *converter) MultipleToGraphQL(in []*model.Bundle) ([]*graphql.Bundle, error) {
   161  	bundles := make([]*graphql.Bundle, 0, len(in))
   162  	for _, r := range in {
   163  		if r == nil {
   164  			continue
   165  		}
   166  		bndl, err := c.ToGraphQL(r)
   167  		if err != nil {
   168  			return nil, errors.Wrap(err, "while converting Bundle to GraphQL")
   169  		}
   170  		bundles = append(bundles, bndl)
   171  	}
   172  
   173  	return bundles, nil
   174  }
   175  
   176  // CreateInputFromGraphQL missing godoc
   177  func (c *converter) CreateInputFromGraphQL(in graphql.BundleCreateInput) (model.BundleCreateInput, error) {
   178  	auth, err := c.auth.InputFromGraphQL(in.DefaultInstanceAuth)
   179  	if err != nil {
   180  		return model.BundleCreateInput{}, errors.Wrap(err, "while converting DefaultInstanceAuth input")
   181  	}
   182  
   183  	apiDefs, apiSpecs, err := c.api.MultipleInputFromGraphQL(in.APIDefinitions)
   184  	if err != nil {
   185  		return model.BundleCreateInput{}, errors.Wrap(err, "while converting APIDefinitions input")
   186  	}
   187  
   188  	documents, err := c.document.MultipleInputFromGraphQL(in.Documents)
   189  	if err != nil {
   190  		return model.BundleCreateInput{}, errors.Wrap(err, "while converting Documents input")
   191  	}
   192  
   193  	eventDefs, eventSpecs, err := c.event.MultipleInputFromGraphQL(in.EventDefinitions)
   194  	if err != nil {
   195  		return model.BundleCreateInput{}, errors.Wrap(err, "while converting EventDefinitions input")
   196  	}
   197  
   198  	correlationIDs, err := c.correlationIDsToJSONRaw(in.CorrelationIDs)
   199  	if err != nil {
   200  		return model.BundleCreateInput{}, errors.Wrap(err, "while converting CorrelationIDs input")
   201  	}
   202  	return model.BundleCreateInput{
   203  		Name:                           in.Name,
   204  		Description:                    in.Description,
   205  		InstanceAuthRequestInputSchema: c.jsonSchemaPtrToStrPtr(in.InstanceAuthRequestInputSchema),
   206  		DefaultInstanceAuth:            auth,
   207  		APIDefinitions:                 apiDefs,
   208  		APISpecs:                       apiSpecs,
   209  		EventDefinitions:               eventDefs,
   210  		EventSpecs:                     eventSpecs,
   211  		Documents:                      documents,
   212  		CorrelationIDs:                 correlationIDs,
   213  	}, nil
   214  }
   215  
   216  // MultipleCreateInputFromGraphQL missing godoc
   217  func (c *converter) MultipleCreateInputFromGraphQL(in []*graphql.BundleCreateInput) ([]*model.BundleCreateInput, error) {
   218  	bundles := make([]*model.BundleCreateInput, 0, len(in))
   219  	for _, item := range in {
   220  		if item == nil {
   221  			continue
   222  		}
   223  		bndl, err := c.CreateInputFromGraphQL(*item)
   224  		if err != nil {
   225  			return nil, err
   226  		}
   227  		bundles = append(bundles, &bndl)
   228  	}
   229  
   230  	return bundles, nil
   231  }
   232  
   233  // UpdateInputFromGraphQL missing godoc
   234  func (c *converter) UpdateInputFromGraphQL(in graphql.BundleUpdateInput) (*model.BundleUpdateInput, error) {
   235  	auth, err := c.auth.InputFromGraphQL(in.DefaultInstanceAuth)
   236  	if err != nil {
   237  		return nil, errors.Wrap(err, "while converting DefaultInstanceAuth from GraphQL")
   238  	}
   239  
   240  	return &model.BundleUpdateInput{
   241  		Name:                           in.Name,
   242  		Description:                    in.Description,
   243  		InstanceAuthRequestInputSchema: c.jsonSchemaPtrToStrPtr(in.InstanceAuthRequestInputSchema),
   244  		DefaultInstanceAuth:            auth,
   245  	}, nil
   246  }
   247  
   248  func (c *converter) marshalDefaultInstanceAuth(defaultInstanceAuth *model.Auth) (*string, error) {
   249  	if defaultInstanceAuth == nil {
   250  		return nil, nil
   251  	}
   252  
   253  	output, err := json.Marshal(defaultInstanceAuth)
   254  	if err != nil {
   255  		return nil, errors.Wrap(err, "while marshaling default auth")
   256  	}
   257  	return str.Ptr(string(output)), nil
   258  }
   259  
   260  func (c *converter) unmarshalDefaultInstanceAuth(defaultInstanceAuthSQL sql.NullString) (*model.Auth, error) {
   261  	var defaultInstanceAuth *model.Auth
   262  	if defaultInstanceAuthSQL.Valid && defaultInstanceAuthSQL.String != "" {
   263  		defaultInstanceAuth = &model.Auth{}
   264  		err := json.Unmarshal([]byte(defaultInstanceAuthSQL.String), defaultInstanceAuth)
   265  		if err != nil {
   266  			return nil, errors.Wrap(err, "while unmarshalling default instance auth")
   267  		}
   268  	}
   269  
   270  	return defaultInstanceAuth, nil
   271  }
   272  
   273  func (c *converter) strPtrToJSONSchemaPtr(in *string) *graphql.JSONSchema {
   274  	if in == nil {
   275  		return nil
   276  	}
   277  	out := graphql.JSONSchema(*in)
   278  	return &out
   279  }
   280  
   281  func (c *converter) jsonSchemaPtrToStrPtr(in *graphql.JSONSchema) *string {
   282  	if in == nil {
   283  		return nil
   284  	}
   285  	out := string(*in)
   286  	return &out
   287  }
   288  
   289  func (c *converter) correlationIDsToStringSlice(in json.RawMessage) ([]string, error) {
   290  	if in == nil {
   291  		return nil, nil
   292  	}
   293  	var out []string
   294  	err := json.Unmarshal(in, &out)
   295  	if err != nil {
   296  		return nil, errors.Wrap(err, "while unmarshal correlation IDs")
   297  	}
   298  	return out, nil
   299  }
   300  
   301  func (c *converter) correlationIDsToJSONRaw(correlationIDs []string) (json.RawMessage, error) {
   302  	if correlationIDs == nil {
   303  		return nil, nil
   304  	}
   305  	out, err := json.Marshal(correlationIDs)
   306  	if err != nil {
   307  		return nil, errors.Wrap(err, "while marshalling correlation IDs")
   308  	}
   309  	return out, nil
   310  }
   311  
   312  func timePtrToTimestampPtr(time *time.Time) *graphql.Timestamp {
   313  	if time == nil {
   314  		return nil
   315  	}
   316  
   317  	t := graphql.Timestamp(*time)
   318  	return &t
   319  }