|
| 1 | +// Copyright (c) 2025 - Stacklet, Inc. |
| 2 | + |
| 3 | +package datasources |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 12 | + |
| 13 | + "github.com/stacklet/terraform-provider-stacklet/internal/api" |
| 14 | + "github.com/stacklet/terraform-provider-stacklet/internal/errors" |
| 15 | + "github.com/stacklet/terraform-provider-stacklet/internal/models" |
| 16 | + "github.com/stacklet/terraform-provider-stacklet/internal/providerdata" |
| 17 | +) |
| 18 | + |
| 19 | +var ( |
| 20 | + _ datasource.DataSource = &configurationProfileEmailDataSource{} |
| 21 | +) |
| 22 | + |
| 23 | +func NewConfigurationProfileEmailDataSource() datasource.DataSource { |
| 24 | + return &configurationProfileEmailDataSource{} |
| 25 | +} |
| 26 | + |
| 27 | +type configurationProfileEmailDataSource struct { |
| 28 | + api *api.API |
| 29 | +} |
| 30 | + |
| 31 | +func (d *configurationProfileEmailDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 32 | + resp.TypeName = req.ProviderTypeName + "_configuration_profile_email" |
| 33 | +} |
| 34 | + |
| 35 | +func (d *configurationProfileEmailDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 36 | + resp.Schema = schema.Schema{ |
| 37 | + Description: "Retrieve information about the email configuration profile.", |
| 38 | + Attributes: map[string]schema.Attribute{ |
| 39 | + "id": schema.StringAttribute{ |
| 40 | + Description: "The GraphQL Node ID of the configuration profile.", |
| 41 | + Computed: true, |
| 42 | + }, |
| 43 | + "profile": schema.StringAttribute{ |
| 44 | + Description: "The profile name.", |
| 45 | + Computed: true, |
| 46 | + }, |
| 47 | + "from": schema.StringAttribute{ |
| 48 | + Description: "The email from field value.", |
| 49 | + Computed: true, |
| 50 | + }, |
| 51 | + "smtp": schema.SingleNestedAttribute{ |
| 52 | + Description: "SMTP configuration.", |
| 53 | + Computed: true, |
| 54 | + Attributes: map[string]schema.Attribute{ |
| 55 | + "server": schema.StringAttribute{ |
| 56 | + Description: "SMTP server hostname or IP address.", |
| 57 | + Computed: true, |
| 58 | + }, |
| 59 | + "port": schema.StringAttribute{ |
| 60 | + Description: "SMTP server port.", |
| 61 | + Computed: true, |
| 62 | + }, |
| 63 | + "ssl": schema.BoolAttribute{ |
| 64 | + Description: "Whether SSL/TLS is enabled.", |
| 65 | + Computed: true, |
| 66 | + }, |
| 67 | + "username": schema.StringAttribute{ |
| 68 | + Description: "Authentication username.", |
| 69 | + Computed: true, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func (d *configurationProfileEmailDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 78 | + if pd, err := providerdata.GetDataSourceProviderData(req); err != nil { |
| 79 | + errors.AddDiagError(&resp.Diagnostics, err) |
| 80 | + } else if pd != nil { |
| 81 | + d.api = pd.API |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func (d *configurationProfileEmailDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 86 | + var data models.ConfigurationProfileEmailDataSource |
| 87 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 88 | + if resp.Diagnostics.HasError() { |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + config, err := d.api.ConfigurationProfile.Read(ctx, api.ConfigurationProfileEmail) |
| 93 | + if err != nil { |
| 94 | + errors.AddDiagError(&resp.Diagnostics, err) |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + data.ID = types.StringValue(config.ID) |
| 99 | + data.Profile = types.StringValue(config.Profile) |
| 100 | + data.From = types.StringValue(config.Record.EmailConfiguration.FromEmail) |
| 101 | + |
| 102 | + if smtp := config.Record.EmailConfiguration.SMTP; smtp != nil { |
| 103 | + smtpAttrs := map[string]attr.Value{ |
| 104 | + "server": types.StringValue(smtp.Server), |
| 105 | + "port": types.StringValue(smtp.Port), |
| 106 | + "ssl": types.BoolValue(smtp.SSL != nil && *smtp.SSL), |
| 107 | + "username": func() types.String { |
| 108 | + if smtp.Username != nil { |
| 109 | + return types.StringValue(*smtp.Username) |
| 110 | + } |
| 111 | + return types.StringNull() |
| 112 | + }(), |
| 113 | + } |
| 114 | + |
| 115 | + smtpObj, diags := types.ObjectValue(models.SMTP{}.AttributeTypes(), smtpAttrs) |
| 116 | + resp.Diagnostics.Append(diags...) |
| 117 | + if resp.Diagnostics.HasError() { |
| 118 | + return |
| 119 | + } |
| 120 | + data.SMTP = smtpObj |
| 121 | + } else { |
| 122 | + data.SMTP = types.ObjectNull(models.SMTP{}.AttributeTypes()) |
| 123 | + } |
| 124 | + |
| 125 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 126 | +} |
0 commit comments