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

     1  package alicloud
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/denverdino/aliyungo/common"
     6  	"github.com/denverdino/aliyungo/ecs"
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	"log"
     9  )
    10  
    11  func dataSourceAlicloudRegions() *schema.Resource {
    12  	return &schema.Resource{
    13  		Read: dataSourceAlicloudRegionsRead,
    14  
    15  		Schema: map[string]*schema.Schema{
    16  			"name": &schema.Schema{
    17  				Type:     schema.TypeString,
    18  				Optional: true,
    19  				Computed: true,
    20  			},
    21  
    22  			"current": &schema.Schema{
    23  				Type:     schema.TypeBool,
    24  				Optional: true,
    25  				Computed: true,
    26  			},
    27  
    28  			//Computed value
    29  			"regions": &schema.Schema{
    30  				Type:     schema.TypeList,
    31  				Computed: true,
    32  				Elem: &schema.Resource{
    33  					Schema: map[string]*schema.Schema{
    34  						"id": {
    35  							Type:     schema.TypeString,
    36  							Computed: true,
    37  						},
    38  						"region_id": {
    39  							Type:     schema.TypeString,
    40  							Computed: true,
    41  						},
    42  						"local_name": {
    43  							Type:     schema.TypeString,
    44  							Computed: true,
    45  						},
    46  					},
    47  				},
    48  			},
    49  		},
    50  	}
    51  }
    52  
    53  func dataSourceAlicloudRegionsRead(d *schema.ResourceData, meta interface{}) error {
    54  	conn := meta.(*AliyunClient).ecsconn
    55  	currentRegion := getRegion(d, meta)
    56  
    57  	resp, err := conn.DescribeRegions()
    58  	if err != nil {
    59  		return err
    60  	}
    61  	if resp == nil || len(resp) == 0 {
    62  		return fmt.Errorf("no matching regions found")
    63  	}
    64  	name, nameOk := d.GetOk("name")
    65  	current := d.Get("current").(bool)
    66  	var filterRegions []ecs.RegionType
    67  	for _, region := range resp {
    68  		if current {
    69  			if nameOk && common.Region(name.(string)) != currentRegion {
    70  				return fmt.Errorf("name doesn't match current region: %#v, please input again.", currentRegion)
    71  			}
    72  			if region.RegionId == currentRegion {
    73  				filterRegions = append(filterRegions, region)
    74  				break
    75  			}
    76  			continue
    77  		}
    78  		if nameOk {
    79  			if common.Region(name.(string)) == region.RegionId {
    80  				filterRegions = append(filterRegions, region)
    81  				break
    82  			}
    83  			continue
    84  		}
    85  		filterRegions = append(filterRegions, region)
    86  	}
    87  	if len(filterRegions) < 1 {
    88  		return fmt.Errorf("Your query region returned no results. Please change your search criteria and try again.")
    89  	}
    90  
    91  	return regionsDescriptionAttributes(d, filterRegions)
    92  }
    93  
    94  func regionsDescriptionAttributes(d *schema.ResourceData, regions []ecs.RegionType) error {
    95  	var ids []string
    96  	var s []map[string]interface{}
    97  	for _, region := range regions {
    98  		mapping := map[string]interface{}{
    99  			"id":         region.RegionId,
   100  			"region_id":  region.RegionId,
   101  			"local_name": region.LocalName,
   102  		}
   103  
   104  		log.Printf("[DEBUG] alicloud_regions - adding region mapping: %v", mapping)
   105  		ids = append(ids, string(region.RegionId))
   106  		s = append(s, mapping)
   107  	}
   108  
   109  	d.SetId(dataResourceIdHash(ids))
   110  	if err := d.Set("regions", s); err != nil {
   111  		return err
   112  	}
   113  	return nil
   114  }