Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement missing data source github_actions_environment_public_key #2500

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions github/data_source_github_actions_environment_public_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package github

import (
"context"
"net/url"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceGithubActionsEnvironmentPublicKey() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubActionsEnvironmentPublicKeyRead,

Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
},
"environment": {
Type: schema.TypeString,
Required: true,
},
"key_id": {
Type: schema.TypeString,
Computed: true,
},
"key": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceGithubActionsEnvironmentPublicKeyRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
repository := d.Get("repository").(string)

envName := d.Get("environment").(string)
escapedEnvName := url.PathEscape(envName)

repo, _, err := client.Repositories.Get(context.TODO(), owner, repository)
if err != nil {
return err
}

publicKey, _, err := client.Actions.GetEnvPublicKey(context.TODO(), int(repo.GetID()), escapedEnvName)
if err != nil {
return err
}

d.SetId(publicKey.GetKeyID())
err = d.Set("key_id", publicKey.GetKeyID())
if err != nil {
return err
}
err = d.Set("key", publicKey.GetKey())
if err != nil {
return err
}

return nil
}
65 changes: 65 additions & 0 deletions github/data_source_github_actions_environment_public_key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package github

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccGithubActionsEnvironmentPublicKeyDataSource(t *testing.T) {

randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

t.Run("queries a repository environment public key without error", func(t *testing.T) {

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test-%[1]s"
auto_init = true
}

resource "github_repository_environment" "test" {
repository = github_repository.test.name
environment = "tf-acc-test-%[1]s"
}

data "github_actions_environment_public_key" "test" {
repository = github_repository.test.name
environment = github_repository_environment.test.environment
}`, randomID)

check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(
"data.github_actions_environment_public_key.test", "key",
),
)

testCase := func(t *testing.T, mode string) {
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, mode) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
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)
})

})
}
1 change: 1 addition & 0 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ func Provider() *schema.Provider {
},

DataSourcesMap: map[string]*schema.Resource{
"github_actions_environment_public_key": dataSourceGithubActionsEnvironmentPublicKey(),
"github_actions_environment_secrets": dataSourceGithubActionsEnvironmentSecrets(),
"github_actions_environment_variables": dataSourceGithubActionsEnvironmentVariables(),
"github_actions_organization_oidc_subject_claim_customization_template": dataSourceGithubActionsOrganizationOIDCSubjectClaimCustomizationTemplate(),
Expand Down
31 changes: 31 additions & 0 deletions website/docs/d/actions_environment_public_key.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
layout: "github"
page_title: "GitHub: github_actions_environment_public_key"
description: |-
Get information on a GitHub Actions Environment Public Key.
---

# github_actions_environment_public_key

Use this data source to retrieve information about a GitHub Actions public key of a specific environment. This data source is required to be used with other GitHub secrets interactions.
Note that the provider `token` must have admin rights to a repository to retrieve the action public keys of it's environments.


## Example Usage

```hcl
data "github_actions_environment_public_key" "example" {
repository = "example_repo"
environment = "example_environment"
}
```

## Argument Reference

* `repository` - (Required) Name of the repository to get public key from.
* `environment` - (Required) Name of the environment to get public key from.

## Attributes Reference

* `key_id` - ID of the key that has been retrieved.
* `key` - Actual key retrieved.
5 changes: 4 additions & 1 deletion website/github.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<li>
<a href="#">Data Sources</a>
<ul class="nav nav-visible">
<li>
<a href="/docs/providers/github/d/actions_environment_public_key.html">actions_environment_public_key</a>
</li>
<li>
<a href="/docs/providers/github/d/actions_environment_secrets.html">actions_environment_secrets</a>
</li>
Expand Down Expand Up @@ -335,7 +338,7 @@
<a href="/docs/providers/github/r/repository_environment_secret.html">github_repository_environment_secret</a>
</li>
<li>
<a href="/docs/providers/github/r/repository_environment_variable.html">github_repository_environment_variable</a>
<a href="/docs/providers/github/r/repository_environment_variable.html">github_repository_environment_variable</a>
</li>
<li>
<a href="/docs/providers/github/r/repository_file.html">github_repository_file</a>
Expand Down