github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/externaltenant/mapping.go (about) 1 package externaltenant 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "os" 7 "path/filepath" 8 9 "github.com/kyma-incubator/compass/components/director/pkg/tenant" 10 11 "github.com/kyma-incubator/compass/components/director/pkg/apperrors" 12 13 "github.com/kyma-incubator/compass/components/director/internal/model" 14 15 "github.com/pkg/errors" 16 ) 17 18 // MapTenants missing godoc 19 func MapTenants(tenantsDirectoryPath, defaultTenantRegion string) ([]model.BusinessTenantMappingInput, error) { 20 files, err := os.ReadDir(tenantsDirectoryPath) 21 if err != nil { 22 return nil, errors.Wrapf(err, "while reading directory with tenant files [%s]", tenantsDirectoryPath) 23 } 24 25 var outputTenants []model.BusinessTenantMappingInput 26 for _, f := range files { 27 if filepath.Ext(f.Name()) != ".json" { 28 return nil, apperrors.NewInternalError(fmt.Sprintf("unsupported file format [%s]", filepath.Ext(f.Name()))) 29 } 30 31 bytes, err := os.ReadFile(tenantsDirectoryPath + f.Name()) 32 if err != nil { 33 return nil, errors.Wrapf(err, "while reading tenants file [%s]", tenantsDirectoryPath+f.Name()) 34 } 35 36 var tenantsFromFile []model.BusinessTenantMappingInput 37 if err := json.Unmarshal(bytes, &tenantsFromFile); err != nil { 38 return nil, errors.Wrapf(err, "while unmarshalling tenants from file [%s]", tenantsDirectoryPath+f.Name()) 39 } 40 41 for i := range tenantsFromFile { 42 tenantsFromFile[i].Provider = f.Name() 43 if tenantsFromFile[i].Region == "" && tenantsFromFile[i].Type == string(tenant.Subaccount) { 44 tenantsFromFile[i].Region = defaultTenantRegion 45 } 46 } 47 48 outputTenants = append(outputTenants, tenantsFromFile...) 49 } 50 51 return outputTenants, nil 52 }