Skip to content
Open
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
4 changes: 2 additions & 2 deletions docs/IMPLEMENTING_FRAMEWORKS.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ err := ctx.Installer.InstallDependency(dep, targetDir)

### Type 1: Service-Bound Frameworks

Detect when a specific Cloud Foundry service is bound via `VCAP_SERVICES`.
Detect when a specific Cloud Foundry service is bound via `VCAP_SERVICES` or `VCAP_SERVICES_FILE_PATH`.

**Examples:** New Relic, AppDynamics, Seeker Security Provider

**Detection:** Looks for service name/label/tags in `VCAP_SERVICES`
**Detection:** Looks for service name/label/tags in `VCAP_SERVICES` or `VCAP_SERVICES_FILE_PATH`.

### Type 2: Configuration-Based Frameworks

Expand Down
22 changes: 17 additions & 5 deletions src/java/common/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,26 @@ type VCAPService struct {
Credentials map[string]interface{} `json:"credentials"`
}

// GetVCAPServices parses the VCAP_SERVICES environment variable
// Returns an empty VCAPServices map if VCAP_SERVICES is not set
// GetVCAPServices parses the VCAP_SERVICES or VCAP_SERVICES_FILE_PATH environment variable
// Returns an empty VCAPServices map if VCAP_SERVICES or VCAP_SERVICES_FILE_PATH is not set
func GetVCAPServices() (VCAPServices, error) {
vcapServicesStr := os.Getenv("VCAP_SERVICES")
if vcapServicesStr == "" {
return VCAPServices{}, nil
if vcapServicesStr, ok := os.LookupEnv("VCAP_SERVICES"); ok && vcapServicesStr != "" {
return loadVcapServices(vcapServicesStr)
}

if vcapServicesFileName, ok := os.LookupEnv("VCAP_SERVICES_FILE_PATH"); ok && vcapServicesFileName != "" {
vcapServicesContent, err := os.ReadFile(vcapServicesFileName)
if err != nil {
return VCAPServices{}, err
}
return loadVcapServices(string(vcapServicesContent))
}

return VCAPServices{}, nil
}

// loadVcapServices loads services json content into VCAPServices map
func loadVcapServices(vcapServicesStr string) (VCAPServices, error) {
var services VCAPServices
if err := json.Unmarshal([]byte(vcapServicesStr), &services); err != nil {
return nil, err
Expand Down
31 changes: 31 additions & 0 deletions src/java/frameworks/framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ var _ = Describe("VCAP Services", func() {
AfterEach(func() {
os.Unsetenv("VCAP_SERVICES")
})
AfterEach(func() {
os.Unsetenv("VCAP_SERVICES_FILE_PATH")
})

Context("with empty VCAP_SERVICES", func() {
It("returns empty services map", func() {
Expand All @@ -104,6 +107,16 @@ var _ = Describe("VCAP Services", func() {
})
})

Context("with empty VCAP_SERVICES_FILE_PATH", func() {
It("returns empty services map", func() {
os.Setenv("VCAP_SERVICES_FILE_PATH", "")

services, err := frameworks.GetVCAPServices()
Expect(err).NotTo(HaveOccurred())
Expect(services).To(HaveLen(0))
})
})

Context("with valid VCAP_SERVICES JSON", func() {
It("parses services correctly", func() {
vcapJSON := `{
Expand Down Expand Up @@ -133,6 +146,24 @@ var _ = Describe("VCAP Services", func() {
})
})

Context("with valid VCAP_SERVICES_FILE_PATH JSON", func() {
It("parses services correctly", func() {
os.Setenv("VCAP_SERVICES_FILE_PATH", "testdata/vcap_services.json")

services, err := frameworks.GetVCAPServices()
Expect(err).NotTo(HaveOccurred())
Expect(services.HasService("newrelic")).To(BeTrue())

service := services.GetService("newrelic")
Expect(service).NotTo(BeNil())
Expect(service.Name).To(Equal("newrelic-service"))

licenseKey, ok := service.Credentials["licenseKey"].(string)
Expect(ok).To(BeTrue())
Expect(licenseKey).To(Equal("test-key-123"))
})
})

Context("with multiple services", func() {
It("handles multiple service instances", func() {
vcapJSON := `{
Expand Down
10 changes: 10 additions & 0 deletions src/java/frameworks/testdata/vcap_services.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"newrelic": [{
"name": "newrelic-service",
"label": "newrelic",
"tags": ["apm", "monitoring"],
"credentials": {
"licenseKey": "test-key-123"
}
}]
}