Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add created_by to binding #1311

Merged
Merged
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
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
}{
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when we got non-nil but empty values, could you add test case(s)?

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);
Loading