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