forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_source_github_repository_deploy_keys.go
More file actions
103 lines (84 loc) · 2.03 KB
/
data_source_github_repository_deploy_keys.go
File metadata and controls
103 lines (84 loc) · 2.03 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package github
import (
"context"
"fmt"
"github.com/google/go-github/v84/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceGithubRepositoryDeployKeys() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceGithubRepositoryDeployKeysRead,
Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
},
"keys": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
},
"key": {
Type: schema.TypeString,
Computed: true,
},
"title": {
Type: schema.TypeString,
Computed: true,
},
"verified": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
},
}
}
func dataSourceGithubRepositoryDeployKeysRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
repository := d.Get("repository").(string)
owner := meta.(*Owner).name
client := meta.(*Owner).v3client
options := &github.ListOptions{
PerPage: 100,
}
results := make([]map[string]any, 0)
for {
keys, resp, err := client.Repositories.ListKeys(ctx, owner, repository, options)
if err != nil {
return diag.FromErr(err)
}
results = append(results, flattenGitHubDeployKeys(keys)...)
if resp.NextPage == 0 {
break
}
options.Page = resp.NextPage
}
d.SetId(fmt.Sprintf("%s/%s", owner, repository))
err := d.Set("keys", results)
if err != nil {
return diag.FromErr(err)
}
return nil
}
func flattenGitHubDeployKeys(keys []*github.Key) []map[string]any {
results := make([]map[string]any, 0)
if keys == nil {
return results
}
for _, c := range keys {
result := make(map[string]any)
result["id"] = c.ID
result["key"] = c.Key
result["title"] = c.Title
result["verified"] = c.Verified
results = append(results, result)
}
return results
}