|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + unleash "github.com/Unleash/unleash-server-api-go/client" |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 10 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + _ datasource.DataSource = &contextFieldDataSource{} |
| 15 | + _ datasource.DataSourceWithConfigure = &contextFieldDataSource{} |
| 16 | +) |
| 17 | + |
| 18 | +func NewContextFieldDataSource() datasource.DataSource { |
| 19 | + return &contextFieldDataSource{} |
| 20 | +} |
| 21 | + |
| 22 | +type contextFieldDataSource struct { |
| 23 | + client *unleash.APIClient |
| 24 | +} |
| 25 | + |
| 26 | +type legalValueResourceModel struct { |
| 27 | + Value types.String `tfsdk:"value"` |
| 28 | + Description types.String `tfsdk:"description"` |
| 29 | +} |
| 30 | + |
| 31 | +type contextFieldDataSourceModel struct { |
| 32 | + Name types.String `tfsdk:"name"` |
| 33 | + Description types.String `tfsdk:"description"` |
| 34 | + Stickiness types.Bool `tfsdk:"stickiness"` |
| 35 | + LegalValues []legalValueResourceModel `tfsdk:"legal_values"` |
| 36 | +} |
| 37 | + |
| 38 | +func (d *contextFieldDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) { |
| 39 | + if req.ProviderData == nil { |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + client, ok := req.ProviderData.(*unleash.APIClient) |
| 44 | + if !ok { |
| 45 | + tflog.Error(ctx, "Unable to prepare client") |
| 46 | + return |
| 47 | + } |
| 48 | + d.client = client |
| 49 | + |
| 50 | +} |
| 51 | + |
| 52 | +func (d *contextFieldDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 53 | + resp.TypeName = req.ProviderTypeName + "_context_field" |
| 54 | +} |
| 55 | + |
| 56 | +func (d *contextFieldDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 57 | + resp.Schema = schema.Schema{ |
| 58 | + Description: "Fetch a context field.", |
| 59 | + Attributes: map[string]schema.Attribute{ |
| 60 | + "name": schema.StringAttribute{ |
| 61 | + Description: "The name of the context field.", |
| 62 | + Required: true, |
| 63 | + }, |
| 64 | + "description": schema.StringAttribute{ |
| 65 | + Description: "A description of the context field.", |
| 66 | + Optional: true, |
| 67 | + Computed: true, |
| 68 | + }, |
| 69 | + "stickiness": schema.BoolAttribute{ |
| 70 | + Description: "Whether this field is available for custom stickiness", |
| 71 | + Optional: true, |
| 72 | + Computed: true, |
| 73 | + }, |
| 74 | + "legal_values": schema.ListNestedAttribute{ |
| 75 | + Description: "Legal values for this context field. If not set, then any value is available for this context field.", |
| 76 | + Optional: true, |
| 77 | + Computed: true, |
| 78 | + NestedObject: schema.NestedAttributeObject{ |
| 79 | + Attributes: map[string]schema.Attribute{ |
| 80 | + "value": schema.StringAttribute{ |
| 81 | + Description: "The allowed value.", |
| 82 | + Computed: true, |
| 83 | + }, |
| 84 | + "description": schema.StringAttribute{ |
| 85 | + Description: "Description of the allowed value.", |
| 86 | + Computed: true, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func (d *contextFieldDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 96 | + tflog.Debug(ctx, "Preparing to hydrate context field") |
| 97 | + var state contextFieldDataSourceModel |
| 98 | + |
| 99 | + resp.Diagnostics.Append(req.Config.Get(ctx, &state)...) |
| 100 | + if resp.Diagnostics.HasError() { |
| 101 | + tflog.Error(ctx, "Unable to read context field data source") |
| 102 | + return |
| 103 | + } |
| 104 | + |
| 105 | + var contextField, apiResponse, err = d.client.ContextAPI.GetContextField(ctx, state.Name.ValueString()).Execute() |
| 106 | + |
| 107 | + if !ValidateApiResponse(apiResponse, 200, &resp.Diagnostics, err) { |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + legalValues := make([]legalValueResourceModel, len(contextField.LegalValues)) |
| 112 | + for i, legalValue := range contextField.LegalValues { |
| 113 | + legalValues[i] = legalValueResourceModel{ |
| 114 | + Value: types.StringValue(legalValue.Value), |
| 115 | + } |
| 116 | + |
| 117 | + if legalValue.Description != nil { |
| 118 | + legalValues[i].Description = types.StringValue(*legalValue.Description) |
| 119 | + } else { |
| 120 | + legalValues[i].Description = types.StringNull() |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + state = contextFieldDataSourceModel{ |
| 125 | + Name: types.StringValue(contextField.Name), |
| 126 | + LegalValues: legalValues, |
| 127 | + } |
| 128 | + |
| 129 | + if contextField.Description.IsSet() { |
| 130 | + state.Description = types.StringValue(*contextField.Description.Get()) |
| 131 | + } else { |
| 132 | + state.Description = types.StringNull() |
| 133 | + } |
| 134 | + |
| 135 | + if contextField.Stickiness != nil { |
| 136 | + state.Stickiness = types.BoolValue(*contextField.Stickiness) |
| 137 | + } else { |
| 138 | + state.Stickiness = types.BoolNull() |
| 139 | + } |
| 140 | + |
| 141 | + resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) |
| 142 | + tflog.Debug(ctx, "Finished reading context field data source", map[string]any{"success": true}) |
| 143 | +} |
0 commit comments