github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/ovh/data_source_ovh_publiccloud_regions.go (about)

     1  package ovh
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  func dataSourcePublicCloudRegions() *schema.Resource {
    11  	return &schema.Resource{
    12  		Read: dataSourcePublicCloudRegionsRead,
    13  		Schema: map[string]*schema.Schema{
    14  			"project_id": &schema.Schema{
    15  				Type:        schema.TypeString,
    16  				Required:    true,
    17  				ForceNew:    true,
    18  				DefaultFunc: schema.EnvDefaultFunc("OVH_PROJECT_ID", nil),
    19  			},
    20  			"names": &schema.Schema{
    21  				Type:     schema.TypeSet,
    22  				Computed: true,
    23  				Elem:     &schema.Schema{Type: schema.TypeString},
    24  				Set:      schema.HashString,
    25  			},
    26  		},
    27  	}
    28  }
    29  
    30  func dataSourcePublicCloudRegionsRead(d *schema.ResourceData, meta interface{}) error {
    31  	config := meta.(*Config)
    32  	projectId := d.Get("project_id").(string)
    33  
    34  	log.Printf("[DEBUG] Will read public cloud regions for project: %s", projectId)
    35  	d.Partial(true)
    36  
    37  	endpoint := fmt.Sprintf("/cloud/project/%s/region", projectId)
    38  	names := make([]string, 0)
    39  	err := config.OVHClient.Get(endpoint, &names)
    40  
    41  	if err != nil {
    42  		return fmt.Errorf("Error calling %s:\n\t %q", endpoint, err)
    43  	}
    44  
    45  	d.Set("names", names)
    46  	d.Partial(false)
    47  	d.SetId(projectId)
    48  
    49  	log.Printf("[DEBUG] Read Public Cloud Regions %s", names)
    50  	return nil
    51  }