Skip to content

Commit

Permalink
support dc (#258)
Browse files Browse the repository at this point in the history
* support dc

* support dc

* support dc

* support dc

* support dc

* support dc

* support dc

* support dc

* support dc

* data center

* data center

* data center

* data center
  • Loading branch information
I065450 authored Mar 28, 2023
1 parent 9c9b4c1 commit 8b71eeb
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ deploy: manifests helm-charts
--create-namespace \
--namespace=sap-btp-operator \
--values=hack/override_values.yaml \
--set manager.image.repository=controller \
--set manager.image.repository=${IMG} \
--set manager.image.tag=latest

undeploy:
Expand Down
5 changes: 5 additions & 0 deletions api/v1/serviceinstance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ type ServiceInstanceSpec struct {
// +kubebuilder:validation:MinLength=1
ServicePlanName string `json:"servicePlanName"`

// The dataCenter in case service offering and plan name exist in other data center and not on main
// +optional
DataCenter string `json:"dataCenter,omitempty"`

// The plan ID in case service offering and plan name are ambiguous
// +optional
ServicePlanID string `json:"servicePlanID,omitempty"`
Expand Down Expand Up @@ -108,6 +112,7 @@ type ServiceInstanceStatus struct {
// +kubebuilder:storageversion
// +kubebuilder:printcolumn:JSONPath=".spec.serviceOfferingName",name="Offering",type=string
// +kubebuilder:printcolumn:JSONPath=".spec.servicePlanName",name="Plan",type=string
// +kubebuilder:printcolumn:JSONPath=".spec.dataCenter",name="dataCenter",type=string
// +kubebuilder:printcolumn:JSONPath=".status.conditions[0].reason",name="Status",type=string
// +kubebuilder:printcolumn:JSONPath=".status.ready",name="Ready",type=string
// +kubebuilder:printcolumn:JSONPath=".metadata.creationTimestamp",name="Age",type=date
Expand Down
1 change: 1 addition & 0 deletions api/v1/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func getInstance() *ServiceInstance {
Groups: []string{"test-group"},
Extra: map[string]v1.ExtraValue{"key": {"val"}},
},
DataCenter: "tel-aviv",
},

Status: ServiceInstanceStatus{},
Expand Down
26 changes: 14 additions & 12 deletions client/sm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ const (
type Client interface {
ListInstances(*Parameters) (*types.ServiceInstances, error)
GetInstanceByID(string, *Parameters) (*types.ServiceInstance, error)
UpdateInstance(id string, updatedInstance *types.ServiceInstance, serviceName string, planName string, q *Parameters, user string) (*types.ServiceInstance, string, error)
Provision(instance *types.ServiceInstance, serviceName string, planName string, q *Parameters, user string) (*ProvisionResponse, error)
UpdateInstance(id string, updatedInstance *types.ServiceInstance, serviceName string, planName string, q *Parameters, user string, dataCenter string) (*types.ServiceInstance, string, error)
Provision(instance *types.ServiceInstance, serviceName string, planName string, q *Parameters, user string, dataCenter string) (*ProvisionResponse, error)
Deprovision(id string, q *Parameters, user string) (string, error)

ListBindings(*Parameters) (*types.ServiceBindings, error)
Expand Down Expand Up @@ -91,7 +91,7 @@ type ProvisionResponse struct {
Tags json.RawMessage
}

// NewClientWithAuth returns new SM Client configured with the provided configuration
// NewClient NewClientWithAuth returns new SM Client configured with the provided configuration
func NewClient(ctx context.Context, config *ClientConfig, httpClient auth.HTTPClient) (Client, error) {
if httpClient != nil {
return &serviceManagerClient{Context: ctx, Config: config, HTTPClient: httpClient}, nil
Expand All @@ -117,14 +117,14 @@ func NewClient(ctx context.Context, config *ClientConfig, httpClient auth.HTTPCl
}

// Provision provisions a new service instance in service manager
func (client *serviceManagerClient) Provision(instance *types.ServiceInstance, serviceName string, planName string, q *Parameters, user string) (*ProvisionResponse, error) {
func (client *serviceManagerClient) Provision(instance *types.ServiceInstance, serviceName string, planName string, q *Parameters, user string, dataCenter string) (*ProvisionResponse, error) {
var newInstance *types.ServiceInstance
var instanceID string
if len(serviceName) == 0 || len(planName) == 0 {
return nil, fmt.Errorf("missing field values. Specify service name and plan name for the instance '%s'", instance.Name)
}

planInfo, err := client.getPlanInfo(instance.ServicePlanID, serviceName, planName)
planInfo, err := client.getPlanInfo(instance.ServicePlanID, serviceName, planName, dataCenter)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -213,10 +213,10 @@ func (client *serviceManagerClient) Unbind(id string, q *Parameters, user string
return client.delete(types.ServiceBindingsURL+"/"+id, q, user)
}

func (client *serviceManagerClient) UpdateInstance(id string, updatedInstance *types.ServiceInstance, serviceName string, planName string, q *Parameters, user string) (*types.ServiceInstance, string, error) {
func (client *serviceManagerClient) UpdateInstance(id string, updatedInstance *types.ServiceInstance, serviceName string, planName string, q *Parameters, user string, dataCenter string) (*types.ServiceInstance, string, error) {
var result *types.ServiceInstance

planInfo, err := client.getPlanInfo(updatedInstance.ServicePlanID, serviceName, planName)
planInfo, err := client.getPlanInfo(updatedInstance.ServicePlanID, serviceName, planName, dataCenter)
if err != nil {
return nil, "", err
}
Expand Down Expand Up @@ -396,21 +396,23 @@ func (client *serviceManagerClient) callWithUser(method string, smpath string, b
return resp, nil
}

func (client *serviceManagerClient) getPlanInfo(planID string, serviceName string, planName string) (*planInfo, error) {
offerings, err := client.getServiceOfferingsByName(serviceName)
func (client *serviceManagerClient) getPlanInfo(planID string, serviceName string, planName string, dataCenter string) (*planInfo, error) {

offerings, err := client.getServiceOfferingsByNameAndDataCenter(serviceName, dataCenter)
if err != nil {
return nil, err
}

var commaSepOfferingIds string
if len(offerings.ServiceOfferings) == 0 {
return nil, fmt.Errorf("couldn't find the service offering '%s'", serviceName)
return nil, fmt.Errorf("couldn't find the service offering '%s' on dataCenter '%s'", serviceName, dataCenter)
}

serviceOfferingIds := make([]string, 0, len(offerings.ServiceOfferings))
for _, svc := range offerings.ServiceOfferings {
serviceOfferingIds = append(serviceOfferingIds, svc.ID)
}

commaSepOfferingIds = "'" + strings.Join(serviceOfferingIds, "', '") + "'"

query := &Parameters{
Expand Down Expand Up @@ -447,9 +449,9 @@ func (client *serviceManagerClient) getPlanInfo(planID string, serviceName strin
return nil, err
}

func (client *serviceManagerClient) getServiceOfferingsByName(serviceName string) (*types.ServiceOfferings, error) {
func (client *serviceManagerClient) getServiceOfferingsByNameAndDataCenter(serviceName string, dataCenter string) (*types.ServiceOfferings, error) {
query := &Parameters{
FieldQuery: []string{fmt.Sprintf("catalog_name eq '%s'", serviceName)},
FieldQuery: []string{fmt.Sprintf("catalog_name eq '%s' and data_center eq '%s'", serviceName, dataCenter)},
}
offerings, err := client.ListOfferings(query)
if err != nil {
Expand Down
38 changes: 19 additions & 19 deletions client/sm/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ var _ = Describe("Client test", func() {

Context("When valid instance is being provisioned synchronously", func() {
It("should provision successfully", func() {
res, err := client.Provision(instance, serviceName, planName, params, "test-user")
res, err := client.Provision(instance, serviceName, planName, params, "test-user", "")

Expect(err).ShouldNot(HaveOccurred())
Expect(res.Location).Should(HaveLen(0))
Expand Down Expand Up @@ -208,7 +208,7 @@ var _ = Describe("Client test", func() {
})

It("should provision successfully", func() {
res, err := client.Provision(instance, "mongo", "small", params, "test-user")
res, err := client.Provision(instance, "mongo", "small", params, "test-user", "")
Expect(err).ShouldNot(HaveOccurred())
Expect(res.Location).Should(HaveLen(0))
Expect(res.InstanceID).To(Equal(instance.ID))
Expand All @@ -218,7 +218,7 @@ var _ = Describe("Client test", func() {
Context("When no plan id provided", func() {
It("should provision successfully", func() {
instance.ServicePlanID = ""
res, err := client.Provision(instance, "mongo", "small", params, "test-user")
res, err := client.Provision(instance, "mongo", "small", params, "test-user", "")
Expect(err).ShouldNot(HaveOccurred())
Expect(res.Location).Should(HaveLen(0))
Expect(res.InstanceID).To(Equal(instance.ID))
Expand All @@ -229,15 +229,15 @@ var _ = Describe("Client test", func() {
Context("When invalid instance is being provisioned synchronously", func() {
When("No service name", func() {
It("should fail to provision", func() {
res, err := client.Provision(instance, "", "small", params, "test-user")
res, err := client.Provision(instance, "", "small", params, "test-user", "")
Expect(err).Should(HaveOccurred())
Expect(res).To(BeNil())
})
})

When("No plan name", func() {
It("should fail to provision", func() {
res, err := client.Provision(instance, "mongo", "", params, "test-user")
res, err := client.Provision(instance, "mongo", "", params, "test-user", "")
Expect(err).Should(HaveOccurred())
Expect(res).To(BeNil())
})
Expand All @@ -246,7 +246,7 @@ var _ = Describe("Client test", func() {
When("Plan id not match plan name", func() {
It("should fail", func() {
instance.ServicePlanID = "some-id"
res, err := client.Provision(instance, "mongo", "small", params, "test-user")
res, err := client.Provision(instance, "mongo", "small", params, "test-user", "")
Expect(err).Should(HaveOccurred())
Expect(res).To(BeNil())
})
Expand All @@ -260,7 +260,7 @@ var _ = Describe("Client test", func() {
handlerDetails[1] = HandlerDetails{Method: http.MethodGet, Path: types.ServiceOfferingsURL, ResponseBody: responseBody, ResponseStatusCode: http.StatusOK}
})
It("should fail", func() {
res, err := client.Provision(instance, "mongo2", "small", params, "test-user")
res, err := client.Provision(instance, "mongo2", "small", params, "test-user", "")
Expect(err).Should(HaveOccurred())
Expect(res).To(BeNil())
})
Expand All @@ -274,7 +274,7 @@ var _ = Describe("Client test", func() {
handlerDetails[0] = HandlerDetails{Method: http.MethodPost, Path: types.ServiceInstancesURL, ResponseStatusCode: http.StatusAccepted, Headers: map[string]string{"Location": locationHeader}}
})
It("should receive operation location", func() {
res, err := client.Provision(instance, serviceName, planName, params, "test-user")
res, err := client.Provision(instance, serviceName, planName, params, "test-user", "")

Expect(err).ShouldNot(HaveOccurred())
Expect(res.Location).Should(Equal(locationHeader))
Expand All @@ -293,7 +293,7 @@ var _ = Describe("Client test", func() {

})
It("should return error", func() {
res, err := client.Provision(instance, serviceName, planName, params, "test-user")
res, err := client.Provision(instance, serviceName, planName, params, "test-user", "")

Expect(err).Should(HaveOccurred())
Expect(res).To(BeNil())
Expand All @@ -307,7 +307,7 @@ var _ = Describe("Client test", func() {
handlerDetails[0] = HandlerDetails{Method: http.MethodPost, Path: types.ServiceInstancesURL, ResponseBody: responseBody, ResponseStatusCode: http.StatusOK}
})
It("should return error with status code", func() {
res, err := client.Provision(instance, serviceName, planName, params, "test-user")
res, err := client.Provision(instance, serviceName, planName, params, "test-user", "")

Expect(err).Should(HaveOccurred())
verifyErrorMsg(err.Error(), handlerDetails[0].Path, handlerDetails[0].ResponseBody, handlerDetails[0].ResponseStatusCode)
Expand All @@ -322,7 +322,7 @@ var _ = Describe("Client test", func() {

})
It("should return error with url and description", func() {
res, err := client.Provision(instance, serviceName, planName, params, "test-user")
res, err := client.Provision(instance, serviceName, planName, params, "test-user", "")

Expect(err).Should(HaveOccurred())
verifyErrorMsg(err.Error(), handlerDetails[0].Path, handlerDetails[0].ResponseBody, handlerDetails[0].ResponseStatusCode)
Expand All @@ -337,7 +337,7 @@ var _ = Describe("Client test", func() {

})
It("should return error without url and description if invalid response body", func() {
res, err := client.Provision(instance, serviceName, planName, params, "test-user")
res, err := client.Provision(instance, serviceName, planName, params, "test-user", "")

Expect(err).Should(HaveOccurred())
verifyErrorMsg(err.Error(), handlerDetails[0].Path, handlerDetails[0].ResponseBody, handlerDetails[0].ResponseStatusCode)
Expand Down Expand Up @@ -443,7 +443,7 @@ var _ = Describe("Client test", func() {
HandlerDetails{Method: http.MethodPatch, Path: types.ServiceInstancesURL + "/" + instance.ID, ResponseBody: responseBody, ResponseStatusCode: http.StatusOK})
})
It("should update successfully", func() {
responseInstance, location, err := client.UpdateInstance(instance.ID, instance, serviceName, planName, params, "test-user")
responseInstance, location, err := client.UpdateInstance(instance.ID, instance, serviceName, planName, params, "test-user", "")

Expect(err).ShouldNot(HaveOccurred())
Expect(location).Should(HaveLen(0))
Expand All @@ -459,7 +459,7 @@ var _ = Describe("Client test", func() {
HandlerDetails{Method: http.MethodPatch, Path: types.ServiceInstancesURL + "/" + instance.ID, ResponseStatusCode: http.StatusAccepted, Headers: map[string]string{"Location": locationHeader}})
})
It("should receive operation location", func() {
responseInstance, location, err := client.UpdateInstance(instance.ID, instance, serviceName, planName, params, "test-user")
responseInstance, location, err := client.UpdateInstance(instance.ID, instance, serviceName, planName, params, "test-user", "")

Expect(err).ShouldNot(HaveOccurred())
Expect(location).Should(Equal(locationHeader))
Expand All @@ -478,7 +478,7 @@ var _ = Describe("Client test", func() {
HandlerDetails{Method: http.MethodPatch, Path: types.ServiceInstancesURL + "/" + instance.ID, ResponseBody: responseBody, ResponseStatusCode: http.StatusOK})
})
It("should return error", func() {
responseInstance, location, err := client.UpdateInstance(instance.ID, instance, serviceName, planName, params, "test-user")
responseInstance, location, err := client.UpdateInstance(instance.ID, instance, serviceName, planName, params, "test-user", "")

Expect(err).Should(HaveOccurred())
Expect(location).Should(BeEmpty())
Expand All @@ -494,7 +494,7 @@ var _ = Describe("Client test", func() {
HandlerDetails{Method: http.MethodPatch, Path: types.ServiceInstancesURL + "/" + instance.ID, ResponseBody: responseBody, ResponseStatusCode: http.StatusTeapot})
})
It("should return error with status code", func() {
responseInstance, location, err := client.UpdateInstance(instance.ID, instance, serviceName, planName, params, "test-user")
responseInstance, location, err := client.UpdateInstance(instance.ID, instance, serviceName, planName, params, "test-user", "")

Expect(err).Should(HaveOccurred())
Expect(location).Should(BeEmpty())
Expand All @@ -510,7 +510,7 @@ var _ = Describe("Client test", func() {
HandlerDetails{Method: http.MethodPatch, Path: types.ServiceInstancesURL + "/" + instance.ID, ResponseBody: responseBody, ResponseStatusCode: http.StatusBadRequest})
})
It("should return error with url and description", func() {
responseInstance, location, err := client.UpdateInstance(instance.ID, instance, serviceName, planName, params, "test-user")
responseInstance, location, err := client.UpdateInstance(instance.ID, instance, serviceName, planName, params, "test-user", "")

Expect(err).Should(HaveOccurred())
Expect(location).Should(BeEmpty())
Expand All @@ -526,7 +526,7 @@ var _ = Describe("Client test", func() {
HandlerDetails{Method: http.MethodPatch, Path: types.ServiceInstancesURL + "/" + instance.ID, ResponseBody: responseBody, ResponseStatusCode: http.StatusBadRequest})
})
It("should return error without url and description if invalid response body", func() {
responseInstance, location, err := client.UpdateInstance(instance.ID, instance, serviceName, planName, params, "test-user")
responseInstance, location, err := client.UpdateInstance(instance.ID, instance, serviceName, planName, params, "test-user", "")

Expect(err).Should(HaveOccurred())
Expect(location).Should(BeEmpty())
Expand All @@ -543,7 +543,7 @@ var _ = Describe("Client test", func() {
var err error
client, err = NewClient(context.TODO(), &ClientConfig{URL: "invalidURL"}, fakeAuthClient)
Expect(err).ToNot(HaveOccurred())
_, location, err := client.UpdateInstance(instance.ID, instance, serviceName, planName, params, "test-user")
_, location, err := client.UpdateInstance(instance.ID, instance, serviceName, planName, params, "test-user", "")

Expect(err).Should(HaveOccurred())
Expect(location).Should(BeEmpty())
Expand Down
Loading

0 comments on commit 8b71eeb

Please sign in to comment.