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 UpdateSubscription endpoint #398

Merged
merged 10 commits into from
Mar 29, 2018
Merged

Add UpdateSubscription endpoint #398

merged 10 commits into from
Mar 29, 2018

Conversation

alexdebrie
Copy link
Contributor

@alexdebrie alexdebrie commented Mar 23, 2018

What did you implement:

Adds an UpdateSubscription API endpoint in the Config API. For now, this should only be called if updating the CORS config on an HTTP subscription as the rest of the metadata is included in the subscriptionId.

How did you implement it:

Most of the implementation is pretty straight-forward. As noted above, we should only allow a Subscription to be updated if the CORS config is being changed, as the rest of the Subscription data is included in the subscriptionID. in very limited scenarios. If you're changing one of those elements, you're not updating a Subscription but rather creating a new Subscription.

To handle this check, I generate a subscriptionID and compare it against the existing subscriptionID. If they're different, I throw an error. I thought this would be better than explicitly checking for CORS changes or otherwise comparing the update to the existing. It also should work in the future if we add additional metadata to the Subscription object that's not in the subscription ID. make sure certain values haven't changed and throw an error if they do.

How can we verify it:

Start the Event Gateway locally, then:

  1. Create a function:
curl -X POST \
  http://localhost:4001/v1/spaces/default/functions \
  -H 'Content-Type: application/json' \
  -d '{
	"functionId": "awslambda",
	"type": "awslambda",
	"provider": {
		"arn": "test",
		"region": "us-west-2",
		"awsAccessKeyId": "test",
		"awsSecretAccessKey": "secret"
	}
}'
  1. Create a subscription:
curl -X POST \
  http://localhost:4001/v1/spaces/default/subscriptions \
  -H 'Content-Type: application/json' \
  -d '{
	"functionId": "awslambda", 
	"event": "http",
	"method": "GET",
	"path": "/"
}'
  1. Update the subscription by adding CORS:
curl -X PUT \
  'http://localhost:4001/v1/spaces/default/subscriptions/http,GET,%2F' \
  -H 'Content-Type: application/json' \
  -d '{
	"functionId": "awslambda", 
	"event": "http",
	"method": "GET",
	"path": "/",
	"cors": {
		"origins": ["*"]
	}
}'

This should succeed and return the full subscription:

{"space":"default","subscriptionId":"http,GET,%2F","event":"http","functionId":"awslambda","method":"GET","path":"/","cors":{"origins":["*"],"methods":["HEAD","GET","POST"],"headers":["Origin","Accept","Content-Type"],"allowCredentials":false}}
  1. Attempt to update the subscription by changing the method to POST:
curl -X PUT \
  'http://localhost:4001/v1/spaces/default/subscriptions/http,GET,%2F' \
  -H 'Content-Type: application/json' \
  -d '{
	"functionId": "awslambda", 
	"event": "http",
	"method": "POST",
	"path": "/",
	"cors": {
		"origins": ["*"]
	}
}'

This should fail as it would change the subscription ID:

{"errors":[{"message":"Invalid update. 'Method' of existing subscription cannot be updated."}]}

Todos:

  • Write tests
  • Write documentation
  • Fix linting errors
  • Make sure code coverage hasn't dropped
  • Provide verification commands / resources
  • Enable "Allow edits from maintainers" for this PR
  • Update the messages below

Is this ready for review?: YES
Is it a breaking change?: NO

@alexdebrie alexdebrie requested a review from mthenw March 23, 2018 22:04
@codecov
Copy link

codecov bot commented Mar 24, 2018

Codecov Report

Merging #398 into master will increase coverage by 1.57%.
The diff coverage is 85.5%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #398      +/-   ##
==========================================
+ Coverage   61.14%   62.72%   +1.57%     
==========================================
  Files          26       26              
  Lines        1521     1588      +67     
==========================================
+ Hits          930      996      +66     
+ Misses        554      550       -4     
- Partials       37       42       +5
Impacted Files Coverage Δ
httpapi/httpapi.go 54.28% <100%> (+10.15%) ⬆️
libkv/subscription.go 87.62% <73.68%> (-0.35%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f673e72...a678883. Read the comment docs.

return nil, err
}

// If the subscriptionID changes, it should be a new subscription rather than an update.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it should be approached differently. validateSubscription should check if event, functionId, path and method are the same, if not it should return validation error. If those values are the same we can just update the key in etcd.

This approach causes that in some cases if we will change the subscription ID we will also need to change this logic. There is no need to do that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This approach causes that in some cases if we will change the subscription ID we will also need to change this logic

Wouldn't we need a migration of existing subscriptions anyway in that case? Also, if we do change the how the subscriptionID is calculated, wouldn't that be implemented in the generateSubscriptionID method?

Copy link
Contributor

Choose a reason for hiding this comment

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

It would, but we should not treat ID as something that contains meaningful ingo. It can be easily achieved by comparing current values with new values, it's just safer and more obvious. It's a bit more confusing the IDs are compared..

}

func (e ErrInvalidSubscriptionUpdate) Error() string {
return fmt.Sprintf("Invalid update. This update would change the SubscriptionID for %q.", e.ID)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if that's important to user. The more important would be "Path of existing subscription cannot be update." or something.

Copy link
Contributor

@mthenw mthenw left a comment

Choose a reason for hiding this comment

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

LGTM, two small requests though.

README.md Outdated
**Request**

* `event` - `string` - event name
* `functionId` - `string` - ID of function to receive events
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add some info that those fields cannot be updated?

@@ -65,6 +65,48 @@ func (service Service) CreateSubscription(s *subscription.Subscription) (*subscr
return s, nil
}

// UpdateSubscription updates subscription.
func (service Service) UpdateSubscription(id subscription.ID, s *subscription.Subscription) (*subscription.Subscription, error) {
err := service.validateSubscription(s)
Copy link
Contributor

Choose a reason for hiding this comment

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

I know that you didn't introduce that but can you remove validateSubscription method from the struct and just make it regular function like validateSubscriptionUpdate?


Status code:

* `200 Created` on success
Copy link
Contributor

Choose a reason for hiding this comment

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

404 if subscription not found

application/json:
schema:
$ref: "#/components/schemas/Subscription"
400:
Copy link
Contributor

Choose a reason for hiding this comment

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

404 missing

if _, ok := err.(*subscription.ErrInvalidSubscriptionUpdate); ok {
w.WriteHeader(http.StatusBadRequest)
} else if _, ok := err.(*subscription.ErrSubscriptionNotFound); ok {
w.WriteHeader(http.StatusBadRequest)
Copy link
Contributor

Choose a reason for hiding this comment

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

it should be 404


httpresp := &httpapi.Response{}
json.Unmarshal(resp.Body.Bytes(), httpresp)
assert.Equal(t, http.StatusBadRequest, resp.Code)
Copy link
Contributor

Choose a reason for hiding this comment

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

404

@mthenw mthenw merged commit acfe7bd into master Mar 29, 2018
@mthenw mthenw deleted the UpdateSubscription branch March 29, 2018 06:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants