Skip to content

Commit

Permalink
Support for a new binding type: backup_agent
Browse files Browse the repository at this point in the history
To be used by ADBR

[#171057745]

Co-authored-by: Derik Evangelista <[email protected]>
  • Loading branch information
pivotal-marcela-campo and Derik Evangelista committed Mar 3, 2020
1 parent a2eac63 commit 447a19e
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 61 deletions.
86 changes: 51 additions & 35 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1546,41 +1546,7 @@ var _ = Describe("Service Broker API", func() {
}
})

Context("the request is malformed", func() {
BeforeEach(func() {
bindingID = uniqueBindingID()
})

It("missing header X-Broker-API-Version", func() {
response := makeBindingRequestWithSpecificAPIVersion(instanceID, bindingID, map[string]interface{}{}, "", false)
Expect(response.StatusCode).To(Equal(412))
Expect(lastLogLine().Message).To(ContainSubstring("version-header-check.broker-api-version-invalid"))
Expect(lastLogLine().Data["error"]).To(ContainSubstring("X-Broker-API-Version Header not set"))
})

It("has wrong version of API", func() {
response := makeBindingRequestWithSpecificAPIVersion(instanceID, bindingID, map[string]interface{}{}, "1.14", false)
Expect(response.StatusCode).To(Equal(412))
Expect(lastLogLine().Message).To(ContainSubstring("version-header-check.broker-api-version-invalid"))
Expect(lastLogLine().Data["error"]).To(ContainSubstring("X-Broker-API-Version Header must be 2.x"))
})

It("missing service-id", func() {
response := makeBindingRequestWithSpecificAPIVersion(instanceID, bindingID, map[string]interface{}{"plan_id": "123"}, "2.14", false)
Expect(response.StatusCode).To(Equal(400))
Expect(lastLogLine().Message).To(ContainSubstring(".bind.service-id-missing"))
Expect(lastLogLine().Data["error"]).To(ContainSubstring("service_id missing"))
})

It("missing plan-id", func() {
response := makeBindingRequestWithSpecificAPIVersion(instanceID, bindingID, map[string]interface{}{"service_id": "123"}, "2.14", false)
Expect(response.StatusCode).To(Equal(400))
Expect(lastLogLine().Message).To(ContainSubstring(".bind.plan-id-missing"))
Expect(lastLogLine().Data["error"]).To(ContainSubstring("plan_id missing"))
})
})

Context("when the associated instance exists", func() {
When("can bind", func() {
It("calls Bind on the service broker with the instance and binding ids", func() {
makeBindingRequest(instanceID, bindingID, details)
Expect(fakeServiceBroker.BoundInstanceIDs).To(ContainElement(instanceID))
Expand Down Expand Up @@ -1766,6 +1732,21 @@ var _ = Describe("Service Broker API", func() {
})
})

When("backup_agent is requested", func() {
BeforeEach(func() {
details["bind_resource"] = map[string]interface{}{"backup_agent": true}
fakeServiceBroker.BackupAgentURL = "http://backup.example.com"
})

It("responds with the backup agent url", func() {
response := makeBindingRequest(instanceID, bindingID, details)
Expect(fakeServiceBroker.BoundBindings[bindingID].BindResource).NotTo(BeNil())
Expect(fakeServiceBroker.BoundBindings[bindingID].BindResource.BackupAgent).To(BeTrue())

Expect(response.StatusCode).To(Equal(http.StatusCreated))
Expect(response.Body).To(MatchJSON(`{"backup_agent_url":"http://backup.example.com"}`))
})
})
})

Context("when the associated instance does not exist", func() {
Expand Down Expand Up @@ -1940,6 +1921,41 @@ var _ = Describe("Service Broker API", func() {
})
})
})

Context("the request is malformed", func() {
BeforeEach(func() {
bindingID = uniqueBindingID()
})

It("missing header X-Broker-API-Version", func() {
response := makeBindingRequestWithSpecificAPIVersion(instanceID, bindingID, map[string]interface{}{}, "", false)
Expect(response.StatusCode).To(Equal(412))
Expect(lastLogLine().Message).To(ContainSubstring("version-header-check.broker-api-version-invalid"))
Expect(lastLogLine().Data["error"]).To(ContainSubstring("X-Broker-API-Version Header not set"))
})

It("has wrong version of API", func() {
response := makeBindingRequestWithSpecificAPIVersion(instanceID, bindingID, map[string]interface{}{}, "1.14", false)
Expect(response.StatusCode).To(Equal(412))
Expect(lastLogLine().Message).To(ContainSubstring("version-header-check.broker-api-version-invalid"))
Expect(lastLogLine().Data["error"]).To(ContainSubstring("X-Broker-API-Version Header must be 2.x"))
})

It("missing service-id", func() {
response := makeBindingRequestWithSpecificAPIVersion(instanceID, bindingID, map[string]interface{}{"plan_id": "123"}, "2.14", false)
Expect(response.StatusCode).To(Equal(400))
Expect(lastLogLine().Message).To(ContainSubstring(".bind.service-id-missing"))
Expect(lastLogLine().Data["error"]).To(ContainSubstring("service_id missing"))
})

It("missing plan-id", func() {
response := makeBindingRequestWithSpecificAPIVersion(instanceID, bindingID, map[string]interface{}{"service_id": "123"}, "2.14", false)
Expect(response.StatusCode).To(Equal(400))
Expect(lastLogLine().Message).To(ContainSubstring(".bind.plan-id-missing"))
Expect(lastLogLine().Data["error"]).To(ContainSubstring("plan_id missing"))
})
})

})

Describe("unbinding", func() {
Expand Down
6 changes: 4 additions & 2 deletions domain/apiresponses/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ type AsyncBindResponse struct {
}

type BindingResponse struct {
Credentials interface{} `json:"credentials"`
Credentials interface{} `json:"credentials,omitempty"`
SyslogDrainURL string `json:"syslog_drain_url,omitempty"`
RouteServiceURL string `json:"route_service_url,omitempty"`
VolumeMounts []domain.VolumeMount `json:"volume_mounts,omitempty"`
BackupAgentURL string `json:"backup_agent_url,omitempty"`
}

type GetBindingResponse struct {
Expand All @@ -75,8 +76,9 @@ type UnbindResponse struct {
}

type ExperimentalVolumeMountBindingResponse struct {
Credentials interface{} `json:"credentials"`
Credentials interface{} `json:"credentials,omitempty"`
SyslogDrainURL string `json:"syslog_drain_url,omitempty"`
RouteServiceURL string `json:"route_service_url,omitempty"`
VolumeMounts []domain.ExperimentalVolumeMount `json:"volume_mounts,omitempty"`
BackupAgentURL string `json:"backup_agent_url,omitempty"`
}
11 changes: 0 additions & 11 deletions domain/apiresponses/responses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,6 @@ var _ = Describe("Update Response", func() {
})
})

var _ = Describe("Binding Response", func() {
Describe("JSON encoding", func() {
It("has a credentials object", func() {
binding := apiresponses.BindingResponse{}
jsonString := `{"credentials":null}`

Expect(json.Marshal(binding)).To(MatchJSON(jsonString))
})
})
})

var _ = Describe("Error Response", func() {
Describe("JSON encoding", func() {
It("has a description field", func() {
Expand Down
2 changes: 2 additions & 0 deletions domain/service_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ type BindResource struct {
SpaceGuid string `json:"space_guid,omitempty"`
Route string `json:"route,omitempty"`
CredentialClientID string `json:"credential_client_id,omitempty"`
BackupAgent bool `json:"backup_agent,omitempty"`
}

type UnbindDetails struct {
Expand All @@ -176,6 +177,7 @@ type Binding struct {
Credentials interface{} `json:"credentials"`
SyslogDrainURL string `json:"syslog_drain_url"`
RouteServiceURL string `json:"route_service_url"`
BackupAgentURL string `json:"backup_agent_url,omitempty"`
VolumeMounts []VolumeMount `json:"volume_mounts"`
}

Expand Down
5 changes: 5 additions & 0 deletions fakes/fake_service_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type FakeServiceBroker struct {
BoundBindings map[string]brokerapi.BindDetails
SyslogDrainURL string
RouteServiceURL string
BackupAgentURL string
VolumeMounts []brokerapi.VolumeMount

UnbindingDetails brokerapi.UnbindDetails
Expand Down Expand Up @@ -386,6 +387,10 @@ func (fakeBroker *FakeServiceBroker) Bind(context context.Context, instanceID, b
VolumeMounts: fakeBroker.VolumeMounts,
}

if fakeBroker.BackupAgentURL != "" {
binding = brokerapi.Binding{BackupAgentURL: fakeBroker.BackupAgentURL}
}

if _, ok := fakeBroker.BoundBindings[bindingID]; ok {
if reflect.DeepEqual(fakeBroker.BoundBindings[bindingID], details) {
binding.AlreadyExists = true
Expand Down
3 changes: 3 additions & 0 deletions handlers/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (h APIHandler) Bind(w http.ResponseWriter, req *http.Request) {
SyslogDrainURL: binding.SyslogDrainURL,
RouteServiceURL: binding.RouteServiceURL,
VolumeMounts: binding.VolumeMounts,
BackupAgentURL: binding.BackupAgentURL,
})
return
}
Expand Down Expand Up @@ -126,6 +127,7 @@ func (h APIHandler) Bind(w http.ResponseWriter, req *http.Request) {
RouteServiceURL: binding.RouteServiceURL,
SyslogDrainURL: binding.SyslogDrainURL,
VolumeMounts: experimentalVols,
BackupAgentURL: binding.BackupAgentURL,
}
h.respond(w, http.StatusCreated, experimentalBinding)
return
Expand All @@ -136,5 +138,6 @@ func (h APIHandler) Bind(w http.ResponseWriter, req *http.Request) {
SyslogDrainURL: binding.SyslogDrainURL,
RouteServiceURL: binding.RouteServiceURL,
VolumeMounts: binding.VolumeMounts,
BackupAgentURL: binding.BackupAgentURL,
})
}
11 changes: 0 additions & 11 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,6 @@ var _ = Describe("Update Response", func() {
})
})

var _ = Describe("Binding Response", func() {
Describe("JSON encoding", func() {
It("has a credentials object", func() {
binding := brokerapi.BindingResponse{}
jsonString := `{"credentials":null}`

Expect(json.Marshal(binding)).To(MatchJSON(jsonString))
})
})
})

var _ = Describe("Error Response", func() {
Describe("JSON encoding", func() {
It("has a description field", func() {
Expand Down
6 changes: 4 additions & 2 deletions v7/domain/apiresponses/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ type AsyncBindResponse struct {
}

type BindingResponse struct {
Credentials interface{} `json:"credentials"`
Credentials interface{} `json:"credentials,omitempty"`
SyslogDrainURL string `json:"syslog_drain_url,omitempty"`
RouteServiceURL string `json:"route_service_url,omitempty"`
VolumeMounts []domain.VolumeMount `json:"volume_mounts,omitempty"`
BackupAgentURL string `json:"backup_agent_url,omitempty"`
}

type GetBindingResponse struct {
Expand All @@ -75,8 +76,9 @@ type UnbindResponse struct {
}

type ExperimentalVolumeMountBindingResponse struct {
Credentials interface{} `json:"credentials"`
Credentials interface{} `json:"credentials,omitempty"`
SyslogDrainURL string `json:"syslog_drain_url,omitempty"`
RouteServiceURL string `json:"route_service_url,omitempty"`
VolumeMounts []domain.ExperimentalVolumeMount `json:"volume_mounts,omitempty"`
BackupAgentURL string `json:"backup_agent_url,omitempty"`
}
2 changes: 2 additions & 0 deletions v7/domain/service_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ type BindResource struct {
SpaceGuid string `json:"space_guid,omitempty"`
Route string `json:"route,omitempty"`
CredentialClientID string `json:"credential_client_id,omitempty"`
BackupAgent bool `json:"backup_agent,omitempty"`
}

type UnbindDetails struct {
Expand All @@ -176,6 +177,7 @@ type Binding struct {
Credentials interface{} `json:"credentials"`
SyslogDrainURL string `json:"syslog_drain_url"`
RouteServiceURL string `json:"route_service_url"`
BackupAgentURL string `json:"backup_agent_url,omitempty"`
VolumeMounts []VolumeMount `json:"volume_mounts"`
}

Expand Down
3 changes: 3 additions & 0 deletions v7/handlers/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (h APIHandler) Bind(w http.ResponseWriter, req *http.Request) {
SyslogDrainURL: binding.SyslogDrainURL,
RouteServiceURL: binding.RouteServiceURL,
VolumeMounts: binding.VolumeMounts,
BackupAgentURL: binding.BackupAgentURL,
})
return
}
Expand Down Expand Up @@ -126,6 +127,7 @@ func (h APIHandler) Bind(w http.ResponseWriter, req *http.Request) {
RouteServiceURL: binding.RouteServiceURL,
SyslogDrainURL: binding.SyslogDrainURL,
VolumeMounts: experimentalVols,
BackupAgentURL: binding.BackupAgentURL,
}
h.respond(w, http.StatusCreated, experimentalBinding)
return
Expand All @@ -136,5 +138,6 @@ func (h APIHandler) Bind(w http.ResponseWriter, req *http.Request) {
SyslogDrainURL: binding.SyslogDrainURL,
RouteServiceURL: binding.RouteServiceURL,
VolumeMounts: binding.VolumeMounts,
BackupAgentURL: binding.BackupAgentURL,
})
}

0 comments on commit 447a19e

Please sign in to comment.