github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/ovh/data_source_ovh_publiccloud_region.go (about) 1 package ovh 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/hashicorp/terraform/helper/hashcode" 8 "github.com/hashicorp/terraform/helper/schema" 9 ) 10 11 func dataSourcePublicCloudRegion() *schema.Resource { 12 return &schema.Resource{ 13 Read: dataSourcePublicCloudRegionRead, 14 Schema: map[string]*schema.Schema{ 15 "project_id": &schema.Schema{ 16 Type: schema.TypeString, 17 Required: true, 18 ForceNew: true, 19 DefaultFunc: schema.EnvDefaultFunc("OVH_PROJECT_ID", nil), 20 }, 21 "name": &schema.Schema{ 22 Type: schema.TypeString, 23 Required: true, 24 ForceNew: true, 25 }, 26 "services": &schema.Schema{ 27 Type: schema.TypeSet, 28 Set: publicCloudServiceHash, 29 Computed: true, 30 Elem: &schema.Resource{ 31 Schema: map[string]*schema.Schema{ 32 "name": &schema.Schema{ 33 Type: schema.TypeString, 34 Computed: true, 35 }, 36 37 "status": &schema.Schema{ 38 Type: schema.TypeString, 39 Computed: true, 40 }, 41 }, 42 }, 43 }, 44 45 "continentCode": &schema.Schema{ 46 Type: schema.TypeString, 47 Computed: true, 48 }, 49 50 "datacenterLocation": &schema.Schema{ 51 Type: schema.TypeString, 52 Computed: true, 53 }, 54 }, 55 } 56 } 57 58 func dataSourcePublicCloudRegionRead(d *schema.ResourceData, meta interface{}) error { 59 config := meta.(*Config) 60 projectId := d.Get("project_id").(string) 61 name := d.Get("name").(string) 62 63 log.Printf("[DEBUG] Will read public cloud region %s for project: %s", name, projectId) 64 d.Partial(true) 65 66 response := &PublicCloudRegionResponse{} 67 endpoint := fmt.Sprintf("/cloud/project/%s/region/%s", projectId, name) 68 err := config.OVHClient.Get(endpoint, response) 69 70 if err != nil { 71 return fmt.Errorf("Error calling %s:\n\t %q", endpoint, err) 72 } 73 74 d.Set("datacenterLocation", response.DatacenterLocation) 75 d.Set("continentCode", response.ContinentCode) 76 77 services := &schema.Set{ 78 F: publicCloudServiceHash, 79 } 80 for i := range response.Services { 81 service := map[string]interface{}{ 82 "name": response.Services[i].Name, 83 "status": response.Services[i].Status, 84 } 85 services.Add(service) 86 } 87 88 d.Set("services", services) 89 90 d.Partial(false) 91 d.SetId(fmt.Sprintf("%s_%s", projectId, name)) 92 93 return nil 94 } 95 96 func publicCloudServiceHash(v interface{}) int { 97 r := v.(map[string]interface{}) 98 return hashcode.String(r["name"].(string)) 99 }