diff --git a/types/server/block.go b/types/server/block.go new file mode 100644 index 0000000..4730360 --- /dev/null +++ b/types/server/block.go @@ -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 +} diff --git a/types/server/common.go b/types/server/common.go index acae9b6..8faa348 100644 --- a/types/server/common.go +++ b/types/server/common.go @@ -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 -} diff --git a/types/server/network.go b/types/server/network.go new file mode 100644 index 0000000..243b1a1 --- /dev/null +++ b/types/server/network.go @@ -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 { +} diff --git a/types/server/request.go b/types/server/request.go index ab1254f..e4d142f 100644 --- a/types/server/request.go +++ b/types/server/request.go @@ -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] +} diff --git a/types/server/response.go b/types/server/response.go index dfea4a2..f31ff97 100644 --- a/types/server/response.go +++ b/types/server/response.go @@ -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 }