Skip to content

Commit

Permalink
Add created_by to binding (#1311)
Browse files Browse the repository at this point in the history
* Add context to binding

* Rename context field to created_by

* Add more test cases for context CreatedBy
  • Loading branch information
KsaweryZietara authored Oct 14, 2024
1 parent 931e8fa commit b2ab728
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 0 deletions.
37 changes: 37 additions & 0 deletions internal/broker/bind_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,33 @@ type BindEndpoint struct {
log logrus.FieldLogger
}

type BindingContext struct {
Email *string `json:"email,omitempty"`
Origin *string `json:"origin,omitempty"`
}

func (b *BindingContext) CreatedBy() string {
if b.Email == nil && b.Origin == nil {
return ""
}

email := ""
if b.Email != nil {
email = *b.Email
}

origin := ""
if b.Origin != nil {
origin = *b.Origin
}

if email != "" && origin != "" {
return email + " " + origin
}

return email + origin
}

type BindingParams struct {
ServiceAccount bool `json:"service_account,omit"`
ExpirationSeconds int `json:"expiration_seconds,omit"`
Expand Down Expand Up @@ -95,6 +122,15 @@ func (b *BindEndpoint) Bind(ctx context.Context, instanceID, bindingID string, d
).WithErrorKey("BindingNotSupported").Build()
}

var bindingContext BindingContext
if len(details.RawContext) != 0 {
err = json.Unmarshal(details.RawContext, &bindingContext)
if err != nil {
message := fmt.Sprintf("failed to unmarshal context: %s", err)
return domain.Binding{}, apiresponses.NewFailureResponse(fmt.Errorf(message), http.StatusBadRequest, message)
}
}

var parameters BindingParams
if len(details.RawParameters) != 0 {
err = json.Unmarshal(details.RawParameters, &parameters)
Expand Down Expand Up @@ -127,6 +163,7 @@ func (b *BindEndpoint) Bind(ctx context.Context, instanceID, bindingID string, d
UpdatedAt: time.Now(),

ExpirationSeconds: int64(expirationSeconds),
CreatedBy: bindingContext.CreatedBy(),
}
if parameters.ServiceAccount {
// get kubeconfig for the instance
Expand Down
57 changes: 57 additions & 0 deletions internal/broker/bind_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ func TestCreateBindingEndpoint(t *testing.T) {
"plan_id": "%s",
"parameters": {
"service_account": true
},
"context": {
"email": "john.smith@email.com",
"origin": "origin"
}
}`, fixture.PlanId), t)
defer response.Body.Close()
Expand Down Expand Up @@ -380,6 +384,59 @@ func TestCreateBindingEndpoint(t *testing.T) {
})
}

func TestCreatedBy(t *testing.T) {
emptyStr := ""
email := "john.smith@email.com"
origin := "origin"
tests := []struct {
name string
context BindingContext
expected string
}{
{
name: "Both Email and Origin are nil",
context: BindingContext{Email: nil, Origin: nil},
expected: "",
},
{
name: "Both Email and Origin are empty",
context: BindingContext{Email: &emptyStr, Origin: &emptyStr},
expected: "",
},
{
name: "Origin is nil",
context: BindingContext{Email: &email, Origin: nil},
expected: "john.smith@email.com",
},
{
name: "Origin is empty",
context: BindingContext{Email: &email, Origin: &emptyStr},
expected: "john.smith@email.com",
},
{
name: "Email is nil",
context: BindingContext{Email: nil, Origin: &origin},
expected: "origin",
},
{
name: "Email is empty",
context: BindingContext{Email: &emptyStr, Origin: &origin},
expected: "origin",
},
{
name: "Both Email and Origin are set",
context: BindingContext{Email: &email, Origin: &origin},
expected: "john.smith@email.com origin",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, tt.context.CreatedBy())
})
}
}

func assertClusterAccess(t *testing.T, response *http.Response, controlSecretName string, binding domain.Binding) {

credentials, ok := binding.Credentials.(map[string]interface{})
Expand Down
1 change: 1 addition & 0 deletions internal/fixture/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ func FixBindingWithInstanceID(bindingID string, instanceID string) internal.Bind
Kubeconfig: "kubeconfig",
ExpirationSeconds: 600,
BindingType: internal.BINDING_TYPE_SERVICE_ACCOUNT,
CreatedBy: "john.smith@email.com",
}
}

Expand Down
1 change: 1 addition & 0 deletions internal/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,4 +577,5 @@ type Binding struct {
Kubeconfig string
ExpirationSeconds int64
BindingType string
CreatedBy string
}
1 change: 1 addition & 0 deletions internal/storage/dbmodel/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ type BindingDTO struct {
Kubeconfig string
ExpirationSeconds int64
BindingType string
CreatedBy string
}
2 changes: 2 additions & 0 deletions internal/storage/driver/postsql/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func (s *Binding) toBindingDTO(binding *internal.Binding) (dbmodel.BindingDTO, e
InstanceID: binding.InstanceID,
CreatedAt: binding.CreatedAt,
ExpirationSeconds: binding.ExpirationSeconds,
CreatedBy: binding.CreatedBy,
}, nil
}

Expand All @@ -106,5 +107,6 @@ func (s *Binding) toBinding(dto dbmodel.BindingDTO) (internal.Binding, error) {
InstanceID: dto.InstanceID,
CreatedAt: dto.CreatedAt,
ExpirationSeconds: dto.ExpirationSeconds,
CreatedBy: dto.CreatedBy,
}, nil
}
1 change: 1 addition & 0 deletions internal/storage/driver/postsql/binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestBinding(t *testing.T) {
assert.Equal(t, fixedBinding.ExpirationSeconds, createdBinding.ExpirationSeconds)
assert.NotNil(t, createdBinding.Kubeconfig)
assert.Equal(t, fixedBinding.Kubeconfig, createdBinding.Kubeconfig)
assert.Equal(t, fixedBinding.CreatedBy, createdBinding.CreatedBy)

// when
err = brokerStorage.Bindings().DeleteByBindingID(testBindingId)
Expand Down
1 change: 1 addition & 0 deletions internal/storage/postsql/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func (ws writeSession) InsertBinding(binding dbmodel.BindingDTO) dberr.Error {
Pair("kubeconfig", binding.Kubeconfig).
Pair("expiration_seconds", binding.ExpirationSeconds).
Pair("binding_type", binding.BindingType).
Pair("created_by", binding.CreatedBy).
Exec()

if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE bindings DROP COLUMN created_by;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE bindings
ADD COLUMN created_by VARCHAR(255);

0 comments on commit b2ab728

Please sign in to comment.