github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/alicloud/data_source_alicloud_zones.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  	"reflect"
     9  )
    10  
    11  func dataSourceAlicloudZones() *schema.Resource {
    12  	return &schema.Resource{
    13  		Read: dataSourceAlicloudZonesRead,
    14  
    15  		Schema: map[string]*schema.Schema{
    16  			"available_instance_type": {
    17  				Type:     schema.TypeString,
    18  				Optional: true,
    19  				ForceNew: true,
    20  			},
    21  			"available_resource_creation": {
    22  				Type:     schema.TypeString,
    23  				Optional: true,
    24  				ForceNew: true,
    25  			},
    26  			"available_disk_category": {
    27  				Type:     schema.TypeString,
    28  				Optional: true,
    29  				ForceNew: true,
    30  			},
    31  			// Computed values.
    32  			"zones": {
    33  				Type:     schema.TypeList,
    34  				Computed: true,
    35  				Elem: &schema.Resource{
    36  					Schema: map[string]*schema.Schema{
    37  						"id": {
    38  							Type:     schema.TypeString,
    39  							Computed: true,
    40  						},
    41  						"local_name": {
    42  							Type:     schema.TypeString,
    43  							Computed: true,
    44  						},
    45  						"available_instance_types": {
    46  							Type:     schema.TypeList,
    47  							Computed: true,
    48  							Elem:     &schema.Schema{Type: schema.TypeString},
    49  						},
    50  						"available_resource_creation": {
    51  							Type:     schema.TypeList,
    52  							Computed: true,
    53  							Elem:     &schema.Schema{Type: schema.TypeString},
    54  						},
    55  						"available_disk_categories": {
    56  							Type:     schema.TypeList,
    57  							Computed: true,
    58  							Elem:     &schema.Schema{Type: schema.TypeString},
    59  						},
    60  					},
    61  				},
    62  			},
    63  		},
    64  	}
    65  }
    66  
    67  func dataSourceAlicloudZonesRead(d *schema.ResourceData, meta interface{}) error {
    68  	conn := meta.(*AliyunClient).ecsconn
    69  
    70  	insType, _ := d.Get("available_instance_type").(string)
    71  	resType, _ := d.Get("available_resource_creation").(string)
    72  	diskType, _ := d.Get("available_disk_category").(string)
    73  
    74  	resp, err := conn.DescribeZones(getRegion(d, meta))
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	var zoneTypes []ecs.ZoneType
    80  	for _, types := range resp {
    81  		if insType != "" && !constraints(types.AvailableInstanceTypes.InstanceTypes, insType) {
    82  			continue
    83  		}
    84  
    85  		if resType != "" && !constraints(types.AvailableResourceCreation.ResourceTypes, resType) {
    86  			continue
    87  		}
    88  
    89  		if diskType != "" && !constraints(types.AvailableDiskCategories.DiskCategories, diskType) {
    90  			continue
    91  		}
    92  		zoneTypes = append(zoneTypes, types)
    93  	}
    94  
    95  	if len(zoneTypes) < 1 {
    96  		return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.")
    97  	}
    98  
    99  	log.Printf("[DEBUG] alicloud_zones - Zones found: %#v", zoneTypes)
   100  	return zonesDescriptionAttributes(d, zoneTypes)
   101  }
   102  
   103  // check array constraints str
   104  func constraints(arr interface{}, v string) bool {
   105  	arrs := reflect.ValueOf(arr)
   106  	len := arrs.Len()
   107  	for i := 0; i < len; i++ {
   108  		if arrs.Index(i).String() == v {
   109  			return true
   110  		}
   111  	}
   112  	return false
   113  }
   114  
   115  func zonesDescriptionAttributes(d *schema.ResourceData, types []ecs.ZoneType) error {
   116  	var ids []string
   117  	var s []map[string]interface{}
   118  	for _, t := range types {
   119  		mapping := map[string]interface{}{
   120  			"id":                          t.ZoneId,
   121  			"local_name":                  t.LocalName,
   122  			"available_instance_types":    t.AvailableInstanceTypes.InstanceTypes,
   123  			"available_resource_creation": t.AvailableResourceCreation.ResourceTypes,
   124  			"available_disk_categories":   t.AvailableDiskCategories.DiskCategories,
   125  		}
   126  
   127  		log.Printf("[DEBUG] alicloud_zones - adding zone mapping: %v", mapping)
   128  		ids = append(ids, t.ZoneId)
   129  		s = append(s, mapping)
   130  	}
   131  
   132  	d.SetId(dataResourceIdHash(ids))
   133  	if err := d.Set("zones", s); err != nil {
   134  		return err
   135  	}
   136  	return nil
   137  }