-
Notifications
You must be signed in to change notification settings - Fork 968
Expand file tree
/
Copy pathdata_source_github_actions_remove_token.go
More file actions
62 lines (54 loc) · 1.77 KB
/
data_source_github_actions_remove_token.go
File metadata and controls
62 lines (54 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package github
import (
"context"
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
// dataSourceGithubActionsRemoveToken is a data source that creates a token that can be
// used to remove a self-hosted runner from a repository.
// https://docs.github.com/en/enterprise-cloud@latest/rest/actions/self-hosted-runners?apiVersion=2022-11-28#create-a-remove-token-for-a-repository
func dataSourceGithubActionsRemoveToken() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubActionsRemoveTokenRead,
Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Repository to remove the self-hosted runner from.",
},
"token": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
Description: "Token used to remove a self-hosted runner from a repository.",
},
"expires_at": {
Type: schema.TypeInt,
Computed: true,
Description: "The token expiration date.",
},
},
}
}
func dataSourceGithubActionsRemoveTokenRead(d *schema.ResourceData, meta any) 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: %w", 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
}