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

optimize: add context to huawei/obs #1820

Merged
merged 18 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 15 additions & 15 deletions bindings/huawei/obs/obs.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (o *HuaweiOBS) Operations() []bindings.OperationKind {
}
}

func (o *HuaweiOBS) create(req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
func (o *HuaweiOBS) create(ctx context.Context, req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
d, err := strconv.Unquote(string(req.Data))
if err == nil {
req.Data = []byte(d)
Expand All @@ -151,7 +151,7 @@ func (o *HuaweiOBS) create(req *bindings.InvokeRequest) (*bindings.InvokeRespons
input.Bucket = o.metadata.Bucket
input.Body = r

out, err := o.service.PutObject(input)
out, err := o.service.PutObject(ctx, input)
if err != nil {
return nil, fmt.Errorf("obs binding error. putobject: %w", err)
}
Expand All @@ -169,7 +169,7 @@ func (o *HuaweiOBS) create(req *bindings.InvokeRequest) (*bindings.InvokeRespons
}, nil
}

func (o *HuaweiOBS) upload(req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
func (o *HuaweiOBS) upload(ctx context.Context, req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
var payload uploadPayload
err := json.Unmarshal(req.Data, &payload)
if err != nil {
Expand All @@ -189,7 +189,7 @@ func (o *HuaweiOBS) upload(req *bindings.InvokeRequest) (*bindings.InvokeRespons
input.Bucket = o.metadata.Bucket
input.SourceFile = payload.SourceFile

out, err := o.service.PutFile(input)
out, err := o.service.PutFile(ctx, input)
if err != nil {
return nil, fmt.Errorf("obs binding error. putfile: %w", err)
}
Expand All @@ -207,7 +207,7 @@ func (o *HuaweiOBS) upload(req *bindings.InvokeRequest) (*bindings.InvokeRespons
}, nil
}

func (o *HuaweiOBS) get(req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
func (o *HuaweiOBS) get(ctx context.Context, req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
var key string
if val, ok := req.Metadata[metadataKey]; ok && val != "" {
key = val
Expand All @@ -219,7 +219,7 @@ func (o *HuaweiOBS) get(req *bindings.InvokeRequest) (*bindings.InvokeResponse,
input.Bucket = o.metadata.Bucket
input.Key = key

out, err := o.service.GetObject(input)
out, err := o.service.GetObject(ctx, input)
if err != nil {
return nil, fmt.Errorf("obs binding error. error getting obs object: %w", err)
}
Expand All @@ -243,7 +243,7 @@ func (o *HuaweiOBS) get(req *bindings.InvokeRequest) (*bindings.InvokeResponse,
}, nil
}

func (o *HuaweiOBS) delete(req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
func (o *HuaweiOBS) delete(ctx context.Context, req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
var key string
if val, ok := req.Metadata[metadataKey]; ok && val != "" {
key = val
Expand All @@ -255,7 +255,7 @@ func (o *HuaweiOBS) delete(req *bindings.InvokeRequest) (*bindings.InvokeRespons
input.Bucket = o.metadata.Bucket
input.Key = key

out, err := o.service.DeleteObject(input)
out, err := o.service.DeleteObject(ctx, input)
if err != nil {
return nil, fmt.Errorf("obs binding error. error deleting obs object: %w", err)
}
Expand All @@ -273,7 +273,7 @@ func (o *HuaweiOBS) delete(req *bindings.InvokeRequest) (*bindings.InvokeRespons
}, nil
}

func (o *HuaweiOBS) list(req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
func (o *HuaweiOBS) list(ctx context.Context, req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
var payload listPayload
err := json.Unmarshal(req.Data, &payload)
if err != nil {
Expand All @@ -292,7 +292,7 @@ func (o *HuaweiOBS) list(req *bindings.InvokeRequest) (*bindings.InvokeResponse,
input.Prefix = payload.Prefix
input.Delimiter = payload.Delimiter

out, err := o.service.ListObjects(input)
out, err := o.service.ListObjects(ctx, input)
if err != nil {
return nil, fmt.Errorf("obs binding error. error listing obs objects: %w", err)
}
Expand All @@ -310,15 +310,15 @@ func (o *HuaweiOBS) list(req *bindings.InvokeRequest) (*bindings.InvokeResponse,
func (o *HuaweiOBS) Invoke(ctx context.Context, req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
switch req.Operation {
case bindings.CreateOperation:
return o.create(req)
return o.create(ctx, req)
case UploadOperation:
return o.upload(req)
return o.upload(ctx, req)
case bindings.GetOperation:
return o.get(req)
return o.get(ctx, req)
case bindings.DeleteOperation:
return o.delete(req)
return o.delete(ctx, req)
case bindings.ListOperation:
return o.list(req)
return o.list(ctx, req)
default:
return nil, fmt.Errorf("obs binding error. unsupported operation %s", req.Operation)
}
Expand Down
26 changes: 15 additions & 11 deletions bindings/huawei/obs/obs_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@ limitations under the License.

package obs

import "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
import (
"context"

"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
)

// HuaweiOBSAPI holds only the necessary API functions from the OBS SDK
// The interface can also provide a way to implement stubs for the purpose of unit testing.
type HuaweiOBSAPI interface {
PutObject(input *obs.PutObjectInput) (output *obs.PutObjectOutput, err error)
PutFile(input *obs.PutFileInput) (output *obs.PutObjectOutput, err error)
GetObject(input *obs.GetObjectInput) (output *obs.GetObjectOutput, err error)
DeleteObject(input *obs.DeleteObjectInput) (output *obs.DeleteObjectOutput, err error)
ListObjects(input *obs.ListObjectsInput) (output *obs.ListObjectsOutput, err error)
PutObject(ctx context.Context, input *obs.PutObjectInput) (output *obs.PutObjectOutput, err error)
PutFile(ctx context.Context, input *obs.PutFileInput) (output *obs.PutObjectOutput, err error)
GetObject(ctx context.Context, input *obs.GetObjectInput) (output *obs.GetObjectOutput, err error)
DeleteObject(ctx context.Context, input *obs.DeleteObjectInput) (output *obs.DeleteObjectOutput, err error)
ListObjects(ctx context.Context, input *obs.ListObjectsInput) (output *obs.ListObjectsOutput, err error)
}

// HuaweiOBSService is a service layer which wraps the actual OBS SDK client to provide the API functions
Expand All @@ -31,22 +35,22 @@ type HuaweiOBSService struct {
client *obs.ObsClient
}

func (s *HuaweiOBSService) PutObject(input *obs.PutObjectInput) (output *obs.PutObjectOutput, err error) {
func (s *HuaweiOBSService) PutObject(ctx context.Context, input *obs.PutObjectInput) (output *obs.PutObjectOutput, err error) {
return s.client.PutObject(input)
1046102779 marked this conversation as resolved.
Show resolved Hide resolved
}

func (s *HuaweiOBSService) PutFile(input *obs.PutFileInput) (output *obs.PutObjectOutput, err error) {
func (s *HuaweiOBSService) PutFile(ctx context.Context, input *obs.PutFileInput) (output *obs.PutObjectOutput, err error) {
return s.client.PutFile(input)
}

func (s *HuaweiOBSService) GetObject(input *obs.GetObjectInput) (output *obs.GetObjectOutput, err error) {
func (s *HuaweiOBSService) GetObject(ctx context.Context, input *obs.GetObjectInput) (output *obs.GetObjectOutput, err error) {
return s.client.GetObject(input)
}

func (s *HuaweiOBSService) DeleteObject(input *obs.DeleteObjectInput) (output *obs.DeleteObjectOutput, err error) {
func (s *HuaweiOBSService) DeleteObject(ctx context.Context, input *obs.DeleteObjectInput) (output *obs.DeleteObjectOutput, err error) {
return s.client.DeleteObject(input)
}

func (s *HuaweiOBSService) ListObjects(input *obs.ListObjectsInput) (output *obs.ListObjectsOutput, err error) {
func (s *HuaweiOBSService) ListObjects(ctx context.Context, input *obs.ListObjectsInput) (output *obs.ListObjectsOutput, err error) {
return s.client.ListObjects(input)
}
Loading