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

feature. add I/F GetDefaultContract() #21

Merged
merged 1 commit into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 23 additions & 0 deletions cmd/server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/golang/protobuf/ptypes/empty"
"github.com/google/uuid"

"github.com/openinfradev/tks-common/pkg/log"
Expand Down Expand Up @@ -173,6 +174,28 @@ func (s *server) GetContract(ctx context.Context, in *pb.GetContractRequest) (*p
return &res, nil
}

// GetDefaultContract implements pbgo.ContractService.GetDefaultContract gRPC
func (s *server) GetDefaultContract(ctx context.Context, in *empty.Empty) (*pb.GetContractResponse, error) {
log.Info("Request 'GetDefaultContract' ")

contract, err := contractAccessor.GetDefaultContract()
if err != nil {
res := pb.GetContractResponse{
Code: pb.Code_NOT_FOUND,
Error: &pb.Error{
Msg: err.Error(),
},
}
return &res, err
}
res := pb.GetContractResponse{
Code: pb.Code_OK_UNSPECIFIED,
Error: nil,
Contract: contract,
}
return &res, nil
}

// GetQuota implements pbgo.ContractService.GetContract gRPC
func (s *server) GetQuota(ctx context.Context, in *pb.GetQuotaRequest) (*pb.GetQuotaResponse, error) {
log.Info("Request 'GetQuota' for contract id ", in.GetContractId())
Expand Down
59 changes: 59 additions & 0 deletions cmd/server/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/golang/mock/gomock"
"github.com/golang/protobuf/ptypes/empty"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"gorm.io/driver/postgres"
Expand Down Expand Up @@ -417,6 +418,64 @@ func TestGetContract(t *testing.T) {

}

func TestGetDefaultContract(t *testing.T) {
testCases := []struct {
name string
in *empty.Empty
buildStubs func()
checkResponse func(req *empty.Empty, res *pb.GetContractResponse, err error)
}{
{
name: "NOT_FOUND",
in: &empty.Empty{},
buildStubs: func() {
_, _ = contractAccessor.Create("NO_DEFAULT_NAME", []string{}, &pb.ContractQuota{})
},
checkResponse: func(req *empty.Empty, res *pb.GetContractResponse, err error) {
require.Error(t, err)
require.Equal(t, res.Code, pb.Code_NOT_FOUND)
},
},
{
name: "OK",
in: &empty.Empty{},
buildStubs: func() {
_, _ = contractAccessor.Create("default", []string{}, &pb.ContractQuota{})
},
checkResponse: func(req *empty.Empty, res *pb.GetContractResponse, err error) {
require.NoError(t, err)
require.Equal(t, res.Code, pb.Code_OK_UNSPECIFIED)

contract := res.GetContract()
require.NotNil(t, contract)
require.Equal(t, contract.GetContractorName(), "default")
},
},
}

for i := range testCases {
tc := testCases[i]

t.Run(tc.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

ctrl := gomock.NewController(t)
defer ctrl.Finish()

contractAccessor, err = getAccessor()

tc.buildStubs()

s := server{}
res, err := s.GetDefaultContract(ctx, tc.in)

tc.checkResponse(tc.in, res, err)
})
}

}

func TestGetQuota(t *testing.T) {
testCases := []struct {
name string
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ go 1.16

require (
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.2
github.com/google/uuid v1.3.0
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/lib/pq v1.10.4
github.com/openinfradev/tks-common v0.0.0-20220321044608-105302d33457 // indirect
github.com/openinfradev/tks-proto v0.0.6-0.20211015003551-ed8f9541f40d
github.com/openinfradev/tks-common v0.0.0-20220321044608-105302d33457
github.com/openinfradev/tks-proto v0.0.6-0.20220324075944-e471af2c8c49
github.com/stretchr/testify v1.7.0
google.golang.org/genproto v0.0.0-20211013025323-ce878158c4d4 // indirect
google.golang.org/protobuf v1.27.1
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -860,8 +860,9 @@ github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417/go.m
github.com/opencontainers/selinux v1.10.0/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI=
github.com/openinfradev/tks-common v0.0.0-20220321044608-105302d33457 h1:yKhmkHl2qi2/pyNBST8zI8fpcjjcpTFzACBwtk2fjik=
github.com/openinfradev/tks-common v0.0.0-20220321044608-105302d33457/go.mod h1:3d+gW0PPeBzEUtVRoupTTXAFlfdJLyHy2Lzlw4rcnOk=
github.com/openinfradev/tks-proto v0.0.6-0.20211015003551-ed8f9541f40d h1:ypM1LN+7tjRGzmTBNVegY/25KhJLFuQor2/+DfuhxDM=
github.com/openinfradev/tks-proto v0.0.6-0.20211015003551-ed8f9541f40d/go.mod h1:ul6kvgOXhNQvXEUmb92Wh5BmsuzknnTCb3wqmVNd/iI=
github.com/openinfradev/tks-proto v0.0.6-0.20220324075944-e471af2c8c49 h1:XYNl9fXGUH02/Laqht+ba2FmgOXrl7sUDHcAZHsSaUE=
github.com/openinfradev/tks-proto v0.0.6-0.20220324075944-e471af2c8c49/go.mod h1:3DrATRNCUJMW0oMLHT6D1teN4hX0vMGH4qOGnadakmo=
github.com/opentracing-contrib/go-observer v0.0.0-20170622124052-a52f23424492/go.mod h1:Ngi6UdF0k5OKD5t5wlmGhe/EDKPoUM3BXZSSfIuJbis=
github.com/opentracing/basictracer-go v1.0.0/go.mod h1:QfBfYuafItcjQuMwinw9GhYKwFXS9KnPs5lxoYwgW74=
github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
Expand Down
15 changes: 15 additions & 0 deletions pkg/contract/accessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ func (x *Accessor) GetContract(id uuid.UUID) (*pb.Contract, error) {
return &resContract, nil
}

// GetDefaultContract returns a contract from database.
func (x *Accessor) GetDefaultContract() (*pb.Contract, error) {
var contract model.Contract
res := x.db.First(&contract, "contractor_name = 'default'")
if res.RowsAffected == 0 || res.Error != nil {
return &pb.Contract{}, fmt.Errorf("Not found default contract")
}
quota, err := x.GetResourceQuota(contract.ID)
if err != nil {
return &pb.Contract{}, err
}
resContract := reflectToPbContract(contract, &quota)
return &resContract, nil
}

// getContract returns a resource quota from database.
func (x *Accessor) GetResourceQuota(contractID uuid.UUID) (pb.ContractQuota, error) {
var quota model.ResourceQuota
Expand Down
22 changes: 15 additions & 7 deletions pkg/contract/accessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package contract_test

import (
"fmt"
"math/rand"
"os"
"testing"
"time"

"github.com/google/uuid"
"gorm.io/driver/postgres"
Expand Down Expand Up @@ -83,7 +81,7 @@ func TestCreateContract(t *testing.T) {
Block: 12800000,
Fs: 12800000,
}
contractName := getRandomString("gotest")
contractName := "default"
contractId, err = accessor.Create(contractName, []string{"lma"}, &quota)
if err != nil {
t.Errorf("an error was unexpected while creating new contract: %s", err)
Expand Down Expand Up @@ -132,8 +130,18 @@ func TestGetContract(t *testing.T) {
t.Logf("quota cpu: %d", contract.Quota.Cpu)
}

func getRandomString(prefix string) string {
s := rand.NewSource(time.Now().UnixNano())
r := rand.New(s)
return fmt.Sprintf("%s-%d", prefix, r.Int31n(1000000000))
func TestGetDefaultContract(t *testing.T) {
accessor, err := getAccessor()
if err != nil {
t.Errorf("an error was unexpected while initilizing database %s", err)
}
contract, err := accessor.GetDefaultContract()

if err != nil {
t.Errorf("an error was unexpected while querying contract data %s", err)
}

t.Logf("contractor name: %s", contract.ContractorName)
t.Logf("quota cpu: %d", contract.Quota.Cpu)
}