|
| 1 | +package enable |
| 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-framework/types" |
| 12 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 13 | + "github.com/stackitcloud/stackit-sdk-go/services/serverupdate" |
| 14 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" |
| 15 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" |
| 16 | + serverUpdateUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/serverupdate/utils" |
| 17 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils" |
| 18 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate" |
| 19 | +) |
| 20 | + |
| 21 | +// Ensure the implementation satisfies the expected interfaces. |
| 22 | +var ( |
| 23 | + _ datasource.DataSource = &serverUpdateEnableDataSource{} |
| 24 | +) |
| 25 | + |
| 26 | +type DataModel struct { |
| 27 | + Id types.String `tfsdk:"id"` // needed by TF |
| 28 | + ProjectId types.String `tfsdk:"project_id"` |
| 29 | + ServerId types.String `tfsdk:"server_id"` |
| 30 | + Enabled types.Bool `tfsdk:"enabled"` |
| 31 | + Region types.String `tfsdk:"region"` |
| 32 | +} |
| 33 | + |
| 34 | +// NewServerUpdateEnableDataSource is a helper function to simplify the provider implementation. |
| 35 | +func NewServerUpdateEnableDataSource() datasource.DataSource { |
| 36 | + return &serverUpdateEnableDataSource{} |
| 37 | +} |
| 38 | + |
| 39 | +// serverUpdateEnableDataSource is the data source implementation. |
| 40 | +type serverUpdateEnableDataSource struct { |
| 41 | + client *serverupdate.APIClient |
| 42 | + providerData core.ProviderData |
| 43 | +} |
| 44 | + |
| 45 | +// Metadata returns the data source type name. |
| 46 | +func (d *serverUpdateEnableDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 47 | + resp.TypeName = req.ProviderTypeName + "_server_update_enable" |
| 48 | +} |
| 49 | + |
| 50 | +// Configure adds the provider configured client to the data source. |
| 51 | +func (d *serverUpdateEnableDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 52 | + var ok bool |
| 53 | + d.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) |
| 54 | + if !ok { |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + apiClient := serverUpdateUtils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics) |
| 59 | + if resp.Diagnostics.HasError() { |
| 60 | + return |
| 61 | + } |
| 62 | + d.client = apiClient |
| 63 | + tflog.Info(ctx, "Server update client client configured") |
| 64 | +} |
| 65 | + |
| 66 | +// Schema defines the schema for the resource. |
| 67 | +func (d *serverUpdateEnableDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 68 | + descriptions := map[string]string{ |
| 69 | + "main": "Server update enable datasource schema. Must have a `region` specified in the provider configuration.", |
| 70 | + "id": "Terraform's internal resource identifier. It is structured as \"`project_id`,`server_id`,`region`\".", |
| 71 | + "project_id": "STACKIT Project ID to which the server update enable is associated.", |
| 72 | + "server_id": "Server ID to which the server update enable is associated.", |
| 73 | + "enabled": "Set to true if the service is enabled.", |
| 74 | + "region": "The resource region. If not defined, the provider region is used.", |
| 75 | + } |
| 76 | + |
| 77 | + resp.Schema = schema.Schema{ |
| 78 | + Description: descriptions["main"], |
| 79 | + Attributes: map[string]schema.Attribute{ |
| 80 | + "id": schema.StringAttribute{ |
| 81 | + Description: descriptions["id"], |
| 82 | + Computed: true, |
| 83 | + }, |
| 84 | + "project_id": schema.StringAttribute{ |
| 85 | + Description: descriptions["project_id"], |
| 86 | + Required: true, |
| 87 | + Validators: []validator.String{ |
| 88 | + validate.UUID(), |
| 89 | + validate.NoSeparator(), |
| 90 | + }, |
| 91 | + }, |
| 92 | + "server_id": schema.StringAttribute{ |
| 93 | + Description: descriptions["server_id"], |
| 94 | + Required: true, |
| 95 | + Validators: []validator.String{ |
| 96 | + validate.UUID(), |
| 97 | + validate.NoSeparator(), |
| 98 | + }, |
| 99 | + }, |
| 100 | + "enabled": schema.BoolAttribute{ |
| 101 | + Description: descriptions["enabled"], |
| 102 | + Computed: true, |
| 103 | + }, |
| 104 | + "region": schema.StringAttribute{ |
| 105 | + Optional: true, |
| 106 | + // the region cannot be found automatically, so it has to be passed |
| 107 | + Description: descriptions["region"], |
| 108 | + }, |
| 109 | + }, |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// Read refreshes the Terraform state with the latest data. |
| 114 | +func (d *serverUpdateEnableDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform |
| 115 | + var model DataModel |
| 116 | + diags := req.Config.Get(ctx, &model) |
| 117 | + resp.Diagnostics.Append(diags...) |
| 118 | + if resp.Diagnostics.HasError() { |
| 119 | + return |
| 120 | + } |
| 121 | + |
| 122 | + ctx = core.InitProviderContext(ctx) |
| 123 | + |
| 124 | + projectId := model.ProjectId.ValueString() |
| 125 | + serverId := model.ServerId.ValueString() |
| 126 | + region := d.providerData.GetRegionWithOverride(model.Region) |
| 127 | + |
| 128 | + ctx = tflog.SetField(ctx, "project_id", projectId) |
| 129 | + ctx = tflog.SetField(ctx, "server_id", serverId) |
| 130 | + ctx = tflog.SetField(ctx, "region", region) |
| 131 | + |
| 132 | + serviceResp, err := d.client.GetServiceResource(ctx, projectId, serverId, region).Execute() |
| 133 | + if err != nil { |
| 134 | + utils.LogError( |
| 135 | + ctx, |
| 136 | + &resp.Diagnostics, |
| 137 | + err, |
| 138 | + "Reading server update enable", |
| 139 | + fmt.Sprintf("Server update enable does not exist for this server %q.", serverId), |
| 140 | + map[int]string{ |
| 141 | + http.StatusForbidden: fmt.Sprintf("Project with ID %q or server with ID %q not found or forbidden access", projectId, serverId), |
| 142 | + }, |
| 143 | + ) |
| 144 | + resp.State.RemoveResource(ctx) |
| 145 | + return |
| 146 | + } |
| 147 | + |
| 148 | + ctx = core.LogResponse(ctx) |
| 149 | + |
| 150 | + // Map response body to schema |
| 151 | + err = mapDataFields(serviceResp, &model, region) |
| 152 | + if err != nil { |
| 153 | + core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading server update enable", fmt.Sprintf("Processing API payload: %v", err)) |
| 154 | + return |
| 155 | + } |
| 156 | + |
| 157 | + // Set refreshed state |
| 158 | + diags = resp.State.Set(ctx, model) |
| 159 | + resp.Diagnostics.Append(diags...) |
| 160 | + if resp.Diagnostics.HasError() { |
| 161 | + return |
| 162 | + } |
| 163 | + tflog.Info(ctx, "Server update enable read") |
| 164 | +} |
| 165 | + |
| 166 | +func mapDataFields(serviceResp *serverupdate.GetUpdateServiceResponse, model *DataModel, region string) error { |
| 167 | + if serviceResp == nil { |
| 168 | + return fmt.Errorf("response input is nil") |
| 169 | + } |
| 170 | + if model == nil { |
| 171 | + return fmt.Errorf("model input is nil") |
| 172 | + } |
| 173 | + |
| 174 | + model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), model.ServerId.ValueString(), region) |
| 175 | + model.Region = types.StringValue(region) |
| 176 | + model.Enabled = types.BoolPointerValue(serviceResp.Enabled) |
| 177 | + |
| 178 | + return nil |
| 179 | +} |
0 commit comments