-
Notifications
You must be signed in to change notification settings - Fork 9.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Terraform provider ProfitBricks - Data Sources #11520
Merged
stack72
merged 25 commits into
hashicorp:master
from
StackPointCloud:terraform-provider-profitbricks
Feb 2, 2017
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
fa0b8fc
Terraform ProfitBricks Builder
3b907d6
Merge remote-tracking branch 'upstream/master' into terraform-provide…
940b3f6
make fmt
5d63bd9
Merge remote-tracking branch 'upstream/master' into terraform-provide…
9c82c60
Merge remote-tracking branch 'upstream/master' into terraform-provide…
5996086
Merge remote-tracking branch 'upstream/master' into terraform-provide…
ac2f78f
Merge remote-tracking branch 'upstream/master' into terraform-provide…
5a6dd69
Merge remote-tracking branch 'upstream/master' into terraform-provide…
379a6cc
Addressing PR remarks
afabb26
Merge remote-tracking branch 'upstream/master' into terraform-provide…
21831c0
Merge remote-tracking branch 'upstream/master' into terraform-provide…
cff8e3f
Removed importers
62c0b04
Merge remote-tracking branch 'upstream/master' into terraform-provide…
501e606
Merge remote-tracking branch 'upstream/master' into terraform-provide…
e1fc5e0
Added ProfitBricks Data Sources
2f6a702
Added documentation
db4afee
Merge remote-tracking branch 'upstream/master' into terraform-provide…
41b73d6
Updated to REST v3:
68e7ddb
Minor code clean up
2e3618d
Fixed typo in volume documentation
9107784
make fmt
c0910a4
Addressing requested changes
80c650e
Added a step in load balancer tests in CheckDestroy where we are maki…
1cf5f09
Changed expected image name
913f0d1
Fixed data center test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we check that Properties != nil ? Otherwise we could panic here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Properties ImageProperties `json:"properties,omitempty"
It can't be nil it is not a pointer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 sorry - still in AWS land where everything is a pointer :)