-
Notifications
You must be signed in to change notification settings - Fork 741
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add data source: github_repository_environments
- Loading branch information
Showing
4 changed files
with
179 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/google/go-github/v52/github" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func dataSourceGithubRepositoryEnvironments() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceGithubRepositoryEnvironmentsRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"repository": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"environments": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"node_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceGithubRepositoryEnvironmentsRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*Owner).v3client | ||
orgName := meta.(*Owner).name | ||
repoName := d.Get("repository").(string) | ||
|
||
results := make([]map[string]interface{}, 0) | ||
|
||
var listOptions *github.EnvironmentListOptions | ||
for { | ||
environments, resp, err := client.Repositories.ListEnvironments(context.Background(), orgName, repoName, listOptions) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
results = append(results, flattenEnvironments(environments)...) | ||
|
||
if resp.NextPage == 0 { | ||
break | ||
} | ||
|
||
listOptions.Page = resp.NextPage | ||
} | ||
|
||
d.SetId(repoName) | ||
d.Set("environments", results) | ||
|
||
return nil | ||
} | ||
|
||
func flattenEnvironments(environments *github.EnvResponse) []map[string]interface{} { | ||
results := make([]map[string]interface{}, 0) | ||
if environments == nil { | ||
return results | ||
} | ||
|
||
for _, environment := range environments.Environments { | ||
environmentMap := make(map[string]interface{}) | ||
environmentMap["name"] = environment.GetName() | ||
environmentMap["node_id"] = environment.GetNodeID() | ||
results = append(results, environmentMap) | ||
} | ||
|
||
return results | ||
} |
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,68 @@ | ||
package github | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
func TestAccGithubRepositoryEnvironmentsDataSource(t *testing.T) { | ||
|
||
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) | ||
|
||
t.Run("queries environments", func(t *testing.T) { | ||
|
||
config := fmt.Sprintf(` | ||
resource "github_repository" "test" { | ||
name = "tf-acc-test-%[1]s" | ||
auto_init = true | ||
} | ||
resource "github_repository_environment" "env1" { | ||
repository = github_repository.test.name | ||
environment = "env_x" | ||
} | ||
`, randomID) | ||
|
||
config2 := config + ` | ||
data "github_repository_environments" "all" { | ||
repository = github_repository.test.name | ||
} | ||
` | ||
check := resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.github_repository_environments.all", "environments.#", "1"), | ||
resource.TestCheckResourceAttr("data.github_repository_environments.all", "environments.0.name", "env_x"), | ||
resource.TestCheckResourceAttrSet("data.github_repository_environments.all", "environments.0.node_id"), | ||
) | ||
|
||
testCase := func(t *testing.T, mode string) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { skipUnlessMode(t, mode) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
}, | ||
{ | ||
Config: config2, | ||
Check: check, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
t.Run("with an anonymous account", func(t *testing.T) { | ||
t.Skip("anonymous account not supported for this operation") | ||
}) | ||
|
||
t.Run("with an individual account", func(t *testing.T) { | ||
testCase(t, individual) | ||
}) | ||
|
||
t.Run("with an organization account", func(t *testing.T) { | ||
testCase(t, organization) | ||
}) | ||
|
||
}) | ||
} |
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,28 @@ | ||
--- | ||
layout: "github" | ||
page_title: "GitHub: repository_environments" | ||
description: |- | ||
Get information on a GitHub repository's environments. | ||
--- | ||
|
||
# github_repository_environments | ||
|
||
Use this data source to retrieve information about environments for a repository. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "github_repository_environments" "example" { | ||
repository = "example-repository" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
* `repository` - (Required) Name of the repository to retrieve the environments from. | ||
|
||
## Attributes Reference | ||
|
||
* `environments` - The list of this repository's environments. Each element of `environments` has the following attributes: | ||
* `name` - Environment name. | ||
* `node_id` - Environment node id. |