|
| 1 | +package instance |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 14 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 15 | + "github.com/stackitcloud/stackit-sdk-go/core/oapierror" |
| 16 | + "github.com/stackitcloud/stackit-sdk-go/services/logs" |
| 17 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" |
| 18 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" |
| 19 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/features" |
| 20 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/logs/utils" |
| 21 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate" |
| 22 | +) |
| 23 | + |
| 24 | +var ( |
| 25 | + _ datasource.DataSource = &logsInstanceDataSource{} |
| 26 | +) |
| 27 | + |
| 28 | +func NewLogsInstanceDataSource() datasource.DataSource { |
| 29 | + return &logsInstanceDataSource{} |
| 30 | +} |
| 31 | + |
| 32 | +type logsInstanceDataSource struct { |
| 33 | + client *logs.APIClient |
| 34 | + providerData core.ProviderData |
| 35 | +} |
| 36 | + |
| 37 | +func (d *logsInstanceDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 38 | + resp.TypeName = req.ProviderTypeName + "_logs_instance" |
| 39 | +} |
| 40 | + |
| 41 | +func (d *logsInstanceDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 42 | + providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) |
| 43 | + if !ok { |
| 44 | + return |
| 45 | + } |
| 46 | + d.providerData = providerData |
| 47 | + |
| 48 | + apiClient := utils.ConfigureClient(ctx, &providerData, &resp.Diagnostics) |
| 49 | + if resp.Diagnostics.HasError() { |
| 50 | + return |
| 51 | + } |
| 52 | + d.client = apiClient |
| 53 | + tflog.Info(ctx, "Logs client configured") |
| 54 | +} |
| 55 | + |
| 56 | +func (d *logsInstanceDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 57 | + resp.Schema = schema.Schema{ |
| 58 | + MarkdownDescription: features.AddBetaDescription("Logs instance data source schema.", core.Datasource), |
| 59 | + Description: fmt.Sprintf("Logs instance resource schema. %s", core.DatasourceRegionFallbackDocstring), |
| 60 | + Attributes: map[string]schema.Attribute{ |
| 61 | + "id": schema.StringAttribute{ |
| 62 | + Description: schemaDescriptions["id"], |
| 63 | + Computed: true, |
| 64 | + }, |
| 65 | + "instance_id": schema.StringAttribute{ |
| 66 | + Description: schemaDescriptions["instance_id"], |
| 67 | + Required: true, |
| 68 | + Validators: []validator.String{ |
| 69 | + validate.UUID(), |
| 70 | + validate.NoSeparator(), |
| 71 | + }, |
| 72 | + }, |
| 73 | + "region": schema.StringAttribute{ |
| 74 | + Description: schemaDescriptions["region"], |
| 75 | + // the region cannot be found, so it has to be passed |
| 76 | + Optional: true, |
| 77 | + }, |
| 78 | + "project_id": schema.StringAttribute{ |
| 79 | + Description: schemaDescriptions["project_id"], |
| 80 | + Required: true, |
| 81 | + Validators: []validator.String{ |
| 82 | + validate.UUID(), |
| 83 | + validate.NoSeparator(), |
| 84 | + }, |
| 85 | + }, |
| 86 | + "acl": schema.ListAttribute{ |
| 87 | + Description: schemaDescriptions["acl"], |
| 88 | + ElementType: types.StringType, |
| 89 | + Computed: true, |
| 90 | + }, |
| 91 | + "created": schema.StringAttribute{ |
| 92 | + Description: schemaDescriptions["created"], |
| 93 | + Computed: true, |
| 94 | + }, |
| 95 | + "datasource_url": schema.StringAttribute{ |
| 96 | + Description: schemaDescriptions["datasource_url"], |
| 97 | + Computed: true, |
| 98 | + }, |
| 99 | + "description": schema.StringAttribute{ |
| 100 | + Description: schemaDescriptions["description"], |
| 101 | + Computed: true, |
| 102 | + }, |
| 103 | + "display_name": schema.StringAttribute{ |
| 104 | + Description: schemaDescriptions["display_name"], |
| 105 | + Computed: true, |
| 106 | + Validators: []validator.String{stringvalidator.LengthAtLeast(1)}, |
| 107 | + }, |
| 108 | + "ingest_otlp_url": schema.StringAttribute{ |
| 109 | + Description: schemaDescriptions["ingest_otlp_url"], |
| 110 | + Computed: true, |
| 111 | + }, |
| 112 | + "ingest_url": schema.StringAttribute{ |
| 113 | + Description: schemaDescriptions["ingest_url"], |
| 114 | + Computed: true, |
| 115 | + }, |
| 116 | + "query_range_url": schema.StringAttribute{ |
| 117 | + Description: schemaDescriptions["query_range_url"], |
| 118 | + Computed: true, |
| 119 | + }, |
| 120 | + "query_url": schema.StringAttribute{ |
| 121 | + Description: schemaDescriptions["query_url"], |
| 122 | + Computed: true, |
| 123 | + }, |
| 124 | + "retention_days": schema.Int64Attribute{ |
| 125 | + Description: schemaDescriptions["retention_days"], |
| 126 | + Computed: true, |
| 127 | + }, |
| 128 | + "status": schema.StringAttribute{ |
| 129 | + Description: schemaDescriptions["status"], |
| 130 | + Computed: true, |
| 131 | + }, |
| 132 | + }, |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func (d *logsInstanceDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform |
| 137 | + var model Model |
| 138 | + diags := req.Config.Get(ctx, &model) |
| 139 | + resp.Diagnostics.Append(diags...) |
| 140 | + if resp.Diagnostics.HasError() { |
| 141 | + return |
| 142 | + } |
| 143 | + |
| 144 | + ctx = core.InitProviderContext(ctx) |
| 145 | + |
| 146 | + projectID := model.ProjectID.ValueString() |
| 147 | + region := d.providerData.GetRegionWithOverride(model.Region) |
| 148 | + instanceID := model.InstanceID.ValueString() |
| 149 | + |
| 150 | + ctx = tflog.SetField(ctx, "project_id", projectID) |
| 151 | + ctx = tflog.SetField(ctx, "region", region) |
| 152 | + ctx = tflog.SetField(ctx, "instance_id", instanceID) |
| 153 | + |
| 154 | + instanceResponse, err := d.client.GetLogsInstance(ctx, projectID, region, instanceID).Execute() |
| 155 | + if err != nil { |
| 156 | + var oapiErr *oapierror.GenericOpenAPIError |
| 157 | + ok := errors.As(err, &oapiErr) |
| 158 | + if ok && oapiErr.StatusCode == http.StatusNotFound { |
| 159 | + resp.State.RemoveResource(ctx) |
| 160 | + return |
| 161 | + } |
| 162 | + core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading logs instance", fmt.Sprintf("Calling API: %v", err)) |
| 163 | + return |
| 164 | + } |
| 165 | + ctx = core.LogResponse(ctx) |
| 166 | + |
| 167 | + err = mapFields(ctx, instanceResponse, &model) |
| 168 | + if err != nil { |
| 169 | + core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading logs instance", fmt.Sprintf("Processing response: %v", err)) |
| 170 | + return |
| 171 | + } |
| 172 | + diags = resp.State.Set(ctx, model) |
| 173 | + resp.Diagnostics.Append(diags...) |
| 174 | + if resp.Diagnostics.HasError() { |
| 175 | + return |
| 176 | + } |
| 177 | + tflog.Info(ctx, "Logs Instance read", map[string]interface{}{ |
| 178 | + "instance_id": instanceID, |
| 179 | + }) |
| 180 | +} |
0 commit comments