Skip to content

Commit 59f43b9

Browse files
author
Johanne Pereau
committed
feat: add Data sources for Ovhcloud Connect (OCC) products
1 parent 76bdaec commit 59f43b9

9 files changed

+618
-0
lines changed

ovh/data_ovhcloud_connect.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package ovh
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/url"
7+
8+
"github.com/hashicorp/terraform-plugin-framework/datasource"
9+
)
10+
11+
var _ datasource.DataSourceWithConfigure = (*ovhcloudConnectDataSource)(nil)
12+
13+
func NewOvhcloudConnectDataSource() datasource.DataSource {
14+
return &ovhcloudConnectDataSource{}
15+
}
16+
17+
type ovhcloudConnectDataSource struct {
18+
config *Config
19+
}
20+
21+
func (d *ovhcloudConnectDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
22+
resp.TypeName = req.ProviderTypeName + "_ovhcloud_connect"
23+
}
24+
25+
func (d *ovhcloudConnectDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
26+
if req.ProviderData == nil {
27+
return
28+
}
29+
30+
config, ok := req.ProviderData.(*Config)
31+
if !ok {
32+
resp.Diagnostics.AddError(
33+
"Unexpected Data Source Configure Type",
34+
fmt.Sprintf("Expected *Config, got: %T. Please report this issue to the provider developers.", req.ProviderData),
35+
)
36+
return
37+
}
38+
39+
d.config = config
40+
}
41+
42+
func (d *ovhcloudConnectDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
43+
resp.Schema = OvhcloudConnectDataSourceSchema(ctx)
44+
}
45+
46+
func (d *ovhcloudConnectDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
47+
var data OvhcloudConnectModel
48+
49+
// Read Terraform configuration data into the model
50+
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
51+
52+
if resp.Diagnostics.HasError() {
53+
return
54+
}
55+
56+
// Read API call logic
57+
endpoint := "/ovhCloudConnect/" + url.PathEscape(data.ServiceName.ValueString()) + ""
58+
59+
if err := d.config.OVHClient.Get(endpoint, &data); err != nil {
60+
resp.Diagnostics.AddError(
61+
fmt.Sprintf("Error calling Get %s", endpoint),
62+
err.Error(),
63+
)
64+
return
65+
}
66+
67+
// Save data into Terraform state
68+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
69+
}

ovh/data_ovhcloud_connect_gen.go

+189
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ovh/data_ovhcloud_connect_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
9+
)
10+
11+
func TestAccDataSourceOvhCloudConnect_basic(t *testing.T) {
12+
serviceName := os.Getenv("OVH_OCC_SERVICE_TEST")
13+
14+
resource.Test(t, resource.TestCase{
15+
PreCheck: func() {
16+
checkEnvOrSkip(t, "OVH_OCC_SERVICE_TEST")
17+
testAccPreCheckCredentials(t)
18+
},
19+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
20+
Steps: []resource.TestStep{
21+
{
22+
Config: fmt.Sprintf(`
23+
data "ovh_ovhcloud_connect" "occ" {
24+
service_name = "%s"
25+
}
26+
`, serviceName),
27+
Check: resource.ComposeTestCheckFunc(
28+
resource.TestCheckResourceAttr("data.ovh_ovhcloud_connect.occ", "uuid", serviceName),
29+
resource.TestCheckResourceAttrSet("data.ovh_ovhcloud_connect.occ", "bandwidth"),
30+
resource.TestCheckResourceAttrSet("data.ovh_ovhcloud_connect.occ", "description"),
31+
resource.TestCheckResourceAttrSet("data.ovh_ovhcloud_connect.occ", "interface_list.0"),
32+
resource.TestCheckResourceAttrSet("data.ovh_ovhcloud_connect.occ", "pop"),
33+
resource.TestCheckResourceAttrSet("data.ovh_ovhcloud_connect.occ", "port_quantity"),
34+
resource.TestCheckResourceAttrSet("data.ovh_ovhcloud_connect.occ", "product"),
35+
resource.TestCheckResourceAttrSet("data.ovh_ovhcloud_connect.occ", "provider_name"),
36+
resource.TestCheckResourceAttrSet("data.ovh_ovhcloud_connect.occ", "status"),
37+
),
38+
},
39+
},
40+
})
41+
}

ovh/data_ovhcloud_connects.go

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package ovh
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/url"
7+
8+
"github.com/hashicorp/terraform-plugin-framework/datasource"
9+
ovhtypes "github.com/ovh/terraform-provider-ovh/ovh/types"
10+
)
11+
12+
var _ datasource.DataSourceWithConfigure = (*ovhcloudConnectsDataSource)(nil)
13+
14+
func NewOvhcloudConnectsDataSource() datasource.DataSource {
15+
return &ovhcloudConnectsDataSource{}
16+
}
17+
18+
type ovhcloudConnectsDataSource struct {
19+
config *Config
20+
}
21+
22+
func (d *ovhcloudConnectsDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
23+
resp.TypeName = req.ProviderTypeName + "_ovhcloud_connects"
24+
}
25+
26+
func (d *ovhcloudConnectsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
27+
if req.ProviderData == nil {
28+
return
29+
}
30+
31+
config, ok := req.ProviderData.(*Config)
32+
if !ok {
33+
resp.Diagnostics.AddError(
34+
"Unexpected Data Source Configure Type",
35+
fmt.Sprintf("Expected *Config, got: %T. Please report this issue to the provider developers.", req.ProviderData),
36+
)
37+
return
38+
}
39+
40+
d.config = config
41+
}
42+
43+
func (d *ovhcloudConnectsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
44+
resp.Schema = OvhcloudConnectsDataSourceSchema(ctx)
45+
}
46+
47+
func (d *ovhcloudConnectsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
48+
var (
49+
occsIDs []string
50+
data OvhcloudConnectsModel
51+
)
52+
53+
// Retrieve list of occs
54+
if err := d.config.OVHClient.Get("/ovhCloudConnect", &occsIDs); err != nil {
55+
resp.Diagnostics.AddError("Error calling Get /ovhCloudConnect", err.Error())
56+
return
57+
}
58+
59+
// Fetch each occ data
60+
for _, occID := range occsIDs {
61+
var occData OvhcloudConnectModel
62+
endpoint := "/ovhCloudConnect/" + url.PathEscape(occID)
63+
if err := d.config.OVHClient.Get(endpoint, &occData); err != nil {
64+
resp.Diagnostics.AddError(
65+
fmt.Sprintf("Error calling Get %s", endpoint),
66+
err.Error(),
67+
)
68+
return
69+
}
70+
71+
occData.ServiceName = ovhtypes.NewTfStringValue(occID)
72+
73+
data.Occs = append(data.Occs, occData)
74+
}
75+
76+
// Save data into Terraform state
77+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
78+
}

0 commit comments

Comments
 (0)