From 316f4dbb4033cf3278880bd0661fbe8533621a40 Mon Sep 17 00:00:00 2001 From: enyinna1234 Date: Mon, 19 Aug 2019 16:47:30 -0700 Subject: [PATCH] Implementing image update inside patchHandler This updates the README, deletes file from file system after DELETE, and does some refactoring. Signed-off-by: Enyinna Ochulor Change-Id: Ic9bacb798ba7cdc567229d6e3191688b9f301ca5 --- cmd/bpa-restapi-agent/README.md | 181 ++++++++++++++++++++++++++++ cmd/bpa-restapi-agent/api/imagehandler.go | 82 ++----------- cmd/bpa-restapi-agent/internal/app/image.go | 69 +++++++++-- cmd/bpa-restapi-agent/sample.json | 6 +- 4 files changed, 254 insertions(+), 84 deletions(-) diff --git a/cmd/bpa-restapi-agent/README.md b/cmd/bpa-restapi-agent/README.md index a74434c..70555cb 100644 --- a/cmd/bpa-restapi-agent/README.md +++ b/cmd/bpa-restapi-agent/README.md @@ -4,3 +4,184 @@ To run the server, follow these simple steps: ``` go run main.go ``` +Integrated Cloud Native (ICN) RESTful API + +This is a Golang application providing a RESTful API to interact with and upload image objects. + +The API application source files are in the icn/cmd/bpa-restapi-agent directory. + +While the database back-end is extensible, this initial release requires mongodb. + +Install + +Install and start mongodb. For instructions: https://docs.mongodb.com/manual/installation/ + +git clone "https://gerrit.akraino.org/r/icn" +cd icn/cmd/bpa-restapi-agent + +Run the application +go run main.go + +Output without a config file: + +2019/08/22 14:08:41 Error loading config file. Using defaults +2019/08/22 14:08:41 Starting Integrated Cloud Native API + +RESTful API usage examples + +Sample Post Request + +curl -i -F "metadata=//jsonfile -X POST http://NODE_IP:9015//baremetalcluster/{owner}/{clustername}/ + +#image type can be binary_image, container_image, or os_image + +Example requests and responses: + +Create image - POST + +#Using a json file called sample.json +#image_length in sample.json can be determined with the command +ls -al + +Request + +curl -i -F "metadata=/{imgname} + + +example: +#continuing with our container image from above + +Request + +curl -i -X GET http://localhost:9015/v1/baremetalcluster/alpha/beta/container_images/asdf246 + +Response + +HTTP/1.1 200 OK +Content-Type: application/json +Date: Thu, 22 Aug 2019 22:57:10 GMT +Content-Length: 239 + +{"owner":"alpha","cluster_name":"beta","type":"container","image_name":"asdf246","image_offset":0,"image_length":29718177,"upload_complete":false,"description":{"image_records":[{"image_record_name":"iuysdi1234","repo":"icn","tag":"1"}]}} + +Upload container image - PATCH +Request + +curl --request PATCH --data-binary "@/home/enyi/workspace/icn/cmd/bpa-restapi-agent/sample_image" http://localhost:9015/v1/baremetalcluster/alpha/beta/container_images/asdf246 --header "Upload-Offset: 0" --header "Expect:" -i + + +Response + +HTTP/1.1 204 No Content +Upload-Offset: 29718177 +Date: Thu, 22 Aug 2019 23:19:44 GMT + +Check uploaded image - GET + +Request + +curl -i -X GET http://localhost:9015/v1/baremetalcluster/alpha/beta/container_images/asdf246 + +Response + +HTTP/1.1 200 OK +Content-Type: application/json +Date: Fri, 23 Aug 2019 17:12:07 GMT +Content-Length: 245 + +{"owner":"alpha","cluster_name":"beta","type":"container","image_name":"asdf246","image_offset":29718177,"image_length":29718177,"upload_complete":true,"description":{"image_records":[{"image_record_name":"iuysdi1234","repo":"icn","tag":"1"}]}} + +#after the upload, the image_offset is now the same as image_length and upload_complete changed to true +#if upload was incomplete + +Resumable upload instructions + +Resumable upload -PATCH + +#this is the current resumable upload mechanism + +Request + +curl --request PATCH --data-binary "@/home/enyi/workspace/icn/cmd/bpa-restapi-agent/sample_image" http://localhost:9015/v1/baremetalcluster/alpha/beta/container_images/asdf246 --header "Upload-Offset: 0" --header "Expect:" -i --limit-rate 200K + +#the above request limits transfer for testing purposes +#'ctl c' out after a few seconds, to stop file transfer +#check image_offset with a GET + +Check upload - GET + +Request + +curl -i -X GET http://localhost:9015/v1/baremetalcluster/alpha/beta/container_images/asdf246 + +Response + +HTTP/1.1 200 OK +Content-Type: application/json +Date: Sat, 24 Aug 2019 00:30:00 GMT +Content-Length: 245 + +{"owner":"alpha","cluster_name":"beta","type":"container","image_name":"asdf246","image_offset":4079616,"image_length":29718177,"upload_complete":false,"description":{"image_records":[{"image_record_name":"iuysdi1234","repo":"icn","tag":"2"}]}} + +#from our response you can see that image_offset is still less than image_length and #upload_complete is still false +#next we use the dd command (no limiting this time) + +Request + +dd if=/home/enyi/workspace/icn/cmd/bpa-restapi-agent/sample_image skip=4079616 bs=1 | curl --request PATCH --data-binary @- http://localhost:9015/v1/baremetalcluster/alpha/beta/container_images/asdf246 --header "Upload-Offset: 4079616" --header "Expect:" -i + +#the request skips already uploaded 4079616 bytes of data + +Response + +25638561+0 records in +25638561+0 records out +25638561 bytes (26 MB, 24 MiB) copied, 207.954 s, 123 kB/s +HTTP/1.1 204 No Content +Upload-Offset: 29718177 +Date: Sat, 24 Aug 2019 00:43:18 GMT + +Update image description - PUT + +# let's change the tag in description from 1 to latest +# once the change is made in sample.json (or your json file) + +Request + +curl -i -F "metadata=