/
data_source_repository_privileges.go
106 lines (95 loc) · 2.6 KB
/
data_source_repository_privileges.go
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
104
105
106
package cloudsmith
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
// dataSourceRepositoryPrivileges returns the data source schema and read function.
func dataSourceRepositoryPrivileges() *schema.Resource {
return &schema.Resource{
Read: dataSourceRepositoryPrivilegesRead,
Schema: map[string]*schema.Schema{
"organization": {
Type: schema.TypeString,
Description: "Organization to which this repository belongs.",
Required: true,
},
"repository": {
Type: schema.TypeString,
Description: "Repository to fetch privileges information.",
Required: true,
},
"service": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"privilege": {
Type: schema.TypeString,
Computed: true,
},
"slug": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"team": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"privilege": {
Type: schema.TypeString,
Computed: true,
},
"slug": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"user": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"privilege": {
Type: schema.TypeString,
Computed: true,
},
"slug": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
// dataSourceRepositoryPrivilegesRead retrieves privileges information for the specified repository.
func dataSourceRepositoryPrivilegesRead(d *schema.ResourceData, m interface{}) error {
pc := m.(*providerConfig)
organization := d.Get("organization").(string)
repository := d.Get("repository").(string)
req := pc.APIClient.ReposApi.ReposPrivilegesList(pc.Auth, organization, repository)
// TODO: add a proper loop here to ensure we always get all privs,
// regardless of how many are configured.
req = req.Page(1)
req = req.PageSize(1000)
privileges, resp, err := pc.APIClient.ReposApi.ReposPrivilegesListExecute(req)
if err != nil {
if is404(resp) {
d.SetId("")
return nil
}
return err
}
d.Set("service", flattenRepositoryPrivilegeServices(privileges.GetPrivileges()))
d.Set("team", flattenRepositoryPrivilegeTeams(privileges.GetPrivileges()))
d.Set("user", flattenRepositoryPrivilegeUsers(privileges.GetPrivileges()))
d.SetId(fmt.Sprintf("%s/%s", organization, repository))
return nil
}