Skip to content

feat: add Data sources for Ovhcloud Connect (OCC) products #871

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

Merged
merged 1 commit into from
Mar 3, 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
69 changes: 69 additions & 0 deletions ovh/data_ovhcloud_connect.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 = (*ovhcloudConnectDataSource)(nil)

func NewOvhcloudConnectDataSource() datasource.DataSource {
return &ovhcloudConnectDataSource{}
}

type ovhcloudConnectDataSource struct {
config *Config
}

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

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

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

// 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 := "/ovhCloudConnect/" + url.PathEscape(data.ServiceName.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)...)
}
189 changes: 189 additions & 0 deletions ovh/data_ovhcloud_connect_gen.go

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

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

import (
"fmt"
"os"
"testing"

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

func TestAccDataSourceOvhCloudConnect_basic(t *testing.T) {
serviceName := os.Getenv("OVH_OCC_SERVICE_TEST")

resource.Test(t, resource.TestCase{
PreCheck: func() {
checkEnvOrSkip(t, "OVH_OCC_SERVICE_TEST")
testAccPreCheckCredentials(t)
},
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
data "ovh_ovhcloud_connect" "occ" {
service_name = "%s"
}
`, serviceName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.ovh_ovhcloud_connect.occ", "uuid", serviceName),
resource.TestCheckResourceAttrSet("data.ovh_ovhcloud_connect.occ", "bandwidth"),
resource.TestCheckResourceAttrSet("data.ovh_ovhcloud_connect.occ", "description"),
resource.TestCheckResourceAttrSet("data.ovh_ovhcloud_connect.occ", "interface_list.0"),
resource.TestCheckResourceAttrSet("data.ovh_ovhcloud_connect.occ", "pop"),
resource.TestCheckResourceAttrSet("data.ovh_ovhcloud_connect.occ", "port_quantity"),
resource.TestCheckResourceAttrSet("data.ovh_ovhcloud_connect.occ", "product"),
resource.TestCheckResourceAttrSet("data.ovh_ovhcloud_connect.occ", "provider_name"),
resource.TestCheckResourceAttrSet("data.ovh_ovhcloud_connect.occ", "status"),
),
},
},
})
}
78 changes: 78 additions & 0 deletions ovh/data_ovhcloud_connects.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package ovh

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

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

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

func NewOvhcloudConnectsDataSource() datasource.DataSource {
return &ovhcloudConnectsDataSource{}
}

type ovhcloudConnectsDataSource struct {
config *Config
}

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

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

func (d *ovhcloudConnectsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var (
occsIDs []string
data OvhcloudConnectsModel
)

// Retrieve list of occs
if err := d.config.OVHClient.Get("/ovhCloudConnect", &occsIDs); err != nil {
resp.Diagnostics.AddError("Error calling Get /ovhCloudConnect", err.Error())
return
}

// Fetch each occ data
for _, occID := range occsIDs {
var occData OvhcloudConnectModel
endpoint := "/ovhCloudConnect/" + url.PathEscape(occID)
if err := d.config.OVHClient.Get(endpoint, &occData); err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error calling Get %s", endpoint),
err.Error(),
)
return
}

occData.ServiceName = ovhtypes.NewTfStringValue(occID)

data.Occs = append(data.Occs, occData)
}

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