Skip to content

Commit 9941e80

Browse files
dmicheneauDavid MICHENEAU
authored andcommitted
feat: Add ALB Service Engine Group datasource (#900)
Co-authored-by: David MICHENEAU <david.micheneau@orange.com>
1 parent 06305e2 commit 9941e80

10 files changed

+486
-0
lines changed

.changelog/892.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-data-source
2+
`datasource/cloudavenue_alb_service_engine_group` - Added a new data source to retrieve information about a Service Engine Group.
3+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
page_title: "cloudavenue_alb_service_engine_group Data Source - cloudavenue"
3+
subcategory: "ALB (Advanced Load Balancer)"
4+
description: |-
5+
The cloudavenue_alb_service_engine_group data source allows you to retrieve information about an ALB Service Engine Group.
6+
---
7+
8+
# cloudavenue_alb_service_engine_group (Data Source)
9+
10+
The `cloudavenue_alb_service_engine_group` data source allows you to retrieve information about an ALB Service Engine Group.
11+
12+
## Example Usage
13+
14+
```terraform
15+
data "cloudavenue_alb_service_engine_group" "example" {
16+
name = "my-service-engine"
17+
edge_gateway_name = data.cloudavenue_edge_gateway.example.name
18+
}
19+
20+
output "example" {
21+
value = data.cloudavenue_alb_service_engine_group.example
22+
}
23+
```
24+
25+
<!-- schema generated by tfplugindocs -->
26+
## Schema
27+
28+
### Optional
29+
30+
- `edge_gateway_id` (String) Edge gateway ID in which ALB Service Engine Group should be located. Ensure that one and only one attribute from this collection is set : `edge_gateway_id`, `edge_gateway_name`.
31+
- `edge_gateway_name` (String) Edge gateway Name in which ALB Service Engine Group should be located. Ensure that one and only one attribute from this collection is set : `edge_gateway_id`, `edge_gateway_name`.
32+
- `id` (String) The ID of the ALB Service Engine Group. Ensure that one and only one attribute from this collection is set : `id`, `name`.
33+
- `name` (String) The name of the ALB Service Engine Group. Ensure that one and only one attribute from this collection is set : `id`, `name`.
34+
35+
### Read-Only
36+
37+
- `deployed_virtual_services` (Number) The number of deployed virtual services on the ALB Service Engine Group.
38+
- `max_virtual_services` (Number) The maximum number of virtual services that can be deployed on the ALB Service Engine Group.
39+
- `reserved_virtual_services` (Number) The number of reserved virtual services for the ALB Service Engine Group.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
data "cloudavenue_alb_service_engine_group" "example" {
2+
name = "my-service-engine"
3+
edge_gateway_name = data.cloudavenue_edge_gateway.example.name
4+
}
5+
6+
output "example" {
7+
value = data.cloudavenue_alb_service_engine_group.example
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Package alb provides a Terraform datasource.
2+
package alb
3+
4+
import (
5+
"context"
6+
"fmt"
7+
8+
"github.com/hashicorp/terraform-plugin-framework/diag"
9+
10+
"github.com/hashicorp/terraform-plugin-framework/datasource"
11+
12+
v1 "github.com/orange-cloudavenue/cloudavenue-sdk-go/v1"
13+
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/client"
14+
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/metrics"
15+
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/common/edgegw"
16+
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/common/org"
17+
)
18+
19+
var (
20+
_ datasource.DataSource = &albServiceEngineGroupDataSource{}
21+
_ datasource.DataSourceWithConfigure = &albServiceEngineGroupDataSource{}
22+
)
23+
24+
func NewALBServiceEngineGroupDataSource() datasource.DataSource {
25+
return &albServiceEngineGroupDataSource{}
26+
}
27+
28+
type albServiceEngineGroupDataSource struct {
29+
client *client.CloudAvenue
30+
edgegw edgegw.EdgeGateway
31+
org org.Org
32+
}
33+
34+
// Init Initializes the data source.
35+
func (d *albServiceEngineGroupDataSource) Init(ctx context.Context, dm *albServiceEngineGroupModel) (diags diag.Diagnostics) {
36+
var err error
37+
38+
d.org, diags = org.Init(d.client)
39+
if diags.HasError() {
40+
return
41+
}
42+
43+
// Retrieve VDC from edge gateway
44+
d.edgegw, err = d.org.GetEdgeGateway(edgegw.BaseEdgeGW{
45+
ID: dm.EdgeGatewayID.StringValue,
46+
Name: dm.EdgeGatewayName.StringValue,
47+
})
48+
if err != nil {
49+
diags.AddError("Error retrieving Edge Gateway", err.Error())
50+
return
51+
}
52+
53+
return
54+
}
55+
56+
func (d *albServiceEngineGroupDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
57+
resp.TypeName = req.ProviderTypeName + "_" + categoryName + "_service_engine_group"
58+
}
59+
60+
func (d *albServiceEngineGroupDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
61+
resp.Schema = albServiceEngineGroupSchema(ctx).GetDataSource(ctx)
62+
}
63+
64+
func (d *albServiceEngineGroupDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
65+
// Prevent panic if the provider has not been configured.
66+
if req.ProviderData == nil {
67+
return
68+
}
69+
70+
client, ok := req.ProviderData.(*client.CloudAvenue)
71+
if !ok {
72+
resp.Diagnostics.AddError(
73+
"Unexpected Data Source Configure Type",
74+
fmt.Sprintf("Expected *client.CloudAvenue, got: %T. Please report this issue to the provider developers.", req.ProviderData),
75+
)
76+
return
77+
}
78+
d.client = client
79+
}
80+
81+
func (d *albServiceEngineGroupDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
82+
defer metrics.New("data.cloudavenue_alb_service_engine_group", d.client.GetOrgName(), metrics.Read)()
83+
84+
config := &albServiceEngineGroupModel{}
85+
86+
// Read Terraform configuration data into the model
87+
resp.Diagnostics.Append(req.Config.Get(ctx, config)...)
88+
if resp.Diagnostics.HasError() {
89+
return
90+
}
91+
92+
// Init the resource
93+
resp.Diagnostics.Append(d.Init(ctx, config)...)
94+
if resp.Diagnostics.HasError() {
95+
return
96+
}
97+
98+
/*
99+
Implement the data source read logic here.
100+
*/
101+
102+
// Get ServiceEngineGroup
103+
var (
104+
err error
105+
albSEG *v1.EdgeGatewayALBServiceEngineGroupModel
106+
)
107+
if config.ID.IsKnown() {
108+
albSEG, err = d.edgegw.GetALBServiceEngineGroup(config.ID.Get())
109+
} else {
110+
albSEG, err = d.edgegw.GetALBServiceEngineGroup(config.Name.Get())
111+
}
112+
if err != nil {
113+
resp.Diagnostics.AddError("Error retrieving Service Engine Group", err.Error())
114+
return
115+
}
116+
117+
config.ID.Set(albSEG.ID)
118+
config.Name.Set(albSEG.Name)
119+
config.EdgeGatewayID.Set(albSEG.GatewayRef.ID)
120+
config.EdgeGatewayName.Set(albSEG.GatewayRef.Name)
121+
config.MaxVirtualServices.SetIntPtr(albSEG.MaxVirtualServices)
122+
config.ReservedVirtualServices.SetIntPtr(albSEG.MinVirtualServices)
123+
config.DeployedVirtualServices.SetInt(albSEG.NumDeployedVirtualServices)
124+
125+
// Set state
126+
resp.Diagnostics.Append(resp.State.Set(ctx, config)...)
127+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package alb
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-framework/path"
7+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
8+
9+
schemaD "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
10+
11+
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
12+
13+
superschema "github.com/FrangipaneTeam/terraform-plugin-framework-superschema"
14+
)
15+
16+
func albServiceEngineGroupSchema(_ context.Context) superschema.Schema {
17+
return superschema.Schema{
18+
DataSource: superschema.SchemaDetails{
19+
MarkdownDescription: "The `cloudavenue_alb_service_engine_group` data source allows you to retrieve information about an ALB Service Engine Group.",
20+
},
21+
Attributes: map[string]superschema.Attribute{
22+
"id": superschema.SuperStringAttribute{
23+
DataSource: &schemaD.StringAttribute{
24+
Computed: true,
25+
Optional: true,
26+
MarkdownDescription: "The ID of the ALB Service Engine Group.",
27+
Validators: []validator.String{
28+
stringvalidator.ExactlyOneOf(path.MatchRoot("id"), path.MatchRoot("name")),
29+
},
30+
},
31+
},
32+
"name": superschema.SuperStringAttribute{
33+
DataSource: &schemaD.StringAttribute{
34+
MarkdownDescription: "The name of the ALB Service Engine Group.",
35+
Computed: true,
36+
Optional: true,
37+
Validators: []validator.String{
38+
stringvalidator.ExactlyOneOf(path.MatchRoot("id"), path.MatchRoot("name")),
39+
},
40+
},
41+
},
42+
"edge_gateway_id": superschema.SuperStringAttribute{
43+
DataSource: &schemaD.StringAttribute{
44+
MarkdownDescription: "Edge gateway ID in which ALB Service Engine Group should be located.",
45+
Optional: true,
46+
Validators: []validator.String{
47+
stringvalidator.ExactlyOneOf(path.MatchRoot("edge_gateway_id"), path.MatchRoot("edge_gateway_name")),
48+
},
49+
Computed: true,
50+
},
51+
},
52+
"edge_gateway_name": superschema.SuperStringAttribute{
53+
DataSource: &schemaD.StringAttribute{
54+
MarkdownDescription: "Edge gateway Name in which ALB Service Engine Group should be located.",
55+
Optional: true,
56+
Validators: []validator.String{
57+
stringvalidator.ExactlyOneOf(path.MatchRoot("edge_gateway_id"), path.MatchRoot("edge_gateway_name")),
58+
},
59+
Computed: true,
60+
},
61+
},
62+
"max_virtual_services": superschema.SuperInt64Attribute{
63+
DataSource: &schemaD.Int64Attribute{
64+
Computed: true,
65+
MarkdownDescription: "The maximum number of virtual services that can be deployed on the ALB Service Engine Group.",
66+
},
67+
},
68+
"reserved_virtual_services": superschema.SuperInt64Attribute{
69+
DataSource: &schemaD.Int64Attribute{
70+
Computed: true,
71+
MarkdownDescription: "The number of reserved virtual services for the ALB Service Engine Group.",
72+
},
73+
},
74+
"deployed_virtual_services": superschema.SuperInt64Attribute{
75+
DataSource: &schemaD.Int64Attribute{
76+
Computed: true,
77+
MarkdownDescription: "The number of deployed virtual services on the ALB Service Engine Group.",
78+
},
79+
},
80+
},
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package alb_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
fwdatasource "github.com/hashicorp/terraform-plugin-framework/datasource"
8+
9+
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/alb"
10+
)
11+
12+
// TODO : Comment or uncomment the following imports if you are using resources or/and datasources
13+
/*
14+
// Unit test for the schema of the resource cloudavenue_alb_AlbServiceEngineGroupDatasourceName
15+
func TestAlbServiceEngineGroupDatasourceNameResourceSchema(t *testing.T) {
16+
t.Parallel()
17+
18+
ctx := context.Background()
19+
schemaResponse := &fwresource.SchemaResponse{}
20+
21+
// Instantiate the resource.Resource and call its Schema method
22+
alb.NewAlbServiceEngineGroupDatasourceNameResource().Schema(ctx, fwresource.SchemaRequest{}, schemaResponse)
23+
24+
if schemaResponse.Diagnostics.HasError() {
25+
t.Fatalf("Schema method diagnostics: %+v", schemaResponse.Diagnostics)
26+
}
27+
28+
// Validate the schema
29+
diagnostics := schemaResponse.Schema.ValidateImplementation(ctx)
30+
31+
if diagnostics.HasError() {
32+
t.Fatalf("Schema validation diagnostics: %+v", diagnostics)
33+
}
34+
}
35+
*/
36+
// Unit test for the schema of the datasource cloudavenue_alb_AlbServiceEngineGroupDatasourceName
37+
38+
func TestALBServiceEngineGroupDataSourceSchema(t *testing.T) {
39+
t.Parallel()
40+
41+
ctx := context.Background()
42+
schemaResponse := &fwdatasource.SchemaResponse{}
43+
44+
// Instantiate the datasource.Datasource and call its Schema method
45+
alb.NewALBServiceEngineGroupDataSource().Schema(ctx, fwdatasource.SchemaRequest{}, schemaResponse)
46+
47+
if schemaResponse.Diagnostics.HasError() {
48+
t.Fatalf("Schema method diagnostics: %+v", schemaResponse.Diagnostics)
49+
}
50+
51+
// Validate the schema
52+
diagnostics := schemaResponse.Schema.ValidateImplementation(ctx)
53+
54+
if diagnostics.HasError() {
55+
t.Fatalf("Schema validation diagnostics: %+v", diagnostics)
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package alb
2+
3+
import (
4+
supertypes "github.com/FrangipaneTeam/terraform-plugin-framework-supertypes"
5+
)
6+
7+
type albServiceEngineGroupModel struct {
8+
ID supertypes.StringValue `tfsdk:"id"`
9+
Name supertypes.StringValue `tfsdk:"name"`
10+
EdgeGatewayID supertypes.StringValue `tfsdk:"edge_gateway_id"`
11+
EdgeGatewayName supertypes.StringValue `tfsdk:"edge_gateway_name"`
12+
MaxVirtualServices supertypes.Int64Value `tfsdk:"max_virtual_services"`
13+
ReservedVirtualServices supertypes.Int64Value `tfsdk:"reserved_virtual_services"`
14+
DeployedVirtualServices supertypes.Int64Value `tfsdk:"deployed_virtual_services"`
15+
}

internal/provider/provider_datasources.go

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func (p *cloudavenueProvider) DataSources(_ context.Context) []func() datasource
3636
return []func() datasource.DataSource{
3737
// * ALB
3838
alb.NewAlbPoolDataSource,
39+
alb.NewALBServiceEngineGroupDataSource,
3940

4041
// * TIER0
4142
vrf.NewTier0VrfsDataSource,

0 commit comments

Comments
 (0)