Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cloud_project_loadbalancer(s) datasource #648

Merged
merged 2 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ export OVH_IP_REVERSE_TEST="..."
export OVH_IP_MOVE_SERVICE_NAME_TEST="..."
export OVH_IPLB_SERVICE_TEST="..."
export OVH_CLOUD_PROJECT_SERVICE_TEST="..."
export OVH_CLOUD_PROJECT_REGION_TEST="..."
export OVH_CLOUD_PROJECT_LOADBALANCER_TEST="..."
export OVH_CLOUD_PROJECT_FAILOVER_IP_TEST="..."
export OVH_CLOUD_PROJECT_FAILOVER_IP_ROUTED_TO_1_TEST="..."
export OVH_CLOUD_PROJECT_FAILOVER_IP_ROUTED_TO_2_TEST="..."
Expand Down
76 changes: 76 additions & 0 deletions ovh/data_cloud_project_loadbalancer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package ovh

import (
"context"
"fmt"
"net/url"
"os"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)

var _ datasource.DataSourceWithConfigure = (*cloudProjectLoadbalancerDataSource)(nil)

func NewCloudProjectLoadbalancerDataSource() datasource.DataSource {
return &cloudProjectLoadbalancerDataSource{}
}

type cloudProjectLoadbalancerDataSource struct {
config *Config
}

func (d *cloudProjectLoadbalancerDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_cloud_project_loadbalancer"
}

func (d *cloudProjectLoadbalancerDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

config, ok := req.ProviderData.(*Config)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *Config, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}

d.config = config
}

func (d *cloudProjectLoadbalancerDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = CloudProjectLoadbalancerDataSourceSchema(ctx)
}

func (d *cloudProjectLoadbalancerDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data CloudProjectLoadbalancerModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}

if data.ServiceName.IsNull() {
data.ServiceName.StringValue = basetypes.NewStringValue(os.Getenv("OVH_CLOUD_PROJECT_SERVICE"))
}

// Read API call logic
endpoint := fmt.Sprintf("/cloud/project/%s/region/%s/loadbalancing/loadbalancer/%s",
url.PathEscape(data.ServiceName.ValueString()),
url.PathEscape(data.RegionName.ValueString()),
url.PathEscape(data.Id.ValueString()),
)

if err := d.config.OVHClient.Get(endpoint, &data); err != nil {
resp.Diagnostics.AddError("Failed to get loadbalancer details", fmt.Sprintf("error calling GET %s: %s", endpoint, err))
return
}

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
Loading