|
| 1 | +package compliancelock |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 11 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 12 | + "github.com/stackitcloud/stackit-sdk-go/services/objectstorage" |
| 13 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" |
| 14 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" |
| 15 | + objectstorageUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/objectstorage/utils" |
| 16 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils" |
| 17 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate" |
| 18 | +) |
| 19 | + |
| 20 | +// Ensure the implementation satisfies the expected interfaces. |
| 21 | +var ( |
| 22 | + _ datasource.DataSource = &compliancelockDataSource{} |
| 23 | +) |
| 24 | + |
| 25 | +// NewComplianceLockDataSource is a helper function to simplify the provider implementation. |
| 26 | +func NewComplianceLockDataSource() datasource.DataSource { |
| 27 | + return &compliancelockDataSource{} |
| 28 | +} |
| 29 | + |
| 30 | +// compliancelockDataSource is the data source implementation. |
| 31 | +type compliancelockDataSource struct { |
| 32 | + client *objectstorage.APIClient |
| 33 | + providerData core.ProviderData |
| 34 | +} |
| 35 | + |
| 36 | +// Metadata returns the data source type name. |
| 37 | +func (d *compliancelockDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 38 | + resp.TypeName = req.ProviderTypeName + "_objectstorage_compliance_lock" |
| 39 | +} |
| 40 | + |
| 41 | +// Configure adds the provider configured client to the data source. |
| 42 | +func (d *compliancelockDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 43 | + var ok bool |
| 44 | + d.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) |
| 45 | + if !ok { |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + apiClient := objectstorageUtils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics) |
| 50 | + if resp.Diagnostics.HasError() { |
| 51 | + return |
| 52 | + } |
| 53 | + d.client = apiClient |
| 54 | + tflog.Info(ctx, "ObjectStorage compliance lock client configured") |
| 55 | +} |
| 56 | + |
| 57 | +// Schema defines the schema for the resource. |
| 58 | +func (d *compliancelockDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 59 | + descriptions := map[string]string{ |
| 60 | + "main": "ObjectStorage compliance lock resource schema. Must have a `region` specified in the provider configuration.", |
| 61 | + "id": "Terraform's internal resource identifier. It is structured as \"`project_id`,`region`\".", |
| 62 | + "project_id": "STACKIT Project ID to which the compliance lock is associated.", |
| 63 | + "region": "The resource region. If not defined, the provider region is used.", |
| 64 | + "max_retention_days": "Maximum retention period in days.", |
| 65 | + } |
| 66 | + |
| 67 | + resp.Schema = schema.Schema{ |
| 68 | + Description: descriptions["main"], |
| 69 | + Attributes: map[string]schema.Attribute{ |
| 70 | + "id": schema.StringAttribute{ |
| 71 | + Description: descriptions["id"], |
| 72 | + Computed: true, |
| 73 | + }, |
| 74 | + "project_id": schema.StringAttribute{ |
| 75 | + Description: descriptions["project_id"], |
| 76 | + Required: true, |
| 77 | + Validators: []validator.String{ |
| 78 | + validate.UUID(), |
| 79 | + validate.NoSeparator(), |
| 80 | + }, |
| 81 | + }, |
| 82 | + "max_retention_days": schema.Int64Attribute{ |
| 83 | + Description: descriptions["max_retention_days"], |
| 84 | + Computed: true, |
| 85 | + }, |
| 86 | + "region": schema.StringAttribute{ |
| 87 | + Optional: true, |
| 88 | + // the region cannot be found automatically, so it has to be passed |
| 89 | + Description: descriptions["region"], |
| 90 | + }, |
| 91 | + }, |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// Read refreshes the Terraform state with the latest data. |
| 96 | +func (d *compliancelockDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform |
| 97 | + var model Model |
| 98 | + diags := req.Config.Get(ctx, &model) |
| 99 | + resp.Diagnostics.Append(diags...) |
| 100 | + if resp.Diagnostics.HasError() { |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + ctx = core.InitProviderContext(ctx) |
| 105 | + |
| 106 | + projectId := model.ProjectId.ValueString() |
| 107 | + region := d.providerData.GetRegionWithOverride(model.Region) |
| 108 | + |
| 109 | + ctx = tflog.SetField(ctx, "project_id", projectId) |
| 110 | + ctx = tflog.SetField(ctx, "region", region) |
| 111 | + |
| 112 | + complianceResp, err := d.client.GetComplianceLock(ctx, projectId, region).Execute() |
| 113 | + if err != nil { |
| 114 | + utils.LogError( |
| 115 | + ctx, |
| 116 | + &resp.Diagnostics, |
| 117 | + err, |
| 118 | + "Reading compliance lock", |
| 119 | + fmt.Sprintf("Compliance lock does not exist in project %q.", projectId), |
| 120 | + map[int]string{ |
| 121 | + http.StatusForbidden: fmt.Sprintf("Project with ID %q not found or forbidden access", projectId), |
| 122 | + }, |
| 123 | + ) |
| 124 | + resp.State.RemoveResource(ctx) |
| 125 | + return |
| 126 | + } |
| 127 | + |
| 128 | + ctx = core.LogResponse(ctx) |
| 129 | + |
| 130 | + // Map response body to schema |
| 131 | + err = mapFields(complianceResp, &model, region) |
| 132 | + if err != nil { |
| 133 | + core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading compliance lock", fmt.Sprintf("Processing API payload: %v", err)) |
| 134 | + return |
| 135 | + } |
| 136 | + |
| 137 | + // Set refreshed state |
| 138 | + diags = resp.State.Set(ctx, model) |
| 139 | + resp.Diagnostics.Append(diags...) |
| 140 | + if resp.Diagnostics.HasError() { |
| 141 | + return |
| 142 | + } |
| 143 | + tflog.Info(ctx, "ObjectStorage compliance lock read") |
| 144 | +} |
0 commit comments