|
| 1 | +package ovh |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/url" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 9 | +) |
| 10 | + |
| 11 | +var _ datasource.DataSourceWithConfigure = (*dedicatedServerSpecificationsNetworkDataSource)(nil) |
| 12 | + |
| 13 | +func NewDedicatedServerSpecificationsNetworkDataSource() datasource.DataSource { |
| 14 | + return &dedicatedServerSpecificationsNetworkDataSource{} |
| 15 | +} |
| 16 | + |
| 17 | +type dedicatedServerSpecificationsNetworkDataSource struct { |
| 18 | + config *Config |
| 19 | +} |
| 20 | + |
| 21 | +func (d *dedicatedServerSpecificationsNetworkDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 22 | + resp.TypeName = req.ProviderTypeName + "_dedicated_server_specifications_network" |
| 23 | +} |
| 24 | + |
| 25 | +func (d *dedicatedServerSpecificationsNetworkDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 26 | + if req.ProviderData == nil { |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + config, ok := req.ProviderData.(*Config) |
| 31 | + if !ok { |
| 32 | + resp.Diagnostics.AddError( |
| 33 | + "Unexpected Data Source Configure Type", |
| 34 | + fmt.Sprintf("Expected *Config, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 35 | + ) |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + d.config = config |
| 40 | +} |
| 41 | + |
| 42 | +func (d *dedicatedServerSpecificationsNetworkDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 43 | + resp.Schema = DedicatedServerSpecificationsNetworkDataSourceSchema(ctx) |
| 44 | +} |
| 45 | + |
| 46 | +func (d *dedicatedServerSpecificationsNetworkDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 47 | + var data DedicatedServerSpecificationsNetworkModel |
| 48 | + |
| 49 | + // Read Terraform configuration data into the model |
| 50 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 51 | + |
| 52 | + if resp.Diagnostics.HasError() { |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + // Read API call logic |
| 57 | + endpoint := "/dedicated/server/" + url.PathEscape(data.ServiceName.ValueString()) + "/specifications/network" |
| 58 | + if err := d.config.OVHClient.Get(endpoint, &data); err != nil { |
| 59 | + resp.Diagnostics.AddError( |
| 60 | + fmt.Sprintf("Error calling Get %s", endpoint), |
| 61 | + err.Error(), |
| 62 | + ) |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + // Save data into Terraform state |
| 67 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 68 | +} |
0 commit comments