github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/edp/client_fake.go (about)

     1  package edp
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  )
     7  
     8  const (
     9  	dataTenantMapKey     = "%s-%s"
    10  	metadataTenantMapKey = "%s-%s-%s"
    11  )
    12  
    13  // FakeClient implements the edp client interface but does not process data nor call real external system
    14  type FakeClient struct {
    15  	mu                 sync.Mutex
    16  	dataTenantData     map[string]DataTenantItem
    17  	metadataTenantData map[string]MetadataItem
    18  }
    19  
    20  // NewFakeClient creates edp fake client
    21  func NewFakeClient() *FakeClient {
    22  	return &FakeClient{
    23  		dataTenantData:     make(map[string]DataTenantItem),
    24  		metadataTenantData: make(map[string]MetadataItem),
    25  	}
    26  }
    27  
    28  func (f *FakeClient) CreateDataTenant(data DataTenantPayload) error {
    29  	f.mu.Lock()
    30  	defer f.mu.Unlock()
    31  
    32  	err := checkDataTenantPayload(data)
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	key := generateDataTenantMapKey(data.Name, data.Environment)
    38  
    39  	_, found := f.dataTenantData[key]
    40  	if found {
    41  		return fmt.Errorf("datatenant already exist")
    42  	}
    43  
    44  	f.dataTenantData[key] = DataTenantItem{
    45  		Name:        data.Name,
    46  		Environment: data.Environment,
    47  	}
    48  	return nil
    49  }
    50  
    51  func (f *FakeClient) CreateMetadataTenant(name, env string, data MetadataTenantPayload) error {
    52  	f.mu.Lock()
    53  	defer f.mu.Unlock()
    54  
    55  	err := checkMetadataTenantPayload(data)
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	dataMapKey := generateDataTenantMapKey(name, env)
    61  	metadataMapKey := generateMetadataTenantMapKey(name, env, data.Key)
    62  
    63  	_, found := f.metadataTenantData[metadataMapKey]
    64  	if found {
    65  		return fmt.Errorf("metadatatenant already exist")
    66  	}
    67  
    68  	f.metadataTenantData[metadataMapKey] = MetadataItem{
    69  		DataTenant: f.dataTenantData[dataMapKey],
    70  		Key:        data.Key,
    71  		Value:      data.Value,
    72  	}
    73  	return nil
    74  }
    75  
    76  func (f *FakeClient) DeleteDataTenant(name, env string) error {
    77  	f.mu.Lock()
    78  	defer f.mu.Unlock()
    79  
    80  	key := generateDataTenantMapKey(name, env)
    81  
    82  	_, found := f.dataTenantData[key]
    83  	if !found {
    84  		return fmt.Errorf("datatenant does not exist")
    85  	}
    86  	delete(f.dataTenantData, key)
    87  	return nil
    88  }
    89  
    90  func (f *FakeClient) DeleteMetadataTenant(name, env, key string) error {
    91  	f.mu.Lock()
    92  	defer f.mu.Unlock()
    93  
    94  	mapKey := generateMetadataTenantMapKey(name, env, key)
    95  
    96  	_, found := f.metadataTenantData[mapKey]
    97  	if !found {
    98  		return fmt.Errorf("metadatatenant does not exist")
    99  	}
   100  	delete(f.metadataTenantData, mapKey)
   101  	return nil
   102  }
   103  
   104  func checkDataTenantPayload(data DataTenantPayload) error {
   105  	if data.Name == "" || data.Environment == "" || data.Secret == "" {
   106  		return fmt.Errorf("one of the fields in DataTenantPayload is missing")
   107  	}
   108  	return nil
   109  }
   110  
   111  func checkMetadataTenantPayload(data MetadataTenantPayload) error {
   112  	if data.Key == "" || data.Value == "" {
   113  		return fmt.Errorf("one of the fields in MetadataTenantPayload is missing")
   114  	}
   115  	return nil
   116  }
   117  
   118  func generateDataTenantMapKey(name, env string) string {
   119  	return fmt.Sprintf(dataTenantMapKey, name, env)
   120  }
   121  
   122  func generateMetadataTenantMapKey(name, env, key string) string {
   123  	return fmt.Sprintf(metadataTenantMapKey, name, env, key)
   124  }
   125  
   126  // assert methods
   127  func (f *FakeClient) GetDataTenantItem(name, env string) (item DataTenantItem, exists bool) {
   128  	key := generateDataTenantMapKey(name, env)
   129  	item, exists = f.dataTenantData[key]
   130  	return item, exists
   131  }
   132  
   133  func (f *FakeClient) GetMetadataItem(name, env, key string) (item MetadataItem, exists bool) {
   134  	mapKey := generateMetadataTenantMapKey(name, env, key)
   135  	item, exists = f.metadataTenantData[mapKey]
   136  	return item, exists
   137  }