/
gen_obj_settings_ui_data_source.go
132 lines (115 loc) · 4.42 KB
/
gen_obj_settings_ui_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
package awx
import (
"context"
"fmt"
"net/http"
c "github.com/ilijamt/terraform-provider-awx/internal/client"
"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/schema/validator"
)
var (
_ datasource.DataSource = &settingsUiDataSource{}
_ datasource.DataSourceWithConfigure = &settingsUiDataSource{}
)
// NewSettingsUIDataSource is a helper function to instantiate the SettingsUI data source.
func NewSettingsUIDataSource() datasource.DataSource {
return &settingsUiDataSource{}
}
// settingsUiDataSource is the data source implementation.
type settingsUiDataSource struct {
client c.Client
endpoint string
}
// Configure adds the provider configured client to the data source.
func (o *settingsUiDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
o.client = req.ProviderData.(c.Client)
o.endpoint = "/api/v2/settings/ui/"
}
// Metadata returns the data source type name.
func (o *settingsUiDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_settings_ui"
}
// Schema defines the schema for the data source.
func (o *settingsUiDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
// Data only elements
"custom_login_info": schema.StringAttribute{
Description: "If needed, you can add specific information (such as a legal notice or a disclaimer) to a text box in the login modal using this setting. Any content added must be in plain text or an HTML fragment, as other markup languages are not supported.",
Sensitive: false,
Computed: true,
Validators: []validator.String{},
},
"custom_logo": schema.StringAttribute{
Description: "To set up a custom logo, provide a file that you create. For the custom logo to look its best, use a .png file with a transparent background. GIF, PNG and JPEG formats are supported.",
Sensitive: false,
Computed: true,
Validators: []validator.String{},
},
"max_ui_job_events": schema.Int64Attribute{
Description: "Maximum number of job events for the UI to retrieve within a single request.",
Sensitive: false,
Computed: true,
Validators: []validator.Int64{},
},
"pendo_tracking_state": schema.StringAttribute{
Description: "Enable or Disable User Analytics Tracking.",
Sensitive: false,
Computed: true,
Validators: []validator.String{
stringvalidator.OneOf([]string{"off", "anonymous", "detailed"}...),
},
},
"ui_live_updates_enabled": schema.BoolAttribute{
Description: "If disabled, the page will not refresh when events are received. Reloading the page will be required to get the latest details.",
Sensitive: false,
Computed: true,
Validators: []validator.Bool{},
},
// Write only elements
},
}
}
func (o *settingsUiDataSource) ConfigValidators(ctx context.Context) []datasource.ConfigValidator {
return []datasource.ConfigValidator{}
}
// Read refreshes the Terraform state with the latest data.
func (o *settingsUiDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state settingsUiTerraformModel
var err error
var endpoint = o.endpoint
// Creates a new request for SettingsUI
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 SettingsUI on %s for read", o.endpoint),
err.Error(),
)
return
}
// Try and actually fetch the data for SettingsUI
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 SettingsUI on %s", endpoint),
err.Error(),
)
return
}
var d diag.Diagnostics
if d, err = state.updateFromApiData(data); err != nil {
resp.Diagnostics.Append(d...)
return
}
// Set state
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
}