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

Merge conflict & CRUD completion #15

Merged
merged 1 commit into from
Dec 31, 2023
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
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ require (
github.com/NaverCloudPlatform/ncloud-sdk-go-v2 v1.6.8
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1
github.com/stretchr/testify v1.8.4
)

require (
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/goldmark v1.6.0 h1:boZcn2GTjpsynOsC0iJHnBWa4Bi0qzfJjthwauItG68=
github.com/yuin/goldmark v1.6.0/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
Expand Down
7 changes: 6 additions & 1 deletion pkg/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ var (
STOP_SERVER_INSTANCE_PATH = "stopServerInstances"
DELETE_SERVER_INSTANCE_PATH = "terminateServerInstances"
GET_NETWORKINTERFACE_LIST_PATH = "getNetworkInterfaceList"
GET_SUBNET_LIST_PATH = "getSubnetList"
GET_ACG_LIST_PATH = "getAccessControlGroupList"

GET_SERVER_IMAGE_PRODUCT_LIST_PATH = "getServerImageProductList"

VPC_API_URL = "https://ncloud.apigw.ntruss.com/vpc/v2/"
GET_VPC_LIST = "getVpcList"
GET_SUBNET_LIST_PATH = "getSubnetList"
)
6 changes: 4 additions & 2 deletions pkg/ncp.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ const (
)

type NcpService struct {
token string
// Server ServerInterface
token string
Server ServerInterface
Network NetworkInterface
Subnet SubnetInterface
AccessControlGroup AccessControlGroupInterface
Vpc VpcInterface
ServerImageProduct ProductInterface
}

func NewNcpService(token string) *NcpService {
Expand Down
139 changes: 134 additions & 5 deletions pkg/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pkg

import (
"errors"
"fmt"
"io"
"log"
Expand All @@ -19,9 +20,19 @@
return &ServerService{accessKey: accessKey, secretKey: secretKey}
}

func (server *ServerService) CallApi(url string, request types.RequestInterface) (interface{}, error) {
type ServerInterface interface {
Get(url string) error
List(url string) error
Create(url string, request *types.CreateServerRequest, params []int) (*types.CreateServerResponse, error)
Update(url string) error
Delete(url string) error
}

func (server *ServerService) Create(url string, request *types.CreateServerRequest, params []int) (*types.CreateServerResponse, error) {
// Set url with query parameters
requestParams := request.RequestString()
networkInterfaceIndex := params[0]
acgNoList := params[1]
requestParams := types.CreateRequestString(request, networkInterfaceIndex, acgNoList)

// Create an HTTP request
req, err := http.NewRequest(http.MethodGet, url+requestParams, nil)
Expand All @@ -39,23 +50,141 @@

// Check the response status
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected response status: %s", resp.Status)
// Read the response body and show the body message in error.
responseByteData, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("%s", responseByteData)
}

responseByteData, err := io.ReadAll(resp.Body)
println(string(responseByteData))
if err != nil {
log.Fatal(err)
return nil, err
}

// fmt.Println("request:", requestParams)
// fmt.Println(string(responseByteData))
var csr *types.CreateServerResponse
responseInterface, err := types.MapResponse(responseByteData, &csr)

responseStruct, err := request.MapResponse(responseByteData)
if err != nil {
log.Fatal(err)
return nil, err
}

return responseStruct, err
// interface{} 타입으로 변환된 responseInterface를 다시 CreateServerResponse 타입으로 변환
responseStruct := responseInterface.(**types.CreateServerResponse)

return *responseStruct, err
}

func (server *ServerService) Get(url string) error {
return nil
}

func (server *ServerService) List(url string) error {
if len(url) == 0 {
return errors.New("please input url")
}
return nil
}

func (server *ServerService) Delete(url string) error {
// url 정의
url += ""

// httpRequest 생성
request, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return fmt.Errorf("Error creating request: ", err)
}
/// NCP 헤더 설정
SetNCPHeader(request, "", "")

// httpRequest token 설정
SetAuthToken(request, server.token)

Check failure on line 108 in pkg/server.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21)

server.token undefined (type *ServerService has no field or method token)

Check failure on line 108 in pkg/server.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.21)

server.token undefined (type *ServerService has no field or method token)

// HTTP 클라이언트 생성
client := &http.Client{}

// 요청 보내기
response, err := client.Do(request)
if err != nil {
return fmt.Errorf("Error sending request:", err)
}
defer response.Body.Close()

// 결과 반환
if response.StatusCode != http.StatusOK {
return fmt.Errorf("Unexpected response status code:", err)
}

return nil
}

func (server *ServerService) Stop(url string) error {
// url 정의
url += ""

// httpRequest 생성
request, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return fmt.Errorf("Error creating request: ", err)
}
/// NCP 헤더 설정
SetNCPHeader(request, "", "")

// httpRequest token 설정
SetAuthToken(request, server.token)

Check failure on line 141 in pkg/server.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21)

server.token undefined (type *ServerService has no field or method token)

Check failure on line 141 in pkg/server.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.21)

server.token undefined (type *ServerService has no field or method token)

// HTTP 클라이언트 생성
client := &http.Client{}

// 요청 보내기
response, err := client.Do(request)
if err != nil {
return fmt.Errorf("Error sending request:", err)
}
defer response.Body.Close()

// 결과 반환
if response.StatusCode != http.StatusOK {
return fmt.Errorf("Unexpected response status code:", err)
}

return nil
}

func (server *ServerService) Update(url string) error {
// url += fmt.Sprintf("?regionCode=%s&serverInstanceNo=%s&serverProductCode=%s", updateParams.regionCode, updateParams.serverInstanceNo, updateParams.serverProductCode)
url += "temp"

// Create an HTTP request
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return err
}

// Set common headers
GetCommonHeader(req)

// Set authorization token
SetAuthToken(req, server.token)

Check failure on line 175 in pkg/server.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21)

server.token undefined (type *ServerService has no field or method token)

Check failure on line 175 in pkg/server.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.21)

server.token undefined (type *ServerService has no field or method token)

// Make the HTTP request
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

// Check the response status
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected response status: %s", resp.Status)
}

return nil
}
46 changes: 0 additions & 46 deletions pkg/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,6 @@ import (
"github.com/stretchr/testify/assert"
)

func TestList(t *testing.T) {
ncp := pkg.NewNcpService("ncp service token")
ncp.Server = pkg.NewServerService("ncp server token")

// given
tests := []struct {
name string
url string
payload interface{}
expectedError string
expectedData interface{}
}{
{
name: "성공",
url: "http://localhost:8080",
payload: nil,
expectedError: "",
expectedData: nil,
},
{
name: "(실패) url을 입력 안함",
url: "",
payload: nil,
expectedError: "please input url",
expectedData: nil,
},
}

for i := range tests {
t.Logf("%s : running scenario %d", tests[i].name, i)
t.Run(tests[i].name, func(t *testing.T) {
err := ncp.Server.List(tests[i].url)
if err != nil {
if err.Error() != tests[i].expectedError {
t.Fatalf("expected error : %v, got : %v", tests[i].expectedError, err)
}
} else {
if tests[i].expectedError != "" {
t.Fatalf("expected error : %v, got : %v", tests[i].expectedError, err)
}
}
})
}

}

func TestCreate(t *testing.T) {
testCases := []struct {
testName string
Expand Down
65 changes: 65 additions & 0 deletions pkg/serverimageproduct.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package pkg

import (
"fmt"
"io"
"log"
"net/http"

serverType "github.com/cloud-club/Aviator-service/types/server"
)

type ProductService struct{}

func NewProductService() ProductInterface {
return &ProductService{}
}

type ProductInterface interface {
Get(url string) (*serverType.ProductList, error)
}

func (product *ProductService) Get(url string) (*serverType.ProductList, error) {
// Set url with query parameters
// However, there is no must required query parameters for this API, so we will comment this line right now.
//requestParams := serverType.CreateRequestString(request)

// Create an HTTP request
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}

// Set HTTP header for NCP authorization
SetNCPHeader(req, "YnCbljlTZfqRkFOXighj", "bDoQVXDGLJ9BhzpOYxnjSyxNB97dohAUPLeiQC0D")

// Make the HTTP request
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}

// Check the response status
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected response status: %s", resp.Status)
}

// Read the response body
responseByteData, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
return nil, err
}

// Convert the response body to the struct
var sl *serverType.ProductList
responseInterface, err := serverType.MapResponse(responseByteData, &sl)
if err != nil {
return nil, err
}

// interface{} to *serverType.SubnetList
responseStruct := responseInterface.(**serverType.ProductList)

return *responseStruct, nil
}
Loading
Loading