Skip to content

Commit

Permalink
Merge pull request #36 from SAP/validateJson
Browse files Browse the repository at this point in the history
* validate the values in the file against actual names in the subaccount
  • Loading branch information
ANUGRAHG authored Sep 13, 2024
2 parents 1d42f56 + 3530a27 commit d7ce4c4
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 16 deletions.
68 changes: 56 additions & 12 deletions cmd/exportFromFileHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func exportFromFile(subaccount string, jsonfile string, resourceFile string, con
jsonFile, err := os.Open(jsonfile)

if err != nil {
fmt.Println("err")
fmt.Println("Error:", err)
return
}

Expand Down Expand Up @@ -68,23 +68,23 @@ func exportFromFile(subaccount string, jsonfile string, resourceFile string, con
generateConfig(resourceFile, configDir)
}

func generateConfigForResource(resource string, values []string, subaccout string, configDir string) {
func generateConfigForResource(resource string, values []string, subaccount string, configDir string) {
//fmt.Println(resource)
//fmt.Println(values)
if resource == "environment-instances" {
getEnvInstanceConfig(values, subaccout, configDir)
getEnvInstanceConfig(values, subaccount, configDir)
}
if resource == "subaccount" {
exportSubaccount(subaccout, configDir)
exportSubaccount(subaccount, configDir, values)
}
if resource == "entitlements" {
getEntitlementConfig(values, subaccout, configDir)
getEntitlementConfig(values, subaccount, configDir)
}
if resource == "subscriptions" {
getSubscriptionConfig(values, subaccout, configDir)
getSubscriptionConfig(values, subaccount, configDir)
}
if resource == "trust-configurations" {
getTrustConfig(values, subaccout, configDir)
getTrustConfig(values, subaccount, configDir)
}
}

Expand Down Expand Up @@ -118,7 +118,7 @@ func getTrustConfig(values []string, subaccountID string, configDir string) {
importBlock, err := getImportBlock4(data, subaccountID, values)
if err != nil {
fmt.Println("error:", err)
return
os.Exit(0)
}

currentDir, err := os.Getwd()
Expand Down Expand Up @@ -153,11 +153,13 @@ func getImportBlock4(data map[string]interface{}, subaccountId string, values []
}

var importBlock string
var subaccountAllTrusts []string
trusts := data["values"].([]interface{})

for x, value := range trusts {

trust := value.(map[string]interface{})
subaccountAllTrusts = append(subaccountAllTrusts, fmt.Sprintf("%v", trust["origin"]))
if slices.Contains(values, fmt.Sprintf("%v", trust["origin"])) {
template := strings.Replace(resource_doc.Import, "<resource_name>", "trust"+fmt.Sprint(x), -1)
template = strings.Replace(template, "<subaccount_id>", subaccountId, -1)
Expand All @@ -166,6 +168,13 @@ func getImportBlock4(data map[string]interface{}, subaccountId string, values []
}
}

missingTrust, subset := isSubset(subaccountAllTrusts, values)

if !subset {
return "", fmt.Errorf("trust configuration %s not found in the subaccount. Please adjust it in the provided file", missingTrust)

}

return importBlock, nil
}

Expand Down Expand Up @@ -199,7 +208,7 @@ func getSubscriptionConfig(values []string, subaccountID string, configDir strin
importBlock, err := getImportBlock3(data, subaccountID, values)
if err != nil {
fmt.Println("error:", err)
return
os.Exit(0)
}

currentDir, err := os.Getwd()
Expand Down Expand Up @@ -235,10 +244,12 @@ func getImportBlock3(data map[string]interface{}, subaccountId string, values []
}

var importBlock string
var subaccountAllSubscriptions []string
subscriptions := data["values"].([]interface{})

for _, value := range subscriptions {
subscription := value.(map[string]interface{})
subaccountAllSubscriptions = append(subaccountAllSubscriptions, fmt.Sprintf("%v", subscription["app_name"])+"_"+fmt.Sprintf("%v", subscription["plan_name"]))
if slices.Contains(values, fmt.Sprintf("%v", subscription["app_name"])+"_"+fmt.Sprintf("%v", subscription["plan_name"])) {
template := strings.Replace(resource_doc.Import, "<resource_name>", strings.Replace(fmt.Sprintf("%v", subscription["app_name"]), "-", "_", -1), -1)
template = strings.Replace(template, "<subaccount_id>", subaccountId, -1)
Expand All @@ -248,6 +259,13 @@ func getImportBlock3(data map[string]interface{}, subaccountId string, values []
}
}

missingSubscription, subset := isSubset(subaccountAllSubscriptions, values)

if !subset {
return "", fmt.Errorf("subscription %s not found in the subaccount. Please adjust it in the provided file", missingSubscription)

}

return importBlock, nil
}

Expand Down Expand Up @@ -281,7 +299,7 @@ func getEntitlementConfig(values []string, subaccountID string, configDir string
importBlock, err := getImportBlock2(data, subaccountID, values)
if err != nil {
fmt.Println("error:", err)
return
os.Exit(0)
}

if len(importBlock) == 0 {
Expand Down Expand Up @@ -318,10 +336,11 @@ func getImportBlock2(data map[string]interface{}, subaccountId string, values []
}

var importBlock string
var subaccountAllEntitlements []string
for key, value := range data {

subaccountAllEntitlements = append(subaccountAllEntitlements, strings.Replace(key, ":", "_", -1))
if slices.Contains(values, strings.Replace(key, ":", "_", -1)) {

template := strings.Replace(resource_doc.Import, "<resource_name>", strings.Replace(key, ":", "_", -1), -1)
template = strings.Replace(template, "<subaccount_id>", subaccountId, -1)
if subMap, ok := value.(map[string]interface{}); ok {
Expand All @@ -333,6 +352,13 @@ func getImportBlock2(data map[string]interface{}, subaccountId string, values []
}
}

missingEntitlement, subset := isSubset(subaccountAllEntitlements, values)

if !subset {
return "", fmt.Errorf("entitlement %s not found in the subaccount. Please adjust it in the provided file", missingEntitlement)

}

return importBlock, nil

}
Expand Down Expand Up @@ -370,7 +396,7 @@ func getEnvInstanceConfig(values []string, subaccountID string, configDir string

if err != nil {
fmt.Println("error:", err)
return
os.Exit(0)
}

if len(importBlock) == 0 {
Expand Down Expand Up @@ -405,11 +431,13 @@ func getImportBlock1(data map[string]interface{}, subaccountId string, values []
}

var importBlock string
var subaccountAllEnvInstances []string
environmentInstances := data["values"].([]interface{})

for _, value := range environmentInstances {

environmentInstance := value.(map[string]interface{})
subaccountAllEnvInstances = append(subaccountAllEnvInstances, fmt.Sprintf("%v", environmentInstance["environment_type"]))
if slices.Contains(values, fmt.Sprintf("%v", environmentInstance["environment_type"])) {
template := strings.Replace(resource_doc.Import, "<resource_name>", fmt.Sprintf("%v", environmentInstance["environment_type"]), -1)
template = strings.Replace(template, "<subaccount_id>", subaccountId, -1)
Expand All @@ -418,5 +446,21 @@ func getImportBlock1(data map[string]interface{}, subaccountId string, values []
}
}

missingEnvInstance, subset := isSubset(subaccountAllEnvInstances, values)

if !subset {
return "", fmt.Errorf("environment instance %s not found in the subaccount. Please adjust it in the provided file", missingEnvInstance)

}

return importBlock, nil
}

func isSubset(superSet []string, subset []string) (string, bool) {
for _, value := range subset {
if !slices.Contains(superSet, value) {
return value, false
}
}
return "", true
}
10 changes: 9 additions & 1 deletion cmd/exportSubaccountHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strings"
)

func exportSubaccount(subaccountID string, configDir string) {
func exportSubaccount(subaccountID string, configDir string, optionalValues ...[]string) {

dataBlock, err := readSubaccountDataSource(subaccountID)
if err != nil {
Expand Down Expand Up @@ -44,6 +44,14 @@ func exportSubaccount(subaccountID string, configDir string) {
return
}

if len(optionalValues) != 0 {
valueFromJson := optionalValues[0]
if valueFromJson[0] != fmt.Sprintf("%v", data["name"]) {
log.Println("Error:", fmt.Errorf("subaccount %s not found. Please adjust it in the provided file", valueFromJson[0]))
os.Exit(0)
}
}

importBlock, err := getSubaccountImportBlock(data, subaccountID)
if err != nil {
log.Fatalf("error: %v", err)
Expand Down
2 changes: 0 additions & 2 deletions cmd/generateConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ func cleanup() {
}

func configureProvider() {
//tmpdir, err := ioutil.TempDir("/tmp", "sampledir")
tmpdir, err := os.MkdirTemp("", "provider.tf")
if err != nil {
panic(err)
Expand Down Expand Up @@ -118,7 +117,6 @@ func configureProvider() {
}

providerContent = providerContent + "}"
//providerContent := "terraform {\nrequired_providers {\nbtp = {\nsource = \"SAP/btp\"\nversion = \"1.4.0\"\n}\n}\n}\n\nprovider \"btp\" {\ncli_server_url=\"https://cpcli.cf.eu10.hana.ondemand.com\"\nglobalaccount = \"terraformintprod\"\n}"

err = tfutils.CreateFileWithContent(abspath, providerContent)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion cmd/generateConfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"os"
"path/filepath"
"strings"
"testing"
)

Expand Down Expand Up @@ -59,7 +60,7 @@ func TestConfigureProvider(t *testing.T) {
required_providers {
btp = {
source = "SAP/btp"
version = "1.6.0"
version = "[VERSION]"
}
}
}
Expand All @@ -69,6 +70,7 @@ globalaccount = "testaccount"
cli_server_url="https://test.com"
}`

expectedContent = strings.Replace(expectedContent, "[VERSION]", BtpProviderVersion[1:], -1)
content, err := os.ReadFile(expectedFilePath)
if err != nil {
t.Fatalf("Failed to read file %s: %v", expectedFilePath, err)
Expand Down

0 comments on commit d7ce4c4

Please sign in to comment.