github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/alicloud/service_alicloud_ecs.go (about)

     1  package alicloud
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"github.com/denverdino/aliyungo/common"
     7  	"github.com/denverdino/aliyungo/ecs"
     8  	"strings"
     9  )
    10  
    11  func (client *AliyunClient) DescribeImage(imageId string) (*ecs.ImageType, error) {
    12  
    13  	pagination := common.Pagination{
    14  		PageNumber: 1,
    15  	}
    16  	args := ecs.DescribeImagesArgs{
    17  		Pagination: pagination,
    18  		RegionId:   client.Region,
    19  		Status:     ecs.ImageStatusAvailable,
    20  	}
    21  
    22  	var allImages []ecs.ImageType
    23  
    24  	for {
    25  		images, _, err := client.ecsconn.DescribeImages(&args)
    26  		if err != nil {
    27  			break
    28  		}
    29  
    30  		if len(images) == 0 {
    31  			break
    32  		}
    33  
    34  		allImages = append(allImages, images...)
    35  
    36  		args.Pagination.PageNumber++
    37  	}
    38  
    39  	if len(allImages) == 0 {
    40  		return nil, common.GetClientErrorFromString("Not found")
    41  	}
    42  
    43  	var image *ecs.ImageType
    44  	imageIds := []string{}
    45  	for _, im := range allImages {
    46  		if im.ImageId == imageId {
    47  			image = &im
    48  		}
    49  		imageIds = append(imageIds, im.ImageId)
    50  	}
    51  
    52  	if image == nil {
    53  		return nil, fmt.Errorf("image_id %s not exists in range %s, all images are %s", imageId, client.Region, imageIds)
    54  	}
    55  
    56  	return image, nil
    57  }
    58  
    59  // DescribeZone validate zoneId is valid in region
    60  func (client *AliyunClient) DescribeZone(zoneID string) (*ecs.ZoneType, error) {
    61  	zones, err := client.ecsconn.DescribeZones(client.Region)
    62  	if err != nil {
    63  		return nil, fmt.Errorf("error to list zones not found")
    64  	}
    65  
    66  	var zone *ecs.ZoneType
    67  	zoneIds := []string{}
    68  	for _, z := range zones {
    69  		if z.ZoneId == zoneID {
    70  			zone = &ecs.ZoneType{
    71  				ZoneId:                    z.ZoneId,
    72  				LocalName:                 z.LocalName,
    73  				AvailableResourceCreation: z.AvailableResourceCreation,
    74  				AvailableDiskCategories:   z.AvailableDiskCategories,
    75  			}
    76  		}
    77  		zoneIds = append(zoneIds, z.ZoneId)
    78  	}
    79  
    80  	if zone == nil {
    81  		return nil, fmt.Errorf("availability_zone not exists in range %s, all zones are %s", client.Region, zoneIds)
    82  	}
    83  
    84  	return zone, nil
    85  }
    86  
    87  // return multiIZ list of current region
    88  func (client *AliyunClient) DescribeMultiIZByRegion() (izs []string, err error) {
    89  	resp, err := client.rdsconn.DescribeRegions()
    90  	if err != nil {
    91  		return nil, fmt.Errorf("error to list regions not found")
    92  	}
    93  	regions := resp.Regions.RDSRegion
    94  
    95  	zoneIds := []string{}
    96  	for _, r := range regions {
    97  		if r.RegionId == string(client.Region) && strings.Contains(r.ZoneId, MULTI_IZ_SYMBOL) {
    98  			zoneIds = append(zoneIds, r.ZoneId)
    99  		}
   100  	}
   101  
   102  	return zoneIds, nil
   103  }
   104  
   105  func (client *AliyunClient) QueryInstancesByIds(ids []string) (instances []ecs.InstanceAttributesType, err error) {
   106  	idsStr, jerr := json.Marshal(ids)
   107  	if jerr != nil {
   108  		return nil, jerr
   109  	}
   110  
   111  	args := ecs.DescribeInstancesArgs{
   112  		RegionId:    client.Region,
   113  		InstanceIds: string(idsStr),
   114  	}
   115  
   116  	instances, _, errs := client.ecsconn.DescribeInstances(&args)
   117  
   118  	if errs != nil {
   119  		return nil, errs
   120  	}
   121  
   122  	return instances, nil
   123  }
   124  
   125  func (client *AliyunClient) QueryInstancesById(id string) (instance *ecs.InstanceAttributesType, err error) {
   126  	ids := []string{id}
   127  
   128  	instances, errs := client.QueryInstancesByIds(ids)
   129  	if errs != nil {
   130  		return nil, errs
   131  	}
   132  
   133  	if len(instances) == 0 {
   134  		return nil, GetNotFoundErrorFromString(InstanceNotfound)
   135  	}
   136  
   137  	return &instances[0], nil
   138  }
   139  
   140  func (client *AliyunClient) QueryInstanceSystemDisk(id string) (disk *ecs.DiskItemType, err error) {
   141  	args := ecs.DescribeDisksArgs{
   142  		RegionId:   client.Region,
   143  		InstanceId: string(id),
   144  		DiskType:   ecs.DiskTypeAllSystem,
   145  	}
   146  	disks, _, err := client.ecsconn.DescribeDisks(&args)
   147  	if err != nil {
   148  		return nil, err
   149  	}
   150  	if len(disks) == 0 {
   151  		return nil, common.GetClientErrorFromString(SystemDiskNotFound)
   152  	}
   153  
   154  	return &disks[0], nil
   155  }
   156  
   157  // ResourceAvailable check resource available for zone
   158  func (client *AliyunClient) ResourceAvailable(zone *ecs.ZoneType, resourceType ecs.ResourceType) error {
   159  	available := false
   160  	for _, res := range zone.AvailableResourceCreation.ResourceTypes {
   161  		if res == resourceType {
   162  			available = true
   163  		}
   164  	}
   165  	if !available {
   166  		return fmt.Errorf("%s is not available in %s zone of %s region", resourceType, zone.ZoneId, client.Region)
   167  	}
   168  
   169  	return nil
   170  }
   171  
   172  func (client *AliyunClient) DiskAvailable(zone *ecs.ZoneType, diskCategory ecs.DiskCategory) error {
   173  	available := false
   174  	for _, dist := range zone.AvailableDiskCategories.DiskCategories {
   175  		if dist == diskCategory {
   176  			available = true
   177  		}
   178  	}
   179  	if !available {
   180  		return fmt.Errorf("%s is not available in %s zone of %s region", diskCategory, zone.ZoneId, client.Region)
   181  	}
   182  	return nil
   183  }
   184  
   185  // todo: support syc
   186  func (client *AliyunClient) JoinSecurityGroups(instanceId string, securityGroupIds []string) error {
   187  	for _, sid := range securityGroupIds {
   188  		err := client.ecsconn.JoinSecurityGroup(instanceId, sid)
   189  		if err != nil {
   190  			e, _ := err.(*common.Error)
   191  			if e.ErrorResponse.Code != InvalidInstanceIdAlreadyExists {
   192  				return err
   193  			}
   194  		}
   195  	}
   196  
   197  	return nil
   198  }
   199  
   200  func (client *AliyunClient) LeaveSecurityGroups(instanceId string, securityGroupIds []string) error {
   201  	for _, sid := range securityGroupIds {
   202  		err := client.ecsconn.LeaveSecurityGroup(instanceId, sid)
   203  		if err != nil {
   204  			e, _ := err.(*common.Error)
   205  			if e.ErrorResponse.Code != InvalidSecurityGroupIdNotFound {
   206  				return err
   207  			}
   208  		}
   209  	}
   210  
   211  	return nil
   212  }
   213  
   214  func (client *AliyunClient) DescribeSecurity(securityGroupId string) (*ecs.DescribeSecurityGroupAttributeResponse, error) {
   215  
   216  	args := &ecs.DescribeSecurityGroupAttributeArgs{
   217  		RegionId:        client.Region,
   218  		SecurityGroupId: securityGroupId,
   219  	}
   220  
   221  	return client.ecsconn.DescribeSecurityGroupAttribute(args)
   222  }
   223  
   224  func (client *AliyunClient) DescribeSecurityByAttr(securityGroupId, direction, nicType string) (*ecs.DescribeSecurityGroupAttributeResponse, error) {
   225  
   226  	args := &ecs.DescribeSecurityGroupAttributeArgs{
   227  		RegionId:        client.Region,
   228  		SecurityGroupId: securityGroupId,
   229  		Direction:       direction,
   230  		NicType:         ecs.NicType(nicType),
   231  	}
   232  
   233  	return client.ecsconn.DescribeSecurityGroupAttribute(args)
   234  }
   235  
   236  func (client *AliyunClient) DescribeSecurityGroupRule(securityGroupId, direction, nicType, ipProtocol, portRange string) (*ecs.PermissionType, error) {
   237  	sg, err := client.DescribeSecurityByAttr(securityGroupId, direction, nicType)
   238  	if err != nil {
   239  		return nil, err
   240  	}
   241  
   242  	for _, p := range sg.Permissions.Permission {
   243  		if strings.ToLower(string(p.IpProtocol)) == ipProtocol && p.PortRange == portRange {
   244  			return &p, nil
   245  		}
   246  	}
   247  	return nil, GetNotFoundErrorFromString("Security group rule not found")
   248  
   249  }
   250  
   251  func (client *AliyunClient) RevokeSecurityGroup(args *ecs.RevokeSecurityGroupArgs) error {
   252  	//when the rule is not exist, api will return success(200)
   253  	return client.ecsconn.RevokeSecurityGroup(args)
   254  }
   255  
   256  func (client *AliyunClient) RevokeSecurityGroupEgress(args *ecs.RevokeSecurityGroupEgressArgs) error {
   257  	//when the rule is not exist, api will return success(200)
   258  	return client.ecsconn.RevokeSecurityGroupEgress(args)
   259  }