Skip to content

Commit

Permalink
Support manifest services without name
Browse files Browse the repository at this point in the history
Both manifest service formats are now supported:

```
services:
  - my_service
  - name: your_service
```

Co-authored-by: Danail Branekov <danailster@gmail.com>
  • Loading branch information
georgethebeatle and danail-branekov committed Jan 29, 2024
1 parent daf04d3 commit e3a0674
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 19 deletions.
17 changes: 17 additions & 0 deletions api/payloads/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package payloads

import (
"errors"
"fmt"
"regexp"

"code.cloudfoundry.org/korifi/api/repositories"
korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"code.cloudfoundry.org/korifi/tools"
"github.com/jellydator/validation"
"gopkg.in/yaml.v3"

"code.cloudfoundry.org/bytefmt"
)
Expand Down Expand Up @@ -70,6 +72,21 @@ type ManifestApplicationService struct {
BindingName *string `json:"binding_name" yaml:"binding_name"`
}

func (s *ManifestApplicationService) UnmarshalYAML(value *yaml.Node) error {
if value.Kind == yaml.ScalarNode && value.Tag == "!!str" {
s.Name = value.Value
return nil
}

type manifestApplicationService ManifestApplicationService
err := value.Decode((*manifestApplicationService)(s))
if err != nil {
return fmt.Errorf("invalid service: line %d, column %d: %w", value.Line, value.Column, err)
}

return nil
}

type ManifestRoute struct {
Route *string `json:"route" yaml:"route"`
}
Expand Down
88 changes: 69 additions & 19 deletions api/payloads/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"
"gopkg.in/yaml.v3"
)

var _ = Describe("Manifest payload", func() {
Expand Down Expand Up @@ -739,33 +740,82 @@ var _ = Describe("Manifest payload", func() {
})
})

Describe("ManifestApplicationServices", func() {
var (
validateErr error
testManifestServices ManifestApplicationService
)
Describe("ManifestApplicationService", func() {
Describe("Unmarshall", func() {
var (
unmarshalErr error
serviceString string
unmarshalledService ManifestApplicationService
)

BeforeEach(func() {
testManifestServices = ManifestApplicationService{
Name: "my-service",
}
})
BeforeEach(func() {
serviceString = "name: my-svc"
unmarshalledService = ManifestApplicationService{}
})

JustBeforeEach(func() {
validateErr = validator.DecodeAndValidateYAMLPayload(createYAMLRequest(testManifestServices), &ManifestApplicationService{})
})
JustBeforeEach(func() {
unmarshalErr = yaml.Unmarshal([]byte(serviceString), &unmarshalledService)
})

It("validates the struct", func() {
Expect(validateErr).NotTo(HaveOccurred())
It("succeeds", func() {
Expect(unmarshalErr).NotTo(HaveOccurred())
Expect(unmarshalledService).To(Equal(ManifestApplicationService{
Name: "my-svc",
}))
})

When("the name tag is missing", func() {
BeforeEach(func() {
serviceString = "my-svc"
})

It("succeeds", func() {
Expect(unmarshalErr).NotTo(HaveOccurred())
Expect(unmarshalledService).To(Equal(ManifestApplicationService{
Name: "my-svc",
}))
})

When("the value is not a string", func() {
BeforeEach(func() {
serviceString = "123"
})

It("errors", func() {
Expect(unmarshalErr).To(MatchError(ContainSubstring("invalid service")))
})
})
})
})

When("name is not specified", func() {
Describe("Validate", func() {
var (
validateErr error
testManifestServices ManifestApplicationService
)

BeforeEach(func() {
testManifestServices.Name = ""
testManifestServices = ManifestApplicationService{
Name: "my-service",
}
})

It("returns a validation error", func() {
expectUnprocessableEntityError(validateErr, "name cannot be blank")
JustBeforeEach(func() {
validateErr = validator.DecodeAndValidateYAMLPayload(createYAMLRequest(testManifestServices), &ManifestApplicationService{})
})

It("validates the struct", func() {
Expect(validateErr).NotTo(HaveOccurred())
})

When("name is not specified", func() {
BeforeEach(func() {
testManifestServices.Name = ""
})

It("returns a validation error", func() {
expectUnprocessableEntityError(validateErr, "name cannot be blank")
})
})
})
})
Expand Down

0 comments on commit e3a0674

Please sign in to comment.