Skip to content

Commit

Permalink
Merge pull request #259 from SAP/fix_mtad_gen
Browse files Browse the repository at this point in the history
Fix mtad gen
  • Loading branch information
ShimiT authored Feb 21, 2019
2 parents e7e341e + a151497 commit d453043
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 13 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
1 change: 1 addition & 0 deletions internal/artifacts/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func generateMeta(parser dir.IMtaParser, ep dir.ITargetArtifacts, targetPathGett
// GenMetaInfo generates a MANIFEST.MF file and updates the build artifacts paths for deployment purposes.
func GenMetaInfo(ep dir.ITargetArtifacts, targetPathGetter dir.ITargetPath, deploymentDesc bool,
platform string, mtaStr *mta.MTA, modules []string, onlyModules bool) (rerr error) {

err := genMtad(mtaStr, ep, deploymentDesc, platform, yaml.Marshal)
if err != nil {
return errors.Wrap(err, "generation of metadata failed when generating the MTAD file")
Expand Down
40 changes: 32 additions & 8 deletions internal/artifacts/mtad.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package artifacts

import (
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"path/filepath"

"github.com/pkg/errors"
"gopkg.in/yaml.v2"

"github.com/SAP/cloud-mta/mta"

"github.com/SAP/cloud-mta-build-tool/internal/buildops"
"github.com/SAP/cloud-mta-build-tool/internal/fs"
"github.com/SAP/cloud-mta-build-tool/internal/logs"

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

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

// 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 {

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

// 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 d453043

Please sign in to comment.