opentofu/builtin/providers/profitbricks/data_source_location.go
Jasmin Gacic b862cd2ccb Terraform provider ProfitBricks - Data Sources (#11520)
* Terraform ProfitBricks Builder

* make fmt

* Merge remote-tracking branch 'upstream/master' into terraform-provider-profitbricks

# Conflicts:
#	command/internal_plugin_list.go

* Addressing PR remarks

* Removed importers

* Added ProfitBricks Data Sources

* Added documentation

* Updated to REST v3:
- nat parameter for Nics
- availabilityZone for Volumes

Minor code clean up

* Minor code clean up

* Fixed typo in volume documentation

* make fmt

* Addressing requested changes

* Added a step in load balancer tests in CheckDestroy where we are making sure that the test doesn't leave dangling resources in ProfitBricks

* Changed expected image name

* Fixed data center test
Code clean up
2017-02-02 13:26:14 +00:00

74 lines
1.7 KiB
Go

package profitbricks
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/profitbricks/profitbricks-sdk-go"
"log"
"strings"
)
func dataSourceLocation() *schema.Resource {
return &schema.Resource{
Read: dataSourceLocationRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
},
"feature": {
Type: schema.TypeString,
Optional: true,
},
},
}
}
func dataSourceLocationRead(d *schema.ResourceData, meta interface{}) error {
locations := profitbricks.ListLocations()
if locations.StatusCode > 299 {
return fmt.Errorf("An error occured while fetching ProfitBricks locations %s", locations.Response)
}
name, nameOk := d.GetOk("name")
feature, featureOk := d.GetOk("features")
if !nameOk && !featureOk {
return fmt.Errorf("Either 'name' or 'feature' must be provided.")
}
results := []profitbricks.Location{}
for _, loc := range locations.Items {
if loc.Properties.Name == name.(string) || strings.Contains(loc.Properties.Name, name.(string)) {
results = append(results, loc)
}
}
if featureOk {
locationResults := []profitbricks.Location{}
for _, loc := range results {
for _, f := range loc.Properties.Features {
if f == feature.(string) {
locationResults = append(locationResults, loc)
}
}
}
results = locationResults
}
log.Printf("[INFO] Results length %d *************", len(results))
if len(results) > 1 {
log.Printf("[INFO] Results length greater than 1")
return fmt.Errorf("There is more than one location that match the search criteria")
}
if len(results) == 0 {
return fmt.Errorf("There are no locations that match the search criteria")
}
d.SetId(results[0].Id)
return nil
}