Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into fix/SCALRCORE-20057
Browse files Browse the repository at this point in the history
  • Loading branch information
vaniakov committed Sep 9, 2021
2 parents 7bcf173 + 870f2d3 commit f758d84
Show file tree
Hide file tree
Showing 22 changed files with 282 additions and 103 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Expand Up @@ -9,7 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- `scalr_environment`: fix provider crash while reading environment without proper permissions ([#82](https://github.com/Scalr/terraform-provider-scalr/pull/82))
- `scalr_environment`: fix provider crash while reading environment without proper permissions ([#82](https://github.com/Scalr/terraform-provider-scalr/pull/82))
- `scalr_environment`: fix empty strings handling in `cloud_credentials` and `policy_groups` attributes ([#81](https://github.com/Scalr/terraform-provider-scalr/pull/81))
- `scalr_webhook`: fix empty strings handling in `events` attribute ([#81](https://github.com/Scalr/terraform-provider-scalr/pull/81))
- `scalr_access_policy`: fix empty strings handling in `role_ids` attribute ([#81](https://github.com/Scalr/terraform-provider-scalr/pull/81))
- `scalr_role`: fix empty strings handling in `permissions` attribute ([#81](https://github.com/Scalr/terraform-provider-scalr/pull/81))
- `scalr_workspace`: fix empty strings handling in `vcs_repo.trigger_prefixes` attribute ([#81](https://github.com/Scalr/terraform-provider-scalr/pull/81))

## [1.0.0-rc19] - 2021-08-19

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -7,7 +7,7 @@ require (
github.com/hashicorp/terraform v0.12.0
github.com/hashicorp/terraform-svchost v0.0.0-20191011084731-65d371908596
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/scalr/go-scalr v0.0.0-20210813140334-b0bce1821414
github.com/scalr/go-scalr v0.0.0-20210902095125-9ac71df3a092
)

go 1.13
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -261,6 +261,8 @@ github.com/scalr/go-scalr v0.0.0-20210813111943-1a7743178357 h1:SLoJWMqDkRg3tsT7
github.com/scalr/go-scalr v0.0.0-20210813111943-1a7743178357/go.mod h1:n2HQ6QxqyTySiSFTOpAL18SjCKtP+xUskMygO0KuuQU=
github.com/scalr/go-scalr v0.0.0-20210813140334-b0bce1821414 h1:F3ecDrzKMrqI701ml5nlCGd4YzHDNEauR0nd632Yrf8=
github.com/scalr/go-scalr v0.0.0-20210813140334-b0bce1821414/go.mod h1:n2HQ6QxqyTySiSFTOpAL18SjCKtP+xUskMygO0KuuQU=
github.com/scalr/go-scalr v0.0.0-20210902095125-9ac71df3a092 h1:KZcw4OdOFTGXcZkyd7shvLHWbtIAbH7OmiQn6+9FxnU=
github.com/scalr/go-scalr v0.0.0-20210902095125-9ac71df3a092/go.mod h1:n2HQ6QxqyTySiSFTOpAL18SjCKtP+xUskMygO0KuuQU=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
Expand Down
4 changes: 2 additions & 2 deletions scalr/client_mock.go
Expand Up @@ -62,7 +62,7 @@ func (m *mockVariables) Read(ctx context.Context, varID string) (*scalr.Variable
v := m.ids[varID]

if v == nil {
return nil, scalr.ErrResourceNotFound
return nil, scalr.ErrResourceNotFound{}
}

return v, nil
Expand All @@ -71,7 +71,7 @@ func (m *mockVariables) Read(ctx context.Context, varID string) (*scalr.Variable
func (m *mockWorkspaces) Read(ctx context.Context, environment string, workspace string) (*scalr.Workspace, error) {
w := m.workspaceNames[workspaceNamesKey{environment, workspace}]
if w == nil {
return nil, scalr.ErrResourceNotFound
return nil, scalr.ErrResourceNotFound{}
}

return w, nil
Expand Down
5 changes: 3 additions & 2 deletions scalr/data_source_current_run.go
@@ -1,6 +1,7 @@
package scalr

import (
"errors"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -112,7 +113,7 @@ func dataSourceScalrCurrentRunRead(d *schema.ResourceData, meta interface{}) err
log.Printf("[DEBUG] Read configuration of run: %s", runID)
run, err := scalrClient.Runs.Read(ctx, runID)
if err != nil {
if err == scalr.ErrResourceNotFound {
if errors.Is(err, scalr.ErrResourceNotFound{}) {
return fmt.Errorf("Could not find run %s", runID)
}
return fmt.Errorf("Error retrieving run: %v", err)
Expand All @@ -121,7 +122,7 @@ func dataSourceScalrCurrentRunRead(d *schema.ResourceData, meta interface{}) err
log.Printf("[DEBUG] Read workspace of run: %s", runID)
workspace, err := scalrClient.Workspaces.ReadByID(ctx, run.Workspace.ID)
if err != nil {
if err == scalr.ErrResourceNotFound {
if errors.Is(err, scalr.ErrResourceNotFound{}) {
return fmt.Errorf("Could not find workspace %s", run.Workspace.ID)
}
return fmt.Errorf("Error retrieving workspace: %v", err)
Expand Down
3 changes: 2 additions & 1 deletion scalr/data_source_endpoint.go
@@ -1,6 +1,7 @@
package scalr

import (
"errors"
"fmt"
"log"

Expand Down Expand Up @@ -63,7 +64,7 @@ func dataSourceScalrEndpointRead(d *schema.ResourceData, meta interface{}) error
log.Printf("[DEBUG] Read endpoint with ID: %s", endpointID)
endpoint, err := scalrClient.Endpoints.Read(ctx, endpointID)
if err != nil {
if err == scalr.ErrResourceNotFound {
if errors.Is(err, scalr.ErrResourceNotFound{}) {
return fmt.Errorf("Could not find endpoint %s: %v", endpointID, err)
}
return fmt.Errorf("Error retrieving endpoint: %v", err)
Expand Down
3 changes: 2 additions & 1 deletion scalr/data_source_environment.go
@@ -1,6 +1,7 @@
package scalr

import (
"errors"
"fmt"
"log"

Expand Down Expand Up @@ -74,7 +75,7 @@ func dataSourceEnvironmentRead(d *schema.ResourceData, meta interface{}) error {

environment, err := scalrClient.Environments.Read(ctx, envID)
if err != nil {
if err == scalr.ErrResourceNotFound {
if errors.Is(err, scalr.ErrResourceNotFound{}) {
// If the resource isn't available, the function should set the ID
// to an empty string so Terraform "destroys" the resource in state.
d.SetId("")
Expand Down
3 changes: 2 additions & 1 deletion scalr/data_source_scalr_access_policy.go
@@ -1,6 +1,7 @@
package scalr

import (
"errors"
"fmt"
"log"

Expand Down Expand Up @@ -72,7 +73,7 @@ func dataSourceScalrAccessPolicyRead(d *schema.ResourceData, meta interface{}) e
ap, err := scalrClient.AccessPolicies.Read(ctx, id)

if err != nil {
if err == scalr.ErrResourceNotFound {
if errors.Is(err, scalr.ErrResourceNotFound{}) {
log.Printf("[DEBUG] AccessPolicy %s not found", id)
d.SetId("")
return nil
Expand Down
3 changes: 2 additions & 1 deletion scalr/data_source_webhook.go
@@ -1,6 +1,7 @@
package scalr

import (
"errors"
"fmt"
"log"

Expand Down Expand Up @@ -69,7 +70,7 @@ func dataSourceScalrWebhookRead(d *schema.ResourceData, meta interface{}) error
log.Printf("[DEBUG] Read endpoint with ID: %s", webhookID)
webhook, err := scalrClient.Webhooks.Read(ctx, webhookID)
if err != nil {
if err == scalr.ErrResourceNotFound {
if errors.Is(err, scalr.ErrResourceNotFound{}) {
return fmt.Errorf("Could not find webhook %s: %v", webhookID, err)
}
return fmt.Errorf("Error retrieving webhook: %v", err)
Expand Down
3 changes: 2 additions & 1 deletion scalr/data_source_workspace.go
@@ -1,6 +1,7 @@
package scalr

import (
"errors"
"fmt"
"log"

Expand Down Expand Up @@ -136,7 +137,7 @@ func dataSourceScalrWorkspaceRead(d *schema.ResourceData, meta interface{}) erro
log.Printf("[DEBUG] Read configuration of workspace: %s", name)
workspace, err := scalrClient.Workspaces.Read(ctx, environmentID, name)
if err != nil {
if err == scalr.ErrResourceNotFound {
if errors.Is(err, scalr.ErrResourceNotFound{}) {
return fmt.Errorf("Could not find workspace %s/%s", environmentID, name)
}
return fmt.Errorf("Error retrieving workspace: %v", err)
Expand Down
10 changes: 10 additions & 0 deletions scalr/helpers.go
Expand Up @@ -36,3 +36,13 @@ func GetEnvironmentByName(environmentName string, scalrClient *scalr.Client) (*s
func GetRandomInteger() int {
return rand.Int()
}

func ValidateIDsDefinitions(d []interface{}) error {
for i, id := range d {
id, ok := id.(string)
if !ok || id == "" {
return fmt.Errorf("%d-th value is empty", i)
}
}
return nil
}
34 changes: 25 additions & 9 deletions scalr/resource_scalr_access_policy.go
Expand Up @@ -117,6 +117,22 @@ func resourceScalrAccessPolicy() *schema.Resource {
}
}

func parseRoleIdDefinitions(d *schema.ResourceData) ([]*scalr.Role, error) {
roles := make([]*scalr.Role, 0)

roleIds := d.Get("role_ids").([]interface{})
err := ValidateIDsDefinitions(roleIds)
if err != nil {
return nil, fmt.Errorf("Got error during parsing role ids: %s", err.Error())
}

for _, roleId := range roleIds {
roles = append(roles, &scalr.Role{ID: roleId.(string)})
}

return roles, nil
}

func resourceScalrAccessPolicyCreate(d *schema.ResourceData, meta interface{}) error {
scalrClient := meta.(*scalr.Client)

Expand All @@ -128,9 +144,9 @@ func resourceScalrAccessPolicyCreate(d *schema.ResourceData, meta interface{}) e
scopeType := scope["type"].(string)
scopeId := scope["id"].(string)

var roles []*scalr.Role
for _, roleId := range d.Get("role_ids").([]interface{}) {
roles = append(roles, &scalr.Role{ID: roleId.(string)})
roles, err := parseRoleIdDefinitions(d)
if err != nil {
return err
}

// Create a new options struct.
Expand Down Expand Up @@ -172,7 +188,7 @@ func resourceScalrAccessPolicyRead(d *schema.ResourceData, meta interface{}) err
ap, err := scalrClient.AccessPolicies.Read(ctx, id)

if err != nil {
if err == scalr.ErrResourceNotFound {
if errors.Is(err, scalr.ErrResourceNotFound{}) {
log.Printf("[DEBUG] AccessPolicy %s not found", id)
d.SetId("")
return nil
Expand Down Expand Up @@ -234,16 +250,16 @@ func resourceScalrAccessPolicyUpdate(d *schema.ResourceData, meta interface{}) e
id := d.Id()

if d.HasChange("role_ids") {
var roles []*scalr.Role
for _, roleId := range d.Get("role_ids").([]interface{}) {
roles = append(roles, &scalr.Role{ID: roleId.(string)})
roles, err := parseRoleIdDefinitions(d)
if err != nil {
return err
}

// Create a new options struct.
options := scalr.AccessPolicyUpdateOptions{Roles: roles}

log.Printf("[DEBUG] Update access policy %s", id)
_, err := scalrClient.AccessPolicies.Update(ctx, id, options)
_, err = scalrClient.AccessPolicies.Update(ctx, id, options)
if err != nil {
return fmt.Errorf(
"Error updating access policy %s: %v", id, err)
Expand All @@ -260,7 +276,7 @@ func resourceScalrAccessPolicyDelete(d *schema.ResourceData, meta interface{}) e
log.Printf("[DEBUG] Delete access policy %s", id)
err := scalrClient.AccessPolicies.Delete(ctx, id)
if err != nil {
if err == scalr.ErrResourceNotFound {
if errors.Is(err, scalr.ErrResourceNotFound{}) {
return nil
}
return fmt.Errorf(
Expand Down
9 changes: 9 additions & 0 deletions scalr/resource_scalr_access_policy_test.go
Expand Up @@ -145,6 +145,11 @@ func TestAccScalrAccessPolicy_update(t *testing.T) {
resource.TestCheckResourceAttr("scalr_access_policy.test", "role_ids.#", "2"),
),
},

{
Config: testAccScalrAccessPolicyEmptyRoleId(rInt),
ExpectError: regexp.MustCompile("Got error during parsing role ids: 0-th value is empty"),
},
},
})
}
Expand Down Expand Up @@ -304,6 +309,10 @@ func testAccScalrAccessPolicyBasic(rInt int) string {
return fmt.Sprintf(iamPolicyTemplate, rInt, defaultAccount, testUser, readOnlyRole)
}

func testAccScalrAccessPolicyEmptyRoleId(rInt int) string {
return fmt.Sprintf(iamPolicyTemplate, rInt, defaultAccount, testUser, "")
}

func testAccScalrAccessPolicyChangedOutside(rInt int) string {
return fmt.Sprintf(iamPolicyTemplate, rInt, defaultAccount, testUser, userRole)
}
Expand Down
5 changes: 3 additions & 2 deletions scalr/resource_scalr_endpoint.go
@@ -1,6 +1,7 @@
package scalr

import (
"errors"
"fmt"
"log"

Expand Down Expand Up @@ -115,7 +116,7 @@ func resourceScalrEndpointRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Read endpoint with ID: %s", endpointID)
endpoint, err := scalrClient.Endpoints.Read(ctx, endpointID)
if err != nil {
if err == scalr.ErrResourceNotFound {
if errors.Is(err, scalr.ErrResourceNotFound{}) {
d.SetId("")
return nil
}
Expand Down Expand Up @@ -169,7 +170,7 @@ func resourceScalrEndpointDelete(d *schema.ResourceData, meta interface{}) error
log.Printf("[DEBUG] Delete endpoint: %s", d.Id())
err := scalrClient.Endpoints.Delete(ctx, d.Id())
if err != nil {
if err == scalr.ErrResourceNotFound {
if errors.Is(err, scalr.ErrResourceNotFound{}) {
return nil
}
return fmt.Errorf("Error deleting endpoint%s: %v", d.Id(), err)
Expand Down

0 comments on commit f758d84

Please sign in to comment.