Skip to content

Commit 521e015

Browse files
committed
package files changes to support infra creation
1 parent 8544fcc commit 521e015

File tree

17 files changed

+957
-33
lines changed

17 files changed

+957
-33
lines changed

pkg/cloud/services/authenticator/authenticator.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,31 @@ func GetAuthenticator() (core.Authenticator, error) {
4848
}
4949
return auth, nil
5050
}
51+
52+
// GetProperties returns a map containing configuration properties for the specified service that are retrieved from external configuration sources.
53+
func GetProperties() (map[string]string, error) {
54+
properties, err := core.GetServiceProperties(serviceIBMCloud)
55+
if err != nil {
56+
return nil, fmt.Errorf("error while fetching service properties")
57+
}
58+
return properties, nil
59+
}
60+
61+
// GetIAMAuthenticator will get the IAM authenticator for ibmcloud.
62+
func GetIAMAuthenticator() (*core.IamAuthenticator, error) {
63+
props, err := GetProperties()
64+
if err != nil {
65+
return nil, fmt.Errorf("error while fetching service properties: %w", err)
66+
}
67+
68+
apiKey := props["APIKEY"]
69+
if apiKey == "" {
70+
fmt.Printf("ibmcloud api key is not provided, set %s environmental variable", "IBMCLOUD_API_KEY")
71+
}
72+
73+
auth := &core.IamAuthenticator{
74+
ApiKey: apiKey,
75+
}
76+
77+
return auth, nil
78+
}

pkg/cloud/services/cos/cos.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
"github.com/IBM/ibm-cos-sdk-go/aws"
21+
"github.com/IBM/ibm-cos-sdk-go/aws/request"
22+
"github.com/IBM/ibm-cos-sdk-go/service/s3"
23+
)
24+
25+
// Cos interface defines a method that a IBMCLOUD service object should implement in order to
26+
// use the cos package for listing resource instances.
27+
type Cos interface {
28+
GetBucketByName(name string) (*s3.HeadBucketOutput, error)
29+
CreateBucket(input *s3.CreateBucketInput) (*s3.CreateBucketOutput, error)
30+
CreateBucketWithContext(ctx aws.Context, input *s3.CreateBucketInput, opts ...request.Option) (*s3.CreateBucketOutput, error)
31+
PutObject(*s3.PutObjectInput) (*s3.PutObjectOutput, error)
32+
GetObjectRequest(*s3.GetObjectInput) (*request.Request, *s3.GetObjectOutput)
33+
ListObjects(input *s3.ListObjectsInput) (*s3.ListObjectsOutput, error)
34+
DeleteObject(input *s3.DeleteObjectInput) (*s3.DeleteObjectOutput, error)
35+
PutPublicAccessBlock(input *s3.PutPublicAccessBlockInput) (*s3.PutPublicAccessBlockOutput, error)
36+
}

pkg/cloud/services/cos/doc.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 implements cos code.
18+
package cos

pkg/cloud/services/cos/service.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
}

pkg/cloud/services/powervs/mock/powervs_generated.go

Lines changed: 74 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cloud/services/powervs/powervs.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type PowerVS interface {
3030
GetAllInstance() (*models.PVMInstances, error)
3131
GetAllImage() (*models.Images, error)
3232
GetAllNetwork() (*models.Networks, error)
33+
GetNetworkByID(id string) (*models.Network, error)
3334
GetInstance(id string) (*models.PVMInstance, error)
3435
GetImage(id string) (*models.Image, error)
3536
DeleteImage(id string) error
@@ -39,4 +40,8 @@ type PowerVS interface {
3940
DeleteJob(id string) error
4041
GetAllDHCPServers() (models.DHCPServers, error)
4142
GetDHCPServer(id string) (*models.DHCPServerDetail, error)
43+
CreateDHCPServer(*models.DHCPServerCreate) (*models.DHCPServer, error)
44+
DeleteDHCPServer(id string) error
45+
WithClients(options ServiceOptions) *Service
46+
GetNetworkByName(networkName string) (*models.NetworkReference, error)
4247
}

0 commit comments

Comments
 (0)