Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into fix/SCALRCORE-25049
- Loading branch information
Showing
14 changed files
with
487 additions
and
37 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,36 @@ | ||
|
||
# Data Source `scalr_environments` | ||
|
||
Retrieves a list of environment ids by name or tags. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "scalr_environments" "exact-names" { | ||
name = "in:production,development" | ||
} | ||
data "scalr_environments" "app-frontend" { | ||
name = "like:app-frontend-" | ||
} | ||
data "scalr_environments" "tagged" { | ||
tag_ids = ["tag-xxxxxxxxxxx", "tag-yyyyyyyyyyy"] | ||
} | ||
data "scalr_environments" "all" { | ||
account_id = "acc-xxxxxxxxxxx" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
* `account_id` - (Optional) ID of the account, in the format `acc-<RANDOM STRING>`. | ||
* `name` - (Optional) The query used in a Scalr environment name filter. | ||
* `tag_ids` - (Optional) List of tag IDs associated with the environment. | ||
|
||
## Attribute Reference | ||
|
||
All arguments plus: | ||
|
||
* `ids` - The list of environment IDs, in the format [`env-xxxxxxxxxxx`, `env-yyyyyyyyy`]. |
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_workspaces` | ||
|
||
Retrieves a list of workspace ids by name or tags. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "scalr_workspaces" "exact-names" { | ||
name = "in:production,development" | ||
} | ||
data "scalr_workspaces" "app-frontend" { | ||
name = "like:app-frontend-" | ||
environment_id = "env-xxxxxxxxxxx" | ||
} | ||
data "scalr_workspaces" "tagged" { | ||
tag_ids = ["tag-xxxxxxxxxxx", "tag-yyyyyyyyyyy"] | ||
} | ||
data "scalr_workspaces" "all" { | ||
environment_id = "env-xxxxxxxxxxx" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
* `account_id` - (Optional) ID of the account, in the format `acc-<RANDOM STRING>`. | ||
* `environment_id` - (Optional) ID of the environment, in the format `env-<RANDOM STRING>`. | ||
* `name` - (Optional) The query used in a Scalr workspace name filter. | ||
* `tag_ids` - (Optional) List of tag IDs associated with the workspace. | ||
|
||
## Attribute Reference | ||
|
||
All arguments plus: | ||
|
||
* `ids` - The list of workspace IDs, in the format [`ws-xxxxxxxxxxx`, `ws-yyyyyyyyy`]. |
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,90 @@ | ||
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 dataSourceScalrEnvironments() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceScalrEnvironmentsRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"account_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
DefaultFunc: scalrAccountIDDefaultFunc, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"tag_ids": { | ||
Type: schema.TypeSet, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Optional: true, | ||
}, | ||
"ids": { | ||
Type: schema.TypeList, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceScalrEnvironmentsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
scalrClient := meta.(*scalr.Client) | ||
accountId := d.Get("account_id").(string) | ||
|
||
options := scalr.EnvironmentListOptions{ | ||
Filter: &scalr.EnvironmentFilter{Account: &accountId}, | ||
} | ||
|
||
id := strings.Builder{} // holds the string to build a unique resource id hash | ||
id.WriteString(accountId) | ||
|
||
ids := make([]string, 0) | ||
|
||
if name, ok := d.GetOk("name"); ok { | ||
id.WriteString(name.(string)) | ||
options.Filter.Name = scalr.String(name.(string)) | ||
} | ||
|
||
if tagIDsI, ok := d.GetOk("tag_ids"); ok { | ||
tagIDs := make([]string, 0) | ||
for _, t := range tagIDsI.(*schema.Set).List() { | ||
id.WriteString(t.(string)) | ||
tagIDs = append(tagIDs, t.(string)) | ||
} | ||
if len(tagIDs) > 0 { | ||
options.Filter.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 { | ||
ids = append(ids, e.ID) | ||
} | ||
|
||
if el.CurrentPage >= el.TotalPages { | ||
break | ||
} | ||
options.PageNumber = el.NextPage | ||
} | ||
|
||
_ = d.Set("ids", ids) | ||
d.SetId(fmt.Sprintf("%d", schema.HashString(id.String()))) | ||
|
||
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,94 @@ | ||
package scalr | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccScalrEnvironmentsDataSource_basic(t *testing.T) { | ||
rInt := GetRandomInteger() | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProviderFactories: testAccProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccScalrEnvironmentsDataSourceConfigBasic(rInt), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
"data.scalr_environments.foobar", "ids.#", "2"), | ||
resource.TestCheckResourceAttrSet("data.scalr_environments.foobar", "id"), | ||
resource.TestCheckResourceAttr("data.scalr_environments.tagged", "ids.#", "2"), | ||
resource.TestCheckResourceAttr("data.scalr_environments.partial", "ids.#", "3"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccScalrEnvironmentsDataSourceConfigBasic(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_environments foobar { | ||
name = "in:${scalr_environment.foo.name},${scalr_environment.bar.name}" | ||
} | ||
data scalr_environments 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_environments partial { | ||
name = "like:bar" | ||
depends_on = [ | ||
scalr_environment.foo, | ||
scalr_environment.bar, | ||
scalr_environment.foobar-tagged, | ||
scalr_environment.barbaz-tagged, | ||
] | ||
}`, rInt) | ||
} |
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
Oops, something went wrong.