123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492 |
- package ecs
- import (
- "encoding/base64"
- "encoding/json"
- "strconv"
- "time"
- "github.com/denverdino/aliyungo/common"
- "github.com/denverdino/aliyungo/util"
- )
- type InstanceStatus string
- const (
- Creating = InstanceStatus("Creating")
- Running = InstanceStatus("Running")
- Starting = InstanceStatus("Starting")
- Stopped = InstanceStatus("Stopped")
- Stopping = InstanceStatus("Stopping")
- )
- type LockReason string
- const (
- LockReasonFinancial = LockReason("financial")
- LockReasonSecurity = LockReason("security")
- )
- type LockReasonType struct {
- LockReason LockReason
- }
- type DescribeInstanceStatusArgs struct {
- RegionId common.Region
- ZoneId string
- common.Pagination
- }
- type InstanceStatusItemType struct {
- InstanceId string
- Status InstanceStatus
- }
- type DescribeInstanceStatusResponse struct {
- common.Response
- common.PaginationResult
- InstanceStatuses struct {
- InstanceStatus []InstanceStatusItemType
- }
- }
- func (client *Client) DescribeInstanceStatus(args *DescribeInstanceStatusArgs) (instanceStatuses []InstanceStatusItemType, pagination *common.PaginationResult, err error) {
- args.Validate()
- response := DescribeInstanceStatusResponse{}
- err = client.Invoke("DescribeInstanceStatus", args, &response)
- if err == nil {
- return response.InstanceStatuses.InstanceStatus, &response.PaginationResult, nil
- }
- return nil, nil, err
- }
- type StopInstanceArgs struct {
- InstanceId string
- ForceStop bool
- }
- type StopInstanceResponse struct {
- common.Response
- }
- func (client *Client) StopInstance(instanceId string, forceStop bool) error {
- args := StopInstanceArgs{
- InstanceId: instanceId,
- ForceStop: forceStop,
- }
- response := StopInstanceResponse{}
- err := client.Invoke("StopInstance", &args, &response)
- return err
- }
- type StartInstanceArgs struct {
- InstanceId string
- }
- type StartInstanceResponse struct {
- common.Response
- }
- func (client *Client) StartInstance(instanceId string) error {
- args := StartInstanceArgs{InstanceId: instanceId}
- response := StartInstanceResponse{}
- err := client.Invoke("StartInstance", &args, &response)
- return err
- }
- type RebootInstanceArgs struct {
- InstanceId string
- ForceStop bool
- }
- type RebootInstanceResponse struct {
- common.Response
- }
- func (client *Client) RebootInstance(instanceId string, forceStop bool) error {
- request := RebootInstanceArgs{
- InstanceId: instanceId,
- ForceStop: forceStop,
- }
- response := RebootInstanceResponse{}
- err := client.Invoke("RebootInstance", &request, &response)
- return err
- }
- type DescribeInstanceAttributeArgs struct {
- InstanceId string
- }
- type OperationLocksType struct {
- LockReason []LockReasonType
- }
- type SecurityGroupIdSetType struct {
- SecurityGroupId string
- }
- type IpAddressSetType struct {
- IpAddress []string
- }
- type VpcAttributesType struct {
- VpcId string
- VSwitchId string
- PrivateIpAddress IpAddressSetType
- NatIpAddress string
- }
- type EipAddressAssociateType struct {
- AllocationId string
- IpAddress string
- Bandwidth int
- InternetChargeType common.InternetChargeType
- }
- type InstanceAttributesType struct {
- InstanceId string
- InstanceName string
- Description string
- ImageId string
- RegionId common.Region
- ZoneId string
- CPU int
- Memory int
- ClusterId string
- InstanceType string
- InstanceTypeFamily string
- HostName string
- SerialNumber string
- Status InstanceStatus
- OperationLocks OperationLocksType
- SecurityGroupIds struct {
- SecurityGroupId []string
- }
- PublicIpAddress IpAddressSetType
- InnerIpAddress IpAddressSetType
- InstanceNetworkType string
- InternetMaxBandwidthIn int
- InternetMaxBandwidthOut int
- InternetChargeType common.InternetChargeType
- CreationTime util.ISO6801Time
- VpcAttributes VpcAttributesType
- EipAddress EipAddressAssociateType
- IoOptimized StringOrBool
- InstanceChargeType common.InstanceChargeType
- ExpiredTime util.ISO6801Time
- Tags struct {
- Tag []TagItemType
- }
- }
- type DescribeInstanceAttributeResponse struct {
- common.Response
- InstanceAttributesType
- }
- func (client *Client) DescribeInstanceAttribute(instanceId string) (instance *InstanceAttributesType, err error) {
- args := DescribeInstanceAttributeArgs{InstanceId: instanceId}
- response := DescribeInstanceAttributeResponse{}
- err = client.Invoke("DescribeInstanceAttribute", &args, &response)
- if err != nil {
- return nil, err
- }
- return &response.InstanceAttributesType, err
- }
- type ModifyInstanceAttributeArgs struct {
- InstanceId string
- InstanceName string
- Description string
- Password string
- HostName string
- }
- type ModifyInstanceAttributeResponse struct {
- common.Response
- }
- func (client *Client) ModifyInstanceAttribute(args *ModifyInstanceAttributeArgs) error {
- response := ModifyInstanceAttributeResponse{}
- err := client.Invoke("ModifyInstanceAttribute", args, &response)
- return err
- }
- const InstanceDefaultTimeout = 120
- func (client *Client) WaitForInstance(instanceId string, status InstanceStatus, timeout int) error {
- if timeout <= 0 {
- timeout = InstanceDefaultTimeout
- }
- for {
- instance, err := client.DescribeInstanceAttribute(instanceId)
- if err != nil {
- return err
- }
- if instance.Status == status {
-
-
- time.Sleep(DefaultWaitForInterval * time.Second)
- break
- }
- timeout = timeout - DefaultWaitForInterval
- if timeout <= 0 {
- return common.GetClientErrorFromString("Timeout")
- }
- time.Sleep(DefaultWaitForInterval * time.Second)
- }
- return nil
- }
- type DescribeInstanceVncUrlArgs struct {
- RegionId common.Region
- InstanceId string
- }
- type DescribeInstanceVncUrlResponse struct {
- common.Response
- VncUrl string
- }
- func (client *Client) DescribeInstanceVncUrl(args *DescribeInstanceVncUrlArgs) (string, error) {
- response := DescribeInstanceVncUrlResponse{}
- err := client.Invoke("DescribeInstanceVncUrl", args, &response)
- if err == nil {
- return response.VncUrl, nil
- }
- return "", err
- }
- type DescribeInstancesArgs struct {
- RegionId common.Region
- VpcId string
- VSwitchId string
- ZoneId string
- InstanceIds string
- InstanceNetworkType string
- InstanceName string
- Status InstanceStatus
- PrivateIpAddresses string
- InnerIpAddresses string
- PublicIpAddresses string
- SecurityGroupId string
- Tag map[string]string
- common.Pagination
- }
- type DescribeInstancesResponse struct {
- common.Response
- common.PaginationResult
- Instances struct {
- Instance []InstanceAttributesType
- }
- }
- func (client *Client) DescribeInstances(args *DescribeInstancesArgs) (instances []InstanceAttributesType, pagination *common.PaginationResult, err error) {
- args.Validate()
- response := DescribeInstancesResponse{}
- err = client.Invoke("DescribeInstances", args, &response)
- if err == nil {
- return response.Instances.Instance, &response.PaginationResult, nil
- }
- return nil, nil, err
- }
- type DeleteInstanceArgs struct {
- InstanceId string
- }
- type DeleteInstanceResponse struct {
- common.Response
- }
- func (client *Client) DeleteInstance(instanceId string) error {
- args := DeleteInstanceArgs{InstanceId: instanceId}
- response := DeleteInstanceResponse{}
- err := client.Invoke("DeleteInstance", &args, &response)
- return err
- }
- type DataDiskType struct {
- Size int
- Category DiskCategory
- SnapshotId string
- DiskName string
- Description string
- Device string
- DeleteWithInstance bool
- }
- type SystemDiskType struct {
- Size int
- Category DiskCategory
- DiskName string
- Description string
- }
- type IoOptimized string
- type StringOrBool struct {
- Value bool
- }
- func (io *StringOrBool) UnmarshalJSON(value []byte) error {
- if value[0] == '"' {
- var str string
- err := json.Unmarshal(value, &str)
- if err == nil {
- io.Value = (str == "true" || str == "optimized")
- }
- return err
- }
- var boolVal bool
- err := json.Unmarshal(value, &boolVal)
- if err == nil {
- io.Value = boolVal
- }
- return err
- }
- func (io StringOrBool) Bool() bool {
- return io.Value
- }
- func (io StringOrBool) String() string {
- return strconv.FormatBool(io.Value)
- }
- var (
- IoOptimizedNone = IoOptimized("none")
- IoOptimizedOptimized = IoOptimized("optimized")
- )
- type CreateInstanceArgs struct {
- RegionId common.Region
- ZoneId string
- ImageId string
- InstanceType string
- SecurityGroupId string
- InstanceName string
- Description string
- InternetChargeType common.InternetChargeType
- InternetMaxBandwidthIn int
- InternetMaxBandwidthOut int
- HostName string
- Password string
- IoOptimized IoOptimized
- SystemDisk SystemDiskType
- DataDisk []DataDiskType
- VSwitchId string
- PrivateIpAddress string
- ClientToken string
- InstanceChargeType common.InstanceChargeType
- Period int
- UserData string
- }
- type CreateInstanceResponse struct {
- common.Response
- InstanceId string
- }
- func (client *Client) CreateInstance(args *CreateInstanceArgs) (instanceId string, err error) {
- if args.UserData != "" {
-
- args.UserData = base64.StdEncoding.EncodeToString([]byte(args.UserData))
- }
- response := CreateInstanceResponse{}
- err = client.Invoke("CreateInstance", args, &response)
- if err != nil {
- return "", err
- }
- return response.InstanceId, err
- }
- type SecurityGroupArgs struct {
- InstanceId string
- SecurityGroupId string
- }
- type SecurityGroupResponse struct {
- common.Response
- }
- func (client *Client) JoinSecurityGroup(instanceId string, securityGroupId string) error {
- args := SecurityGroupArgs{InstanceId: instanceId, SecurityGroupId: securityGroupId}
- response := SecurityGroupResponse{}
- err := client.Invoke("JoinSecurityGroup", &args, &response)
- return err
- }
- func (client *Client) LeaveSecurityGroup(instanceId string, securityGroupId string) error {
- args := SecurityGroupArgs{InstanceId: instanceId, SecurityGroupId: securityGroupId}
- response := SecurityGroupResponse{}
- err := client.Invoke("LeaveSecurityGroup", &args, &response)
- return err
- }
|