-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: add support for remote state access configuration (#634)
* Feat: add support for remote state access configuration * modified post to put * update staticcheck * update to golang 1.19 * added an example * removed integration test * id is now environment id
- Loading branch information
1 parent
7192f61
commit 5bca0d1
Showing
14 changed files
with
411 additions
and
8 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 |
---|---|---|
|
@@ -6,7 +6,7 @@ on: | |
- main | ||
|
||
env: | ||
GO_VERSION: 1.18 | ||
GO_VERSION: 1.19 | ||
|
||
jobs: | ||
generate-docs: | ||
|
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 |
---|---|---|
|
@@ -16,7 +16,7 @@ on: | |
- 'v*.*.*' | ||
|
||
env: | ||
GO_VERSION: 1.18 | ||
GO_VERSION: 1.19 | ||
|
||
jobs: | ||
goreleaser: | ||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,35 @@ | ||
package client | ||
|
||
type RemoteStateAccessConfiguration struct { | ||
EnvironmentId string `json:"environmentId"` | ||
AccessibleFromEntireOrganization bool `json:"accessibleFromEntireOrganization"` | ||
AllowedProjectIds []string `json:"allowedProjectIds" tfschema:",omitempty"` | ||
} | ||
|
||
type RemoteStateAccessConfigurationCreate struct { | ||
AccessibleFromEntireOrganization bool `json:"accessibleFromEntireOrganization"` | ||
AllowedProjectIds []string `json:"allowedProjectIds,omitempty"` | ||
} | ||
|
||
func (client *ApiClient) RemoteStateAccessConfiguration(environmentId string) (*RemoteStateAccessConfiguration, error) { | ||
var result RemoteStateAccessConfiguration | ||
|
||
if err := client.http.Get("/remote-backend/states/"+environmentId+"/access-control", nil, &result); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &result, nil | ||
} | ||
|
||
func (client *ApiClient) RemoteStateAccessConfigurationCreate(environmentId string, payload RemoteStateAccessConfigurationCreate) (*RemoteStateAccessConfiguration, error) { | ||
var result RemoteStateAccessConfiguration | ||
if err := client.http.Put("/remote-backend/states/"+environmentId+"/access-control", payload, &result); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &result, nil | ||
} | ||
|
||
func (client *ApiClient) RemoteStateAccessConfigurationDelete(environmentId string) error { | ||
return client.http.Delete("/remote-backend/states/" + environmentId + "/access-control") | ||
} |
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,84 @@ | ||
package client_test | ||
|
||
import ( | ||
. "github.com/env0/terraform-provider-env0/client" | ||
"github.com/golang/mock/gomock" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("RemoteStateAccess", func() { | ||
environmentId := "environmnet_id" | ||
|
||
remoteStateAccess := RemoteStateAccessConfiguration{ | ||
EnvironmentId: environmentId, | ||
AccessibleFromEntireOrganization: false, | ||
AllowedProjectIds: []string{ | ||
"pid1", | ||
}, | ||
} | ||
|
||
Describe("Create", func() { | ||
var err error | ||
var remoteStateAccessResponse *RemoteStateAccessConfiguration | ||
|
||
BeforeEach(func() { | ||
createRequest := RemoteStateAccessConfigurationCreate{ | ||
AllowedProjectIds: remoteStateAccess.AllowedProjectIds, | ||
} | ||
|
||
httpCall = mockHttpClient.EXPECT(). | ||
Put("/remote-backend/states/"+environmentId+"/access-control", createRequest, gomock.Any()). | ||
Do(func(path string, request interface{}, response *RemoteStateAccessConfiguration) { | ||
*response = remoteStateAccess | ||
}) | ||
httpCall.Times(1) | ||
remoteStateAccessResponse, err = apiClient.RemoteStateAccessConfigurationCreate(environmentId, createRequest) | ||
}) | ||
|
||
It("Should not return error", func() { | ||
Expect(err).To(BeNil()) | ||
}) | ||
|
||
It("Should return created remote state access configuration", func() { | ||
Expect(*remoteStateAccessResponse).To(Equal(remoteStateAccess)) | ||
}) | ||
}) | ||
|
||
Describe("Get", func() { | ||
var err error | ||
var remoteStateAccessResponse *RemoteStateAccessConfiguration | ||
|
||
BeforeEach(func() { | ||
httpCall = mockHttpClient.EXPECT(). | ||
Get("/remote-backend/states/"+environmentId+"/access-control", gomock.Nil(), gomock.Any()). | ||
Do(func(path string, request interface{}, response *RemoteStateAccessConfiguration) { | ||
*response = remoteStateAccess | ||
}) | ||
httpCall.Times(1) | ||
remoteStateAccessResponse, err = apiClient.RemoteStateAccessConfiguration(environmentId) | ||
}) | ||
|
||
It("Should return remote state access configuration", func() { | ||
Expect(*remoteStateAccessResponse).To(Equal(remoteStateAccess)) | ||
}) | ||
|
||
It("Should not return error", func() { | ||
Expect(err).To(BeNil()) | ||
}) | ||
}) | ||
|
||
Describe("Delete", func() { | ||
var err error | ||
|
||
BeforeEach(func() { | ||
httpCall = mockHttpClient.EXPECT().Delete("/remote-backend/states/" + environmentId + "/access-control") | ||
httpCall.Times(1) | ||
err = apiClient.RemoteStateAccessConfigurationDelete(environmentId) | ||
}) | ||
|
||
It("Should not return error", func() { | ||
Expect(err).To(BeNil()) | ||
}) | ||
}) | ||
}) |
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,95 @@ | ||
package env0 | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/env0/terraform-provider-env0/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func resourceEnvironmentStateAccess() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceEnvironmentStateAccessCreate, | ||
ReadContext: resourceEnvironmentStateAccessRead, | ||
DeleteContext: resourceEnvironmentStateAccessDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"environment_id": { | ||
Type: schema.TypeString, | ||
Description: "id of the environment", | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"accessible_from_entire_organization": { | ||
Type: schema.TypeBool, | ||
Description: "when this parameter is 'false', allowed_project_ids should be provided. Defaults to 'false'", | ||
Optional: true, | ||
Default: false, | ||
ForceNew: true, | ||
}, | ||
"allowed_project_ids": { | ||
Type: schema.TypeList, | ||
Description: "list of allowed project_ids. Used when 'accessible_from_entire_organization' is 'false'", | ||
Optional: true, | ||
ForceNew: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceEnvironmentStateAccessCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
environmentId := d.Get("environment_id").(string) | ||
|
||
var payload client.RemoteStateAccessConfigurationCreate | ||
if err := readResourceData(&payload, d); err != nil { | ||
return diag.Errorf("schema resource data deserialization failed: %v", err) | ||
} | ||
|
||
if payload.AccessibleFromEntireOrganization && payload.AllowedProjectIds != nil { | ||
return diag.Errorf("'allowed_project_ids' should not be set when 'accessible_from_entire_organization' is set to 'true'") | ||
} | ||
|
||
apiClient := meta.(client.ApiClientInterface) | ||
|
||
remoteStateAccess, err := apiClient.RemoteStateAccessConfigurationCreate(environmentId, payload) | ||
if err != nil { | ||
return diag.Errorf("could not create a remote state access configation: %v", err) | ||
} | ||
|
||
d.SetId(remoteStateAccess.EnvironmentId) | ||
|
||
return nil | ||
} | ||
|
||
func resourceEnvironmentStateAccessRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
environmentId := d.Get("environment_id").(string) | ||
|
||
apiClient := meta.(client.ApiClientInterface) | ||
|
||
remoteStateAccess, err := apiClient.RemoteStateAccessConfiguration(environmentId) | ||
if err != nil { | ||
return ResourceGetFailure("remote state access configation", d, err) | ||
} | ||
|
||
if err := writeResourceData(remoteStateAccess, d); err != nil { | ||
return diag.Errorf("schema resource data serialization failed: %v", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceEnvironmentStateAccessDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
environmentId := d.Get("environment_id").(string) | ||
|
||
apiClient := meta.(client.ApiClientInterface) | ||
|
||
if err := apiClient.RemoteStateAccessConfigurationDelete(environmentId); err != nil { | ||
return diag.Errorf("could not delete remote state access configation: %v", err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.