/
oauth_auth_server_settings_scopes_exclusive_scope_resource.go
223 lines (191 loc) · 10.3 KB
/
oauth_auth_server_settings_scopes_exclusive_scope_resource.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
216
217
218
219
220
221
222
223
package oauthauthserversettingsscopesexclusivescope
import (
"context"
"strings"
"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/booldefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
client "github.com/pingidentity/pingfederate-go-client/v1200/configurationapi"
"github.com/pingidentity/terraform-provider-pingfederate/internal/resource/common/id"
"github.com/pingidentity/terraform-provider-pingfederate/internal/resource/config"
internaltypes "github.com/pingidentity/terraform-provider-pingfederate/internal/types"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ resource.Resource = &oauthAuthServerSettingsScopesExclusiveScopeResource{}
_ resource.ResourceWithConfigure = &oauthAuthServerSettingsScopesExclusiveScopeResource{}
_ resource.ResourceWithImportState = &oauthAuthServerSettingsScopesExclusiveScopeResource{}
)
// OauthAuthServerSettingsScopesExclusiveScopeResource is a helper function to simplify the provider implementation.
func OauthAuthServerSettingsScopesExclusiveScopeResource() resource.Resource {
return &oauthAuthServerSettingsScopesExclusiveScopeResource{}
}
// oauthAuthServerSettingsScopesExclusiveScopeResource is the resource implementation.
type oauthAuthServerSettingsScopesExclusiveScopeResource struct {
providerConfig internaltypes.ProviderConfiguration
apiClient *client.APIClient
}
// GetSchema defines the schema for the resource.
func (r *oauthAuthServerSettingsScopesExclusiveScopeResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
schema := schema.Schema{
Description: "Manages an exclusive scope in the authorization server settings.",
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Description: "The name of the scope.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"description": schema.StringAttribute{
Description: "The description of the scope that appears when the user is prompted for authorization.",
Required: true,
},
"dynamic": schema.BoolAttribute{
Description: "True if the scope is dynamic. (Defaults to false)",
Computed: true,
Optional: true,
Default: booldefault.StaticBool(false),
},
},
}
id.ToSchema(&schema)
resp.Schema = schema
}
func (r *oauthAuthServerSettingsScopesExclusiveScopeResource) ValidateConfig(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
var model oauthAuthServerSettingsScopesExclusiveScopeModel
req.Plan.Get(ctx, &model)
if model.Dynamic.ValueBool() && (model.Name.ValueString() != "" || !model.Name.IsNull()) {
{
containsAsteriskPrefix := strings.Index(model.Name.ValueString(), "*")
if containsAsteriskPrefix != 0 {
resp.Diagnostics.AddError("Dynamic property is set to true with Name property incorrectly specified!", "The Name property must be prefixed with an \"*\". For example, \"*example\"")
}
}
}
}
func addOptionalOauthAuthServerSettingsScopesExclusiveScopesFields(ctx context.Context, addRequest *client.ScopeEntry, plan oauthAuthServerSettingsScopesExclusiveScopeModel) error {
if internaltypes.IsDefined(plan.Name) {
addRequest.Name = plan.Name.ValueString()
}
if internaltypes.IsDefined(plan.Description) {
addRequest.Description = plan.Description.ValueString()
}
if internaltypes.IsDefined(plan.Dynamic) {
addRequest.Dynamic = plan.Dynamic.ValueBoolPointer()
}
return nil
}
// Metadata returns the resource type name.
func (r *oauthAuthServerSettingsScopesExclusiveScopeResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_oauth_auth_server_settings_scopes_exclusive_scope"
}
func (r *oauthAuthServerSettingsScopesExclusiveScopeResource) Configure(_ context.Context, req resource.ConfigureRequest, _ *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
providerCfg := req.ProviderData.(internaltypes.ResourceConfiguration)
r.providerConfig = providerCfg.ProviderConfig
r.apiClient = providerCfg.ApiClient
}
func (r *oauthAuthServerSettingsScopesExclusiveScopeResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan oauthAuthServerSettingsScopesExclusiveScopeModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
createOauthAuthServerSettingsScopesExclusiveScopes := client.NewScopeEntry(plan.Name.ValueString(), plan.Description.ValueString())
err := addOptionalOauthAuthServerSettingsScopesExclusiveScopesFields(ctx, createOauthAuthServerSettingsScopesExclusiveScopes, plan)
if err != nil {
resp.Diagnostics.AddError("Failed to add optional properties to add request for OAuth Auth Server Settings Scopes Exclusive Scope", err.Error())
return
}
apiCreateOauthAuthServerSettingsScopesExclusiveScopes := r.apiClient.OauthAuthServerSettingsAPI.AddExclusiveScope(config.AuthContext(ctx, r.providerConfig))
apiCreateOauthAuthServerSettingsScopesExclusiveScopes = apiCreateOauthAuthServerSettingsScopesExclusiveScopes.Body(*createOauthAuthServerSettingsScopesExclusiveScopes)
oauthAuthServerSettingsScopesExclusiveScopesResponse, httpResp, err := r.apiClient.OauthAuthServerSettingsAPI.AddExclusiveScopeExecute(apiCreateOauthAuthServerSettingsScopesExclusiveScopes)
if err != nil {
config.ReportHttpError(ctx, &resp.Diagnostics, "An error occurred while creating the OAuth Auth Server Settings Scopes Exclusive Scope", err, httpResp)
return
}
// Read the response into the state
var state oauthAuthServerSettingsScopesExclusiveScopeModel
readOauthAuthServerSettingsScopesExclusiveScopeResponse(ctx, oauthAuthServerSettingsScopesExclusiveScopesResponse, &state)
diags = resp.State.Set(ctx, state)
resp.Diagnostics.Append(diags...)
}
func (r *oauthAuthServerSettingsScopesExclusiveScopeResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state oauthAuthServerSettingsScopesExclusiveScopeModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
apiReadOauthAuthServerSettingsScopesExclusiveScopes, httpResp, err := r.apiClient.OauthAuthServerSettingsAPI.GetExclusiveScope(config.AuthContext(ctx, r.providerConfig), state.Name.ValueString()).Execute()
if err != nil {
if httpResp != nil && httpResp.StatusCode == 404 {
config.ReportHttpErrorAsWarning(ctx, &resp.Diagnostics, "An error occurred while getting an OAuth Auth Server Settings Scopes Exclusive Scope", err, httpResp)
resp.State.RemoveResource(ctx)
} else {
config.ReportHttpError(ctx, &resp.Diagnostics, "An error occurred while getting an OAuth Auth Server Settings Scopes Exclusive Scope", err, httpResp)
}
return
}
// Read the response into the state
readOauthAuthServerSettingsScopesExclusiveScopeResponse(ctx, apiReadOauthAuthServerSettingsScopesExclusiveScopes, &state)
// Set refreshed state
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
}
// Update updates the resource and sets the updated Terraform state on success.
func (r *oauthAuthServerSettingsScopesExclusiveScopeResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Retrieve values from plan
var plan oauthAuthServerSettingsScopesExclusiveScopeModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Get the current state to see how any attributes are changing
var state oauthAuthServerSettingsScopesExclusiveScopeModel
req.State.Get(ctx, &state)
updateOauthAuthServerSettingsScopesExclusiveScopes := r.apiClient.OauthAuthServerSettingsAPI.UpdateExclusiveScope(config.AuthContext(ctx, r.providerConfig), plan.Name.ValueString())
createUpdateRequest := client.NewScopeEntry(plan.Name.ValueString(), plan.Description.ValueString())
err := addOptionalOauthAuthServerSettingsScopesExclusiveScopesFields(ctx, createUpdateRequest, plan)
if err != nil {
resp.Diagnostics.AddError("Failed to add optional properties to add request for OAuth Auth Server Settings Scopes Exclusive Scope", err.Error())
return
}
updateOauthAuthServerSettingsScopesExclusiveScopes = updateOauthAuthServerSettingsScopesExclusiveScopes.Body(*createUpdateRequest)
updateOauthAuthServerSettingsScopesExclusiveScopesResponse, httpResp, err := r.apiClient.OauthAuthServerSettingsAPI.UpdateExclusiveScopeExecute(updateOauthAuthServerSettingsScopesExclusiveScopes)
if err != nil {
config.ReportHttpError(ctx, &resp.Diagnostics, "An error occurred while updating OAuth Auth Server Settings Scopes Exclusive Scope", err, httpResp)
return
}
// Read the response
readOauthAuthServerSettingsScopesExclusiveScopeResponse(ctx, updateOauthAuthServerSettingsScopesExclusiveScopesResponse, &state)
// Update computed values
diags = resp.State.Set(ctx, state)
resp.Diagnostics.Append(diags...)
}
// // Delete deletes the resource and removes the Terraform state on success.
func (r *oauthAuthServerSettingsScopesExclusiveScopeResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
// Retrieve values from state
var state oauthAuthServerSettingsScopesExclusiveScopeModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
httpResp, err := r.apiClient.OauthAuthServerSettingsAPI.RemoveExclusiveScope(config.AuthContext(ctx, r.providerConfig), state.Name.ValueString()).Execute()
if err != nil && (httpResp == nil || httpResp.StatusCode != 404) {
config.ReportHttpError(ctx, &resp.Diagnostics, "An error occurred while deleting a OAuth Auth Server Settings Scopes Exclusive Scope", err, httpResp)
}
}
func (r *oauthAuthServerSettingsScopesExclusiveScopeResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
// Retrieve import ID and save to id attribute
resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp)
}