/
gen_obj_user_adg_role.go
215 lines (187 loc) · 6.93 KB
/
gen_obj_user_adg_role.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package awx
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
p "path"
"strconv"
"strings"
"github.com/hashicorp/terraform-plugin-framework-validators/resourcevalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
c "github.com/ilijamt/terraform-provider-awx/internal/client"
"github.com/ilijamt/terraform-provider-awx/internal/models"
)
var (
_ resource.Resource = &userAssociateDisassociateRole{}
_ resource.ResourceWithConfigure = &userAssociateDisassociateRole{}
_ resource.ResourceWithImportState = &userAssociateDisassociateRole{}
)
type userAssociateDisassociateRoleTerraformModel struct {
UserID types.Int64 `tfsdk:"user_id"`
RoleID types.Int64 `tfsdk:"role_id"`
}
// NewUserAssociateDisassociateRoleResource is a helper function to simplify the provider implementation.
func NewUserAssociateDisassociateRoleResource() resource.Resource {
return &userAssociateDisassociateRole{}
}
type userAssociateDisassociateRole struct {
client c.Client
endpoint string
}
func (o *userAssociateDisassociateRole) Configure(ctx context.Context, request resource.ConfigureRequest, response *resource.ConfigureResponse) {
if request.ProviderData == nil {
return
}
o.client = request.ProviderData.(c.Client)
o.endpoint = "/api/v2/users/%d/roles/"
}
func (o *userAssociateDisassociateRole) Metadata(ctx context.Context, request resource.MetadataRequest, response *resource.MetadataResponse) {
response.TypeName = request.ProviderTypeName + "_user_associate_role"
}
func (o *userAssociateDisassociateRole) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"user_id": schema.Int64Attribute{
Description: "Database ID for this User.",
Required: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.RequiresReplace(),
},
},
"role_id": schema.Int64Attribute{
Description: "Database ID of the role to assign.",
Required: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.RequiresReplace(),
},
},
},
}
}
func (o *userAssociateDisassociateRole) ConfigValidators(ctx context.Context) []resource.ConfigValidator {
return []resource.ConfigValidator{
resourcevalidator.RequiredTogether(
path.MatchRoot("user_id"),
path.MatchRoot("role_id"),
),
}
}
func (o *userAssociateDisassociateRole) ImportState(ctx context.Context, request resource.ImportStateRequest, response *resource.ImportStateResponse) {
var state userAssociateDisassociateRoleTerraformModel
var parts = strings.Split(request.ID, "/")
var err error
if len(parts) != 2 {
err = fmt.Errorf("requires the identifier to be set to <user_id>/<role_id>, currently set to %s", request.ID)
response.Diagnostics.AddError(
"Unable to import state for User association, invalid format.",
err.Error(),
)
return
}
var userId, roleId int64
userId, err = strconv.ParseInt(parts[0], 10, 64)
if err != nil {
response.Diagnostics.AddError(
fmt.Sprintf("Unable to parse '%v' as an int64 number, please provide the userId for the User association.", request.ID),
err.Error(),
)
return
}
state.UserID = types.Int64Value(userId)
roleId, err = strconv.ParseInt(parts[1], 10, 64)
if err != nil {
response.Diagnostics.AddError(
fmt.Sprintf("Unable to parse '%v' as an int64 number, please provide the role_id for the User association.", request.ID),
err.Error(),
)
return
}
state.RoleID = types.Int64Value(roleId)
response.Diagnostics.Append(response.State.Set(ctx, &state)...)
}
func (o *userAssociateDisassociateRole) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) {
var err error
// Retrieve values from state
var plan, state userAssociateDisassociateRoleTerraformModel
response.Diagnostics.Append(request.Plan.Get(ctx, &plan)...)
if response.Diagnostics.HasError() {
return
}
// Creates a new request for association of User
var r *http.Request
var endpoint = p.Clean(fmt.Sprintf(o.endpoint, plan.UserID.ValueInt64())) + "/"
var buf bytes.Buffer
var bodyRequest = models.AssociateDisassociateRequestModel{ID: plan.RoleID.ValueInt64(), Disassociate: false}
tflog.Debug(ctx, "[User/Create/Associate] Making a request", map[string]interface{}{
"payload": bodyRequest,
"method": http.MethodPost,
"endpoint": endpoint,
})
_ = json.NewEncoder(&buf).Encode(bodyRequest)
if r, err = o.client.NewRequest(ctx, http.MethodPost, endpoint, &buf); err != nil {
response.Diagnostics.AddError(
fmt.Sprintf("Unable to create a new request for User on %s for create of type ", endpoint),
err.Error(),
)
return
}
if _, err = o.client.Do(ctx, r); err != nil {
response.Diagnostics.AddError(
fmt.Sprintf("Unable to associate for User on %s with a payload of %#v", endpoint, bodyRequest),
err.Error(),
)
return
}
state.UserID = plan.UserID
state.RoleID = plan.RoleID
response.Diagnostics.Append(response.State.Set(ctx, &state)...)
if response.Diagnostics.HasError() {
return
}
}
func (o *userAssociateDisassociateRole) Delete(ctx context.Context, request resource.DeleteRequest, response *resource.DeleteResponse) {
var err error
// Retrieve values from state
var state userAssociateDisassociateRoleTerraformModel
response.Diagnostics.Append(request.State.Get(ctx, &state)...)
if response.Diagnostics.HasError() {
return
}
// Creates a new request for disassociation of User
var r *http.Request
var endpoint = p.Clean(fmt.Sprintf(o.endpoint, state.UserID.ValueInt64())) + "/"
var buf bytes.Buffer
var bodyRequest = models.AssociateDisassociateRequestModel{ID: state.RoleID.ValueInt64(), Disassociate: true}
tflog.Debug(ctx, "[User/Delete/Disassociate] Making a request", map[string]interface{}{
"payload": bodyRequest,
"method": http.MethodPost,
"endpoint": endpoint,
})
_ = json.NewEncoder(&buf).Encode(bodyRequest)
if r, err = o.client.NewRequest(ctx, http.MethodPost, endpoint, &buf); err != nil {
response.Diagnostics.AddError(
fmt.Sprintf("Unable to create a new request for User on %s for delete of type ", o.endpoint),
err.Error(),
)
return
}
if _, err = o.client.Do(ctx, r); err != nil {
response.Diagnostics.AddError(
fmt.Sprintf("Unable to disassociate for User on %s", o.endpoint),
err.Error(),
)
return
}
}
func (o *userAssociateDisassociateRole) Read(ctx context.Context, request resource.ReadRequest, response *resource.ReadResponse) {
}
func (o *userAssociateDisassociateRole) Update(ctx context.Context, request resource.UpdateRequest, response *resource.UpdateResponse) {
}