Skip to content

Commit

Permalink
Merge branch 'develop' into fix/SCALRCORE-25049
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanMytsko committed May 2, 2023
2 parents 6a99c4d + f4c08af commit e2aa4d3
Show file tree
Hide file tree
Showing 14 changed files with 487 additions and 37 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- **New data source:** `scalr_environments` ([#225](https://github.com/Scalr/terraform-provider-scalr/pull/225))
- **New data source:** `scalr_workspaces` ([#225](https://github.com/Scalr/terraform-provider-scalr/pull/225))

### Changed

- `scalr_vcs_provider`: added new attribute `agent_pool_id` ([#233](https://github.com/Scalr/terraform-provider-scalr/pull/233))
Expand All @@ -22,7 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Required

- scalr-server >= `8.64.0`
- scalr-server >= `8.65.0`

## [1.0.5] - 2023-04-21

Expand Down
36 changes: 36 additions & 0 deletions docs/data-sources/scalr_environments.md
@@ -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`].
38 changes: 38 additions & 0 deletions docs/data-sources/scalr_workspaces.md
@@ -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`].
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-20230424091123-5437ed6e096e
github.com/scalr/go-scalr v0.0.0-20230501083407-653e7b8c404c
)

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-20230424091123-5437ed6e096e h1:dnFu7ozDG1exX+KnYqtfStR/nYobB2k1OhqT+jIEVgg=
github.com/scalr/go-scalr v0.0.0-20230424091123-5437ed6e096e/go.mod h1:p34SHb25YRvbgft7SUjSDYESeoQhWzAlxGXId/BbaSE=
github.com/scalr/go-scalr v0.0.0-20230501083407-653e7b8c404c h1:k5DjiMHp5sK+q/iGsaGgvdthe4MmDYx7VdzJtZj4lIo=
github.com/scalr/go-scalr v0.0.0-20230501083407-653e7b8c404c/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
90 changes: 90 additions & 0 deletions scalr/data_source_scalr_environments.go
@@ -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
}
94 changes: 94 additions & 0 deletions scalr/data_source_scalr_environments_test.go
@@ -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)
}
8 changes: 4 additions & 4 deletions scalr/data_source_scalr_workspace.go
Expand Up @@ -184,16 +184,16 @@ func dataSourceScalrWorkspaceRead(ctx context.Context, d *schema.ResourceData, m
environmentID := d.Get("environment_id").(string)

options := scalr.WorkspaceListOptions{
Environment: scalr.String(environmentID),
Include: "created-by",
Include: "created-by",
Filter: &scalr.WorkspaceFilter{Environment: scalr.String(environmentID)},
}

if workspaceID != "" {
options.Workspace = scalr.String(workspaceID)
options.Filter.Id = scalr.String(workspaceID)
}

if name != "" {
options.Name = scalr.String(name)
options.Filter.Name = scalr.String(name)
}

log.Printf("[DEBUG] Read configuration of workspace with ID '%s', name '%s', and environment_id '%s'", workspaceID, name, environmentID)
Expand Down
4 changes: 3 additions & 1 deletion scalr/data_source_scalr_workspace_ids.go
Expand Up @@ -50,7 +50,9 @@ func dataSourceScalrWorkspaceIDsRead(ctx context.Context, d *schema.ResourceData
// Create a map to store workspace IDs
ids := make(map[string]string, len(names))

options := scalr.WorkspaceListOptions{Environment: &environmentID}
options := scalr.WorkspaceListOptions{
Filter: &scalr.WorkspaceFilter{Environment: &environmentID},
}
for {
wl, err := scalrClient.Workspaces.List(ctx, options)
if err != nil {
Expand Down

0 comments on commit e2aa4d3

Please sign in to comment.