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