/
gen_obj_credential_data_source.go
235 lines (212 loc) · 7.13 KB
/
gen_obj_credential_data_source.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
224
225
226
227
228
229
230
231
232
233
234
235
package awx
import (
"context"
"fmt"
"net/http"
"net/url"
p "path"
c "github.com/ilijamt/terraform-provider-awx/internal/client"
"github.com/ilijamt/terraform-provider-awx/internal/helpers"
"github.com/ilijamt/terraform-provider-awx/internal/hooks"
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)
var (
_ datasource.DataSource = &credentialDataSource{}
_ datasource.DataSourceWithConfigure = &credentialDataSource{}
)
// NewCredentialDataSource is a helper function to instantiate the Credential data source.
func NewCredentialDataSource() datasource.DataSource {
return &credentialDataSource{}
}
// credentialDataSource is the data source implementation.
type credentialDataSource struct {
client c.Client
endpoint string
}
// Configure adds the provider configured client to the data source.
func (o *credentialDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
o.client = req.ProviderData.(c.Client)
o.endpoint = "/api/v2/credentials/"
}
// Metadata returns the data source type name.
func (o *credentialDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_credential"
}
// Schema defines the schema for the data source.
func (o *credentialDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
// Data only elements
"cloud": schema.BoolAttribute{
Description: "Cloud",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
"credential_type": schema.Int64Attribute{
Description: "Specify the type of credential you want to create. Refer to the documentation for details on each type.",
Sensitive: false,
Computed: true,
Validators: []validator.Int64{},
},
"description": schema.StringAttribute{
Description: "Optional description of this credential.",
Sensitive: false,
Computed: true,
Validators: []validator.String{},
},
"id": schema.Int64Attribute{
Description: "Database ID for this credential.",
Sensitive: false,
Optional: true,
Computed: true,
Validators: []validator.Int64{
int64validator.ExactlyOneOf(
path.MatchRoot("id"),
path.MatchRoot("name"),
),
},
},
"inputs": schema.StringAttribute{
Description: "Enter inputs using either JSON or YAML syntax. Refer to the documentation for example syntax.",
Sensitive: false,
Computed: true,
Validators: []validator.String{},
},
"kind": schema.StringAttribute{
Description: "Kind",
Sensitive: false,
Computed: true,
Validators: []validator.String{},
},
"kubernetes": schema.BoolAttribute{
Description: "Kubernetes",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
"managed": schema.BoolAttribute{
Description: "Managed",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
"name": schema.StringAttribute{
Description: "Name of this credential.",
Sensitive: false,
Optional: true,
Computed: true,
Validators: []validator.String{
stringvalidator.ExactlyOneOf(
path.MatchRoot("id"),
path.MatchRoot("name"),
),
},
},
"organization": schema.Int64Attribute{
Description: "Inherit permissions from organization roles. If provided on creation, do not give either user or team.",
Sensitive: false,
Computed: true,
Validators: []validator.Int64{},
},
// Write only elements
},
}
}
func (o *credentialDataSource) ConfigValidators(ctx context.Context) []datasource.ConfigValidator {
return []datasource.ConfigValidator{}
}
// Read refreshes the Terraform state with the latest data.
func (o *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state credentialTerraformModel
var err error
var endpoint string
var searchDefined bool
// Only one group should evaluate to True, terraform should prevent from being able to set
// the conflicting groups
// Evaluate group 'by_id' based on the schema definition
var groupByIdExists = func() bool {
var groupByIdExists = true
var paramsById = []any{o.endpoint}
var attrID types.Int64
req.Config.GetAttribute(ctx, path.Root("id"), &attrID)
groupByIdExists = groupByIdExists && (!attrID.IsNull() && !attrID.IsUnknown())
paramsById = append(paramsById, attrID.ValueInt64())
if groupByIdExists {
endpoint = p.Clean(fmt.Sprintf("%s/%d/", paramsById...))
}
return groupByIdExists
}()
searchDefined = searchDefined || groupByIdExists
// Evaluate group 'by_name' based on the schema definition
var groupByNameExists = func() bool {
var groupByNameExists = true
var paramsByName = []any{o.endpoint}
var attrName types.String
req.Config.GetAttribute(ctx, path.Root("name"), &attrName)
groupByNameExists = groupByNameExists && (!attrName.IsNull() && !attrName.IsUnknown())
paramsByName = append(paramsByName, url.PathEscape(attrName.ValueString()))
if groupByNameExists {
endpoint = p.Clean(fmt.Sprintf("%s//?name__exact=%s", paramsByName...))
}
return groupByNameExists
}()
searchDefined = searchDefined || groupByNameExists
if !searchDefined {
var detailMessage string
resp.Diagnostics.AddError(
"missing configuration for one of the predefined search groups",
detailMessage,
)
return
}
// Creates a new request for Credential
var r *http.Request
if r, err = o.client.NewRequest(ctx, http.MethodGet, endpoint, nil); err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Unable to create a new request for Credential on %s for read", o.endpoint),
err.Error(),
)
return
}
// Try and actually fetch the data for Credential
var data map[string]any
if data, err = o.client.Do(ctx, r); err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Unable to read resource for Credential on %s", endpoint),
err.Error(),
)
return
}
var d diag.Diagnostics
if data, d, err = helpers.ExtractDataIfSearchResult(data); err != nil {
resp.Diagnostics.Append(d...)
return
}
if d, err = state.updateFromApiData(data); err != nil {
resp.Diagnostics.Append(d...)
return
}
// Set state
if err = hookCredential(ctx, ApiVersion, hooks.SourceData, hooks.CalleeRead, nil, &state); err != nil {
resp.Diagnostics.AddError(
"Unable to process custom hook for the state on Credential",
err.Error(),
)
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
}