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

     1  package tenant
     2  
     3  import (
     4  	"github.com/kyma-incubator/compass/components/director/internal/model"
     5  	"github.com/kyma-incubator/compass/components/director/internal/repo"
     6  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
     7  	"github.com/kyma-incubator/compass/components/director/pkg/resource"
     8  	"github.com/kyma-incubator/compass/components/director/pkg/str"
     9  	"github.com/kyma-incubator/compass/components/director/pkg/tenant"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  type converter struct{}
    14  
    15  // NewConverter returns a new Converter that can later be used to make the conversions between the GraphQL, service, and repository layer representations of a Compass tenant.
    16  func NewConverter() *converter {
    17  	return &converter{}
    18  }
    19  
    20  // ToEntity converts the provided service-layer representation of a tenant to the repository-layer one tenant.Entity.
    21  func (c *converter) ToEntity(in *model.BusinessTenantMapping) *tenant.Entity {
    22  	if in == nil {
    23  		return nil
    24  	}
    25  	return &tenant.Entity{
    26  		ID:             in.ID,
    27  		Name:           in.Name,
    28  		ExternalTenant: in.ExternalTenant,
    29  		Parent:         str.NewNullString(in.Parent),
    30  		Type:           in.Type,
    31  		ProviderName:   in.Provider,
    32  		Status:         in.Status,
    33  	}
    34  }
    35  
    36  // FromEntity converts the provided tenant.Entity repo-layer representation of a tenant to the service-layer representation model.BusinessTenantMapping.
    37  func (c *converter) FromEntity(in *tenant.Entity) *model.BusinessTenantMapping {
    38  	if in == nil {
    39  		return nil
    40  	}
    41  	return &model.BusinessTenantMapping{
    42  		ID:             in.ID,
    43  		Name:           in.Name,
    44  		ExternalTenant: in.ExternalTenant,
    45  		Parent:         in.Parent.String,
    46  		Type:           in.Type,
    47  		Provider:       in.ProviderName,
    48  		Status:         in.Status,
    49  		Initialized:    in.Initialized,
    50  	}
    51  }
    52  
    53  // ToGraphQL converts the provided model.BusinessTenantMapping service-layer representation of a tenant to the GraphQL-layer representation graphql.Tenant.
    54  func (c *converter) ToGraphQL(in *model.BusinessTenantMapping) *graphql.Tenant {
    55  	if in == nil {
    56  		return nil
    57  	}
    58  
    59  	return &graphql.Tenant{
    60  		ID:          in.ExternalTenant,
    61  		InternalID:  in.ID,
    62  		Name:        str.Ptr(in.Name),
    63  		Type:        tenant.TypeToStr(in.Type),
    64  		ParentID:    in.Parent,
    65  		Initialized: in.Initialized,
    66  		Provider:    in.Provider,
    67  	}
    68  }
    69  
    70  func (c *converter) ToGraphQLInput(in model.BusinessTenantMappingInput) graphql.BusinessTenantMappingInput {
    71  	return graphql.BusinessTenantMappingInput{
    72  		Name:           in.Name,
    73  		ExternalTenant: in.ExternalTenant,
    74  		Parent:         str.Ptr(in.Parent),
    75  		Subdomain:      str.Ptr(in.Subdomain),
    76  		Region:         str.Ptr(in.Region),
    77  		Type:           in.Type,
    78  		Provider:       in.Provider,
    79  		LicenseType:    in.LicenseType,
    80  	}
    81  }
    82  
    83  func (c *converter) MultipleInputFromGraphQL(in []*graphql.BusinessTenantMappingInput) []model.BusinessTenantMappingInput {
    84  	res := make([]model.BusinessTenantMappingInput, 0, len(in))
    85  
    86  	for _, tnt := range in {
    87  		res = append(res, model.BusinessTenantMappingInput{
    88  			Name:           tnt.Name,
    89  			ExternalTenant: tnt.ExternalTenant,
    90  			Parent:         str.PtrStrToStr(tnt.Parent),
    91  			Subdomain:      str.PtrStrToStr(tnt.Subdomain),
    92  			Region:         str.PtrStrToStr(tnt.Region),
    93  			Type:           tnt.Type,
    94  			Provider:       tnt.Provider,
    95  			LicenseType:    tnt.LicenseType,
    96  		})
    97  	}
    98  
    99  	return res
   100  }
   101  
   102  func (c *converter) InputFromGraphQL(tnt graphql.BusinessTenantMappingInput) model.BusinessTenantMappingInput {
   103  	return model.BusinessTenantMappingInput{
   104  		Name:           tnt.Name,
   105  		ExternalTenant: tnt.ExternalTenant,
   106  		Parent:         str.PtrStrToStr(tnt.Parent),
   107  		Subdomain:      str.PtrStrToStr(tnt.Subdomain),
   108  		Region:         str.PtrStrToStr(tnt.Region),
   109  		Type:           tnt.Type,
   110  		Provider:       tnt.Provider,
   111  		LicenseType:    tnt.LicenseType,
   112  	}
   113  }
   114  
   115  // MultipleToGraphQL converts all the provided model.BusinessTenantMapping service-layer representations of a tenant to the GraphQL-layer representations graphql.Tenant.
   116  func (c *converter) MultipleToGraphQL(in []*model.BusinessTenantMapping) []*graphql.Tenant {
   117  	tenants := make([]*graphql.Tenant, 0, len(in))
   118  	for _, r := range in {
   119  		if r == nil {
   120  			continue
   121  		}
   122  
   123  		tenants = append(tenants, c.ToGraphQL(r))
   124  	}
   125  
   126  	return tenants
   127  }
   128  
   129  func (c *converter) MultipleInputToGraphQLInput(in []model.BusinessTenantMappingInput) []graphql.BusinessTenantMappingInput {
   130  	tenants := make([]graphql.BusinessTenantMappingInput, 0, len(in))
   131  	for _, tnt := range in {
   132  		tenants = append(tenants, c.ToGraphQLInput(tnt))
   133  	}
   134  	return tenants
   135  }
   136  
   137  // TenantAccessInputFromGraphQL converts the provided graphql.TenantAccessInput GraphQL-layer representation of a tenant accessto the service-layer representation model.TenantAccess.
   138  func (c *converter) TenantAccessInputFromGraphQL(in graphql.TenantAccessInput) (*model.TenantAccess, error) {
   139  	resourceType, err := fromTenantAccessObjectTypeToResourceType(in.ResourceType)
   140  	if err != nil {
   141  		return nil, err
   142  	}
   143  
   144  	return &model.TenantAccess{
   145  		ExternalTenantID: in.TenantID,
   146  		ResourceType:     resourceType,
   147  		ResourceID:       in.ResourceID,
   148  		Owner:            in.Owner,
   149  	}, nil
   150  }
   151  
   152  // TenantAccessToGraphQL converts the provided model.TenantAccess service-layer representation of a tenant access to the GraphQL-layer representation graphql.TenantAccess.
   153  func (c *converter) TenantAccessToGraphQL(in *model.TenantAccess) (*graphql.TenantAccess, error) {
   154  	if in == nil {
   155  		return nil, nil
   156  	}
   157  
   158  	resourceType, err := fromResourceTypeToTenantAccessObjectType(in.ResourceType)
   159  	if err != nil {
   160  		return nil, err
   161  	}
   162  
   163  	return &graphql.TenantAccess{
   164  		TenantID:     in.ExternalTenantID,
   165  		ResourceType: resourceType,
   166  		ResourceID:   in.ResourceID,
   167  		Owner:        in.Owner,
   168  	}, nil
   169  }
   170  
   171  // TenantAccessToEntity converts the provided service-layer representation of a tenant access to the repository-layer one.
   172  func (c *converter) TenantAccessToEntity(in *model.TenantAccess) *repo.TenantAccess {
   173  	if in == nil {
   174  		return nil
   175  	}
   176  
   177  	return &repo.TenantAccess{
   178  		TenantID:   in.InternalTenantID,
   179  		ResourceID: in.ResourceID,
   180  		Owner:      in.Owner,
   181  	}
   182  }
   183  
   184  // TenantAccessFromEntity converts the provided repository-layer representation of a tenant access to the service-layer one.
   185  func (c *converter) TenantAccessFromEntity(in *repo.TenantAccess) *model.TenantAccess {
   186  	if in == nil {
   187  		return nil
   188  	}
   189  
   190  	return &model.TenantAccess{
   191  		InternalTenantID: in.TenantID,
   192  		ResourceID:       in.ResourceID,
   193  		Owner:            in.Owner,
   194  	}
   195  }
   196  
   197  func fromTenantAccessObjectTypeToResourceType(objectType graphql.TenantAccessObjectType) (resource.Type, error) {
   198  	switch objectType {
   199  	case graphql.TenantAccessObjectTypeApplication:
   200  		return resource.Application, nil
   201  	case graphql.TenantAccessObjectTypeRuntime:
   202  		return resource.Runtime, nil
   203  	case graphql.TenantAccessObjectTypeRuntimeContext:
   204  		return resource.RuntimeContext, nil
   205  	default:
   206  		return "", errors.Errorf("Unknown tenant access resource type %q", objectType)
   207  	}
   208  }
   209  
   210  func fromResourceTypeToTenantAccessObjectType(objectType resource.Type) (graphql.TenantAccessObjectType, error) {
   211  	switch objectType {
   212  	case resource.Application:
   213  		return graphql.TenantAccessObjectTypeApplication, nil
   214  	case resource.Runtime:
   215  		return graphql.TenantAccessObjectTypeRuntime, nil
   216  	case resource.RuntimeContext:
   217  		return graphql.TenantAccessObjectTypeRuntimeContext, nil
   218  	default:
   219  		return "", errors.Errorf("Unknown tenant access resource type %q", objectType)
   220  	}
   221  }