-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/env0/terraform-provider-env0 …
…into feat_cloud_cost_credentials-#218
- Loading branch information
Showing
13 changed files
with
308 additions
and
7 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
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,33 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "env0_agents Data Source - terraform-provider-env0" | ||
subcategory: "" | ||
description: |- | ||
--- | ||
|
||
# env0_agents (Data Source) | ||
|
||
|
||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Optional | ||
|
||
- **id** (String) The ID of this resource. | ||
|
||
### Read-Only | ||
|
||
- **agents** (List of Object) list of organization agents (see [below for nested schema](#nestedatt--agents)) | ||
|
||
<a id="nestedatt--agents"></a> | ||
### Nested Schema for `agents` | ||
|
||
Read-Only: | ||
|
||
- **agent_key** (String) | ||
|
||
|
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,54 @@ | ||
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 dataAgents() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: agentsRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"agents": { | ||
Type: schema.TypeList, | ||
Description: "list of organization agents", | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"agent_key": { | ||
Type: schema.TypeString, | ||
Description: "agent key identifier", | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func agentsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
apiClient := meta.(client.ApiClientInterface) | ||
|
||
agents, err := apiClient.Agents() | ||
if err != nil { | ||
return diag.Errorf("could not read agents: %v", err) | ||
} | ||
|
||
data := struct { | ||
Agents []client.Agent | ||
}{agents} | ||
|
||
if err := writeResourceData(&data, d); err != nil { | ||
return diag.Errorf("schema resource data serialization failed: %v", err) | ||
} | ||
|
||
// Not really needed. But required by Terraform SDK - https://github.com/hashicorp/terraform-plugin-sdk/issues/541 | ||
d.SetId("1") | ||
|
||
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,67 @@ | ||
package env0 | ||
|
||
import ( | ||
"errors" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/env0/terraform-provider-env0/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAgentsDataSource(t *testing.T) { | ||
agent1 := client.Agent{ | ||
AgentKey: "akey1", | ||
} | ||
|
||
agent2 := client.Agent{ | ||
AgentKey: "akey2", | ||
} | ||
|
||
resourceType := "env0_agents" | ||
resourceName := "test_agents" | ||
accessor := dataSourceAccessor(resourceType, resourceName) | ||
|
||
getTestCase := func() resource.TestCase { | ||
return resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{}), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr(accessor, "agents.0.agent_key", agent1.AgentKey), | ||
resource.TestCheckResourceAttr(accessor, "agents.1.agent_key", agent2.AgentKey), | ||
), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
mockAgents := func(returnValue []client.Agent) func(mockFunc *client.MockApiClientInterface) { | ||
return func(mock *client.MockApiClientInterface) { | ||
mock.EXPECT().Agents().AnyTimes().Return(returnValue, nil) | ||
} | ||
} | ||
|
||
t.Run("Success", func(t *testing.T) { | ||
runUnitTest(t, | ||
getTestCase(), | ||
mockAgents([]client.Agent{agent1, agent2}), | ||
) | ||
}) | ||
|
||
t.Run("API Call Error", func(t *testing.T) { | ||
runUnitTest(t, | ||
resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{}), | ||
ExpectError: regexp.MustCompile("error"), | ||
}, | ||
}, | ||
}, | ||
func(mock *client.MockApiClientInterface) { | ||
mock.EXPECT().Agents().AnyTimes().Return(nil, errors.New("error")) | ||
}, | ||
) | ||
}) | ||
} |
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 @@ | ||
terraform import env0_agent_project_assigment.example agent-id_project-id |
11 changes: 11 additions & 0 deletions
11
examples/resources/env0_agent_project_assignment/resource.tf
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,11 @@ | ||
resource "env0_project" "example" { | ||
name = "example" | ||
description = "Example project" | ||
} | ||
|
||
data "env0_agents" "agents" {} | ||
|
||
resource "env0_agent_project_assignment" { | ||
agent_id = data.env0_agents.agents.0.agent_key | ||
project_id = env0_project.example.id | ||
} |