Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SCALRCORE-24694 Add data.scalr_environment_ids, add tags filter to da…
…ta.scalr_workspace_ids
- Loading branch information
1 parent
2cf4f32
commit 5f81b4f
Showing
10 changed files
with
381 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
# Data Source `scalr_environment_ids` | ||
|
||
Retrieves a map of environment IDs based on the names or/and tags provided. Wildcards are accepted. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "scalr_environment_ids" "exact-names" { | ||
names = ["production", "development"] | ||
} | ||
data "scalr_environment_ids" "all" { | ||
names = ["*"] | ||
} | ||
data "scalr_environment_ids" "partial-match" { | ||
names = ["dev-"] | ||
exact_match = false | ||
} | ||
data "scalr_environment_ids" "tagged" { | ||
tag_ids = ["tag-xxxxxxxxxxx", "tag-yyyyyyyyyyy"] | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
* `account_id` - (Optional) ID of the account, in the format `acc-<RANDOM STRING>`. | ||
* `names` - (Optional) A list of names to search for. If a name does not exist, it will not throw an error, it will just not exist in the returned output. Use `["*"]` to select all environments. | ||
* `tag_ids` - (Optional) List of tag IDs associated with the environment. | ||
* `exact_match` - (Optional) If `true`, performs exact match on environment names, otherwise will match names that contain given values. Defaults to `true`. | ||
|
||
## Attribute Reference | ||
|
||
All arguments plus: | ||
|
||
* `ids` - A map of environment names and their opaque IDs, in the format `acc_id/name`. |
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
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,100 @@ | ||
package scalr | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/scalr/go-scalr" | ||
"strings" | ||
) | ||
|
||
func dataSourceScalrEnvironmentIDs() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceScalrEnvironmentIDsRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"account_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
DefaultFunc: scalrAccountIDDefaultFunc, | ||
}, | ||
"names": { | ||
Type: schema.TypeList, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Optional: true, | ||
AtLeastOneOf: []string{"tag_ids"}, | ||
}, | ||
"tag_ids": { | ||
Type: schema.TypeSet, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Optional: true, | ||
}, | ||
"exact_match": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: true, | ||
}, | ||
"ids": { | ||
Type: schema.TypeMap, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceScalrEnvironmentIDsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
scalrClient := meta.(*scalr.Client) | ||
accountId := d.Get("account_id").(string) | ||
exact := d.Get("exact_match").(bool) | ||
var id string | ||
ids := make(map[string]string, 0) | ||
options := scalr.EnvironmentListOptions{Account: &accountId} | ||
|
||
names := make(map[string]bool) | ||
if namesI, ok := d.GetOk("names"); ok { | ||
for _, name := range namesI.([]interface{}) { | ||
id += name.(string) | ||
names[name.(string)] = true | ||
} | ||
} | ||
|
||
if tagIDsI, ok := d.GetOk("tag_ids"); ok { | ||
tagIDs := make([]string, 0) | ||
for _, t := range tagIDsI.(*schema.Set).List() { | ||
id += t.(string) | ||
tagIDs = append(tagIDs, t.(string)) | ||
} | ||
if len(tagIDs) > 0 { | ||
options.Tag = scalr.String("in:" + strings.Join(tagIDs, ",")) | ||
} | ||
} | ||
|
||
for { | ||
el, err := scalrClient.Environments.List(ctx, options) | ||
if err != nil { | ||
return diag.Errorf("Error retrieving environments: %v", err) | ||
} | ||
|
||
for _, e := range el.Items { | ||
if len(names) > 0 { | ||
if names["*"] || (exact && names[e.Name]) || (!exact && matchesPattern(e.Name, names)) { | ||
ids[e.Name] = e.ID | ||
} | ||
} else { | ||
ids[e.Name] = e.ID | ||
} | ||
} | ||
|
||
if el.CurrentPage >= el.TotalPages { | ||
break | ||
} | ||
options.PageNumber = el.NextPage | ||
} | ||
|
||
_ = d.Set("ids", ids) | ||
d.SetId(fmt.Sprintf("%s/%d", accountId, schema.HashString(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,117 @@ | ||
package scalr | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccScalrEnvironmentIDsDataSource_basic(t *testing.T) { | ||
rInt := GetRandomInteger() | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProviderFactories: testAccProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccScalrEnvironmentIDsDataSourceConfigBasic(rInt), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
"data.scalr_environment_ids.foobar", "names.#", "2"), | ||
resource.TestCheckResourceAttr( | ||
"data.scalr_environment_ids.foobar", "names.0", fmt.Sprintf("env-foo-%d", rInt)), | ||
resource.TestCheckResourceAttr( | ||
"data.scalr_environment_ids.foobar", "names.1", fmt.Sprintf("env-bar-%d", rInt)), | ||
resource.TestCheckResourceAttr( | ||
"data.scalr_environment_ids.foobar", "ids.%", "2"), | ||
resource.TestCheckResourceAttrSet( | ||
"data.scalr_environment_ids.foobar", fmt.Sprintf("ids.env-foo-%d", rInt)), | ||
resource.TestCheckResourceAttrSet( | ||
"data.scalr_environment_ids.foobar", fmt.Sprintf("ids.env-bar-%d", rInt)), | ||
resource.TestCheckResourceAttrSet("data.scalr_environment_ids.foobar", "id"), | ||
resource.TestCheckResourceAttr("data.scalr_environment_ids.tagged", "ids.%", "2"), | ||
resource.TestCheckResourceAttrSet( | ||
"data.scalr_environment_ids.tagged", fmt.Sprintf("ids.foobar-tagged-%d", rInt)), | ||
resource.TestCheckResourceAttrSet( | ||
"data.scalr_environment_ids.tagged", fmt.Sprintf("ids.barbaz-tagged-%d", rInt)), | ||
resource.TestCheckResourceAttr("data.scalr_environment_ids.partial", "ids.%", "4"), | ||
resource.TestCheckResourceAttrSet( | ||
"data.scalr_environment_ids.partial", fmt.Sprintf("ids.env-foo-%d", rInt)), | ||
resource.TestCheckResourceAttrSet( | ||
"data.scalr_environment_ids.partial", fmt.Sprintf("ids.env-bar-%d", rInt)), | ||
resource.TestCheckResourceAttrSet( | ||
"data.scalr_environment_ids.partial", fmt.Sprintf("ids.foobar-tagged-%d", rInt)), | ||
resource.TestCheckResourceAttrSet( | ||
"data.scalr_environment_ids.partial", fmt.Sprintf("ids.barbaz-tagged-%d", rInt)), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccScalrEnvironmentIDsDataSourceConfigBasic(rInt int) string { | ||
return fmt.Sprintf(` | ||
resource scalr_tag foo { | ||
name = "foo" | ||
} | ||
resource scalr_tag bar { | ||
name = "bar" | ||
} | ||
resource scalr_tag baz { | ||
name = "baz" | ||
} | ||
resource scalr_environment foo { | ||
name = "env-foo-%d" | ||
} | ||
resource scalr_environment bar { | ||
name = "env-bar-%[1]d" | ||
} | ||
resource scalr_environment dummy { | ||
name = "env-dummy-%[1]d" | ||
} | ||
resource scalr_environment foobar-tagged { | ||
name = "foobar-tagged-%[1]d" | ||
tag_ids = [scalr_tag.foo.id, scalr_tag.bar.id] | ||
} | ||
resource scalr_environment barbaz-tagged { | ||
name = "barbaz-tagged-%[1]d" | ||
tag_ids = [scalr_tag.bar.id, scalr_tag.baz.id] | ||
} | ||
resource scalr_environment baz-tagged { | ||
name = "baz-tagged-%[1]d" | ||
tag_ids = [scalr_tag.baz.id] | ||
} | ||
data scalr_environment_ids foobar { | ||
names = [scalr_environment.foo.name, scalr_environment.bar.name] | ||
} | ||
data scalr_environment_ids tagged { | ||
tag_ids = [scalr_tag.foo.id, scalr_tag.bar.id] | ||
depends_on = [ | ||
scalr_environment.foobar-tagged, | ||
scalr_environment.barbaz-tagged, | ||
scalr_environment.baz-tagged, | ||
] | ||
} | ||
data scalr_environment_ids partial { | ||
names = ["foo", "bar"] | ||
exact_match = false | ||
depends_on = [ | ||
scalr_environment.foo, | ||
scalr_environment.bar, | ||
scalr_environment.foobar-tagged, | ||
scalr_environment.barbaz-tagged, | ||
] | ||
}`, rInt) | ||
} |
Oops, something went wrong.