Skip to content

Commit

Permalink
SCALRCORE-24694 Add data.scalr_environment_ids, add tags filter to da…
Browse files Browse the repository at this point in the history
…ta.scalr_workspace_ids
  • Loading branch information
petroprotsakh committed Mar 22, 2023
1 parent 2cf4f32 commit 5f81b4f
Show file tree
Hide file tree
Showing 10 changed files with 381 additions and 20 deletions.
38 changes: 38 additions & 0 deletions docs/data-sources/scalr_environment_ids.md
@@ -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`.
17 changes: 15 additions & 2 deletions docs/data-sources/scalr_workspace_ids.md
@@ -1,7 +1,7 @@

# Data Source `scalr_workspace_ids`

Retrieves a map of workspace IDs based on the names provided. Wildcards are accepted.
Retrieves a map of workspace IDs based on the names or/and tags provided. Wildcards are accepted.

## Example Usage

Expand All @@ -15,12 +15,25 @@ data "scalr_workspace_ids" "all" {
names = ["*"]
environment_id = "env-xxxxxxxxxxx"
}
data "scalr_workspace_ids" "partial-match" {
names = ["dev-"]
exact_match = false
environment_id = "env-xxxxxxxxxxx"
}
data "scalr_workspace_ids" "tagged" {
tag_ids = ["tag-xxxxxxxxxxx", "tag-yyyyyyyyyyy"]
environment_id = "env-xxxxxxxxxxx"
}
```

## Argument Reference

* `names` - (Required) * 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 workspaces.
* `environment_id` - (Required) ID of the environment, in the format `env-<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 workspaces.
* `tag_ids` - (Optional) List of tag IDs associated with the workspace.
* `exact_match` - (Optional) If `true`, performs exact match on workspace names, otherwise will match names that contain given values. Defaults to `true`.

## Attribute Reference

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -5,7 +5,7 @@ require (
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734
github.com/scalr/go-scalr v0.0.0-20230113121456-acdac16a6fc8
github.com/scalr/go-scalr v0.0.0-20230321094703-993d02055620
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -242,8 +242,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/scalr/go-scalr v0.0.0-20230113121456-acdac16a6fc8 h1:qcqhjq3rVK/NBLdB/TboYWCqmAUvmcoiDn65BqLHi/c=
github.com/scalr/go-scalr v0.0.0-20230113121456-acdac16a6fc8/go.mod h1:p34SHb25YRvbgft7SUjSDYESeoQhWzAlxGXId/BbaSE=
github.com/scalr/go-scalr v0.0.0-20230321094703-993d02055620 h1:vLMsp17xK8aC2zR3EQmz8sjU51p0oaqlYW8gs51tJPw=
github.com/scalr/go-scalr v0.0.0-20230321094703-993d02055620/go.mod h1:p34SHb25YRvbgft7SUjSDYESeoQhWzAlxGXId/BbaSE=
github.com/sebdah/goldie v1.0.0/go.mod h1:jXP4hmWywNEwZzhMuv2ccnqTSFpuq8iyQhtQdkkZBH4=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
Expand Down
100 changes: 100 additions & 0 deletions scalr/data_source_scalr_environment_ids.go
@@ -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
}
117 changes: 117 additions & 0 deletions scalr/data_source_scalr_environment_ids_test.go
@@ -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)
}

0 comments on commit 5f81b4f

Please sign in to comment.