Skip to content

Commit

Permalink
added platform validation to mtad generation
Browse files Browse the repository at this point in the history
  • Loading branch information
allaVolkov committed Feb 21, 2019
1 parent e7e341e commit e00e2c8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
7 changes: 4 additions & 3 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ func init() {
"the path to the MTA project; the current path is default")
mtadCmd.Flags().StringVarP(&mtadCmdTrg, "target", "t",
"", "the path to the MBT results folder; the current path is default")
mtadCmd.Flags().StringVarP(&mtadCmdPlatform, "platform", "p", "", "Provide MTA platform ")
mtadCmd.Flags().StringVarP(&mtadCmdPlatform, "platform", "p", "cf",
"the deployment platform; supported plaforms: cf (default), xsa, neo")

// set flags of meta command
metaCmd.Flags().StringVarP(&metaCmdSrc, "source", "s", "",
Expand All @@ -41,8 +42,8 @@ func init() {
"the path to the MBT results folder; the current path is default")
metaCmd.Flags().StringVarP(&metaCmdDesc, "desc", "d", "",
"the MTA descriptor; supported values: dev (development descriptor, default value) and dep (deployment descriptor)")
metaCmd.Flags().StringVarP(&metaCmdPlatform, "platform", "p", "",
"the deployment platform; supported plaforms: cf, xsa")
metaCmd.Flags().StringVarP(&metaCmdPlatform, "platform", "p", "cf",
"the deployment platform; supported plaforms: cf (default), xsa, neo")

// set flags of mtar command
mtarCmd.Flags().StringVarP(&mtarCmdSrc, "source", "s", "",
Expand Down
4 changes: 2 additions & 2 deletions cmd/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func init() {
packModuleCmd.Flags().StringVarP(&packCmdModule, "module", "m", "",
"the name of the module")
packModuleCmd.Flags().StringVarP(&packCmdPlatform, "platform", "p", "",
"the deployment platform; supported plaforms: cf, xsa")
"the deployment platform; supported plaforms: cf, xsa, neo")

// set flags of command build Module
buildModuleCmd.Flags().StringVarP(&buildCmdSrc, "source", "s", "",
Expand All @@ -46,7 +46,7 @@ func init() {
buildModuleCmd.Flags().StringVarP(&buildCmdModule, "module", "m", "",
"the name of the module")
buildModuleCmd.Flags().StringVarP(&buildCmdPlatform, "platform", "p", "",
"the deployment platform; supported plaforms: cf, xsa")
"the deployment platform; supported plaforms: cf, xsa, neo")
}

// buildModuleCmd - Build module
Expand Down
31 changes: 27 additions & 4 deletions internal/artifacts/mtad.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/SAP/cloud-mta/mta"
"path/filepath"
"fmt"
)

type mtadLoc struct {
Expand Down Expand Up @@ -43,33 +44,55 @@ func ExecuteGenMtad(source, target, platform string, wdGetter func() (string, er
return errors.Wrap(err, "generation of the MTAD file failed when initializing the location")
}

// validate platform
err = validatePlatform(platform)
if err != nil {
return err
}

// get mta object
mtaStr, err := loc.ParseFile()
if err != nil {
return errors.Wrapf(err, `generation of the MTAD file failed when parsing the "%v" file`, loc.GetMtaYamlFilename())
}

// get extension object if defined
mtaExt, err := loc.ParseExtFile(platform)
if err != nil {
return errors.Wrapf(err, `generation of the MTAD file failed when parsing the "%v" file`, loc.GetMtaExtYamlPath(platform))
}

// merge mta and extension objects
mta.Merge(mtaStr, mtaExt)
// init mtad object from the extended mta
adaptMtadForDeployment(mtaStr, platform)

return genMtad(mtaStr, &mtadLoc{target}, false, platform, yaml.Marshal)
}

func validatePlatform(platform string) error {
if platform != "xsa" && platform != "cf" && platform != "neo" {
return fmt.Errorf("the %s deployment platform is not supported; supported values: cf, xsa, neo", platform)
}
return nil
}

// genMtad generates an mtad.yaml file from a mta.yaml file and a platform configuration file.
func genMtad(mtaStr *mta.MTA, ep dir.ITargetArtifacts, deploymentDesc bool, platform string,
marshal func(interface{}) (out []byte, err error)) error {
// Create META-INF folder under the mtar folder
metaPath := ep.GetMetaPath()
err := dir.CreateDirIfNotExist(metaPath)
if err != nil {
logs.Logger.Infof(`the "%v" folder already exists`, metaPath)

// if meta folder provided, mtad will be saved in this folder, so we create it if not exists
if metaPath != "" {
err := dir.CreateDirIfNotExist(metaPath)
if err != nil {
logs.Logger.Infof(`the "%v" folder already exists`, metaPath)
}
}
if !deploymentDesc {
err = ConvertTypes(*mtaStr, platform)
// convert modules types according to platform
err := ConvertTypes(*mtaStr, platform)
if err != nil {
return errors.Wrapf(err,
`generation of the MTAD file failed when converting types according to the "%v" platform`,
Expand Down
16 changes: 16 additions & 0 deletions internal/artifacts/mtad_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ var _ = Describe("Mtad", func() {
return "", errors.New("err")
})).Should(HaveOccurred())
})
It("Fails on platform validation", func() {
Ω(ExecuteGenMtad(getTestPath("mta"), getTestPath("resultMtad"), "ab", func() (string, error) {
return "", errors.New("err")
})).Should(HaveOccurred())
})
It("Fails on wrong source path - parse fails", func() {
Ω(ExecuteGenMtad(getTestPath("mtax"), getTestPath("resultMtad"), "cf", os.Getwd)).Should(HaveOccurred())
})
Expand Down Expand Up @@ -116,3 +121,14 @@ var _ = Describe("adaptMtadForDeployment", func() {
Ω(mta.Parameters["hcp-deployer-version"]).ShouldNot(BeNil())
})
})

var _ = Describe("mtadLoc", func() {
It("GetManifestPath", func() {
loc := mtadLoc{"anyPath"}
Ω(loc.GetManifestPath()).Should(Equal(""))
})
It("GetMtarDir", func() {
loc := mtadLoc{"anyPath"}
Ω(loc.GetMtarDir()).Should(Equal(""))
})
})

0 comments on commit e00e2c8

Please sign in to comment.