|
| 1 | +/* |
| 2 | +Copyright 2022 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cos |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "net" |
| 22 | + "net/http" |
| 23 | + "net/url" |
| 24 | + "time" |
| 25 | + |
| 26 | + "golang.org/x/net/http/httpproxy" |
| 27 | + |
| 28 | + "github.com/IBM/ibm-cos-sdk-go/aws" |
| 29 | + "github.com/IBM/ibm-cos-sdk-go/aws/credentials/ibmiam" |
| 30 | + "github.com/IBM/ibm-cos-sdk-go/aws/request" |
| 31 | + cosSession "github.com/IBM/ibm-cos-sdk-go/aws/session" |
| 32 | + "github.com/IBM/ibm-cos-sdk-go/service/s3" |
| 33 | +) |
| 34 | + |
| 35 | +// iamEndpoint represent the IAM authorisation URL. |
| 36 | +const ( |
| 37 | + iamEndpoint = "https://iam.cloud.ibm.com/identity/token" |
| 38 | + cosURLDomain = "cloud-object-storage.appdomain.cloud" |
| 39 | +) |
| 40 | + |
| 41 | +// Service holds the IBM Cloud Resource Controller Service specific information. |
| 42 | +type Service struct { |
| 43 | + client *s3.S3 |
| 44 | +} |
| 45 | + |
| 46 | +// ServiceOptions holds the IBM Cloud Resource Controller Service Options specific information. |
| 47 | +type ServiceOptions struct { |
| 48 | + *cosSession.Options |
| 49 | +} |
| 50 | + |
| 51 | +// GetBucketByName returns a bucket with the given name. |
| 52 | +func (s *Service) GetBucketByName(name string) (*s3.HeadBucketOutput, error) { |
| 53 | + input := &s3.HeadBucketInput{ |
| 54 | + Bucket: &name, |
| 55 | + } |
| 56 | + return s.client.HeadBucket(input) |
| 57 | +} |
| 58 | + |
| 59 | +// CreateBucket creates a new bucket in the COS instance. |
| 60 | +func (s *Service) CreateBucket(input *s3.CreateBucketInput) (*s3.CreateBucketOutput, error) { |
| 61 | + return s.client.CreateBucket(input) |
| 62 | +} |
| 63 | + |
| 64 | +// CreateBucketWithContext creates a new bucket with an addition ability to pass context. |
| 65 | +func (s *Service) CreateBucketWithContext(ctx aws.Context, input *s3.CreateBucketInput, opts ...request.Option) (*s3.CreateBucketOutput, error) { |
| 66 | + return s.client.CreateBucketWithContext(ctx, input, opts...) |
| 67 | +} |
| 68 | + |
| 69 | +// PutObject adds an object to a bucket. |
| 70 | +func (s *Service) PutObject(input *s3.PutObjectInput) (*s3.PutObjectOutput, error) { |
| 71 | + return s.client.PutObject(input) |
| 72 | +} |
| 73 | + |
| 74 | +// GetObjectRequest generates a "aws/request.Request" representing the client's request for the GetObject operation. |
| 75 | +func (s *Service) GetObjectRequest(input *s3.GetObjectInput) (*request.Request, *s3.GetObjectOutput) { |
| 76 | + return s.client.GetObjectRequest(input) |
| 77 | +} |
| 78 | + |
| 79 | +// ListObjects returns the list of objects in a bucket. |
| 80 | +func (s *Service) ListObjects(input *s3.ListObjectsInput) (*s3.ListObjectsOutput, error) { |
| 81 | + return s.client.ListObjects(input) |
| 82 | +} |
| 83 | + |
| 84 | +// DeleteObject deletes a object in a bucket. |
| 85 | +func (s *Service) DeleteObject(input *s3.DeleteObjectInput) (*s3.DeleteObjectOutput, error) { |
| 86 | + return s.client.DeleteObject(input) |
| 87 | +} |
| 88 | + |
| 89 | +// PutPublicAccessBlock creates or modifies the PublicAccessBlock configuration for a bucket. |
| 90 | +func (s *Service) PutPublicAccessBlock(input *s3.PutPublicAccessBlockInput) (*s3.PutPublicAccessBlockOutput, error) { |
| 91 | + return s.client.PutPublicAccessBlock(input) |
| 92 | +} |
| 93 | + |
| 94 | +// NewService returns a new service for the IBM Cloud Resource Controller api client. |
| 95 | +// TODO(karthik-k-n): pass location as a part of options. |
| 96 | +func NewService(options ServiceOptions, location, apikey, serviceInstance string) (*Service, error) { |
| 97 | + if options.Options == nil { |
| 98 | + options.Options = &cosSession.Options{} |
| 99 | + } |
| 100 | + serviceEndpoint := fmt.Sprintf("s3.%s.%s", location, cosURLDomain) |
| 101 | + // TODO(karthik-k-n): handle URL |
| 102 | + options.Config = aws.Config{ |
| 103 | + Endpoint: &serviceEndpoint, |
| 104 | + Region: &location, |
| 105 | + HTTPClient: &http.Client{ |
| 106 | + Transport: &http.Transport{ |
| 107 | + Proxy: func(req *http.Request) (*url.URL, error) { |
| 108 | + return httpproxy.FromEnvironment().ProxyFunc()(req.URL) |
| 109 | + }, |
| 110 | + DialContext: (&net.Dialer{ |
| 111 | + Timeout: 30 * time.Second, |
| 112 | + KeepAlive: 30 * time.Second, |
| 113 | + DualStack: true, |
| 114 | + }).DialContext, |
| 115 | + ForceAttemptHTTP2: true, |
| 116 | + MaxIdleConns: 100, |
| 117 | + IdleConnTimeout: 90 * time.Second, |
| 118 | + TLSHandshakeTimeout: 10 * time.Second, |
| 119 | + ExpectContinueTimeout: 1 * time.Second, |
| 120 | + }, |
| 121 | + }, |
| 122 | + S3ForcePathStyle: aws.Bool(true), |
| 123 | + } |
| 124 | + |
| 125 | + options.Config.Credentials = ibmiam.NewStaticCredentials(aws.NewConfig(), iamEndpoint, apikey, serviceInstance) |
| 126 | + |
| 127 | + sess, err := cosSession.NewSessionWithOptions(*options.Options) |
| 128 | + if err != nil { |
| 129 | + return nil, err |
| 130 | + } |
| 131 | + return &Service{ |
| 132 | + client: s3.New(sess), |
| 133 | + }, nil |
| 134 | +} |
0 commit comments