generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 41
/
resource_roles.go
139 lines (114 loc) · 4.11 KB
/
resource_roles.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
134
135
136
137
138
139
package roles
import (
"context"
"net/http"
"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/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func ResourceRoles() *schema.Resource {
resource := &schema.Resource{
Description: "Resource for creating roles.",
ReadContext: resourceRolesRead,
UpdateContext: resourceRolesCreateOrUpdate,
DeleteContext: resourceRolesDelete,
CreateContext: resourceRolesCreateOrUpdate,
Importer: helpers.MultiLevelResourceImporter,
Schema: map[string]*schema.Schema{
"permissions": {
Description: "List of the permission identifiers ",
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"allowed_scope_levels": {
Description: "The scope levels at which this role can be used",
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
helpers.SetMultiLevelResourceSchema(resource.Schema)
return resource
}
func resourceRolesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
id := d.Id()
rolesApiGetRoleOpts := &nextgen.RolesApiGetRoleOpts{
AccountIdentifier: optional.NewString(c.AccountId),
OrgIdentifier: helpers.BuildField(d, "org_id"),
ProjectIdentifier: helpers.BuildField(d, "project_id"),
}
resp, httpResp, err := c.RolesApi.GetRole(ctx, id, rolesApiGetRoleOpts)
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
readRoles(d, resp.Data.Role)
return nil
}
func resourceRolesCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
var err error
var resp nextgen.ResponseDtoRoleResponse
var httpResp *http.Response
id := d.Id()
role := buildRoles(d)
if id == "" {
resp, httpResp, err = c.RolesApi.PostRole(ctx, *role, &nextgen.RolesApiPostRoleOpts{
AccountIdentifier: optional.NewString(c.AccountId),
OrgIdentifier: helpers.BuildField(d, "org_id"),
ProjectIdentifier: helpers.BuildField(d, "project_id"),
})
} else {
resp, httpResp, err = c.RolesApi.PutRole(ctx, *role, id, &nextgen.RolesApiPutRoleOpts{
AccountIdentifier: optional.NewString(c.AccountId),
OrgIdentifier: helpers.BuildField(d, "org_id"),
ProjectIdentifier: helpers.BuildField(d, "project_id"),
})
}
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
readRoles(d, resp.Data.Role)
return nil
}
func resourceRolesDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
_, httpResp, err := c.RolesApi.DeleteRole(ctx, d.Id(), &nextgen.RolesApiDeleteRoleOpts{
AccountIdentifier: optional.NewString(c.AccountId),
OrgIdentifier: helpers.BuildField(d, "org_id"),
ProjectIdentifier: helpers.BuildField(d, "project_id"),
})
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
return nil
}
func buildRoles(d *schema.ResourceData) *nextgen.Role {
return &nextgen.Role{
Identifier: d.Get("identifier").(string),
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Tags: helpers.ExpandTags(d.Get("tags").(*schema.Set).List()),
Permissions: helpers.ExpandField(d.Get("permissions").(*schema.Set).List()),
AllowedScopeLevels: helpers.ExpandField(d.Get("allowed_scope_levels").(*schema.Set).List()),
}
}
func readRoles(d *schema.ResourceData, role *nextgen.Role) {
d.SetId(role.Identifier)
d.Set("identifier", role.Identifier)
d.Set("name", role.Name)
d.Set("description", role.Description)
d.Set("tags", helpers.FlattenTags(role.Tags))
d.Set("permissions", role.Permissions)
d.Set("allowed_scope_levels", role.AllowedScopeLevels)
}