123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- // API on Network
- package ecs
- import (
- "time"
- "github.com/denverdino/aliyungo/common"
- "github.com/denverdino/aliyungo/util"
- )
- type AllocatePublicIpAddressArgs struct {
- InstanceId string
- }
- type AllocatePublicIpAddressResponse struct {
- common.Response
- IpAddress string
- }
- // AllocatePublicIpAddress allocates Public Ip Address
- //
- // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/network&allocatepublicipaddress
- func (client *Client) AllocatePublicIpAddress(instanceId string) (ipAddress string, err error) {
- args := AllocatePublicIpAddressArgs{
- InstanceId: instanceId,
- }
- response := AllocatePublicIpAddressResponse{}
- err = client.Invoke("AllocatePublicIpAddress", &args, &response)
- if err != nil {
- return "", err
- }
- return response.IpAddress, nil
- }
- type ModifyInstanceNetworkSpec struct {
- InstanceId string
- InternetMaxBandwidthOut *int
- InternetMaxBandwidthIn *int
- }
- type ModifyInstanceNetworkSpecResponse struct {
- common.Response
- }
- // ModifyInstanceNetworkSpec modifies instance network spec
- //
- // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/network&modifyinstancenetworkspec
- func (client *Client) ModifyInstanceNetworkSpec(args *ModifyInstanceNetworkSpec) error {
- response := ModifyInstanceNetworkSpecResponse{}
- return client.Invoke("ModifyInstanceNetworkSpec", args, &response)
- }
- type AllocateEipAddressArgs struct {
- RegionId common.Region
- Bandwidth int
- InternetChargeType common.InternetChargeType
- ClientToken string
- }
- type AllocateEipAddressResponse struct {
- common.Response
- EipAddress string
- AllocationId string
- }
- // AllocateEipAddress allocates Eip Address
- //
- // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/network&allocateeipaddress
- func (client *Client) AllocateEipAddress(args *AllocateEipAddressArgs) (EipAddress string, AllocationId string, err error) {
- if args.Bandwidth == 0 {
- args.Bandwidth = 5
- }
- response := AllocateEipAddressResponse{}
- err = client.Invoke("AllocateEipAddress", args, &response)
- if err != nil {
- return "", "", err
- }
- return response.EipAddress, response.AllocationId, nil
- }
- type AssociateEipAddressArgs struct {
- AllocationId string
- InstanceId string
- }
- type AssociateEipAddressResponse struct {
- common.Response
- }
- // AssociateEipAddress associates EIP address to VM instance
- //
- // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/network&associateeipaddress
- func (client *Client) AssociateEipAddress(allocationId string, instanceId string) error {
- args := AssociateEipAddressArgs{
- AllocationId: allocationId,
- InstanceId: instanceId,
- }
- response := ModifyInstanceNetworkSpecResponse{}
- return client.Invoke("AssociateEipAddress", &args, &response)
- }
- // Status of disks
- type EipStatus string
- const (
- EipStatusAssociating = EipStatus("Associating")
- EipStatusUnassociating = EipStatus("Unassociating")
- EipStatusInUse = EipStatus("InUse")
- EipStatusAvailable = EipStatus("Available")
- )
- type DescribeEipAddressesArgs struct {
- RegionId common.Region
- Status EipStatus //enum Associating | Unassociating | InUse | Available
- EipAddress string
- AllocationId string
- common.Pagination
- }
- //
- // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/datatype&eipaddresssettype
- type EipAddressSetType struct {
- RegionId common.Region
- IpAddress string
- AllocationId string
- Status EipStatus
- InstanceId string
- Bandwidth string // Why string
- InternetChargeType common.InternetChargeType
- OperationLocks OperationLocksType
- AllocationTime util.ISO6801Time
- }
- type DescribeEipAddressesResponse struct {
- common.Response
- common.PaginationResult
- EipAddresses struct {
- EipAddress []EipAddressSetType
- }
- }
- // DescribeInstanceStatus describes instance status
- //
- // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/network&describeeipaddresses
- func (client *Client) DescribeEipAddresses(args *DescribeEipAddressesArgs) (eipAddresses []EipAddressSetType, pagination *common.PaginationResult, err error) {
- args.Validate()
- response := DescribeEipAddressesResponse{}
- err = client.Invoke("DescribeEipAddresses", args, &response)
- if err == nil {
- return response.EipAddresses.EipAddress, &response.PaginationResult, nil
- }
- return nil, nil, err
- }
- type ModifyEipAddressAttributeArgs struct {
- AllocationId string
- Bandwidth int
- }
- type ModifyEipAddressAttributeResponse struct {
- common.Response
- }
- // ModifyEipAddressAttribute Modifies EIP attribute
- //
- // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/network&modifyeipaddressattribute
- func (client *Client) ModifyEipAddressAttribute(allocationId string, bandwidth int) error {
- args := ModifyEipAddressAttributeArgs{
- AllocationId: allocationId,
- Bandwidth: bandwidth,
- }
- response := ModifyEipAddressAttributeResponse{}
- return client.Invoke("ModifyEipAddressAttribute", &args, &response)
- }
- type UnallocateEipAddressArgs struct {
- AllocationId string
- InstanceId string
- }
- type UnallocateEipAddressResponse struct {
- common.Response
- }
- // UnassociateEipAddress unallocates Eip Address from instance
- //
- // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/network&unassociateeipaddress
- func (client *Client) UnassociateEipAddress(allocationId string, instanceId string) error {
- args := UnallocateEipAddressArgs{
- AllocationId: allocationId,
- InstanceId: instanceId,
- }
- response := UnallocateEipAddressResponse{}
- return client.Invoke("UnassociateEipAddress", &args, &response)
- }
- type ReleaseEipAddressArgs struct {
- AllocationId string
- }
- type ReleaseEipAddressResponse struct {
- common.Response
- }
- // ReleaseEipAddress releases Eip address
- //
- // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/network&releaseeipaddress
- func (client *Client) ReleaseEipAddress(allocationId string) error {
- args := ReleaseEipAddressArgs{
- AllocationId: allocationId,
- }
- response := ReleaseEipAddressResponse{}
- return client.Invoke("ReleaseEipAddress", &args, &response)
- }
- // WaitForVSwitchAvailable waits for VSwitch to given status
- func (client *Client) WaitForEip(regionId common.Region, allocationId string, status EipStatus, timeout int) error {
- if timeout <= 0 {
- timeout = DefaultTimeout
- }
- args := DescribeEipAddressesArgs{
- RegionId: regionId,
- AllocationId: allocationId,
- }
- for {
- eips, _, err := client.DescribeEipAddresses(&args)
- if err != nil {
- return err
- }
- if len(eips) == 0 {
- return common.GetClientErrorFromString("Not found")
- }
- if eips[0].Status == status {
- break
- }
- timeout = timeout - DefaultWaitForInterval
- if timeout <= 0 {
- return common.GetClientErrorFromString("Timeout")
- }
- time.Sleep(DefaultWaitForInterval * time.Second)
- }
- return nil
- }
|