Skip to content

Commit 6378f3c

Browse files
authored
Merge pull request #2 from sktelecom/implement-csp
implemented csp info CRUD
2 parents bd7c676 + bf968bc commit 6378f3c

File tree

7 files changed

+261
-5
lines changed

7 files changed

+261
-5
lines changed

cmd/application/handler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (*Server) GetAppID(ctx context.Context, req *pb.GetAppRequest) (*pb.IDsResp
2424
return res, nil
2525
}
2626

27-
func (*Server) GetAllApps(ctx context.Context, req *pb.IDRequest) (*pb.GetAppsResponse, error) {
27+
func (s *Server) GetAllApps(ctx context.Context, req *pb.IDRequest) (*pb.GetAppsResponse, error) {
2828
clusterId := req.GetId()
2929
log.Info("clusterId: ", clusterId)
3030

cmd/info/handler.go

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package info
2+
3+
import (
4+
"context"
5+
6+
"github.com/sktelecom/tks-contract/pkg/log"
7+
"github.com/sktelecom/tks-info/pkg/info/csp"
8+
pb "github.com/sktelecom/tks-proto/pbgo"
9+
"google.golang.org/protobuf/types/known/emptypb"
10+
)
11+
12+
var (
13+
cspAccessor *csp.Accessor
14+
)
15+
16+
type Server struct {
17+
pb.UnimplementedInfoServiceServer
18+
}
19+
20+
func init() {
21+
cspAccessor = csp.NewCSPAccessor()
22+
}
23+
24+
// CreateCSPInfo create new CSP Info for the contract id.
25+
func (s *Server) CreateCSPInfo(ctx context.Context, in *pb.CreateCSPInfoRequest) (*pb.IDResponse, error) {
26+
log.Debug("request CreateCSPInfo for contractID ", in.GetContractId())
27+
id, err := cspAccessor.Create(csp.ID(in.GetContractId()), in.GetAuth())
28+
if err != nil {
29+
return &pb.IDResponse{
30+
Code: pb.Code_INTERNAL,
31+
Error: &pb.Error{
32+
Msg: err.Error(),
33+
},
34+
}, err
35+
}
36+
return &pb.IDResponse{
37+
Code: pb.Code_OK_UNSPECIFIED,
38+
Error: nil,
39+
Id: string(id),
40+
}, nil
41+
}
42+
43+
// GetCSPIDs returns all CSP ids.
44+
func (s *Server) GetCSPIDs(ctx context.Context, empty *emptypb.Empty) (*pb.IDsResponse, error) {
45+
log.Debug("request GetCSPIDs")
46+
cspinfos := cspAccessor.List()
47+
ids := []string{}
48+
49+
for _, csp := range cspinfos {
50+
ids = append(ids, string(csp.ID))
51+
}
52+
53+
return &pb.IDsResponse{
54+
Code: pb.Code_OK_UNSPECIFIED,
55+
Error: nil,
56+
Ids: ids,
57+
}, nil
58+
}
59+
60+
// GetCSPIDsByContractID returns the CSP ids by the contract id.
61+
func (s *Server) GetCSPIDsByContractID(ctx context.Context, in *pb.IDRequest) (*pb.IDsResponse, error) {
62+
log.Debug("request GetCSPIDsByContractID for contract ID ", in.GetId())
63+
ids, err := cspAccessor.GetCSPIDsByContractID(csp.ID(in.GetId()))
64+
if err != nil {
65+
return &pb.IDsResponse{
66+
Code: pb.Code_NOT_FOUND,
67+
Error: &pb.Error{
68+
Msg: err.Error(),
69+
},
70+
}, err
71+
}
72+
73+
res := []string{}
74+
for _, id := range ids {
75+
res = append(res, string(id))
76+
}
77+
return &pb.IDsResponse{
78+
Code: pb.Code_OK_UNSPECIFIED,
79+
Error: nil,
80+
Ids: res,
81+
}, nil
82+
}
83+
84+
// UpdateCSPInfo updates an authentication config for CSP.
85+
func (s *Server) UpdateCSPInfo(ctx context.Context, in *pb.UpdateCSPInfoRequest) (*pb.SimpleResponse, error) {
86+
log.Debug("request UpdateCSPInfo for CSP ID ", in.GetCspId())
87+
if err := cspAccessor.Update(csp.ID(in.GetCspId()), in.GetAuth()); err != nil {
88+
return &pb.SimpleResponse{
89+
Code: pb.Code_INTERNAL,
90+
Error: &pb.Error{
91+
Msg: err.Error(),
92+
},
93+
}, err
94+
}
95+
return &pb.SimpleResponse{
96+
Code: pb.Code_OK_UNSPECIFIED,
97+
Error: nil,
98+
}, nil
99+
}
100+
101+
// GetCSPAuth returns an authentication info by csp id.
102+
func (s *Server) GetCSPAuth(ctx context.Context, in *pb.IDRequest) (*pb.GetCSPAuthResponse, error) {
103+
log.Debug("request GetCSPAuth for CSP ID ", in.GetId())
104+
csp, err := cspAccessor.Get(csp.ID(in.GetId()))
105+
if err != nil {
106+
return &pb.GetCSPAuthResponse{
107+
Code: pb.Code_NOT_FOUND,
108+
Error: &pb.Error{
109+
Msg: err.Error(),
110+
},
111+
}, err
112+
}
113+
return &pb.GetCSPAuthResponse{
114+
Code: pb.Code_OK_UNSPECIFIED,
115+
Error: nil,
116+
Auth: csp.Auth,
117+
}, nil
118+
}
119+
120+
// CreateCluster create cluster on the multicuster with csp id
121+
func (s *Server) CreateCluster(ctx context.Context, in *pb.CreateClusterRequest) (*pb.IDResponse, error) {
122+
return nil, nil
123+
}
124+
125+
// GetClusterget cluster for the id of the cluster
126+
func (s *Server) GetCluster(ctx context.Context, in *pb.GetClusterRequest) (*pb.GetClusterResponse, error) {
127+
return nil, nil
128+
}
129+
130+
// GetClusters get every clusters on the mutlcluster
131+
func (s *Server) GetClusters(ctx context.Context, in *pb.GetClustersRequest) (*pb.GetClustersResponse, error) {
132+
return nil, nil
133+
}
134+
135+
// UpdateClusterStatus update Status of the Cluster
136+
func (s *Server) UpdateClusterStatus(ctx context.Context, in *pb.UpdateClusterStatusRequest) (*pb.SimpleResponse, error) {
137+
return nil, nil
138+
}
139+
140+
// ValidateLabelUniqueness check uniqueness of the label
141+
func (s *Server) ValidateLabelUniqueness(ctx context.Context, in *pb.ValidateLabelUniquenessRequest) (*pb.ValidateLabelUniquenessResponse, error) {
142+
return nil, nil
143+
}

cmd/server.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/sktelecom/tks-contract/pkg/log"
1212
app "github.com/sktelecom/tks-info/cmd/application"
13+
info "github.com/sktelecom/tks-info/cmd/info"
1314
"github.com/sktelecom/tks-info/pkg/cert"
1415
pb "github.com/sktelecom/tks-proto/pbgo"
1516
// grpclog "github.com/openinfradev/tks-info/pkg/log"
@@ -52,7 +53,7 @@ func main() {
5253

5354
s := grpc.NewServer(opts...)
5455
pb.RegisterAppInfoServiceServer(s, &app.Server{})
55-
//pb.RegisterInfoServiceServer(s, &cluster.Server{})
56+
pb.RegisterInfoServiceServer(s, &info.Server{})
5657

5758
if err := s.Serve(lis); err != nil {
5859
log.Fatal("failed to serve: ", err)

go.mod

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ module github.com/sktelecom/tks-info
33
go 1.16
44

55
require (
6+
github.com/google/uuid v1.2.0
67
github.com/sktelecom/tks-contract v0.1.0
7-
github.com/sktelecom/tks-proto v0.0.4
8+
github.com/sktelecom/tks-proto v0.0.4-0.20210416044312-03b6d2655a9d
89
google.golang.org/grpc v1.37.0
10+
google.golang.org/protobuf v1.26.0
911
)
12+
13+
replace github.com/sktelecom/tks-info => ./

go.sum

+7-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
3333
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
3434
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
3535
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
36+
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
3637
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
3738
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3839
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@@ -42,8 +43,12 @@ github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic
4243
github.com/sktelecom/tks-contract v0.1.0 h1:hBbCJ48ZMp7aM1CCvDYtWbK/xBY2YYwlXKlUb/n2Eb4=
4344
github.com/sktelecom/tks-contract v0.1.0/go.mod h1:i2uuH5rf1dW3wyIbdTS1E16sztpr6mU5xnkvDS7fhYQ=
4445
github.com/sktelecom/tks-proto v0.0.2/go.mod h1:5r0c5Sq4RhX5IuVIyD/aRunO7WUHmpymBOBz9LTCaRY=
45-
github.com/sktelecom/tks-proto v0.0.4 h1:yHaT829Bp2kQxxpVy5knwkxPT0KPeKoLBkYsxWos/Sk=
46-
github.com/sktelecom/tks-proto v0.0.4/go.mod h1:5r0c5Sq4RhX5IuVIyD/aRunO7WUHmpymBOBz9LTCaRY=
46+
github.com/sktelecom/tks-proto v0.0.4-0.20210415020726-a2f0aa64ce68 h1:Ml6UdifDet7bNzVY5LI3zJBpgMCf0ZYPm7ruujSLEWE=
47+
github.com/sktelecom/tks-proto v0.0.4-0.20210415020726-a2f0aa64ce68/go.mod h1:5r0c5Sq4RhX5IuVIyD/aRunO7WUHmpymBOBz9LTCaRY=
48+
github.com/sktelecom/tks-proto v0.0.4-0.20210416024508-5b272aaa0e9c h1:U/2lS9HTjvbbQmHYhlPxdAcweLoia9rm9MCjdpNa17g=
49+
github.com/sktelecom/tks-proto v0.0.4-0.20210416024508-5b272aaa0e9c/go.mod h1:5r0c5Sq4RhX5IuVIyD/aRunO7WUHmpymBOBz9LTCaRY=
50+
github.com/sktelecom/tks-proto v0.0.4-0.20210416044312-03b6d2655a9d h1:bNG5d3t/JjUt7EKIGWC7si2l/fCGjO4yR3NJsVSw+ag=
51+
github.com/sktelecom/tks-proto v0.0.4-0.20210416044312-03b6d2655a9d/go.mod h1:5r0c5Sq4RhX5IuVIyD/aRunO7WUHmpymBOBz9LTCaRY=
4752
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
4853
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
4954
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=

pkg/info/csp/accessor.go

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package csp
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/google/uuid"
8+
"github.com/sktelecom/tks-contract/pkg/log"
9+
)
10+
11+
// Accessor accesses to csp info in-memory data.
12+
type Accessor struct {
13+
cspinfos map[ID]CSPInfo
14+
}
15+
16+
// NewCSPAccessor returns new Accessor to access csp info.
17+
func NewCSPAccessor() *Accessor {
18+
return &Accessor{
19+
cspinfos: map[ID]CSPInfo{},
20+
}
21+
}
22+
23+
// Get returns a CSP Info if it exists.
24+
func (c Accessor) Get(id ID) (CSPInfo, error) {
25+
csp, exists := c.cspinfos[id]
26+
if !exists {
27+
return CSPInfo{}, fmt.Errorf("CSP ID %s does not exist.", id)
28+
}
29+
return csp, nil
30+
}
31+
32+
// GetCSPIDsByContractID returns a list of CSP ID by contract ID if it exists.
33+
func (c Accessor) GetCSPIDsByContractID(id ID) ([]ID, error) {
34+
res := []ID{}
35+
for _, csp := range c.cspinfos {
36+
if csp.ContractID == id {
37+
log.Info("same")
38+
res = append(res, csp.ID)
39+
}
40+
}
41+
if len(res) == 0 {
42+
return res, fmt.Errorf("CSP for contract id %s does not exist.", id)
43+
}
44+
return res, nil
45+
}
46+
47+
// List returns a list of CSP Infos in array.
48+
func (c Accessor) List() []CSPInfo {
49+
res := []CSPInfo{}
50+
51+
for _, t := range c.cspinfos {
52+
res = append(res, t)
53+
}
54+
return res
55+
}
56+
57+
// Create creates new CSP info with contractID and auth.
58+
func (c *Accessor) Create(contractID ID, auth string) (ID, error) {
59+
newCSPID := ID(uuid.New().String())
60+
if _, exists := c.cspinfos[newCSPID]; exists {
61+
return "", fmt.Errorf("csp id %s does already exist.", newCSPID)
62+
}
63+
c.cspinfos[newCSPID] = CSPInfo{
64+
ID: newCSPID,
65+
ContractID: contractID,
66+
Auth: auth,
67+
CreatedTs: time.Now(),
68+
LastUpdatedTs: time.Now(),
69+
}
70+
return newCSPID, nil
71+
}
72+
73+
// Update updates an authentication info for CSP.
74+
func (c *Accessor) Update(id ID, auth string) error {
75+
if _, exists := c.cspinfos[id]; !exists {
76+
return fmt.Errorf("CSP ID %s does not exist.", id)
77+
}
78+
c.cspinfos[id] = CSPInfo{
79+
ID: c.cspinfos[id].ID,
80+
ContractID: c.cspinfos[id].ContractID,
81+
Auth: auth,
82+
CreatedTs: c.cspinfos[id].CreatedTs,
83+
LastUpdatedTs: time.Now(),
84+
}
85+
return nil
86+
}

pkg/info/csp/csp.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package csp
2+
3+
import (
4+
"time"
5+
)
6+
7+
// CSPInfo represents an IaaS information used to deploy K8S clusters.
8+
type CSPInfo struct {
9+
ID ID `json:"id"`
10+
ContractID ID `json:"contract_id"`
11+
Auth string `json:"auth"`
12+
CreatedTs time.Time `json:"created_ts"`
13+
LastUpdatedTs time.Time `json:"last_updated_ts"`
14+
}
15+
16+
// ID is a global unique ID.
17+
type ID string

0 commit comments

Comments
 (0)