generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 40
/
data_source_permissions.go
133 lines (116 loc) · 3.89 KB
/
data_source_permissions.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package permissions
import (
"context"
"fmt"
"github.com/antihax/optional"
"github.com/harness/harness-go-sdk/harness/nextgen"
"github.com/harness/terraform-provider-harness/helpers"
"github.com/harness/terraform-provider-harness/internal"
"github.com/harness/terraform-provider-harness/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourcePermissions() *schema.Resource {
resource := &schema.Resource{
Description: "Data source for retrieving permissions.",
ReadContext: dataSourcePermissionsRead,
Schema: map[string]*schema.Schema{
"org_id": {
Description: "Organization Identifier",
Type: schema.TypeString,
Optional: true,
},
"project_id": {
Description: "Project Identifier",
Type: schema.TypeString,
Optional: true,
},
"permissions": {
Description: "Response of the api",
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"identifier": {
Description: "Identifier of the permission",
Type: schema.TypeString,
Computed: true,
},
"name": {
Description: "Name of the permission",
Type: schema.TypeString,
Computed: true,
},
"status": {
Description: "Status of the permission",
Type: schema.TypeString,
Computed: true,
},
"resource_type": {
Description: "Resource type for the given permission",
Type: schema.TypeString,
Computed: true,
},
"action": {
Description: "Action performed by the permission",
Type: schema.TypeString,
Computed: true,
},
"allowed_scope_levels": {
Description: "The scope levels at which this resource group can be used",
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"include_in_all_roles": {
Description: "Is included in all roles",
Type: schema.TypeBool,
Computed: true,
},
}},
},
},
}
return resource
}
func dataSourcePermissionsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
var err error
permissionsApiGetRoleOpts := &nextgen.PermissionsApiGetPermissionListOpts{
AccountIdentifier: optional.NewString(c.AccountId),
OrgIdentifier: helpers.BuildField(d, "org_id"),
ProjectIdentifier: helpers.BuildField(d, "project_id"),
}
resp, httpResp, err := c.PermissionsApi.GetPermissionList(ctx, permissionsApiGetRoleOpts)
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
var id = c.AccountId
id = fmt.Sprintf("%d", utils.StringHashcode(c.AccountId+d.Get("org_id").(string)+d.Get("project_id").(string)))
readPermissions(d, resp.Data, id)
return nil
}
func FlattenPermissions(permissionResponse []nextgen.PermissionResponse) []map[string]interface{} {
if permissionResponse == nil {
return make([]map[string]interface{}, 0)
}
results := make([]map[string]interface{}, len(permissionResponse))
for i, res := range permissionResponse {
results[i] = map[string]interface{}{
"identifier": res.Permission.Identifier,
"name": res.Permission.Name,
"status": res.Permission.Status,
"resource_type": res.Permission.ResourceType,
"action": res.Permission.Action,
"allowed_scope_levels": res.Permission.AllowedScopeLevels,
"include_in_all_roles": res.Permission.IncludeInAllRoles,
}
}
return results
}
func readPermissions(d *schema.ResourceData, permissionResponse []nextgen.PermissionResponse, id string) {
d.SetId(id)
d.Set("permissions", FlattenPermissions(permissionResponse))
}