-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
33 changed files
with
716 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package profitbricks | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/profitbricks/profitbricks-sdk-go" | ||
"log" | ||
"strings" | ||
) | ||
|
||
func dataSourceDataCenter() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceDataCenterRead, | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"location": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceDataCenterRead(d *schema.ResourceData, meta interface{}) error { | ||
datacenters := profitbricks.ListDatacenters() | ||
|
||
if datacenters.StatusCode > 299 { | ||
return fmt.Errorf("An error occured while fetching datacenters %s", datacenters.Response) | ||
} | ||
|
||
name := d.Get("name").(string) | ||
location, locationOk := d.GetOk("location") | ||
|
||
results := []profitbricks.Datacenter{} | ||
|
||
for _, dc := range datacenters.Items { | ||
if dc.Properties.Name == name || strings.Contains(dc.Properties.Name, name) { | ||
results = append(results, dc) | ||
} | ||
} | ||
|
||
if locationOk { | ||
log.Printf("[INFO] searching dcs by location***********") | ||
locationResults := []profitbricks.Datacenter{} | ||
for _, dc := range results { | ||
if dc.Properties.Location == location.(string) { | ||
locationResults = append(locationResults, dc) | ||
} | ||
} | ||
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 datacenters that match the search criteria") | ||
} | ||
|
||
if len(results) == 0 { | ||
return fmt.Errorf("There are no datacenters that match the search criteria") | ||
} | ||
|
||
d.SetId(results[0].Id) | ||
|
||
return nil | ||
} |
48 changes: 48 additions & 0 deletions
48
builtin/providers/profitbricks/data_source_datacenter_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package profitbricks | ||
|
||
import ( | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"testing" | ||
) | ||
|
||
func TestAccDataSourceDatacenter_matching(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheck(t) | ||
}, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
|
||
Config: testAccDataSourceProfitBricksDataCenter_matching, | ||
}, | ||
{ | ||
|
||
Config: testAccDataSourceProfitBricksDataCenter_matchingWithDataSource, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.profitbricks_datacenter.foobar", "name", "test_name"), | ||
resource.TestCheckResourceAttr("data.profitbricks_datacenter.foobar", "location", "us/las"), | ||
), | ||
}, | ||
}, | ||
}) | ||
|
||
} | ||
|
||
const testAccDataSourceProfitBricksDataCenter_matching = ` | ||
resource "profitbricks_datacenter" "foobar" { | ||
name = "test_name" | ||
location = "us/las" | ||
} | ||
` | ||
|
||
const testAccDataSourceProfitBricksDataCenter_matchingWithDataSource = ` | ||
resource "profitbricks_datacenter" "foobar" { | ||
name = "test_name" | ||
location = "us/las" | ||
} | ||
data "profitbricks_datacenter" "foobar" { | ||
name = "${profitbricks_datacenter.foobar.name}" | ||
location = "us/las" | ||
}` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package profitbricks | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/profitbricks/profitbricks-sdk-go" | ||
"strings" | ||
) | ||
|
||
func dataSourceImage() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceImageRead, | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"type": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"location": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"version": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceImageRead(d *schema.ResourceData, meta interface{}) error { | ||
profitbricks.SetDepth("5") | ||
|
||
images := profitbricks.ListImages() | ||
|
||
if images.StatusCode > 299 { | ||
return fmt.Errorf("An error occured while fetching ProfitBricks locations %s", images.Response) | ||
} | ||
|
||
name := d.Get("name").(string) | ||
imageType, imageTypeOk := d.GetOk("type") | ||
location, locationOk := d.GetOk("location") | ||
version, versionOk := d.GetOk("version") | ||
|
||
results := []profitbricks.Image{} | ||
|
||
// if version value is present then concatenate name - version | ||
// otherwise search by name or part of the name | ||
if versionOk { | ||
name_ver := fmt.Sprintf("%s-%s", name, version.(string)) | ||
for _, img := range images.Items { | ||
if strings.Contains(strings.ToLower(img.Properties.Name), strings.ToLower(name_ver)) { | ||
results = append(results, img) | ||
} | ||
} | ||
} else { | ||
for _, img := range images.Items { | ||
if strings.Contains(strings.ToLower(img.Properties.Name), strings.ToLower(name)) { | ||
results = append(results, img) | ||
} | ||
} | ||
} | ||
|
||
if imageTypeOk { | ||
imageTypeResults := []profitbricks.Image{} | ||
for _, img := range results { | ||
if img.Properties.ImageType == imageType.(string) { | ||
imageTypeResults = append(imageTypeResults, img) | ||
} | ||
|
||
} | ||
results = imageTypeResults | ||
} | ||
|
||
if locationOk { | ||
locationResults := []profitbricks.Image{} | ||
for _, img := range results { | ||
if img.Properties.Location == location.(string) { | ||
locationResults = append(locationResults, img) | ||
} | ||
|
||
} | ||
results = locationResults | ||
} | ||
|
||
if len(results) > 1 { | ||
return fmt.Errorf("There is more than one image that match the search criteria") | ||
} | ||
|
||
if len(results) == 0 { | ||
return fmt.Errorf("There are no images that match the search criteria") | ||
} | ||
|
||
d.Set("name", results[0].Properties.Name) | ||
|
||
d.SetId(results[0].Id) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package profitbricks | ||
|
||
import ( | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"testing" | ||
) | ||
|
||
func TestAccDataSourceImage_basic(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheck(t) | ||
}, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
|
||
Config: testAccDataSourceProfitBricksImage_basic, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.profitbricks_image.img", "location", "us/las"), | ||
resource.TestCheckResourceAttr("data.profitbricks_image.img", "name", "Ubuntu-16.04-LTS-server-2017-02-01"), | ||
resource.TestCheckResourceAttr("data.profitbricks_image.img", "type", "HDD"), | ||
), | ||
}, | ||
}, | ||
}) | ||
|
||
} | ||
|
||
const testAccDataSourceProfitBricksImage_basic = ` | ||
data "profitbricks_image" "img" { | ||
name = "Ubuntu" | ||
type = "HDD" | ||
version = "16" | ||
location = "us/las" | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
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 | ||
} |
32 changes: 32 additions & 0 deletions
32
builtin/providers/profitbricks/data_source_location_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package profitbricks | ||
|
||
import ( | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"testing" | ||
) | ||
|
||
func TestAccDataSourceLocation_basic(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheck(t) | ||
}, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
|
||
Config: testAccDataSourceProfitBricksLocation_basic, | ||
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttr("data.profitbricks_location.loc", "id", "de/fkb"), | ||
resource.TestCheckResourceAttr("data.profitbricks_location.loc", "name", "karlsruhe"), | ||
), | ||
}, | ||
}, | ||
}) | ||
|
||
} | ||
|
||
const testAccDataSourceProfitBricksLocation_basic = ` | ||
data "profitbricks_location" "loc" { | ||
name = "karlsruhe" | ||
feature = "SSD" | ||
} | ||
` |
Oops, something went wrong.