diff --git a/github/data_source_github_actions_organization_remove_token.go b/github/data_source_github_actions_organization_remove_token.go new file mode 100644 index 0000000000..926993ed7a --- /dev/null +++ b/github/data_source_github_actions_organization_remove_token.go @@ -0,0 +1,49 @@ +package github + +import ( + "context" + "fmt" + "log" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceGithubActionsOrganizationRemoveToken() *schema.Resource { + return &schema.Resource{ + Read: dataSourceGithubActionsOrganizationRemoveTokenRead, + + Schema: map[string]*schema.Schema{ + "token": { + Type: schema.TypeString, + Computed: true, + }, + "expires_at": { + Type: schema.TypeInt, + Computed: true, + }, + }, + } +} + +func dataSourceGithubActionsOrganizationRemoveTokenRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*Owner).v3client + owner := meta.(*Owner).name + + log.Printf("[DEBUG] Creating a GitHub Actions organization remove token for %s", owner) + token, _, err := client.Actions.CreateOrganizationRemoveToken(context.TODO(), owner) + if err != nil { + return fmt.Errorf("error creating a GitHub Actions organization remove token for %s: %s", owner, err) + } + + d.SetId(owner) + err = d.Set("token", token.Token) + if err != nil { + return err + } + err = d.Set("expires_at", token.ExpiresAt.Unix()) + if err != nil { + return err + } + + return nil +} diff --git a/github/data_source_github_actions_organization_remove_token_test.go b/github/data_source_github_actions_organization_remove_token_test.go new file mode 100644 index 0000000000..71babfdfca --- /dev/null +++ b/github/data_source_github_actions_organization_remove_token_test.go @@ -0,0 +1,49 @@ +package github + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccGithubActionsOrganizationRemoveTokenDataSource(t *testing.T) { + + t.Run("get an organization remove token without error", func(t *testing.T) { + + config := ` + data "github_actions_organization_remove_token" "test" { + } + ` + + check := resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.github_actions_organization_remove_token.test", "token"), + resource.TestCheckResourceAttrSet("data.github_actions_organization_remove_token.test", "expires_at"), + ) + + 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) + }) + + }) +} diff --git a/github/data_source_github_actions_registration_token_test.go b/github/data_source_github_actions_registration_token_test.go index ca96873df9..6fdb944b05 100644 --- a/github/data_source_github_actions_registration_token_test.go +++ b/github/data_source_github_actions_registration_token_test.go @@ -18,6 +18,7 @@ func TestAccGithubActionsRegistrationTokenDataSource(t *testing.T) { resource "github_repository" "test" { name = "tf-acc-test-%[1]s" auto_init = true + vulnerability_alerts = true } data "github_actions_registration_token" "test" { diff --git a/github/data_source_github_actions_remove_token.go b/github/data_source_github_actions_remove_token.go new file mode 100644 index 0000000000..535f918fe5 --- /dev/null +++ b/github/data_source_github_actions_remove_token.go @@ -0,0 +1,55 @@ +package github + +import ( + "context" + "fmt" + "log" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceGithubActionsRemoveToken() *schema.Resource { + return &schema.Resource{ + Read: dataSourceGithubActionsRemoveTokenRead, + + Schema: map[string]*schema.Schema{ + "repository": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "token": { + Type: schema.TypeString, + Computed: true, + }, + "expires_at": { + Type: schema.TypeInt, + Computed: true, + }, + }, + } +} + +func dataSourceGithubActionsRemoveTokenRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*Owner).v3client + owner := meta.(*Owner).name + repoName := d.Get("repository").(string) + + log.Printf("[DEBUG] Creating a GitHub Actions repository remove token for %s/%s", owner, repoName) + token, _, err := client.Actions.CreateRemoveToken(context.TODO(), owner, repoName) + if err != nil { + return fmt.Errorf("error creating a GitHub Actions repository remove token for %s/%s: %s", owner, repoName, err) + } + + d.SetId(fmt.Sprintf("%s/%s", owner, repoName)) + err = d.Set("token", token.Token) + if err != nil { + return err + } + err = d.Set("expires_at", token.ExpiresAt.Unix()) + if err != nil { + return err + } + + return nil +} diff --git a/github/data_source_github_actions_remove_token_test.go b/github/data_source_github_actions_remove_token_test.go new file mode 100644 index 0000000000..dd850cae03 --- /dev/null +++ b/github/data_source_github_actions_remove_token_test.go @@ -0,0 +1,61 @@ +package github + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccGithubActionsRemoveTokenDataSource(t *testing.T) { + + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + + t.Run("get a repository remove token without error", func(t *testing.T) { + + config := fmt.Sprintf(` + resource "github_repository" "test" { + name = "tf-acc-test-%[1]s" + auto_init = true + vulnerability_alerts = true + } + + data "github_actions_remove_token" "test" { + repository = github_repository.test.id + } + `, randomID) + + check := resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.github_actions_remove_token.test", "repository", fmt.Sprintf("tf-acc-test-%s", randomID)), + resource.TestCheckResourceAttrSet("data.github_actions_remove_token.test", "token"), + resource.TestCheckResourceAttrSet("data.github_actions_remove_token.test", "expires_at"), + ) + + 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) + }) + + }) +} diff --git a/github/provider.go b/github/provider.go index 8f44c95098..9daf5ee15f 100644 --- a/github/provider.go +++ b/github/provider.go @@ -203,10 +203,12 @@ func Provider() *schema.Provider { "github_actions_organization_oidc_subject_claim_customization_template": dataSourceGithubActionsOrganizationOIDCSubjectClaimCustomizationTemplate(), "github_actions_organization_public_key": dataSourceGithubActionsOrganizationPublicKey(), "github_actions_organization_registration_token": dataSourceGithubActionsOrganizationRegistrationToken(), + "github_actions_organization_remove_token": dataSourceGithubActionsOrganizationRemoveToken(), "github_actions_organization_secrets": dataSourceGithubActionsOrganizationSecrets(), "github_actions_organization_variables": dataSourceGithubActionsOrganizationVariables(), "github_actions_public_key": dataSourceGithubActionsPublicKey(), "github_actions_registration_token": dataSourceGithubActionsRegistrationToken(), + "github_actions_remove_token": dataSourceGithubActionsRemoveToken(), "github_actions_repository_oidc_subject_claim_customization_template": dataSourceGithubActionsRepositoryOIDCSubjectClaimCustomizationTemplate(), "github_actions_secrets": dataSourceGithubActionsSecrets(), "github_actions_variables": dataSourceGithubActionsVariables(), diff --git a/website/docs/d/actions_organization_remove_token.html.markdown b/website/docs/d/actions_organization_remove_token.html.markdown new file mode 100644 index 0000000000..ee33a9b0d2 --- /dev/null +++ b/website/docs/d/actions_organization_remove_token.html.markdown @@ -0,0 +1,24 @@ +--- +layout: "github" +page_title: "GitHub: actions_organization_remove_token" +description: |- + Get a GitHub Actions organization remove token. +--- + +# actions_remove_token + +Use this data source to retrieve a GitHub Actions organization remove token. This token can then be used to remove a self-hosted runner. + +## Example Usage + +```hcl +data "github_actions_organization_remove_token" "example" { +} +``` + +## Argument Reference + +## Attributes Reference + + * `token` - The token that has been retrieved. + * `expires_at` - The token expiration date. diff --git a/website/docs/d/actions_remove_token.html.markdown b/website/docs/d/actions_remove_token.html.markdown new file mode 100644 index 0000000000..842ce93ca8 --- /dev/null +++ b/website/docs/d/actions_remove_token.html.markdown @@ -0,0 +1,27 @@ +--- +layout: "github" +page_title: "GitHub: actions_remove_token" +description: |- + Get a GitHub Actions repository remove token. +--- + +# actions_remove_token + +Use this data source to retrieve a GitHub Actions repository remove token. This token can then be used to remove a self-hosted runner. + +## Example Usage + +```hcl +data "github_actions_remove_token" "example" { + repository = "example_repo" +} +``` + +## Argument Reference + + * `repository` - (Required) Name of the repository to get a GitHub Actions remove token for. + +## Attributes Reference + + * `token` - The token that has been retrieved. + * `expires_at` - The token expiration date.