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

feat: Add datasource ovh_cloud_project_flavors #865

Merged
merged 1 commit into from
Feb 26, 2025
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
86 changes: 86 additions & 0 deletions ovh/data_cloud_project_flavors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package ovh

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

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
ovhtypes "github.com/ovh/terraform-provider-ovh/ovh/types"
)

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

func NewCloudProjectFlavorsDataSource() datasource.DataSource {
return &cloudProjectFlavorsDataSource{}
}

type cloudProjectFlavorsDataSource struct {
config *Config
}

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

func (d *cloudProjectFlavorsDataSource) 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 *cloudProjectFlavorsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = CloudProjectFlavorsDataSourceSchema(ctx)
}

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

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

if resp.Diagnostics.HasError() {
return
}

// Read API call logic
query := ""
if !data.Region.IsNull() && !data.Region.IsUnknown() {
query = "?region=" + url.QueryEscape(data.Region.ValueString())
}
endpoint := "/cloud/project/" + url.PathEscape(data.ServiceName.ValueString()) + "/flavor" + query

var arr []CloudProjectFlavorValue
if err := d.config.OVHClient.Get(endpoint, &arr); err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error calling Get %s", endpoint),
err.Error(),
)
return
}

var b []attr.Value
for _, a := range arr {
b = append(b, a)
}

data.Flavors = ovhtypes.TfListNestedValue[CloudProjectFlavorValue]{
ListValue: basetypes.NewListValueMust(CloudProjectFlavorValue{}.Type(ctx), b),
}

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