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

     1  package alicloud
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/denverdino/aliyungo/ecs"
     6  	"github.com/hashicorp/terraform/helper/schema"
     7  	"log"
     8  )
     9  
    10  func dataSourceAlicloudInstanceTypes() *schema.Resource {
    11  	return &schema.Resource{
    12  		Read: dataSourceAlicloudInstanceTypesRead,
    13  
    14  		Schema: map[string]*schema.Schema{
    15  			"instance_type_family": {
    16  				Type:     schema.TypeString,
    17  				Optional: true,
    18  				ForceNew: true,
    19  			},
    20  			"cpu_core_count": {
    21  				Type:     schema.TypeInt,
    22  				Optional: true,
    23  				ForceNew: true,
    24  			},
    25  			"memory_size": {
    26  				Type:     schema.TypeFloat,
    27  				Optional: true,
    28  				ForceNew: true,
    29  			},
    30  			// Computed values.
    31  			"instance_types": {
    32  				Type:     schema.TypeList,
    33  				Computed: true,
    34  				Elem: &schema.Resource{
    35  					Schema: map[string]*schema.Schema{
    36  						"id": {
    37  							Type:     schema.TypeString,
    38  							Computed: true,
    39  						},
    40  						"cpu_core_count": {
    41  							Type:     schema.TypeInt,
    42  							Computed: true,
    43  						},
    44  						"memory_size": {
    45  							Type:     schema.TypeFloat,
    46  							Computed: true,
    47  						},
    48  						"family": {
    49  							Type:     schema.TypeString,
    50  							Computed: true,
    51  						},
    52  					},
    53  				},
    54  			},
    55  		},
    56  	}
    57  }
    58  
    59  func dataSourceAlicloudInstanceTypesRead(d *schema.ResourceData, meta interface{}) error {
    60  	conn := meta.(*AliyunClient).ecsconn
    61  
    62  	cpu, _ := d.Get("cpu_core_count").(int)
    63  	mem, _ := d.Get("memory_size").(float64)
    64  
    65  	args, err := buildAliyunAlicloudInstanceTypesArgs(d, meta)
    66  
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	resp, err := conn.DescribeInstanceTypesNew(args)
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	var instanceTypes []ecs.InstanceTypeItemType
    77  	for _, types := range resp {
    78  		if cpu > 0 && types.CpuCoreCount != cpu {
    79  			continue
    80  		}
    81  
    82  		if mem > 0 && types.MemorySize != mem {
    83  			continue
    84  		}
    85  		instanceTypes = append(instanceTypes, types)
    86  	}
    87  
    88  	if len(instanceTypes) < 1 {
    89  		return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.")
    90  	}
    91  
    92  	log.Printf("[DEBUG] alicloud_instance_type - Types found: %#v", instanceTypes)
    93  	return instanceTypesDescriptionAttributes(d, instanceTypes)
    94  }
    95  
    96  func instanceTypesDescriptionAttributes(d *schema.ResourceData, types []ecs.InstanceTypeItemType) error {
    97  	var ids []string
    98  	var s []map[string]interface{}
    99  	for _, t := range types {
   100  		mapping := map[string]interface{}{
   101  			"id":             t.InstanceTypeId,
   102  			"cpu_core_count": t.CpuCoreCount,
   103  			"memory_size":    t.MemorySize,
   104  			"family":         t.InstanceTypeFamily,
   105  		}
   106  
   107  		log.Printf("[DEBUG] alicloud_instance_type - adding type mapping: %v", mapping)
   108  		ids = append(ids, t.InstanceTypeId)
   109  		s = append(s, mapping)
   110  	}
   111  
   112  	d.SetId(dataResourceIdHash(ids))
   113  	if err := d.Set("instance_types", s); err != nil {
   114  		return err
   115  	}
   116  	return nil
   117  }
   118  
   119  func buildAliyunAlicloudInstanceTypesArgs(d *schema.ResourceData, meta interface{}) (*ecs.DescribeInstanceTypesArgs, error) {
   120  	args := &ecs.DescribeInstanceTypesArgs{}
   121  
   122  	if v := d.Get("instance_type_family").(string); v != "" {
   123  		args.InstanceTypeFamily = v
   124  	}
   125  
   126  	return args, nil
   127  }