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

datasource volume(s) #736

Merged
merged 5 commits into from
Oct 11, 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
13 changes: 13 additions & 0 deletions .cds/terraform-provider-ovh.yml
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,18 @@ workflow:
pipeline: terraform-provider-ovh-testacc
when:
- success
Tests_CloudProjectVolume:
application: terraform-provider-ovh
depends_on:
- terraform-provider-ovh-pre-sweepers
environment: acctests
one_at_a_time: true
parameters:
testargs: -run CloudProjectVolume
skipthispipeline: "{{.workflow.terraform-provider-ovh.pip.skipthistest.cloudproject}}"
pipeline: terraform-provider-ovh-testacc
when:
- success
Tests_CloudProjectLoadBalancer:
application: terraform-provider-ovh
depends_on:
Expand Down Expand Up @@ -781,6 +793,7 @@ workflow:
- Tests_TestAccCloudProjectRegions
- Tests_CloudProjectUser
- Tests_CloudProjectLoadBalancer
- Tests_CloudProjectVolume
- Tests_DbaasLogs
- Tests_DedicatedCeph
- Tests_TestAccDedicatedInstallationTemplate
Expand Down
69 changes: 69 additions & 0 deletions ovh/data_cloud_project_volume.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package ovh

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

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

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

func NewCloudProjectVolumeDataSource() datasource.DataSource {
return &cloudProjectVolumeDataSource{}
}

type cloudProjectVolumeDataSource struct {
config *Config
}

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

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

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

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

if resp.Diagnostics.HasError() {
return
}

// Read API call logic
endpoint := "/cloud/project/" + url.PathEscape(data.ServiceName.ValueString()) + "/region/" + url.PathEscape(data.RegionName.ValueString()) + "/volume/" + url.PathEscape(data.VolumeId.ValueString()) + ""

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

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
93 changes: 93 additions & 0 deletions ovh/data_cloud_project_volume_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions ovh/data_cloud_project_volume_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ovh

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccDataSourceCloudProjectVolume_basic(t *testing.T) {
serviceName := os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST")
regionName := os.Getenv("OVH_CLOUD_PROJECT_REGION_TEST")
volumeId := os.Getenv("OVH_CLOUD_PROJECT_VOLUME_ID_TEST")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckCloud(t); testAccCheckCloudProjectExists(t) },
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
data "ovh_cloud_project_volume" "volume" {
service_name = "%s"
region_name = "%s"
volume_id = "%s"
}
`,
serviceName,
regionName,
volumeId,
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.ovh_cloud_project_volume.volume", "region_name", os.Getenv("OVH_CLOUD_PROJECT_REGION_TEST")),
resource.TestCheckResourceAttrSet("data.ovh_cloud_project_volume.volume", "name"),
resource.TestCheckResourceAttrSet("data.ovh_cloud_project_volume.volume", "volume_id"),
),
},
},
})
}
83 changes: 83 additions & 0 deletions ovh/data_cloud_project_volumes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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 = (*cloudProjectVolumesDataSource)(nil)

func NewCloudProjectVolumesDataSource() datasource.DataSource {
return &cloudProjectVolumesDataSource{}
}

type cloudProjectVolumesDataSource struct {
config *Config
}

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

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

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

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

if resp.Diagnostics.HasError() {
return
}

// Read API call logic
endpoint := "/cloud/project/" + url.PathEscape(data.ServiceName.ValueString()) + "/region/" + url.PathEscape(data.RegionName.ValueString()) + "/volume"

var arr []CloudProjectVolumesValue

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.CloudProjectVolumes = ovhtypes.TfListNestedValue[CloudProjectVolumesValue]{
ListValue: basetypes.NewListValueMust(CloudProjectVolumesValue{}.Type(ctx), b),
}

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