Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix rest dsl generation #441

Merged
merged 1 commit into from
Feb 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 31 additions & 23 deletions deploy/resources.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions pkg/trait/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ type Catalog struct {
tIstio Trait
tEnvironment Trait
tClasspath Trait
tRest Trait
tRestDsl Trait
}

// NewCatalog creates a new trait Catalog
func NewCatalog(ctx context.Context, c client.Client) *Catalog {
catalog := Catalog{
L: log.Log.WithName("trait"),
tDebug: newDebugTrait(),
tRest: newRestTrait(),
tRestDsl: newRestDslTrait(),
tKnative: newKnativeTrait(),
tDependencies: newDependenciesTrait(),
tDeployment: newDeploymentTrait(),
Expand Down Expand Up @@ -91,7 +91,7 @@ func NewCatalog(ctx context.Context, c client.Client) *Catalog {
func (c *Catalog) allTraits() []Trait {
return []Trait{
c.tDebug,
c.tRest,
c.tRestDsl,
c.tKnative,
c.tDependencies,
c.tDeployment,
Expand Down Expand Up @@ -120,7 +120,7 @@ func (c *Catalog) traitsFor(environment *Environment) []Trait {
return []Trait{
c.tGarbageCollector,
c.tDebug,
c.tRest,
c.tRestDsl,
c.tDependencies,
c.tImages,
c.tBuilder,
Expand All @@ -138,7 +138,7 @@ func (c *Catalog) traitsFor(environment *Environment) []Trait {
return []Trait{
c.tGarbageCollector,
c.tDebug,
c.tRest,
c.tRestDsl,
c.tDependencies,
c.tImages,
c.tBuilder,
Expand All @@ -156,7 +156,7 @@ func (c *Catalog) traitsFor(environment *Environment) []Trait {
return []Trait{
c.tGarbageCollector,
c.tDebug,
c.tRest,
c.tRestDsl,
c.tKnative,
c.tDependencies,
c.tImages,
Expand Down
125 changes: 108 additions & 17 deletions pkg/trait/rest.go → pkg/trait/rest-dsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ limitations under the License.
package trait

import (
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"os"
Expand All @@ -36,17 +38,17 @@ import (
"github.com/apache/camel-k/pkg/util/maven"
)

type restTrait struct {
type restDslTrait struct {
BaseTrait `property:",squash"`
}

func newRestTrait() *restTrait {
return &restTrait{
BaseTrait: newBaseTrait("rest"),
func newRestDslTrait() *restDslTrait {
return &restDslTrait{
BaseTrait: newBaseTrait("rest-dsl"),
}
}

func (t *restTrait) Configure(e *Environment) (bool, error) {
func (t *restDslTrait) Configure(e *Environment) (bool, error) {
if t.Enabled != nil && !*t.Enabled {
return false, nil
}
Expand All @@ -64,7 +66,7 @@ func (t *restTrait) Configure(e *Environment) (bool, error) {
return false, nil
}

func (t *restTrait) Apply(e *Environment) error {
func (t *restDslTrait) Apply(e *Environment) error {
if len(e.Integration.Spec.Resources) == 0 {
return nil
}
Expand All @@ -82,6 +84,12 @@ func (t *restTrait) Apply(e *Environment) error {
continue
}

tmpDir = path.Join(tmpDir, strconv.Itoa(i))
err := os.MkdirAll(tmpDir, os.ModePerm)
if err != nil {
return err
}

content := []byte(resource.Content)
if resource.Compression {
content, err = gzip.UncompressBase64(content)
Expand All @@ -93,21 +101,32 @@ func (t *restTrait) Apply(e *Environment) error {
in := path.Join(tmpDir, "openapi-spec.json")
out := path.Join(tmpDir, "openapi-dsl.xml")

if err := ioutil.WriteFile(in, content, 0644); err != nil {
err = ioutil.WriteFile(in, content, 0644)
if err != nil {
return err
}

opts := make([]string, 0, 4)
opts = append(opts, maven.ExtraOptions(e.Platform.Spec.Build.LocalRepository)...)
opts = append(opts, fmt.Sprintf("org.apache.camel.k:camel-k-maven-plugin:%s:generate-rest-xml", version.Version))
opts = append(opts, "-Dopenapi.spec="+in)
opts = append(opts, "-Ddsl.out="+out)

if err := maven.Run(tmpDir, opts...); err != nil {
project, err := t.generateProject(e)
if err != nil {
return err
}

err = maven.CreateStructure(tmpDir, project)
if err != nil {
return err
}

err = maven.Run(tmpDir, opts...)
if err != nil {
return err
}

content, err := ioutil.ReadFile(out)
content, err = ioutil.ReadFile(out)
if err != nil {
return err
}
Expand All @@ -121,16 +140,28 @@ func (t *restTrait) Apply(e *Environment) error {
content = c
}

name := fmt.Sprintf("%s-openapi-%03d", e.Integration.Name, i)
generatedContentName := fmt.Sprintf("%s-openapi-%03d", e.Integration.Name, i)
generatedSourceName := strings.TrimSuffix(resource.Name, filepath.Ext(resource.Name)) + ".xml"
generatedSources := make([]v1alpha1.SourceSpec, 0, len(e.Integration.Status.GeneratedSources))

if e.Integration.Status.GeneratedSources != nil {
//
// Filter out the previously generated source
//
for _, x := range e.Integration.Status.GeneratedSources {
if x.Name != generatedSourceName {
generatedSources = append(generatedSources, x)
}
}
}

//
// Add an additional source that references the previously
// created config map
// Add an additional source that references the config map
//
e.Integration.Status.GeneratedSources = append(e.Integration.Status.GeneratedSources, v1alpha1.SourceSpec{
generatedSources = append(generatedSources, v1alpha1.SourceSpec{
DataSpec: v1alpha1.DataSpec{
Name: strings.TrimSuffix(resource.Name, filepath.Ext(resource.Name)) + ".xml",
ContentRef: name,
Name: generatedSourceName,
ContentRef: generatedContentName,
Compression: resource.Compression,
},
Language: v1alpha1.LanguageXML,
Expand All @@ -146,7 +177,7 @@ func (t *restTrait) Apply(e *Environment) error {
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Name: generatedContentName,
Namespace: e.Integration.Namespace,
Labels: map[string]string{
"camel.apache.org/integration": e.Integration.Name,
Expand All @@ -164,8 +195,68 @@ func (t *restTrait) Apply(e *Environment) error {
},
}

e.Integration.Status.GeneratedSources = generatedSources
e.Resources.Add(&cm)
}

return nil
}

func (t *restDslTrait) generateProject(e *Environment) (maven.Project, error) {
if e.CamelCatalog == nil {
return maven.Project{}, errors.New("unknown camel catalog")
}

p := maven.Project{
XMLName: xml.Name{Local: "project"},
XMLNs: "http://maven.apache.org/POM/4.0.0",
XMLNsXsi: "http://www.w3.org/2001/XMLSchema-instance",
XsiSchemaLocation: "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd",
ModelVersion: "4.0.0",
GroupID: "org.apache.camel.k.integration",
ArtifactID: "camel-k-red-dsl-generator",
Version: version.Version,
Build: maven.Build{
DefaultGoal: "generate-resources",
Plugins: []maven.Plugin{
{
GroupID: "org.apache.camel.k",
ArtifactID: "camel-k-maven-plugin",
Version: version.Version,
Executions: []maven.Execution{
{
Phase: "generate-resources",
Goals: []string{
"generate-rest-xml",
},
},
},
Dependencies: []maven.Dependency{
{
GroupID: "org.apache.camel",
ArtifactID: "camel-swagger-rest-dsl-generator",
Version: e.CamelCatalog.Version,
},
},
},
},
},
}

//
// Repositories
//

p.Repositories = make([]maven.Repository, 0, len(e.Platform.Spec.Build.Repositories))

for i, r := range e.Platform.Spec.Build.Repositories {
repo := maven.NewRepository(r)
if repo.ID == "" {
repo.ID = fmt.Sprintf("repo-%03d", i)
}

p.Repositories = append(p.Repositories, repo)
}

return p, nil
}
13 changes: 8 additions & 5 deletions pkg/util/maven/maven_project_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Project struct {
Dependencies []Dependency `xml:"dependencies>dependency,omitempty"`
Repositories []Repository `xml:"repositories>repository,omitempty"`
PluginRepositories []Repository `xml:"pluginRepositories>pluginRepository,omitempty"`
Build Build `xml:"build,omitempty"`
}

// Exclusion represent a maven's dependency exlucsion
Expand Down Expand Up @@ -77,15 +78,17 @@ type RepositoryPolicy struct {

// Build --
type Build struct {
Plugins []Plugin `xml:"plugins>plugin,omitempty"`
DefaultGoal string `xml:"defaultGoal,omitempty"`
Plugins []Plugin `xml:"plugins>plugin,omitempty"`
}

// Plugin --
type Plugin struct {
GroupID string `xml:"groupId"`
ArtifactID string `xml:"artifactId"`
Version string `xml:"version,omitempty"`
Executions []Execution `xml:"executions>execution,omitempty"`
GroupID string `xml:"groupId"`
ArtifactID string `xml:"artifactId"`
Version string `xml:"version,omitempty"`
Executions []Execution `xml:"executions>execution,omitempty"`
Dependencies []Dependency `xml:"dependencies>dependency,omitempty"`
}

// Execution --
Expand Down
3 changes: 3 additions & 0 deletions pkg/util/maven/maven_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ const expectedPom = `<?xml version="1.0" encoding="UTF-8"?>
</releases>
</pluginRepository>
</pluginRepositories>
<build>
<plugins></plugins>
</build>
</project>`

func TestPomGeneration(t *testing.T) {
Expand Down