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

Request & Response 추가 #4

Merged
merged 4 commits into from
Dec 4, 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
27 changes: 27 additions & 0 deletions types/server/block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package types

// BlockDevicePartition means block disk parition info (such as AWS EBS)
type BlockDevicePartition struct {
MountPoint string
PartitionSize string
}

type BlockDevicePartitionList struct {
BlockDevicePartitionList []BlockDevicePartition
}

type BlockStorageMapping struct {
Order int
BlockStorageSnapshotInstanceNo string
BlockStorageSnapshotInstanceName string
BlockStorageSize int64
BlockStorageName string
BlockStorageVolumeType CommonCode
Iops int64
Throughput int64
IsEncryptedVolume bool
}

type BlockStorageMappingList struct {
BlockStorageMappingList []BlockStorageMapping
}
11 changes: 0 additions & 11 deletions types/server/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,3 @@ type CommonCode struct {
Code string // server status : int | creat | run | nstop
CodeName string
}

// BlockDevicePartition means block disk parition info (such as AWS EBS)
type BlockDevicePartition struct {
MountPoint string
PartitionSize string
}

// NetworkInterfaceNoList is same as NIC number list
type NetworkInterfaceNoList struct {
NetworkInterfaceNoList []string
}
33 changes: 33 additions & 0 deletions types/server/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package types

type NetworkInterface struct {
NetworkInterfaceNo string
NetworkInterfaceName string
SubnetNo string
DeleteOnTermination bool
IsDefault bool
DeviceName string
NetworkInterfaceStatus CommonCode
InstanceType CommonCode
InstanceNo string
IP string
MacAddress string
AccessControlGroupNoList []AccessControlGroup
NetworkInterfaceDescription string
SecondaryIPList []SecondaryIP
}

type NetworkInterfaceList struct {
TotalRows int
NetworkInterfaceList []NetworkInterface
}

type NetworkInterfaceNoList struct {
NetworkInterfaceNoList []string
}

type AccessControlGroup struct {
}

type SecondaryIP struct {
}
87 changes: 87 additions & 0 deletions types/server/request.go
Original file line number Diff line number Diff line change
@@ -1 +1,88 @@
package types

import (
"fmt"
"reflect"
)

// 필수가 아닌 필드(필수 여부: No)는 주석 처리 해두었음.
// 필요할 때 주석 해제
// 필수가 아닌 필드 중 (필수 여부: Conditional)는 주석 처리 안 했음.
// 필요할 때 주석 처리
type CreateServerRequest struct {
//RegionCode string `json:"regionCode"`
MemberServerImageInstanceNo string `json:"memberServerImageInstanceNo"` // Conditional
ServerImageProductCode string `json:"serverImageProductCode"` // Conditional
ServerImageNo string `json:"serverImageNo"` // Conditional
VpcNo string `json:"vpcNo"`
SubnetNo string `json:"subnetNo"`
//ServerProductCode string `json:"serverProductCode"`
ServerSpecCode string `json:"serverSpecCode"` // Conditional
//IsEncryptedBaseBlockStorageVolume bool `json:"isEncryptedBaseBlockStorageVolume"`
//FeeSystemTypeCode string `json:"feeSystemTypeCode"`
//ServerCreateCount int `json:"serverCreateCount"`
//ServerCreateStartNo int `json:"serverCreateStartNo"`
//ServerName string `json:"serverName"`

// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// NetworkInterfaceList.N의 값들
// NetworkInterfaceList를 먼저 호출한 뒤, N 번째 NetworkInterface 정보에서 필요한 부분들 추출해서 사용
NetworkInterfaceOrder int `json:"networkInterfaceList"`
//NetworkInterfaceNo string `json:"networkInterfaceNo"`
//NetworkInterfaceSubnetNo string `json:"networkInterfaceSubnetNo"`
//NetworkInterfaceIp string `json:"networkInterfaceIp"`
//accessControlGroupNoListN []string `json:"accessControlGroupNoList"`
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

// PlacementGroupNo string `json:"placementGroupNo"`
// IsProtectServerTermination bool `json:"isProtectServerTermination"`
// ServerDescription string `json:"serverDescription"`
// InitScriptNo string `json:"initScriptNo"`
// LoginKeyName string `json:"loginKeyName"`
// AssociateWithPublicIp bool `json:"associateWithPublicIp"`
RaidTypeName string `json:"raidTypeName"` // Conditional

// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// BlockDevicePartitionList.N의 값들
// BlockDevicePartitionList를 먼저 호출한 뒤, N 번째 BlockDevicePartition 정보에서 필요한 부분들 추출해서 사용
//BlockDevicePartitionMountPoint string `json:"blockDevicePartitionMountPoint"`
//BlockDevicePartitionSize string `json:"blockDevicePartitionSize"`
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// BlockStorageMappingList.N의 값들
// BlockStorageMappingList를 먼저 호출한 뒤, N 번째 BlockStorageMapping 정보에서 필요한 부분들 추출해서 사용
//BlockStorageMappingOrder int `json:"blockStorageMappingList"`
//BlockStorageMappingSnapshotInstanceNo string `json:"blockStorageMappingSnapshotInstanceNo"`
//BlockStorageMappingBlockStorageSize string `json:"blockStorageMappingBlockStorageSize"`
//BlockStorageMappingBlockStorageName string `json:"blockStorageMappingBlockStorageName"`
//BlockStorageMappingBlockStorageVolumeTypeCode string `json:"blockStorageMappingBlockStorageVolumeTypeCode"`
//BlockStorageMappingEncrypted string `json:"blockStorageMappingEncrypted"`
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

//ResponseFormatType string `json:"responseFormatType"`
}

type GetServerRequest struct{}

type ListServerRequest struct{}

type UpdateServerRequest struct{}

// Request 구조체를 모두 하나의 String으로 변환해주는 함수
// NetworkInterfaceList.N, BlockDevicePartitionList.N, BlockStorageMappingList.N의 값들에 대한 수정 필요
func GenerateRequestString(request interface{}) string {
var requestString string
requestType := reflect.TypeOf(request)
requestValue := reflect.ValueOf(request)

for i := 0; i < requestType.NumField(); i++ {
fieldValue := requestValue.Field(i)
if fieldValue.IsZero() {
continue
}
requestString += fmt.Sprintf("%s=%v&", requestType.Field(i).Name, fmt.Sprintf("%v", fieldValue))
}

return "?" + requestString[:len(requestString)-1]
}
116 changes: 76 additions & 40 deletions types/server/response.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,81 @@
package types

import "time"

// ServerInstanceResponse is same as compute server in Naver Cloud (such as AWS EC2)
type ServerInstanceResponse struct {
ServerInstanceNo string
ServerName string
ServerDescription string
CpuCount int
MemorySize int64
PlatformType CommonCode
LoginKeyName string
PublicIpInstanceNo string
PublicIp string
ServerInstanceStatus CommonCode
ServerInstanceOperation CommonCode
ServerInstanceStatusName string
CreateDate time.Time
Uptime time.Time
ServerImageProductCode string
ServerProductCode string
IsProtectServerTermination bool
ZoneCode string
RegionCode string
VpcNo string
SubnetNo string
NetworkInterfaceNoList NetworkInterfaceNoList
InitScriptNo string
ServerInstanceType CommonCode
BaseBlockStorageDiskType CommonCode
BaseBlockStorageDiskDetailType CommonCode
PlacementGroupNo string
PlacementGroupName string
MemberServerImageInstanceNo string
BlockDevicePartitionList []BlockDevicePartition
HypervisorType CommonCode
ServerImageNo string
ServerSpecCode string
import (
"encoding/xml"
"fmt"
"reflect"
"time"
)

// ServerInstance is same as compute server in Naver Cloud (such as AWS EC2)
// 필수가 아닌 필드(필수 여부: No)는 주석 처리 해두었음.
// 필요할 때 주석 해제
type ServerInstance struct {
ServerInstanceNo string `xml:"serverInstanceNo" json:"serverInstanceNo"`
ServerName string `xml:"serverName" json:"serverName"`
//ServerDescription string
CpuCount int `xml:"cpuCount" json:"cpuCount"`
MemorySize int64 `xml:"memorySize" json:"memorySize"`
PlatformType CommonCode `xml:"platformType" json:"platformType"`
LoginKeyName string `xml:"loginKeyName" json:"loginKeyName"`
//PublicIpInstanceNo string
//PublicIp string
ServerInstanceStatus CommonCode `xml:"serverInstanceStatus" json:"serverInstanceStatus"`
ServerInstanceOperation CommonCode `xml:"serverInstanceOperation" json:"serverInstanceOperation"`
ServerInstanceStatusName string `xml:"serverInstanceStatusName" json:"serverInstanceStatusName"`
CreateDate time.Time `xml:"createDate" json:"createDate"`
Uptime time.Time `xml:"uptime" json:"uptime"`
ServerImageProductCode string `xml:"serverImageProductCode" json:"serverImageProductCode"`
ServerProductCode string `xml:"serverProductCode" json:"serverProductCode"`
IsProtectServerTermination bool `xml:"isProtectServerTermination" json:"isProtectServerTermination"`
ZoneCode string `xml:"zoneCode" json:"zoneCode"`
RegionCode string `xml:"regionCode" json:"regionCode"`
VpcNo string `xml:"vpcNo" json:"vpcNo"`
SubnetNo string `xml:"subnetNo" json:"subnetNo"`
NetworkInterfaceNoList NetworkInterfaceNoList `xml:"networkInterfaceNoList" json:"networkInterfaceNoList"`
//InitScriptNo string
ServerInstanceType CommonCode `xml:"serverInstanceType" json:"serverInstanceType"`
BaseBlockStorageDiskType CommonCode `xml:"baseBlockStorageDiskType" json:"baseBlockStorageDiskType"`
BaseBlockStorageDiskDetailType CommonCode `xml:"baseBlockStorageDiskDetailType" json:"baseBlockStorageDiskDetailType"`
//PlacementGroupNo string
//PlacementGroupName string
//MemberServerImageInstanceNo string
//BlockDevicePartitionList []BlockDevicePartition // Assuming BlockDevicePartition is a defined struct
HypervisorType CommonCode `xml:"hypervisorType" json:"hypervisorType"`
ServerImageNo string `xml:"serverImageNo" json:"serverImageNo"`
ServerSpecCode string `xml:"serverSpecCode" json:"serverSpecCode"`
}

type CreateServerResponse struct {
RequestId string `xml:"requestId"`
ReturnCode int `xml:"returnCode"`
ReturnMessage string `xml:"returnMessage"`
TotalRows int `xml:"totalRows"`
ServerInstanceList []ServerInstance `xml:"serverInstanceList>serverInstance"`
}

type ServerInstanceListResponse struct {
TotalCount int
InstanceList []ServerInstanceResponse
type GetServerResponse struct{}

type ListServerResponse struct{}

type UpdateServerResponse struct{}

type DeleteServerResponse struct{}

// Response XML을 Go struct로 변환하는 함수
// responseBody: API 서버로부터 받은 XML response body
// v: interface{} 타입의 struct 포인터
// 예시: mapResponse(responseBody, &CreateServerResponse{})
func MapResponse(responseBody []byte, v interface{}) (interface{}, error) {
rv := reflect.ValueOf(v) // reflect를 이용해 v가 어떤 구조체 타입인지 알아냄
if rv.Kind() != reflect.Ptr || rv.IsNil() { // 만약 v가 포인터가 아니거나 nil이면 에러 반환
return nil, fmt.Errorf("non-nil pointer expected")
}

err := xml.Unmarshal(responseBody, v) // responseBody를 v로 매핑. 만약 CreateServerResponse 타입이면 CreateServerResponse로 매핑
if err != nil {
return nil, fmt.Errorf("error unmarshalling response: %v", err)
}

return v, nil
}